-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.f
339 lines (302 loc) · 6.24 KB
/
float.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
\ hi level floating point library
\ From Forth Dimensions vol IV # 1
\ author Michael Jesch
\ adapted to 32 bits system by PICATOUT
\ float format:
\ seee eeee smmm mmmm mmmm mmmm mmmm mmmm
\ 'm' mantissa bits
\ 's' sign bit
\ 'e' exponent bits
\ mantissa 24 bits 2's complement signed
\ exponent 8 bits 2's complement signed
PRESET
FLOAT \ forget float library if already loaded
MARK FLOAT \ float vocabulary bound
VARIABLE FPSW \ floating point state flags
VARIABLE FBASE \ floating point base
: FRESET ( -- ) \ reset state
0 FPSW C! ;
: FINIT ( -- ) \ initialize floating point library
FRESET BASE @ FBASE ! ;
: FER FPSW C@ ; \ return all flags
: FZE ( -- zeroflag ) \ return zero flag
FER 1 AND 0= NOT ;
: FNE FER ( -- negflag ) \ return negative flag
2 AND 0= NOT ;
: FOV ( -- ovf_flag ) \ return overflow flag
FER 4 AND 0= NOT ;
: SFZ ( f# -- f# ; z ) \ set zero flag from float on TOS
FER $FE AND OVER $FFFFFF AND 0= 1 AND OR FPSW C! ;
: SFN FER ( f# -- f# ; n ) \ set negative flag from float on TOS
$FD AND OVER $800000 AND 22 RSHIFT OR FPSW C! ;
: @EXPONENT ( F# -- m e ; z n ) \ split mantissa and exponent, set flags
FRESET SFZ SFN DUP
FNE IF $FF000000 OR ELSE $FFFFFF AND THEN SWAP \ sign extend mantissa
24 RSHIFT \ exponent
;
: !EXPONENT ( m e -- f# ; z n v )
\ format float from mantissa and exponent
DUP ABS 255 > IF 4 FPSW C! $FF AND THEN \ exponent overflow
OVER ABS $7FFFFF > IF 4 FPSW C! THEN \ mantissa overflow
24 LSHIFT SWAP
$FFFFFF AND OR
SFZ SFN ;
\ print float in scientific notation
\ d.fffffE[-]ee
: E. ( F# -- )
SPACE
DUP $FFFFFF AND
0= IF
DROP
." 0.0"
ELSE
SPACE
BASE @ >R
FBASE @ BASE !
@EXPONENT
SWAP
DUP >R \ save mantissa copy
FNE IF ABS THEN
S>D
<#
BEGIN
# ROT 1+ -ROT
OVER BASE @ U< UNTIL
[CHAR] . HOLD
#S R> SIGN #> TYPE
?DUP IF
[CHAR] E EMIT
DUP 0< IF
ABS
[CHAR] - EMIT
THEN
S>D <# #S #> TYPE
THEN
R> BASE !
THEN
;
: F. ( F# -- )
\ print float in fixed point format
DUP @EXPONENT >R
I ABS 32 U> IF
R> 2DROP E.
ELSE
SPACE
FNE IF ABS THEN
S>D
<#
I 0< IF
I ABS 0 DO # LOOP [CHAR] . HOLD
ELSE
[CHAR] . HOLD I IF
I 0 DO [CHAR] 0 HOLD LOOP
THEN
THEN
R> DROP
#S SWAP 8 LSHIFT SIGN #> TYPE
THEN
;
\ float product
: F* ( F#1 F#2 -- F#3 )
@EXPONENT >R
SWAP @EXPONENT R> + >R
M* DUP 31 RSHIFT -ROT
DABS
BEGIN
2DUP $7FFFFF S>D UD> WHILE
10 D/MOD ROT DROP
R> 1+ >R
REPEAT
DROP SWAP IF NEGATE $FFFFFF AND THEN
R> !EXPONENT
;
\ float division
: F/ ( F#1 F#2 -- F#1/F#2 )
@EXPONENT >R
SWAP @EXPONENT R> + >R
SWAP /
R> !EXPONENT
;
\ align 2 floats to same exponent
\ for addition or substraction
: F-ALIGN ( F#1 F#2 -- M1 M2 E )
@EXPONENT >R
SWAP @EXPONENT ROT R>
BEGIN
>R SWAP >R
J I <> WHILE
J I < IF \ J IS E2
SWAP FBASE @ * SWAP R> 1- SWAP R>
ELSE
R> SWAP FBASE @ * R> 1-
THEN
REPEAT
R> R> DROP
;
\ add 2 floats
: F+ ( f#1 f#2 -- f#1+f#2 )
F-ALIGN
>R +
R> !EXPONENT
;
\ substract 2 floats
: F- ( f#1 f#2 -- f#1-f#2 )
F-ALIGN
>R -
R> !EXPONENT
;
\ increment number
\ of digits displayed after '.'
\ when using F.
: RSCALE ( F# -- F# )
@EXPONENT
1-
SWAP FBASE @ *
SWAP !EXPONENT
;
\ decrement number
\ of digits diplayed after '.'
\ when using F.
: LSCALE ( f# -- f# )
@EXPONENT 1+
SWAP FBASE @ /
SWAP !EXPONENT
;
\ convert float to single
: F>S ( F# -- S )
@EXPONENT >R
BEGIN
I WHILE
I 0> IF
FBASE @ * R> 1- >R
ELSE
FBASE @ / R> 1+ >R
THEN
REPEAT
R> DROP
;
\ convert single to float
: S>F ( s -- f# )
DUP 0< IF
ABS -1
ELSE
0
THEN
SWAP
0
BEGIN
>R
DUP $7FFFFF > WHILE
FBASE @ /
R> 1+
REPEAT
SWAP
IF NEGATE $FFFFFF AND THEN
R> 24 LSHIFT OR
;
\ float absulute value
: FABS ( f# -- f# )
@EXPONENT
SWAP ABS
SWAP !EXPONENT
;
\ float negate
: FNEGATE ( f# -- f# )
@EXPONENT
SWAP
NEGATE
SWAP
!EXPONENT
;
\ float min
: FMIN ( f#1 f#2 -- smallest float )
2DUP
F-ALIGN
DROP
< IF DROP ELSE SWAP DROP THEN
;
\ float max
: FMAX ( f#1 f#2 -- largest float )
2DUP
F-ALIGN
DROP
< IF SWAP DROP ELSE DROP THEN
;
\ f#1 > f#2
: F> ( f#1 f#2 -- f )
F-ALIGN
DROP
>
;
\ f#1 < f#2
: F< ( f#1 f#2 -- f )
F-ALIGN
DROP
<
;
\ check for charcter c
\ move pointer if true
: C? ( a c -- a+ t | a f )
SWAP
COUNT
ROT
= DUP NOT IF
SWAP 1- SWAP
THEN
;
\ parse digits
\ d digits count
\ n parsed integer
\ a+ updated pointer
: PARSE_DIGITS ( d n a -- d+ n+ a+ )
BEGIN
COUNT FBASE @ DIGIT? WHILE
ROT FBASE @ * + SWAP
ROT 1+ -ROT
REPEAT
DROP
1- \ decrement a
;
\ check for exponent
: EXPONENT ( a -- e a+ )
[CHAR] E C? IF
[CHAR] - C? >R
0 0 ROT
PARSE_DIGITS
ROT DROP \ discard digits count
R> IF SWAP NEGATE SWAP THEN
ELSE
0 SWAP
THEN
;
\ parse float number
: FLOAT? ( a -- f# -2 | a 0 )
\ simpler to find the end of null terminated string
DUP ASCIZ
0 0 ROT \ -- a d n asciz
\ check for sign
[CHAR] - C? >R
PARSE_DIGITS
ROT DROP \ d not used
0 -ROT \ reset it
\ check for '.'
[CHAR] . C?
IF PARSE_DIGITS
ROT NEGATE -ROT \ negate digit count
THEN
EXPONENT
\ a d n e asciz
COUNT 0= IF
DROP \ a d n e
ROT + \ a n e-
ROT DROP
SWAP
R> IF NEGATE THEN
SWAP !EXPONENT
-2
ELSE
2DROP 2DROP
0
THEN
;
FINIT