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
21 #define MANT_WORDS 10 /* 112 bits + 48 for accuracy == 160 */
22 #define MANT_DIGITS 49 /* 50 digits don't fit in 160 bits */
25 * guaranteed top bit of from is set
26 * => we only have to worry about _one_ bit shift to the left
29 static int ieee_multiply(uint16_t *to
, uint16_t *from
)
31 uint32_t temp
[MANT_WORDS
* 2];
34 for (i
= 0; i
< MANT_WORDS
* 2; i
++)
37 for (i
= 0; i
< MANT_WORDS
; i
++)
38 for (j
= 0; j
< MANT_WORDS
; j
++) {
40 n
= (uint32_t)to
[i
] * (uint32_t)from
[j
];
41 temp
[i
+ j
] += n
>> 16;
42 temp
[i
+ j
+ 1] += n
& 0xFFFF;
45 for (i
= MANT_WORDS
* 2; --i
;) {
46 temp
[i
- 1] += temp
[i
] >> 16;
49 if (temp
[0] & 0x8000) {
50 memcpy(to
, temp
, 2*MANT_WORDS
);
53 for (i
= 0; i
< MANT_WORDS
; i
++)
54 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+ 1] & 0x8000);
59 static int hexval(char c
)
61 if (c
>= '0' && c
<= '9')
63 else if (c
>= 'a' && c
<= 'f')
69 static void ieee_flconvert_hex(char *string
, uint16_t *mant
,
70 int32_t *exponent
, efunc error
)
72 static const int log2tbl
[16] =
73 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
74 uint16_t mult
[MANT_WORDS
+1], *mp
;
77 int seendot
, seendigit
;
81 seendot
= seendigit
= 0;
85 memset(mult
, 0, sizeof mult
);
87 while ((c
= *string
++) != '\0') {
93 "too many periods in floating-point constant");
96 } else if (isxdigit(c
)) {
99 if (!seendigit
&& v
) {
106 twopwr
= seendot
? twopwr
-4+l
: l
-3;
113 if (mp
> &mult
[MANT_WORDS
])
114 mp
= &mult
[MANT_WORDS
]; /* Guard slot */
126 } else if (c
== 'p' || c
== 'P') {
127 twopwr
+= atoi(string
);
131 "floating-point constant: `%c' is invalid character",
138 memset(mant
, 0, 2*MANT_WORDS
); /* Zero */
141 memcpy(mant
, mult
, 2*MANT_WORDS
);
146 static void ieee_flconvert(char *string
, uint16_t *mant
,
147 int32_t *exponent
, efunc error
)
149 char digits
[MANT_DIGITS
];
151 uint16_t mult
[MANT_WORDS
], bit
;
153 int32_t tenpwr
, twopwr
;
154 int extratwos
, started
, seendot
;
156 if (string
[0] == '0' && (string
[1] == 'x' || string
[1] == 'X')) {
157 ieee_flconvert_hex(string
+2, mant
, exponent
, error
);
163 started
= seendot
= false;
164 while (*string
&& *string
!= 'E' && *string
!= 'e') {
165 if (*string
== '.') {
170 "too many periods in floating-point constant");
173 } else if (*string
>= '0' && *string
<= '9') {
174 if (*string
== '0' && !started
) {
179 if (p
< digits
+ sizeof(digits
))
180 *p
++ = *string
- '0';
186 "floating-point constant: `%c' is invalid character",
193 string
++; /* eat the E */
194 tenpwr
+= atoi(string
);
198 * At this point, the memory interval [digits,p) contains a
199 * series of decimal digits zzzzzzz such that our number X
202 * X = 0.zzzzzzz * 10^tenpwr
206 for (m
= mant
; m
< mant
+ MANT_WORDS
; m
++)
212 while (m
< mant
+ MANT_WORDS
) {
214 while (p
> q
&& !p
[-1])
218 for (r
= p
; r
-- > q
;) {
229 *m
|= bit
, started
= true;
241 * At this point the `mant' array contains the first six
242 * fractional places of a base-2^16 real number, which when
243 * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
244 * really do multiply by 5^tenpwr.
248 for (m
= mult
; m
< mult
+ MANT_WORDS
; m
++)
252 } else if (tenpwr
> 0) {
254 for (m
= mult
+ 1; m
< mult
+ MANT_WORDS
; m
++)
261 twopwr
+= extratwos
+ ieee_multiply(mant
, mult
);
262 extratwos
= extratwos
* 2 + ieee_multiply(mult
, mult
);
267 * Conversion is done. The elements of `mant' contain the first
268 * fractional places of a base-2^16 real number in [0.5,1)
269 * which we can multiply by 2^twopwr to get X. Or, of course,
276 * Shift a mantissa to the right by i (i < 16) bits.
278 static void ieee_shr(uint16_t *mant
, int i
)
283 for (j
= 0; j
< MANT_WORDS
; j
++) {
284 m
= (mant
[j
] << (16 - i
)) & 0xFFFF;
285 mant
[j
] = (mant
[j
] >> i
) | n
;
291 * Round a mantissa off after i words.
293 static int ieee_round(uint16_t *mant
, int i
)
295 if (mant
[i
] & 0x8000) {
299 } while (i
> 0 && !mant
[i
]);
300 return !i
&& !mant
[i
];
305 #define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
307 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
308 static void set_bit(uint16_t *mant
, int bit
)
310 mant
[bit
>> 4] |= 1 << (~bit
& 15);
313 /* Produce standard IEEE formats, with implicit "1" bit; this makes
314 the following assumptions:
316 - the sign bit is the MSB, followed by the exponent.
317 - the sign bit plus exponent fit in 16 bits.
318 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
322 int mantissa
; /* Bits in the mantissa */
323 int exponent
; /* Bits in the exponent */
326 static const struct ieee_format ieee_16
= { 1, 10, 5 };
327 static const struct ieee_format ieee_32
= { 2, 23, 8 };
328 static const struct ieee_format ieee_64
= { 4, 52, 11 };
329 static const struct ieee_format ieee_128
= { 8, 112, 15 };
331 /* Produce all the standard IEEE formats: 16, 32, 64, and 128 bits */
332 static int to_float(char *str
, int32_t sign
, uint8_t *result
,
333 const struct ieee_format
*fmt
, efunc error
)
335 uint16_t mant
[MANT_WORDS
], *mp
;
337 int32_t expmax
= 1 << (fmt
->exponent
-1);
338 uint16_t implicit_one
= 0x8000 >> fmt
->exponent
;
341 sign
= (sign
< 0 ? 0x8000L
: 0L);
344 /* NaN or Infinity */
345 int32_t expmask
= (1 << fmt
->exponent
)-1;
347 memset(mant
, 0, sizeof mant
);
348 mant
[0] = expmask
<< (15-fmt
->exponent
); /* Exponent: all bits one */
351 case 'n': /* __nan__ */
353 case 'q': /* __qnan__ */
355 set_bit(mant
, fmt
->exponent
+1); /* Highest bit in mantissa */
357 case 's': /* __snan__ */
359 set_bit(mant
, fmt
->exponent
+fmt
->mantissa
); /* Last bit */
361 case 'i': /* __infinity__ */
366 ieee_flconvert(str
, mant
, &exponent
, error
);
367 if (mant
[0] & 0x8000) {
372 if (exponent
>= 2-expmax
&& exponent
<= expmax
) {
376 exponent
+= expmax
-1;
377 ieee_shr(mant
, fmt
->exponent
);
378 ieee_round(mant
, fmt
->words
);
379 /* did we scale up by one? */
380 if (mant
[0] & (implicit_one
<< 1)) {
385 mant
[0] &= (implicit_one
-1); /* remove leading one */
386 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
387 } else if (exponent
< 2-expmax
&&
388 exponent
>= 2-expmax
-fmt
->mantissa
) {
392 int shift
= -(exponent
+ expmax
-2-fmt
->exponent
);
393 int sh
= shift
% 16, wds
= shift
/ 16;
395 if (ieee_round(mant
, fmt
->words
- wds
)
396 || (sh
> 0 && (mant
[0] & (0x8000 >> (sh
- 1))))) {
404 for (i
= fmt
->words
-1; i
>= wds
; i
--)
405 mant
[i
] = mant
[i
-wds
];
411 error(ERR_NONFATAL
, "overflow in floating-point constant");
414 memset(mant
, 0, 2*fmt
->words
);
419 memset(mant
, 0, 2*fmt
->words
);
425 for (mp
= &mant
[fmt
->words
], i
= 0; i
< fmt
->words
; i
++) {
431 return 1; /* success */
434 /* 80-bit format with 64-bit mantissa *including an explicit integer 1*
435 and 15-bit exponent. */
436 static int to_ldoub(char *str
, int32_t sign
, uint8_t *result
,
439 uint16_t mant
[MANT_WORDS
];
442 sign
= (sign
< 0 ? 0x8000L
: 0L);
445 uint16_t is_snan
= 0, is_qnan
= 0x8000;
461 put(result
+ 0, is_snan
);
464 put(result
+ 6, is_qnan
);
465 put(result
+ 8, 0x7fff|sign
);
469 ieee_flconvert(str
, mant
, &exponent
, error
);
470 if (mant
[0] & 0x8000) {
475 if (exponent
>= -16383 && exponent
<= 16384) {
480 if (ieee_round(mant
, 4)) /* did we scale up by one? */
481 ieee_shr(mant
, 1), mant
[0] |= 0x8000, exponent
++;
482 put(result
+ 0, mant
[3]);
483 put(result
+ 2, mant
[2]);
484 put(result
+ 4, mant
[1]);
485 put(result
+ 6, mant
[0]);
486 put(result
+ 8, exponent
| sign
);
487 } else if (exponent
< -16383 && exponent
>= -16446) {
491 int shift
= -(exponent
+ 16383);
492 int sh
= shift
% 16, wds
= shift
/ 16;
494 if (ieee_round(mant
, 4 - wds
)
495 || (sh
> 0 && (mant
[0] & (0x8000 >> (sh
- 1))))) {
501 put(result
+ 0, (wds
<= 3 ? mant
[3 - wds
] : 0));
502 put(result
+ 2, (wds
<= 2 ? mant
[2 - wds
] : 0));
503 put(result
+ 4, (wds
<= 1 ? mant
[1 - wds
] : 0));
504 put(result
+ 6, (wds
== 0 ? mant
[0] : 0));
505 put(result
+ 8, sign
);
508 error(ERR_NONFATAL
, "overflow in floating-point constant");
523 put(result
+ 8, sign
);
528 int float_const(char *number
, int32_t sign
, uint8_t *result
, int bytes
,
533 return to_float(number
, sign
, result
, &ieee_16
, error
);
535 return to_float(number
, sign
, result
, &ieee_32
, error
);
537 return to_float(number
, sign
, result
, &ieee_64
, error
);
539 return to_ldoub(number
, sign
, result
, error
);
541 return to_float(number
, sign
, result
, &ieee_128
, error
);
543 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);