1 /* float.c floating-point constant support for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 13/ix/96 by Simon Tatham
20 #define MANT_WORDS 6 /* 64 bits + 32 for accuracy == 96 */
21 #define MANT_DIGITS 28 /* 29 digits don't fit in 96 bits */
24 * guaranteed top bit of from is set
25 * => we only have to worry about _one_ bit shift to the left
28 static int multiply(unsigned short *to
, unsigned short *from
) {
29 unsigned long temp
[MANT_WORDS
*2];
32 for (i
=0; i
<MANT_WORDS
*2; i
++)
35 for (i
=0; i
<MANT_WORDS
; i
++)
36 for (j
=0; j
<MANT_WORDS
; j
++) {
38 n
= (unsigned long)to
[i
] * (unsigned long)from
[j
];
40 temp
[i
+j
+1] += n
& 0xFFFF;
43 for (i
=MANT_WORDS
*2; --i
;) {
44 temp
[i
-1] += temp
[i
] >> 16;
47 if (temp
[0] & 0x8000) {
48 for (i
=0; i
<MANT_WORDS
; i
++)
49 to
[i
] = temp
[i
] & 0xFFFF;
52 for (i
=0; i
<MANT_WORDS
; i
++)
53 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+1] & 0x8000);
58 static void flconvert(char *string
, unsigned short *mant
, long *exponent
,
60 char digits
[MANT_DIGITS
], *p
, *q
, *r
;
61 unsigned short mult
[MANT_WORDS
], *m
, bit
;
63 int extratwos
, started
, seendot
;
67 started
= seendot
= FALSE
;
68 while (*string
&& *string
!= 'E' && *string
!= 'e') {
74 "too many periods in floating-point constant");
77 } else if (*string
>= '0' && *string
<= '9') {
78 if (*string
== '0' && !started
) {
83 if (p
< digits
+sizeof(digits
))
90 "floating-point constant: `%c' is invalid character",
97 string
++; /* eat the E */
98 tenpwr
+= atoi(string
);
102 * At this point, the memory interval [digits,p) contains a
103 * series of decimal digits zzzzzzz such that our number X
106 * X = 0.zzzzzzz * 10^tenpwr
110 for (m
=mant
; m
<mant
+MANT_WORDS
; m
++)
116 while (m
< mant
+MANT_WORDS
) {
117 unsigned short carry
= 0;
118 while (p
> q
&& !p
[-1])
122 for (r
= p
; r
-- > q
;) {
133 *m
|= bit
, started
= TRUE
;
145 * At this point the `mant' array contains the first six
146 * fractional places of a base-2^16 real number, which when
147 * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
148 * really do multiply by 5^tenpwr.
152 for (m
=mult
; m
<mult
+MANT_WORDS
; m
++)
156 } else if (tenpwr
> 0) {
158 for (m
=mult
+1; m
<mult
+MANT_WORDS
; m
++)
165 twopwr
+= extratwos
+ multiply (mant
, mult
);
166 extratwos
= extratwos
* 2 + multiply (mult
, mult
);
171 * Conversion is done. The elements of `mant' contain the first
172 * fractional places of a base-2^16 real number in [0.5,1)
173 * which we can multiply by 2^twopwr to get X. Or, of course,
180 * Shift a mantissa to the right by i (i < 16) bits.
182 static void shr(unsigned short *mant
, int i
) {
183 unsigned short n
= 0, m
;
186 for (j
=0; j
<MANT_WORDS
; j
++) {
187 m
= (mant
[j
] << (16-i
)) & 0xFFFF;
188 mant
[j
] = (mant
[j
] >> i
) | n
;
194 * Round a mantissa off after i words.
196 static int round(unsigned short *mant
, int i
) {
197 if (mant
[i
] & 0x8000) {
201 } while (i
> 0 && !mant
[i
]);
202 return !i
&& !mant
[i
];
207 #define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
209 static int to_double(char *str
, long sign
, unsigned char *result
,
211 unsigned short mant
[MANT_WORDS
];
214 sign
= (sign
< 0 ? 0x8000L
: 0L);
216 flconvert (str
, mant
, &exponent
, error
);
217 if (mant
[0] & 0x8000) {
222 if (exponent
>= -1022 && exponent
<= 1024) {
229 if (mant
[0] & 0x20) /* did we scale up by one? */
230 shr(mant
, 1), exponent
++;
231 mant
[0] &= 0xF; /* remove leading one */
232 put(result
+6,(exponent
<< 4) | mant
[0] | sign
);
233 put(result
+4,mant
[1]);
234 put(result
+2,mant
[2]);
235 put(result
+0,mant
[3]);
236 } else if (exponent
< -1022 && exponent
>= -1074) {
240 int shift
= -(exponent
+1011);
241 int sh
= shift
% 16, wds
= shift
/ 16;
243 if (round(mant
, 4-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
249 put(result
+6,(wds
== 0 ? mant
[0] : 0) | sign
);
250 put(result
+4,(wds
<= 1 ? mant
[1-wds
] : 0));
251 put(result
+2,(wds
<= 2 ? mant
[2-wds
] : 0));
252 put(result
+0,(wds
<= 3 ? mant
[3-wds
] : 0));
255 error(ERR_NONFATAL
, "overflow in floating-point constant");
258 memset (result
, 0, 8);
264 memset (result
, 0, 8);
266 return 1; /* success */
269 static int to_float(char *str
, long sign
, unsigned char *result
,
271 unsigned short mant
[MANT_WORDS
];
274 sign
= (sign
< 0 ? 0x8000L
: 0L);
276 flconvert (str
, mant
, &exponent
, error
);
277 if (mant
[0] & 0x8000) {
282 if (exponent
>= -126 && exponent
<= 128) {
289 if (mant
[0] & 0x100) /* did we scale up by one? */
290 shr(mant
, 1), exponent
++;
291 mant
[0] &= 0x7F; /* remove leading one */
292 put(result
+2,(exponent
<< 7) | mant
[0] | sign
);
293 put(result
+0,mant
[1]);
294 } else if (exponent
< -126 && exponent
>= -149) {
298 int shift
= -(exponent
+118);
299 int sh
= shift
% 16, wds
= shift
/ 16;
301 if (round(mant
, 2-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
307 put(result
+2,(wds
== 0 ? mant
[0] : 0) | sign
);
308 put(result
+0,(wds
<= 1 ? mant
[1-wds
] : 0));
311 error(ERR_NONFATAL
, "overflow in floating-point constant");
314 memset (result
, 0, 4);
317 memset (result
, 0, 4);
322 static int to_ldoub(char *str
, long sign
, unsigned char *result
,
324 unsigned short mant
[MANT_WORDS
];
327 sign
= (sign
< 0 ? 0x8000L
: 0L);
329 flconvert (str
, mant
, &exponent
, error
);
330 if (mant
[0] & 0x8000) {
335 if (exponent
>= -16383 && exponent
<= 16384) {
340 if (round(mant
, 4)) /* did we scale up by one? */
341 shr(mant
, 1), mant
[0] |= 0x8000, exponent
++;
342 put(result
+8,exponent
| sign
);
343 put(result
+6,mant
[0]);
344 put(result
+4,mant
[1]);
345 put(result
+2,mant
[2]);
346 put(result
+0,mant
[3]);
347 } else if (exponent
< -16383 && exponent
>= -16446) {
351 int shift
= -(exponent
+16383);
352 int sh
= shift
% 16, wds
= shift
/ 16;
354 if (round(mant
, 4-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
361 put(result
+6,(wds
== 0 ? mant
[0] : 0));
362 put(result
+4,(wds
<= 1 ? mant
[1-wds
] : 0));
363 put(result
+2,(wds
<= 2 ? mant
[2-wds
] : 0));
364 put(result
+0,(wds
<= 3 ? mant
[3-wds
] : 0));
367 error(ERR_NONFATAL
, "overflow in floating-point constant");
370 memset (result
, 0, 10);
376 memset (result
, 0, 10);
381 int float_const (char *number
, long sign
, unsigned char *result
, int bytes
,
384 return to_float (number
, sign
, result
, error
);
386 return to_double (number
, sign
, result
, error
);
387 else if (bytes
== 10)
388 return to_ldoub (number
, sign
, result
, error
);
390 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);