1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number 2 Copyright (C) 1987, 1992 Free Software Foundation, Inc. 3 4 This file is part of GAS, the GNU Assembler. 5 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) 9 any later version. 10 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. 15 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19 20 #include "as.h" 21 22 extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */ 23 24 #ifndef NULL 25 #define NULL (0) 26 #endif 27 28 extern const char EXP_CHARS[]; 29 /* Precision in LittleNums. */ 30 /* Don't count the gap in the m68k extended precision format. */ 31 #define MAX_PRECISION (5) 32 #define F_PRECISION (2) 33 #define D_PRECISION (4) 34 #define X_PRECISION (5) 35 #define P_PRECISION (5) 36 37 /* Length in LittleNums of guard bits. */ 38 #define GUARD (2) 39 40 static const unsigned long mask[] = 41 { 42 0x00000000, 43 0x00000001, 44 0x00000003, 45 0x00000007, 46 0x0000000f, 47 0x0000001f, 48 0x0000003f, 49 0x0000007f, 50 0x000000ff, 51 0x000001ff, 52 0x000003ff, 53 0x000007ff, 54 0x00000fff, 55 0x00001fff, 56 0x00003fff, 57 0x00007fff, 58 0x0000ffff, 59 0x0001ffff, 60 0x0003ffff, 61 0x0007ffff, 62 0x000fffff, 63 0x001fffff, 64 0x003fffff, 65 0x007fffff, 66 0x00ffffff, 67 0x01ffffff, 68 0x03ffffff, 69 0x07ffffff, 70 0x0fffffff, 71 0x1fffffff, 72 0x3fffffff, 73 0x7fffffff, 74 0xffffffff, 75 }; 76 77 78 static int bits_left_in_littlenum; 79 static int littlenums_left; 80 static LITTLENUM_TYPE *littlenum_pointer; 81 82 static int 83 next_bits (number_of_bits) 84 int number_of_bits; 85 { 86 int return_value; 87 88 if (!littlenums_left) 89 return (0); 90 if (number_of_bits >= bits_left_in_littlenum) 91 { 92 return_value = mask[bits_left_in_littlenum] & *littlenum_pointer; 93 number_of_bits -= bits_left_in_littlenum; 94 return_value <<= number_of_bits; 95 96 if (--littlenums_left) 97 { 98 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits; 99 --littlenum_pointer; 100 return_value |= (*littlenum_pointer >> bits_left_in_littlenum) & mask[number_of_bits]; 101 } 102 } 103 else 104 { 105 bits_left_in_littlenum -= number_of_bits; 106 return_value = mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum); 107 } 108 return (return_value); 109 } 110 111 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS */ 112 static void 113 unget_bits (num) 114 int num; 115 { 116 if (!littlenums_left) 117 { 118 ++littlenum_pointer; 119 ++littlenums_left; 120 bits_left_in_littlenum = num; 121 } 122 else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS) 123 { 124 bits_left_in_littlenum = num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum); 125 ++littlenum_pointer; 126 ++littlenums_left; 127 } 128 else 129 bits_left_in_littlenum += num; 130 } 131 132 static void 133 make_invalid_floating_point_number (words) 134 LITTLENUM_TYPE *words; 135 { 136 as_bad ("cannot create floating-point number"); 137 words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1; /* Zero the leftmost bit */ 138 words[1] = (LITTLENUM_TYPE) -1; 139 words[2] = (LITTLENUM_TYPE) -1; 140 words[3] = (LITTLENUM_TYPE) -1; 141 words[4] = (LITTLENUM_TYPE) -1; 142 words[5] = (LITTLENUM_TYPE) -1; 143 } 144 145 /************************************************************************\ 146 * Warning: this returns 16-bit LITTLENUMs. It is up to the caller * 147 * to figure out any alignment problems and to conspire for the * 148 * bytes/word to be emitted in the right order. Bigendians beware! * 149 * * 150 \************************************************************************/ 151 152 /* Note that atof-ieee always has X and P precisions enabled. it is up 153 to md_atof to filter them out if the target machine does not support 154 them. */ 155 156 /* Returns pointer past text consumed. */ 157 char * 158 atof_ieee (str, what_kind, words) 159 char *str; /* Text to convert to binary. */ 160 char what_kind; /* 'd', 'f', 'g', 'h' */ 161 LITTLENUM_TYPE *words; /* Build the binary here. */ 162 { 163 /* Extra bits for zeroed low-order bits. The 1st MAX_PRECISION are 164 zeroed, the last contain flonum bits. */ 165 static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD]; 166 char *return_value; 167 /* Number of 16-bit words in the format. */ 168 int precision; 169 long exponent_bits; 170 FLONUM_TYPE save_gen_flonum; 171 172 /* We have to save the generic_floating_point_number because it 173 contains storage allocation about the array of LITTLENUMs where 174 the value is actually stored. We will allocate our own array of 175 littlenums below, but have to restore the global one on exit. */ 176 save_gen_flonum = generic_floating_point_number; 177 178 return_value = str; 179 generic_floating_point_number.low = bits + MAX_PRECISION; 180 generic_floating_point_number.high = NULL; 181 generic_floating_point_number.leader = NULL; 182 generic_floating_point_number.exponent = 0; 183 generic_floating_point_number.sign = '\0'; 184 185 /* Use more LittleNums than seems necessary: the highest flonum may 186 have 15 leading 0 bits, so could be useless. */ 187 188 memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION); 189 190 switch (what_kind) 191 { 192 case 'f': 193 case 'F': 194 case 's': 195 case 'S': 196 precision = F_PRECISION; 197 exponent_bits = 8; 198 break; 199 200 case 'd': 201 case 'D': 202 case 'r': 203 case 'R': 204 precision = D_PRECISION; 205 exponent_bits = 11; 206 break; 207 208 case 'x': 209 case 'X': 210 case 'e': 211 case 'E': 212 precision = X_PRECISION; 213 exponent_bits = 15; 214 break; 215 216 case 'p': 217 case 'P': 218 219 precision = P_PRECISION; 220 exponent_bits = -1; 221 break; 222 223 default: 224 make_invalid_floating_point_number (words); 225 return (NULL); 226 } 227 228 generic_floating_point_number.high 229 = generic_floating_point_number.low + precision - 1 + GUARD; 230 231 if (atof_generic (&return_value, ".", EXP_CHARS, 232 &generic_floating_point_number)) 233 { 234 make_invalid_floating_point_number (words); 235 return (NULL); 236 } 237 gen_to_words (words, precision, exponent_bits); 238 239 /* Restore the generic_floating_point_number's storage alloc (and 240 everything else). */ 241 generic_floating_point_number = save_gen_flonum; 242 243 return return_value; 244 } 245 246 /* Turn generic_floating_point_number into a real float/double/extended. */ 247 int 248 gen_to_words (words, precision, exponent_bits) 249 LITTLENUM_TYPE *words; 250 int precision; 251 long exponent_bits; 252 { 253 int return_value = 0; 254 255 long exponent_1; 256 long exponent_2; 257 long exponent_3; 258 long exponent_4; 259 int exponent_skippage; 260 LITTLENUM_TYPE word1; 261 LITTLENUM_TYPE *lp; 262 LITTLENUM_TYPE *words_end; 263 264 words_end = words + precision; 265 #ifdef TC_M68K 266 if (precision == X_PRECISION) 267 /* On the m68k the extended precision format has a gap of 16 bits 268 between the exponent and the mantissa. */ 269 words_end++; 270 #endif 271 272 if (generic_floating_point_number.low > generic_floating_point_number.leader) 273 { 274 /* 0.0e0 seen. */ 275 if (generic_floating_point_number.sign == '+') 276 words[0] = 0x0000; 277 else 278 words[0] = 0x8000; 279 memset (&words[1], '\0', 280 (words_end - words - 1) * sizeof (LITTLENUM_TYPE)); 281 return (return_value); 282 } 283 284 /* NaN: Do the right thing */ 285 if (generic_floating_point_number.sign == 0) 286 { 287 if (precision == F_PRECISION) 288 { 289 words[0] = 0x7fff; 290 words[1] = 0xffff; 291 } 292 else if (precision == X_PRECISION) 293 { 294 #ifdef TC_M68K 295 words[0] = 0x7fff; 296 words[1] = 0; 297 words[2] = 0xffff; 298 words[3] = 0xffff; 299 words[4] = 0xffff; 300 words[5] = 0xffff; 301 #else /* ! TC_M68K */ 302 #ifdef TC_I386 303 words[0] = 0xffff; 304 words[1] = 0xc000; 305 words[2] = 0; 306 words[3] = 0; 307 words[4] = 0; 308 #else /* ! TC_I386 */ 309 abort (); 310 #endif /* ! TC_I386 */ 311 #endif /* ! TC_M68K */ 312 } 313 else 314 { 315 words[0] = 0x7fff; 316 words[1] = 0xffff; 317 words[2] = 0xffff; 318 words[3] = 0xffff; 319 } 320 return return_value; 321 } 322 else if (generic_floating_point_number.sign == 'P') 323 { 324 /* +INF: Do the right thing */ 325 if (precision == F_PRECISION) 326 { 327 words[0] = 0x7f80; 328 words[1] = 0; 329 } 330 else if (precision == X_PRECISION) 331 { 332 #ifdef TC_M68K 333 words[0] = 0x7fff; 334 words[1] = 0; 335 words[2] = 0; 336 words[3] = 0; 337 words[4] = 0; 338 words[5] = 0; 339 #else /* ! TC_M68K */ 340 #ifdef TC_I386 341 words[0] = 0x7fff; 342 words[1] = 0x8000; 343 words[2] = 0; 344 words[3] = 0; 345 words[4] = 0; 346 #else /* ! TC_I386 */ 347 abort (); 348 #endif /* ! TC_I386 */ 349 #endif /* ! TC_M68K */ 350 } 351 else 352 { 353 words[0] = 0x7ff0; 354 words[1] = 0; 355 words[2] = 0; 356 words[3] = 0; 357 } 358 return (return_value); 359 } 360 else if (generic_floating_point_number.sign == 'N') 361 { 362 /* Negative INF */ 363 if (precision == F_PRECISION) 364 { 365 words[0] = 0xff80; 366 words[1] = 0x0; 367 } 368 else if (precision == X_PRECISION) 369 { 370 #ifdef TC_M68K 371 words[0] = 0xffff; 372 words[1] = 0; 373 words[2] = 0; 374 words[3] = 0; 375 words[4] = 0; 376 words[5] = 0; 377 #else /* ! TC_M68K */ 378 #ifdef TC_I386 379 words[0] = 0xffff; 380 words[1] = 0x8000; 381 words[2] = 0; 382 words[3] = 0; 383 words[4] = 0; 384 #else /* ! TC_I386 */ 385 abort (); 386 #endif /* ! TC_I386 */ 387 #endif /* ! TC_M68K */ 388 } 389 else 390 { 391 words[0] = 0xfff0; 392 words[1] = 0x0; 393 words[2] = 0x0; 394 words[3] = 0x0; 395 } 396 return (return_value); 397 } 398 /* 399 * The floating point formats we support have: 400 * Bit 15 is sign bit. 401 * Bits 14:n are excess-whatever exponent. 402 * Bits n-1:0 (if any) are most significant bits of fraction. 403 * Bits 15:0 of the next word(s) are the next most significant bits. 404 * 405 * So we need: number of bits of exponent, number of bits of 406 * mantissa. 407 */ 408 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS; 409 littlenum_pointer = generic_floating_point_number.leader; 410 littlenums_left = (1 411 + generic_floating_point_number.leader 412 - generic_floating_point_number.low); 413 /* Seek (and forget) 1st significant bit */ 414 for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage);; 415 exponent_1 = (generic_floating_point_number.exponent 416 + generic_floating_point_number.leader 417 + 1 418 - generic_floating_point_number.low); 419 /* Radix LITTLENUM_RADIX, point just higher than 420 generic_floating_point_number.leader. */ 421 exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS; 422 /* Radix 2. */ 423 exponent_3 = exponent_2 - exponent_skippage; 424 /* Forget leading zeros, forget 1st bit. */ 425 exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2); 426 /* Offset exponent. */ 427 428 lp = words; 429 430 /* Word 1. Sign, exponent and perhaps high bits. */ 431 word1 = ((generic_floating_point_number.sign == '+') 432 ? 0 433 : (1 << (LITTLENUM_NUMBER_OF_BITS - 1))); 434 435 /* Assume 2's complement integers. */ 436 if (exponent_4 <= 0) 437 { 438 int prec_bits; 439 int num_bits; 440 441 unget_bits (1); 442 num_bits = -exponent_4; 443 prec_bits = LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits); 444 #ifdef TC_I386 445 if (precision == X_PRECISION && exponent_bits == 15) 446 { 447 /* On the i386 a denormalized extended precision float is 448 shifted down by one, effectively decreasing the exponent 449 bias by one. */ 450 prec_bits -= 1; 451 num_bits += 1; 452 } 453 #endif 454 455 if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits) 456 { 457 /* Bigger than one littlenum */ 458 num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits; 459 *lp++ = word1; 460 if (num_bits + exponent_bits + 1 >= precision * LITTLENUM_NUMBER_OF_BITS) 461 { 462 /* Exponent overflow */ 463 make_invalid_floating_point_number (words); 464 return (return_value); 465 } 466 #ifdef TC_M68K 467 if (precision == X_PRECISION && exponent_bits == 15) 468 *lp++ = 0; 469 #endif 470 while (num_bits >= LITTLENUM_NUMBER_OF_BITS) 471 { 472 num_bits -= LITTLENUM_NUMBER_OF_BITS; 473 *lp++ = 0; 474 } 475 if (num_bits) 476 *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - (num_bits)); 477 } 478 else 479 { 480 if (precision == X_PRECISION && exponent_bits == 15) 481 { 482 *lp++ = word1; 483 #ifdef TC_M68K 484 *lp++ = 0; 485 #endif 486 *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - num_bits); 487 } 488 else 489 { 490 word1 |= next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - (exponent_bits + num_bits)); 491 *lp++ = word1; 492 } 493 } 494 while (lp < words_end) 495 *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS); 496 497 /* Round the mantissa up, but don't change the number */ 498 if (next_bits (1)) 499 { 500 --lp; 501 if (prec_bits > LITTLENUM_NUMBER_OF_BITS) 502 { 503 int n = 0; 504 int tmp_bits; 505 506 n = 0; 507 tmp_bits = prec_bits; 508 while (tmp_bits > LITTLENUM_NUMBER_OF_BITS) 509 { 510 if (lp[n] != (LITTLENUM_TYPE) - 1) 511 break; 512 --n; 513 tmp_bits -= LITTLENUM_NUMBER_OF_BITS; 514 } 515 if (tmp_bits > LITTLENUM_NUMBER_OF_BITS || (lp[n] & mask[tmp_bits]) != mask[tmp_bits]) 516 { 517 unsigned long carry; 518 519 for (carry = 1; carry && (lp >= words); lp--) 520 { 521 carry = *lp + carry; 522 *lp = carry; 523 carry >>= LITTLENUM_NUMBER_OF_BITS; 524 } 525 } 526 } 527 else if ((*lp & mask[prec_bits]) != mask[prec_bits]) 528 *lp += 1; 529 } 530 531 return return_value; 532 } 533 else if (exponent_4 >= mask[exponent_bits]) 534 { 535 /* 536 * Exponent overflow. Lose immediately. 537 */ 538 539 /* 540 * We leave return_value alone: admit we read the 541 * number, but return a floating exception 542 * because we can't encode the number. 543 */ 544 make_invalid_floating_point_number (words); 545 return return_value; 546 } 547 else 548 { 549 word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits)) 550 | next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits); 551 } 552 553 *lp++ = word1; 554 555 /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the 556 middle. Either way, it is then followed by a 1 bit. */ 557 if (exponent_bits == 15 && precision == X_PRECISION) 558 { 559 #ifdef TC_M68K 560 *lp++ = 0; 561 #endif 562 *lp++ = (1 << (LITTLENUM_NUMBER_OF_BITS - 1) 563 | next_bits (LITTLENUM_NUMBER_OF_BITS - 1)); 564 } 565 566 /* The rest of the words are just mantissa bits. */ 567 while (lp < words_end) 568 *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS); 569 570 if (next_bits (1)) 571 { 572 unsigned long carry; 573 /* 574 * Since the NEXT bit is a 1, round UP the mantissa. 575 * The cunning design of these hidden-1 floats permits 576 * us to let the mantissa overflow into the exponent, and 577 * it 'does the right thing'. However, we lose if the 578 * highest-order bit of the lowest-order word flips. 579 * Is that clear? 580 */ 581 582 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2) 583 Please allow at least 1 more bit in carry than is in a LITTLENUM. 584 We need that extra bit to hold a carry during a LITTLENUM carry 585 propagation. Another extra bit (kept 0) will assure us that we 586 don't get a sticky sign bit after shifting right, and that 587 permits us to propagate the carry without any masking of bits. 588 #endif */ 589 for (carry = 1, lp--; carry && (lp >= words); lp--) 590 { 591 carry = *lp + carry; 592 *lp = carry; 593 carry >>= LITTLENUM_NUMBER_OF_BITS; 594 } 595 if (precision == X_PRECISION && exponent_bits == 15) 596 { 597 /* Extended precision numbers have an explicit integer bit 598 that we may have to restore. */ 599 if (lp == words) 600 { 601 #ifdef TC_M68K 602 /* On the m68k there is a gap of 16 bits. We must 603 explicitly propagate the carry into the exponent. */ 604 words[0] += words[1]; 605 words[1] = 0; 606 lp++; 607 #endif 608 /* Put back the integer bit. */ 609 lp[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS - 1); 610 } 611 } 612 if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1))) 613 { 614 /* We leave return_value alone: admit we read the 615 * number, but return a floating exception 616 * because we can't encode the number. 617 */ 618 *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1)); 619 /* make_invalid_floating_point_number (words); */ 620 /* return return_value; */ 621 } 622 } 623 return (return_value); 624 } 625 626 #if 0 /* unused */ 627 /* This routine is a real kludge. Someone really should do it better, 628 but I'm too lazy, and I don't understand this stuff all too well 629 anyway. (JF) */ 630 static void 631 int_to_gen (x) 632 long x; 633 { 634 char buf[20]; 635 char *bufp; 636 637 sprintf (buf, "%ld", x); 638 bufp = &buf[0]; 639 if (atof_generic (&bufp, ".", EXP_CHARS, &generic_floating_point_number)) 640 as_bad ("Error converting number to floating point (Exponent overflow?)"); 641 } 642 #endif 643 644 #ifdef TEST 645 char * 646 print_gen (gen) 647 FLONUM_TYPE *gen; 648 { 649 FLONUM_TYPE f; 650 LITTLENUM_TYPE arr[10]; 651 double dv; 652 float fv; 653 static char sbuf[40]; 654 655 if (gen) 656 { 657 f = generic_floating_point_number; 658 generic_floating_point_number = *gen; 659 } 660 gen_to_words (&arr[0], 4, 11); 661 memcpy (&dv, &arr[0], sizeof (double)); 662 sprintf (sbuf, "%x %x %x %x %.14G ", arr[0], arr[1], arr[2], arr[3], dv); 663 gen_to_words (&arr[0], 2, 8); 664 memcpy (&fv, &arr[0], sizeof (float)); 665 sprintf (sbuf + strlen (sbuf), "%x %x %.12g\n", arr[0], arr[1], fv); 666 667 if (gen) 668 { 669 generic_floating_point_number = f; 670 } 671 672 return (sbuf); 673 } 674 675 #endif 676 677 /* end of atof-ieee.c */ 678