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
)
30 unsigned long temp
[MANT_WORDS
*2];
33 for (i
=0; i
<MANT_WORDS
*2; i
++)
36 for (i
=0; i
<MANT_WORDS
; i
++)
37 for (j
=0; j
<MANT_WORDS
; j
++) {
39 n
= (unsigned long)to
[i
] * (unsigned long)from
[j
];
41 temp
[i
+j
+1] += n
& 0xFFFF;
44 for (i
=MANT_WORDS
*2; --i
;) {
45 temp
[i
-1] += temp
[i
] >> 16;
48 if (temp
[0] & 0x8000) {
49 for (i
=0; i
<MANT_WORDS
; i
++)
50 to
[i
] = temp
[i
] & 0xFFFF;
53 for (i
=0; i
<MANT_WORDS
; i
++)
54 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+1] & 0x8000);
59 static void flconvert(char *string
, unsigned short *mant
, long *exponent
,
62 char digits
[MANT_DIGITS
];
64 unsigned short mult
[MANT_WORDS
], bit
;
67 int extratwos
, started
, seendot
;
71 started
= seendot
= FALSE
;
72 while (*string
&& *string
!= 'E' && *string
!= 'e') {
78 "too many periods in floating-point constant");
81 } else if (*string
>= '0' && *string
<= '9') {
82 if (*string
== '0' && !started
) {
87 if (p
< digits
+sizeof(digits
))
94 "floating-point constant: `%c' is invalid character",
101 string
++; /* eat the E */
102 tenpwr
+= atoi(string
);
106 * At this point, the memory interval [digits,p) contains a
107 * series of decimal digits zzzzzzz such that our number X
110 * X = 0.zzzzzzz * 10^tenpwr
114 for (m
=mant
; m
<mant
+MANT_WORDS
; m
++)
120 while (m
< mant
+MANT_WORDS
) {
121 unsigned short carry
= 0;
122 while (p
> q
&& !p
[-1])
126 for (r
= p
; r
-- > q
;) {
137 *m
|= bit
, started
= TRUE
;
149 * At this point the `mant' array contains the first six
150 * fractional places of a base-2^16 real number, which when
151 * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
152 * really do multiply by 5^tenpwr.
156 for (m
=mult
; m
<mult
+MANT_WORDS
; m
++)
160 } else if (tenpwr
> 0) {
162 for (m
=mult
+1; m
<mult
+MANT_WORDS
; m
++)
169 twopwr
+= extratwos
+ multiply (mant
, mult
);
170 extratwos
= extratwos
* 2 + multiply (mult
, mult
);
175 * Conversion is done. The elements of `mant' contain the first
176 * fractional places of a base-2^16 real number in [0.5,1)
177 * which we can multiply by 2^twopwr to get X. Or, of course,
184 * Shift a mantissa to the right by i (i < 16) bits.
186 static void shr(unsigned short *mant
, int i
)
188 unsigned short n
= 0, m
;
191 for (j
=0; j
<MANT_WORDS
; j
++) {
192 m
= (mant
[j
] << (16-i
)) & 0xFFFF;
193 mant
[j
] = (mant
[j
] >> i
) | n
;
199 * Round a mantissa off after i words.
201 static int round(unsigned short *mant
, int i
)
203 if (mant
[i
] & 0x8000) {
207 } while (i
> 0 && !mant
[i
]);
208 return !i
&& !mant
[i
];
213 #define put(a,b) ( ((a)[0]=(unsigned char)((b) & 0xFF)), \
214 ((a)[1]=(unsigned char)(((b) >> 8) & 0xFF)) )
216 static int to_double(char *str
, long sign
, unsigned char *result
,
219 unsigned short mant
[MANT_WORDS
];
222 sign
= (sign
< 0 ? 0x8000L
: 0L);
224 flconvert (str
, mant
, &exponent
, error
);
225 if (mant
[0] & 0x8000) {
230 if (exponent
>= -1022 && exponent
<= 1024) {
237 if (mant
[0] & 0x20) /* did we scale up by one? */
238 shr(mant
, 1), exponent
++;
239 mant
[0] &= 0xF; /* remove leading one */
240 put(result
+6,(exponent
<< 4) | mant
[0] | sign
);
241 put(result
+4,mant
[1]);
242 put(result
+2,mant
[2]);
243 put(result
+0,mant
[3]);
244 } else if (exponent
< -1022 && exponent
>= -1074) {
248 int shift
= -(exponent
+1011);
249 int sh
= shift
% 16, wds
= shift
/ 16;
251 if (round(mant
, 4-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
257 put(result
+6,(wds
== 0 ? mant
[0] : 0) | sign
);
258 put(result
+4,(wds
<= 1 ? mant
[1-wds
] : 0));
259 put(result
+2,(wds
<= 2 ? mant
[2-wds
] : 0));
260 put(result
+0,(wds
<= 3 ? mant
[3-wds
] : 0));
263 error(ERR_NONFATAL
, "overflow in floating-point constant");
266 memset (result
, 0, 8);
272 memset (result
, 0, 8);
274 return 1; /* success */
277 static int to_float(char *str
, long sign
, unsigned char *result
,
280 unsigned short mant
[MANT_WORDS
];
283 sign
= (sign
< 0 ? 0x8000L
: 0L);
285 flconvert (str
, mant
, &exponent
, error
);
286 if (mant
[0] & 0x8000) {
291 if (exponent
>= -126 && exponent
<= 128) {
298 if (mant
[0] & 0x100) /* did we scale up by one? */
299 shr(mant
, 1), exponent
++;
300 mant
[0] &= 0x7F; /* remove leading one */
301 put(result
+2,(exponent
<< 7) | mant
[0] | sign
);
302 put(result
+0,mant
[1]);
303 } else if (exponent
< -126 && exponent
>= -149) {
307 int shift
= -(exponent
+118);
308 int sh
= shift
% 16, wds
= shift
/ 16;
310 if (round(mant
, 2-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
316 put(result
+2,(wds
== 0 ? mant
[0] : 0) | sign
);
317 put(result
+0,(wds
<= 1 ? mant
[1-wds
] : 0));
320 error(ERR_NONFATAL
, "overflow in floating-point constant");
323 memset (result
, 0, 4);
326 memset (result
, 0, 4);
331 static int to_ldoub(char *str
, long sign
, unsigned char *result
,
334 unsigned short mant
[MANT_WORDS
];
337 sign
= (sign
< 0 ? 0x8000L
: 0L);
339 flconvert (str
, mant
, &exponent
, error
);
340 if (mant
[0] & 0x8000) {
345 if (exponent
>= -16383 && exponent
<= 16384) {
350 if (round(mant
, 4)) /* did we scale up by one? */
351 shr(mant
, 1), mant
[0] |= 0x8000, exponent
++;
352 put(result
+8,exponent
| sign
);
353 put(result
+6,mant
[0]);
354 put(result
+4,mant
[1]);
355 put(result
+2,mant
[2]);
356 put(result
+0,mant
[3]);
357 } else if (exponent
< -16383 && exponent
>= -16446) {
361 int shift
= -(exponent
+16383);
362 int sh
= shift
% 16, wds
= shift
/ 16;
364 if (round(mant
, 4-wds
) || (sh
>0 && (mant
[0]&(0x8000>>(sh
-1))))) {
371 put(result
+6,(wds
== 0 ? mant
[0] : 0));
372 put(result
+4,(wds
<= 1 ? mant
[1-wds
] : 0));
373 put(result
+2,(wds
<= 2 ? mant
[2-wds
] : 0));
374 put(result
+0,(wds
<= 3 ? mant
[3-wds
] : 0));
377 error(ERR_NONFATAL
, "overflow in floating-point constant");
380 memset (result
, 0, 10);
386 memset (result
, 0, 10);
391 int float_const (char *number
, long sign
, unsigned char *result
, int bytes
,
395 return to_float (number
, sign
, result
, error
);
397 return to_double (number
, sign
, result
, error
);
398 else if (bytes
== 10)
399 return to_ldoub (number
, sign
, result
, error
);
401 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);