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
22 #define MANT_WORDS 10 /* 112 bits + 48 for accuracy == 160 */
23 #define MANT_DIGITS 49 /* 50 digits don't fit in 160 bits */
26 * guaranteed top bit of from is set
27 * => we only have to worry about _one_ bit shift to the left
30 static int ieee_multiply(uint16_t *to
, uint16_t *from
)
32 uint32_t temp
[MANT_WORDS
* 2];
35 for (i
= 0; i
< MANT_WORDS
* 2; i
++)
38 for (i
= 0; i
< MANT_WORDS
; i
++)
39 for (j
= 0; j
< MANT_WORDS
; j
++) {
41 n
= (uint32_t)to
[i
] * (uint32_t)from
[j
];
42 temp
[i
+ j
] += n
>> 16;
43 temp
[i
+ j
+ 1] += n
& 0xFFFF;
46 for (i
= MANT_WORDS
* 2; --i
;) {
47 temp
[i
- 1] += temp
[i
] >> 16;
50 if (temp
[0] & 0x8000) {
51 memcpy(to
, temp
, 2*MANT_WORDS
);
54 for (i
= 0; i
< MANT_WORDS
; i
++)
55 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+ 1] & 0x8000);
60 static int hexval(char c
)
62 if (c
>= '0' && c
<= '9')
64 else if (c
>= 'a' && c
<= 'f')
70 static void ieee_flconvert_hex(char *string
, uint16_t *mant
,
71 int32_t *exponent
, efunc error
)
73 static const int log2tbl
[16] =
74 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
75 uint16_t mult
[MANT_WORDS
+1], *mp
;
78 int seendot
, seendigit
;
82 seendot
= seendigit
= 0;
84 memset(mult
, 0, sizeof mult
);
86 while ((c
= *string
++) != '\0') {
92 "too many periods in floating-point constant");
95 } else if (isxdigit(c
)) {
98 if (!seendigit
&& v
) {
105 twopwr
= seendot
? twopwr
-4+l
: l
-3;
112 if (mp
> &mult
[MANT_WORDS
])
113 mp
= &mult
[MANT_WORDS
]; /* Guard slot */
125 } else if (c
== 'p' || c
== 'P') {
126 twopwr
+= atoi(string
);
130 "floating-point constant: `%c' is invalid character",
137 memset(mant
, 0, 2*MANT_WORDS
); /* Zero */
140 memcpy(mant
, mult
, 2*MANT_WORDS
);
145 static void ieee_flconvert(char *string
, uint16_t *mant
,
146 int32_t *exponent
, efunc error
)
148 char digits
[MANT_DIGITS
];
150 uint16_t mult
[MANT_WORDS
], bit
;
152 int32_t tenpwr
, twopwr
;
153 int extratwos
, started
, seendot
;
155 if (string
[0] == '0' && (string
[1] == 'x' || string
[1] == 'X')) {
156 ieee_flconvert_hex(string
+2, mant
, exponent
, error
);
162 started
= seendot
= FALSE
;
163 while (*string
&& *string
!= 'E' && *string
!= 'e') {
164 if (*string
== '.') {
169 "too many periods in floating-point constant");
172 } else if (*string
>= '0' && *string
<= '9') {
173 if (*string
== '0' && !started
) {
178 if (p
< digits
+ sizeof(digits
))
179 *p
++ = *string
- '0';
185 "floating-point constant: `%c' is invalid character",
192 string
++; /* eat the E */
193 tenpwr
+= atoi(string
);
197 * At this point, the memory interval [digits,p) contains a
198 * series of decimal digits zzzzzzz such that our number X
201 * X = 0.zzzzzzz * 10^tenpwr
205 for (m
= mant
; m
< mant
+ MANT_WORDS
; m
++)
211 while (m
< mant
+ MANT_WORDS
) {
213 while (p
> q
&& !p
[-1])
217 for (r
= p
; r
-- > q
;) {
228 *m
|= bit
, started
= TRUE
;
240 * At this point the `mant' array contains the first six
241 * fractional places of a base-2^16 real number, which when
242 * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
243 * really do multiply by 5^tenpwr.
247 for (m
= mult
; m
< mult
+ MANT_WORDS
; m
++)
251 } else if (tenpwr
> 0) {
253 for (m
= mult
+ 1; m
< mult
+ MANT_WORDS
; m
++)
260 twopwr
+= extratwos
+ ieee_multiply(mant
, mult
);
261 extratwos
= extratwos
* 2 + ieee_multiply(mult
, mult
);
266 * Conversion is done. The elements of `mant' contain the first
267 * fractional places of a base-2^16 real number in [0.5,1)
268 * which we can multiply by 2^twopwr to get X. Or, of course,
275 * Shift a mantissa to the right by i (i < 16) bits.
277 static void ieee_shr(uint16_t *mant
, int i
)
282 for (j
= 0; j
< MANT_WORDS
; j
++) {
283 m
= (mant
[j
] << (16 - i
)) & 0xFFFF;
284 mant
[j
] = (mant
[j
] >> i
) | n
;
290 * Round a mantissa off after i words.
292 static int ieee_round(uint16_t *mant
, int i
)
294 if (mant
[i
] & 0x8000) {
298 } while (i
> 0 && !mant
[i
]);
299 return !i
&& !mant
[i
];
304 #define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
306 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
307 static void set_bit(uint16_t *mant
, int bit
)
309 mant
[bit
>> 4] |= 1 << (~bit
& 15);
312 /* Produce standard IEEE formats, with implicit "1" bit; this makes
313 the following assumptions:
315 - the sign bit is the MSB, followed by the exponent.
316 - the sign bit plus exponent fit in 16 bits.
317 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
321 int mantissa
; /* Bits in the mantissa */
322 int exponent
; /* Bits in the exponent */
325 static const struct ieee_format ieee_16
= { 1, 10, 5 };
326 static const struct ieee_format ieee_32
= { 2, 23, 8 };
327 static const struct ieee_format ieee_64
= { 4, 52, 11 };
328 static const struct ieee_format ieee_128
= { 8, 112, 15 };
330 /* Produce all the standard IEEE formats: 16, 32, 64, and 128 bits */
331 static int to_float(char *str
, int32_t sign
, uint8_t *result
,
332 const struct ieee_format
*fmt
, efunc error
)
334 uint16_t mant
[MANT_WORDS
], *mp
;
336 int32_t expmax
= 1 << (fmt
->exponent
-1);
337 uint16_t implicit_one
= 0x8000 >> fmt
->exponent
;
340 sign
= (sign
< 0 ? 0x8000L
: 0L);
343 /* NaN or Infinity */
344 int32_t expmask
= (1 << fmt
->exponent
)-1;
346 memset(mant
, 0, sizeof mant
);
347 mant
[0] = expmask
<< (15-fmt
->exponent
); /* Exponent: all bits one */
350 case 'n': /* __nan__ */
352 case 'q': /* __qnan__ */
354 set_bit(mant
, fmt
->exponent
+1); /* Highest bit in mantissa */
356 case 's': /* __snan__ */
358 set_bit(mant
, fmt
->exponent
+fmt
->mantissa
); /* Last bit */
360 case 'i': /* __infinity__ */
365 ieee_flconvert(str
, mant
, &exponent
, error
);
366 if (mant
[0] & 0x8000) {
371 if (exponent
>= 2-expmax
&& exponent
<= expmax
) {
376 ieee_shr(mant
, fmt
->exponent
);
377 ieee_round(mant
, fmt
->words
);
378 /* did we scale up by one? */
379 if (mant
[0] & (implicit_one
<< 1)) {
384 mant
[0] &= (implicit_one
-1); /* remove leading one */
385 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
386 } else if (exponent
< 2-expmax
&&
387 exponent
>= 2-expmax
-fmt
->mantissa
) {
391 int shift
= -(exponent
+ expmax
-2-fmt
->exponent
);
392 int sh
= shift
% 16, wds
= shift
/ 16;
394 if (ieee_round(mant
, fmt
->words
- wds
)
395 || (sh
> 0 && (mant
[0] & (0x8000 >> (sh
- 1))))) {
403 for (i
= fmt
->words
-1; i
>= wds
; i
--)
404 mant
[i
] = mant
[i
-wds
];
410 error(ERR_NONFATAL
, "overflow in floating-point constant");
413 memset(mant
, 0, 2*fmt
->words
);
418 memset(mant
, 0, 2*fmt
->words
);
424 for (mp
= &mant
[fmt
->words
], i
= 0; i
< fmt
->words
; i
++) {
430 return 1; /* success */
433 /* 80-bit format with 64-bit mantissa *including an explicit integer 1*
434 and 15-bit exponent. */
435 static int to_ldoub(char *str
, int32_t sign
, uint8_t *result
,
438 uint16_t mant
[MANT_WORDS
];
441 sign
= (sign
< 0 ? 0x8000L
: 0L);
444 uint16_t is_snan
= 0, is_qnan
= 0x8000;
460 put(result
+ 0, is_snan
);
463 put(result
+ 6, is_qnan
);
464 put(result
+ 8, 0x7fff|sign
);
468 ieee_flconvert(str
, mant
, &exponent
, error
);
469 if (mant
[0] & 0x8000) {
474 if (exponent
>= -16383 && exponent
<= 16384) {
479 if (ieee_round(mant
, 4)) /* did we scale up by one? */
480 ieee_shr(mant
, 1), mant
[0] |= 0x8000, exponent
++;
481 put(result
+ 0, mant
[3]);
482 put(result
+ 2, mant
[2]);
483 put(result
+ 4, mant
[1]);
484 put(result
+ 6, mant
[0]);
485 put(result
+ 8, exponent
| sign
);
486 } else if (exponent
< -16383 && exponent
>= -16446) {
490 int shift
= -(exponent
+ 16383);
491 int sh
= shift
% 16, wds
= shift
/ 16;
493 if (ieee_round(mant
, 4 - wds
)
494 || (sh
> 0 && (mant
[0] & (0x8000 >> (sh
- 1))))) {
500 put(result
+ 0, (wds
<= 3 ? mant
[3 - wds
] : 0));
501 put(result
+ 2, (wds
<= 2 ? mant
[2 - wds
] : 0));
502 put(result
+ 4, (wds
<= 1 ? mant
[1 - wds
] : 0));
503 put(result
+ 6, (wds
== 0 ? mant
[0] : 0));
504 put(result
+ 8, sign
);
507 error(ERR_NONFATAL
, "overflow in floating-point constant");
522 put(result
+ 8, sign
);
527 int float_const(char *number
, int32_t sign
, uint8_t *result
, int bytes
,
532 return to_float(number
, sign
, result
, &ieee_16
, error
);
534 return to_float(number
, sign
, result
, &ieee_32
, error
);
536 return to_float(number
, sign
, result
, &ieee_64
, error
);
538 return to_ldoub(number
, sign
, result
, error
);
540 return to_float(number
, sign
, result
, &ieee_128
, error
);
542 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);