1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright (C) 1987, 1992 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define bzero(s,n) memset(s,0,n)
24 #define bcopy(from,to,n) memcpy((to),(from),(n))
27 extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
33 extern char EXP_CHARS[];
34 /* Precision in LittleNums. */
35 #define MAX_PRECISION (6)
36 #define F_PRECISION (2)
37 #define D_PRECISION (4)
38 #define X_PRECISION (6)
39 #define P_PRECISION (6)
41 /* Length in LittleNums of guard bits. */
44 static unsigned long mask [] = {
81 static int bits_left_in_littlenum;
82 static int littlenums_left;
83 static LITTLENUM_TYPE *littlenum_pointer;
86 next_bits (number_of_bits)
93 if (number_of_bits >= bits_left_in_littlenum) {
94 return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
95 number_of_bits -= bits_left_in_littlenum;
96 return_value <<= number_of_bits;
98 if (--littlenums_left) {
99 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
101 return_value |= (*littlenum_pointer >> bits_left_in_littlenum) & mask[number_of_bits];
104 bits_left_in_littlenum -= number_of_bits;
105 return_value = mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum);
107 return(return_value);
110 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS */
115 if (!littlenums_left) {
118 bits_left_in_littlenum = num;
119 } else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS) {
120 bits_left_in_littlenum = num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum);
124 bits_left_in_littlenum += num;
128 make_invalid_floating_point_number(words)
129 LITTLENUM_TYPE *words;
131 as_bad("cannot create floating-point number");
132 words[0] = ((unsigned) -1) >> 1; /* Zero the leftmost bit */
140 /***********************************************************************\
141 * Warning: this returns 16-bit LITTLENUMs. It is up to the caller *
142 * to figure out any alignment problems and to conspire for the *
143 * bytes/word to be emitted in the right order. Bigendians beware! *
145 \***********************************************************************/
147 /* Note that atof-ieee always has X and P precisions enabled. it is up
148 to md_atof to filter them out if the target machine does not support
151 char * /* Return pointer past text consumed. */
152 atof_ieee(str, what_kind, words)
153 char *str; /* Text to convert to binary. */
154 char what_kind; /* 'd', 'f', 'g', 'h' */
155 LITTLENUM_TYPE *words; /* Build the binary here. */
157 static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
158 /* Extra bits for zeroed low-order bits. */
159 /* The 1st MAX_PRECISION are zeroed, */
160 /* the last contain flonum bits. */
162 int precision; /* Number of 16-bit words in the format. */
164 FLONUM_TYPE save_gen_flonum;
166 /* We have to save the generic_floating_point_number because it
167 contains storage allocation about the array of LITTLENUMs
168 where the value is actually stored. We will allocate our
169 own array of littlenums below, but have to restore the global
171 save_gen_flonum = generic_floating_point_number;
174 generic_floating_point_number.low = bits + MAX_PRECISION;
175 generic_floating_point_number.high = NULL;
176 generic_floating_point_number.leader = NULL;
177 generic_floating_point_number.exponent = NULL;
178 generic_floating_point_number.sign = '\0';
180 /* Use more LittleNums than seems */
181 /* necessary: the highest flonum may have */
182 /* 15 leading 0 bits, so could be useless. */
184 bzero(bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
191 precision = F_PRECISION;
199 precision = D_PRECISION;
207 precision = X_PRECISION;
214 precision = P_PRECISION;
219 make_invalid_floating_point_number(words);
223 generic_floating_point_number.high = generic_floating_point_number.low + precision - 1 + GUARD;
225 if (atof_generic(&return_value, ".", EXP_CHARS, &generic_floating_point_number)) {
226 /* as_bad("Error converting floating point number (Exponent overflow?)"); */
227 make_invalid_floating_point_number(words);
230 gen_to_words(words, precision, exponent_bits);
232 /* Restore the generic_floating_point_number's storage alloc
233 (and everything else). */
234 generic_floating_point_number = save_gen_flonum;
236 return(return_value);
239 /* Turn generic_floating_point_number into a real float/double/extended */
240 int gen_to_words(words, precision, exponent_bits)
241 LITTLENUM_TYPE *words;
245 int return_value = 0;
251 int exponent_skippage;
252 LITTLENUM_TYPE word1;
255 if (generic_floating_point_number.low > generic_floating_point_number.leader) {
257 if (generic_floating_point_number.sign == '+')
261 bzero(&words[1], sizeof(LITTLENUM_TYPE) * (precision - 1));
262 return(return_value);
265 /* NaN: Do the right thing */
266 if (generic_floating_point_number.sign == 0) {
267 if (precision == F_PRECISION) {
277 } else if (generic_floating_point_number.sign == 'P') {
278 /* +INF: Do the right thing */
279 if (precision == F_PRECISION) {
288 return(return_value);
289 } else if (generic_floating_point_number.sign == 'N') {
291 if (precision == F_PRECISION) {
300 return(return_value);
303 * The floating point formats we support have:
304 * Bit 15 is sign bit.
305 * Bits 14:n are excess-whatever exponent.
306 * Bits n-1:0 (if any) are most significant bits of fraction.
307 * Bits 15:0 of the next word(s) are the next most significant bits.
309 * So we need: number of bits of exponent, number of bits of
312 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
313 littlenum_pointer = generic_floating_point_number.leader;
314 littlenums_left = 1 + generic_floating_point_number.leader - generic_floating_point_number.low;
315 /* Seek (and forget) 1st significant bit */
316 for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++) ;;
317 exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader
318 + 1 - generic_floating_point_number.low;
319 /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
320 exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
322 exponent_3 = exponent_2 - exponent_skippage;
323 /* Forget leading zeros, forget 1st bit. */
324 exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
325 /* Offset exponent. */
329 /* Word 1. Sign, exponent and perhaps high bits. */
330 word1 = (generic_floating_point_number.sign == '+') ? 0 : (1 << (LITTLENUM_NUMBER_OF_BITS - 1));
332 /* Assume 2's complement integers. */
333 if (exponent_4 < 1 && exponent_4 >= -62) {
338 num_bits = -exponent_4;
339 prec_bits = LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits);
340 if(precision == X_PRECISION && exponent_bits == 15)
341 prec_bits -= LITTLENUM_NUMBER_OF_BITS + 1;
343 if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits) {
344 /* Bigger than one littlenum */
345 num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits;
347 if (num_bits + exponent_bits + 1 >= precision * LITTLENUM_NUMBER_OF_BITS) {
348 /* Exponent overflow */
349 make_invalid_floating_point_number(words);
350 return(return_value);
352 if (precision == X_PRECISION && exponent_bits == 15) {
355 num_bits -= LITTLENUM_NUMBER_OF_BITS - 1;
357 while (num_bits >= LITTLENUM_NUMBER_OF_BITS) {
358 num_bits -= LITTLENUM_NUMBER_OF_BITS;
362 *lp++ = next_bits(LITTLENUM_NUMBER_OF_BITS - (num_bits));
364 if (precision == X_PRECISION && exponent_bits == 15) {
367 if (num_bits == LITTLENUM_NUMBER_OF_BITS) {
369 *lp++ = next_bits(LITTLENUM_NUMBER_OF_BITS - 1);
370 } else if (num_bits == LITTLENUM_NUMBER_OF_BITS - 1)
373 *lp++ = next_bits(LITTLENUM_NUMBER_OF_BITS - 1 - num_bits);
376 word1 |= next_bits((LITTLENUM_NUMBER_OF_BITS - 1) - (exponent_bits + num_bits));
380 while (lp < words + precision)
381 *lp++ = next_bits(LITTLENUM_NUMBER_OF_BITS);
383 /* Round the mantissa up, but don't change the number */
386 if (prec_bits > LITTLENUM_NUMBER_OF_BITS) {
391 tmp_bits = prec_bits;
392 while (tmp_bits > LITTLENUM_NUMBER_OF_BITS) {
393 if (lp[n] != (LITTLENUM_TYPE) - 1)
396 tmp_bits -= LITTLENUM_NUMBER_OF_BITS;
398 if (tmp_bits > LITTLENUM_NUMBER_OF_BITS || (lp[n] & mask[tmp_bits]) != mask[tmp_bits]) {
401 for (carry = 1; carry && (lp >= words); lp --) {
404 carry >>= LITTLENUM_NUMBER_OF_BITS;
407 } else if ((*lp & mask[prec_bits]) != mask[prec_bits])
412 } else if (exponent_4 & ~ mask [exponent_bits]) {
414 * Exponent overflow. Lose immediately.
418 * We leave return_value alone: admit we read the
419 * number, but return a floating exception
420 * because we can't encode the number.
422 make_invalid_floating_point_number (words);
425 word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits))
426 | next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits);
431 /* X_PRECISION is special: it has 16 bits of zero in the middle,
432 followed by a 1 bit. */
433 if (exponent_bits == 15 && precision == X_PRECISION) {
435 *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS) | next_bits(LITTLENUM_NUMBER_OF_BITS - 1);
438 /* The rest of the words are just mantissa bits. */
439 while(lp < words + precision)
440 *lp++ = next_bits(LITTLENUM_NUMBER_OF_BITS);
445 * Since the NEXT bit is a 1, round UP the mantissa.
446 * The cunning design of these hidden-1 floats permits
447 * us to let the mantissa overflow into the exponent, and
448 * it 'does the right thing'. However, we lose if the
449 * highest-order bit of the lowest-order word flips.
453 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
454 Please allow at least 1 more bit in carry than is in a LITTLENUM.
455 We need that extra bit to hold a carry during a LITTLENUM carry
456 propagation. Another extra bit (kept 0) will assure us that we
457 don't get a sticky sign bit after shifting right, and that
458 permits us to propagate the carry without any masking of bits.
460 for (carry = 1, lp--; carry && (lp >= words); lp--) {
463 carry >>= LITTLENUM_NUMBER_OF_BITS;
465 if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1))) {
466 /* We leave return_value alone: admit we read the
467 * number, but return a floating exception
468 * because we can't encode the number.
470 *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1));
471 /* make_invalid_floating_point_number (words); */
472 /* return return_value; */
475 return (return_value);
478 /* This routine is a real kludge. Someone really should do it better, but
479 I'm too lazy, and I don't understand this stuff all too well anyway
489 sprintf(buf,"%ld",x);
491 if (atof_generic(&bufp, ".", EXP_CHARS, &generic_floating_point_number))
492 as_bad("Error converting number to floating point (Exponent overflow?)");
501 LITTLENUM_TYPE arr[10];
504 static char sbuf[40];
507 f = generic_floating_point_number;
508 generic_floating_point_number = *gen;
510 gen_to_words(&arr[0], 4, 11);
511 bcopy(&arr[0], &dv, sizeof(double));
512 sprintf(sbuf, "%x %x %x %x %.14G ", arr[0], arr[1], arr[2], arr[3], dv);
513 gen_to_words(&arr[0],2,8);
514 bcopy(&arr[0],&fv,sizeof(float));
515 sprintf(sbuf + strlen(sbuf), "%x %x %.12g\n", arr[0], arr[1],
519 generic_floating_point_number = f;
526 /* end of atof-ieee.c */