blob: d22aa19cf292bfe448f6353dda206186d7f3205a [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* float.c floating-point constant support for the Netwide Assembler
2 *
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.
7 *
8 * initial version 13/ix/96 by Simon Tatham
9 */
10
H. Peter Anvinfe2177f2007-09-18 18:31:26 -070011#include <ctype.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000012#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000015#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000016
17#include "nasm.h"
18
19#define TRUE 1
20#define FALSE 0
21
H. Peter Anvine31747e2007-09-18 17:50:34 -070022#define MANT_WORDS 10 /* 112 bits + 48 for accuracy == 160 */
23#define MANT_DIGITS 49 /* 50 digits don't fit in 160 bits */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000024
25/*
26 * guaranteed top bit of from is set
27 * => we only have to worry about _one_ bit shift to the left
28 */
29
Keith Kaniosb7a89542007-04-12 02:40:54 +000030static int ieee_multiply(uint16_t *to, uint16_t *from)
H. Peter Anvineba20a72002-04-30 20:53:55 +000031{
Keith Kaniosb7a89542007-04-12 02:40:54 +000032 uint32_t temp[MANT_WORDS * 2];
H. Peter Anvine2c80182005-01-15 22:15:51 +000033 int i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034
H. Peter Anvine2c80182005-01-15 22:15:51 +000035 for (i = 0; i < MANT_WORDS * 2; i++)
36 temp[i] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037
H. Peter Anvine2c80182005-01-15 22:15:51 +000038 for (i = 0; i < MANT_WORDS; i++)
39 for (j = 0; j < MANT_WORDS; j++) {
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 uint32_t n;
41 n = (uint32_t)to[i] * (uint32_t)from[j];
H. Peter Anvine2c80182005-01-15 22:15:51 +000042 temp[i + j] += n >> 16;
43 temp[i + j + 1] += n & 0xFFFF;
44 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045
H. Peter Anvine2c80182005-01-15 22:15:51 +000046 for (i = MANT_WORDS * 2; --i;) {
47 temp[i - 1] += temp[i] >> 16;
48 temp[i] &= 0xFFFF;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049 }
50 if (temp[0] & 0x8000) {
H. Peter Anvine31747e2007-09-18 17:50:34 -070051 memcpy(to, temp, 2*MANT_WORDS);
52 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +000054 for (i = 0; i < MANT_WORDS; i++)
55 to[i] = (temp[i] << 1) + !!(temp[i + 1] & 0x8000);
56 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057 }
58}
59
H. Peter Anvinfe2177f2007-09-18 18:31:26 -070060static int hexval(char c)
61{
62 if (c >= '0' && c <= '9')
63 return c-'0';
64 else if (c >= 'a' && c <= 'f')
65 return c-'a'+10;
66 else
67 return c-'A'+10;
68}
69
70static void ieee_flconvert_hex(char *string, uint16_t *mant,
71 int32_t *exponent, efunc error)
72{
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;
76 int ms;
77 int32_t twopwr;
78 int seendot, seendigit;
79 unsigned char c;
80
81 twopwr = 0;
82 seendot = seendigit = 0;
83
84 memset(mult, 0, sizeof mult);
85
86 while ((c = *string++) != '\0') {
87 if (c == '.') {
88 if (!seendot)
89 seendot = TRUE;
90 else {
91 error(ERR_NONFATAL,
92 "too many periods in floating-point constant");
93 return;
94 }
95 } else if (isxdigit(c)) {
96 int v = hexval(c);
97
98 if (!seendigit && v) {
99 int l = log2tbl[v];
100
101 seendigit = 1;
102 mp = mult;
103 ms = 15-l;
104
105 twopwr = seendot ? twopwr-4+l : l-3;
106 }
107
108 if (seendigit) {
109 if (ms <= 0) {
110 *mp |= v >> -ms;
111 mp++;
112 if (mp > &mult[MANT_WORDS])
113 mp = &mult[MANT_WORDS]; /* Guard slot */
114 ms += 16;
115 }
116 *mp |= v << ms;
117 ms -= 4;
118
119 if (!seendot)
120 twopwr += 4;
121 } else {
122 if (seendot)
123 twopwr -= 4;
124 }
125 } else if (c == 'p' || c == 'P') {
126 twopwr += atoi(string);
127 break;
128 } else {
129 error(ERR_NONFATAL,
130 "floating-point constant: `%c' is invalid character",
H. Peter Anvin26976a12007-09-18 18:33:17 -0700131 c);
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700132 return;
133 }
134 }
135
136 if (!seendigit) {
137 memset(mant, 0, 2*MANT_WORDS); /* Zero */
138 *exponent = 0;
139 } else {
140 memcpy(mant, mult, 2*MANT_WORDS);
141 *exponent = twopwr;
142 }
143}
144
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000145static void ieee_flconvert(char *string, uint16_t *mant,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000146 int32_t *exponent, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000147{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000148 char digits[MANT_DIGITS];
149 char *p, *q, *r;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000150 uint16_t mult[MANT_WORDS], bit;
151 uint16_t *m;
152 int32_t tenpwr, twopwr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000153 int extratwos, started, seendot;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700155 if (string[0] == '0' && (string[1] == 'x' || string[1] == 'X')) {
156 ieee_flconvert_hex(string+2, mant, exponent, error);
157 return;
158 }
159
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000160 p = digits;
161 tenpwr = 0;
162 started = seendot = FALSE;
163 while (*string && *string != 'E' && *string != 'e') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164 if (*string == '.') {
165 if (!seendot)
166 seendot = TRUE;
167 else {
168 error(ERR_NONFATAL,
169 "too many periods in floating-point constant");
170 return;
171 }
172 } else if (*string >= '0' && *string <= '9') {
173 if (*string == '0' && !started) {
174 if (seendot)
175 tenpwr--;
176 } else {
177 started = TRUE;
178 if (p < digits + sizeof(digits))
179 *p++ = *string - '0';
180 if (!seendot)
181 tenpwr++;
182 }
183 } else {
184 error(ERR_NONFATAL,
185 "floating-point constant: `%c' is invalid character",
186 *string);
187 return;
188 }
189 string++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000190 }
191 if (*string) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000192 string++; /* eat the E */
193 tenpwr += atoi(string);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000194 }
195
196 /*
197 * At this point, the memory interval [digits,p) contains a
198 * series of decimal digits zzzzzzz such that our number X
199 * satisfies
200 *
201 * X = 0.zzzzzzz * 10^tenpwr
202 */
203
204 bit = 0x8000;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000205 for (m = mant; m < mant + MANT_WORDS; m++)
206 *m = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000207 m = mant;
208 q = digits;
209 started = FALSE;
210 twopwr = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 while (m < mant + MANT_WORDS) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000212 uint16_t carry = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000213 while (p > q && !p[-1])
214 p--;
215 if (p <= q)
216 break;
217 for (r = p; r-- > q;) {
218 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000219
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 i = 2 * *r + carry;
221 if (i >= 10)
222 carry = 1, i -= 10;
223 else
224 carry = 0;
225 *r = i;
226 }
227 if (carry)
228 *m |= bit, started = TRUE;
229 if (started) {
230 if (bit == 1)
231 bit = 0x8000, m++;
232 else
233 bit >>= 1;
234 } else
235 twopwr--;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000236 }
237 twopwr += tenpwr;
238
239 /*
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.
244 */
245
246 if (tenpwr < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247 for (m = mult; m < mult + MANT_WORDS; m++)
248 *m = 0xCCCC;
249 extratwos = -2;
250 tenpwr = -tenpwr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000251 } else if (tenpwr > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000252 mult[0] = 0xA000;
253 for (m = mult + 1; m < mult + MANT_WORDS; m++)
254 *m = 0;
255 extratwos = 3;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000256 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257 extratwos = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000258 while (tenpwr) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259 if (tenpwr & 1)
260 twopwr += extratwos + ieee_multiply(mant, mult);
261 extratwos = extratwos * 2 + ieee_multiply(mult, mult);
262 tenpwr >>= 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000263 }
264
265 /*
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,
269 * it contains zero.
270 */
271 *exponent = twopwr;
272}
273
274/*
275 * Shift a mantissa to the right by i (i < 16) bits.
276 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000277static void ieee_shr(uint16_t *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000278{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000279 uint16_t n = 0, m;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000280 int j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000281
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282 for (j = 0; j < MANT_WORDS; j++) {
283 m = (mant[j] << (16 - i)) & 0xFFFF;
284 mant[j] = (mant[j] >> i) | n;
285 n = m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000286 }
287}
288
289/*
290 * Round a mantissa off after i words.
291 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000292static int ieee_round(uint16_t *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000293{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000294 if (mant[i] & 0x8000) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295 do {
296 ++mant[--i];
297 mant[i] &= 0xFFFF;
298 } while (i > 0 && !mant[i]);
299 return !i && !mant[i];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000300 }
301 return 0;
302}
303
304#define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
305
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700306/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
307static void set_bit(uint16_t *mant, int bit)
308{
309 mant[bit >> 4] |= 1 << (~bit & 15);
310}
311
H. Peter Anvine31747e2007-09-18 17:50:34 -0700312/* Produce standard IEEE formats, with implicit "1" bit; this makes
313 the following assumptions:
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000314
H. Peter Anvine31747e2007-09-18 17:50:34 -0700315 - 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 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000318
H. Peter Anvine31747e2007-09-18 17:50:34 -0700319struct ieee_format {
320 int words;
321 int mantissa; /* Bits in the mantissa */
322 int exponent; /* Bits in the exponent */
323};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000324
H. Peter Anvine31747e2007-09-18 17:50:34 -0700325static const struct ieee_format ieee_16 = { 1, 10, 5 };
326static const struct ieee_format ieee_32 = { 2, 23, 8 };
327static const struct ieee_format ieee_64 = { 4, 52, 11 };
328static const struct ieee_format ieee_128 = { 8, 112, 15 };
329
330/* Produce all the standard IEEE formats: 16, 32, 64, and 128 bits */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000331static int to_float(char *str, int32_t sign, uint8_t *result,
H. Peter Anvine31747e2007-09-18 17:50:34 -0700332 const struct ieee_format *fmt, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000333{
H. Peter Anvine31747e2007-09-18 17:50:34 -0700334 uint16_t mant[MANT_WORDS], *mp;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000335 int32_t exponent;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700336 int32_t expmax = 1 << (fmt->exponent-1);
337 uint16_t implicit_one = 0x8000 >> fmt->exponent;
338 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339
340 sign = (sign < 0 ? 0x8000L : 0L);
341
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700342 if (str[0] == '_') {
343 /* NaN or Infinity */
344 int32_t expmask = (1 << fmt->exponent)-1;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700345
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700346 memset(mant, 0, sizeof mant);
347 mant[0] = expmask << (15-fmt->exponent); /* Exponent: all bits one */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700348
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700349 switch (str[2]) {
350 case 'n': /* __nan__ */
351 case 'N':
352 case 'q': /* __qnan__ */
353 case 'Q':
354 set_bit(mant, fmt->exponent+1); /* Highest bit in mantissa */
355 break;
356 case 's': /* __snan__ */
357 case 'S':
358 set_bit(mant, fmt->exponent+fmt->mantissa); /* Last bit */
359 break;
360 case 'i': /* __infinity__ */
361 case 'I':
362 break;
363 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000364 } else {
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700365 ieee_flconvert(str, mant, &exponent, error);
366 if (mant[0] & 0x8000) {
367 /*
368 * Non-zero.
369 */
370 exponent--;
371 if (exponent >= 2-expmax && exponent <= expmax) {
372 /*
373 * Normalised.
374 */
375 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)) {
380 ieee_shr(mant, 1);
381 exponent++;
382 }
383
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) {
388 /*
389 * Denormal.
390 */
391 int shift = -(exponent + expmax-2-fmt->exponent);
392 int sh = shift % 16, wds = shift / 16;
393 ieee_shr(mant, sh);
394 if (ieee_round(mant, fmt->words - wds)
395 || (sh > 0 && (mant[0] & (0x8000 >> (sh - 1))))) {
396 ieee_shr(mant, 1);
397 if (sh == 0)
398 mant[0] |= 0x8000;
399 exponent++;
400 }
401
402 if (wds) {
403 for (i = fmt->words-1; i >= wds; i--)
404 mant[i] = mant[i-wds];
405 for (; i >= 0; i--)
406 mant[i] = 0;
407 }
408 } else {
409 if (exponent > 0) {
410 error(ERR_NONFATAL, "overflow in floating-point constant");
411 return 0;
412 } else {
413 memset(mant, 0, 2*fmt->words);
414 }
415 }
416 } else {
417 /* Zero */
418 memset(mant, 0, 2*fmt->words);
419 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000420 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000421
H. Peter Anvine31747e2007-09-18 17:50:34 -0700422 mant[0] |= sign;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700423
H. Peter Anvine31747e2007-09-18 17:50:34 -0700424 for (mp = &mant[fmt->words], i = 0; i < fmt->words; i++) {
425 uint16_t m = *--mp;
426 put(result, m);
427 result += 2;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700428 }
H. Peter Anvine31747e2007-09-18 17:50:34 -0700429
430 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700431}
432
433/* 80-bit format with 64-bit mantissa *including an explicit integer 1*
434 and 15-bit exponent. */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000435static int to_ldoub(char *str, int32_t sign, uint8_t *result,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000437{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000438 uint16_t mant[MANT_WORDS];
439 int32_t exponent;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000440
441 sign = (sign < 0 ? 0x8000L : 0L);
442
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700443 if (str[0] == '_') {
444 uint16_t is_snan = 0, is_qnan = 0x8000;
445 switch (str[2]) {
446 case 'n':
447 case 'N':
448 case 'q':
449 case 'Q':
450 is_qnan = 0xc000;
451 break;
452 case 's':
453 case 'S':
454 is_snan = 1;
455 break;
456 case 'i':
457 case 'I':
458 break;
459 }
460 put(result + 0, is_snan);
461 put(result + 2, 0);
462 put(result + 4, 0);
463 put(result + 6, is_qnan);
464 put(result + 8, 0x7fff|sign);
465 return 1;
466 }
467
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 ieee_flconvert(str, mant, &exponent, error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000469 if (mant[0] & 0x8000) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 /*
471 * Non-zero.
472 */
473 exponent--;
474 if (exponent >= -16383 && exponent <= 16384) {
475 /*
476 * Normalised.
477 */
478 exponent += 16383;
479 if (ieee_round(mant, 4)) /* did we scale up by one? */
480 ieee_shr(mant, 1), mant[0] |= 0x8000, exponent++;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000481 put(result + 0, mant[3]);
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700482 put(result + 2, mant[2]);
483 put(result + 4, mant[1]);
484 put(result + 6, mant[0]);
485 put(result + 8, exponent | sign);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 } else if (exponent < -16383 && exponent >= -16446) {
487 /*
488 * Denormal.
489 */
490 int shift = -(exponent + 16383);
491 int sh = shift % 16, wds = shift / 16;
492 ieee_shr(mant, sh);
493 if (ieee_round(mant, 4 - wds)
494 || (sh > 0 && (mant[0] & (0x8000 >> (sh - 1))))) {
495 ieee_shr(mant, 1);
496 if (sh == 0)
497 mant[0] |= 0x8000;
498 exponent++;
499 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 put(result + 0, (wds <= 3 ? mant[3 - wds] : 0));
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700501 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);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 } else {
506 if (exponent > 0) {
507 error(ERR_NONFATAL, "overflow in floating-point constant");
508 return 0;
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700509 } else {
510 goto zero;
511 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000513 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 /*
515 * Zero.
516 */
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700517 zero:
518 put(result + 0, 0);
519 put(result + 2, 0);
520 put(result + 4, 0);
521 put(result + 6, 0);
522 put(result + 8, sign);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523 }
524 return 1;
525}
526
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000527int float_const(char *number, int32_t sign, uint8_t *result, int bytes,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529{
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700530 switch (bytes) {
531 case 2:
H. Peter Anvine31747e2007-09-18 17:50:34 -0700532 return to_float(number, sign, result, &ieee_16, error);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700533 case 4:
H. Peter Anvine31747e2007-09-18 17:50:34 -0700534 return to_float(number, sign, result, &ieee_32, error);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700535 case 8:
H. Peter Anvine31747e2007-09-18 17:50:34 -0700536 return to_float(number, sign, result, &ieee_64, error);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700537 case 10:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 return to_ldoub(number, sign, result, error);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700539 case 16:
540 return to_float(number, sign, result, &ieee_128, error);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700541 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
543 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000544 }
545}