1 /* atof_generic.c - turn a string of digits into a Flonum 2 Copyright (C) 1987-2020 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 3, or (at your option) 9 any later version. 10 11 GAS is distributed in the hope that it will be useful, but WITHOUT 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 14 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 the Free 18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 19 02110-1301, USA. */ 20 21 #include "as.h" 22 #include "safe-ctype.h" 23 24 #ifndef FALSE 25 #define FALSE (0) 26 #endif 27 #ifndef TRUE 28 #define TRUE (1) 29 #endif 30 31 #ifdef TRACE 32 static void flonum_print (const FLONUM_TYPE *); 33 #endif 34 35 #define ASSUME_DECIMAL_MARK_IS_DOT 36 37 /***********************************************************************\ 38 * * 39 * Given a string of decimal digits , with optional decimal * 40 * mark and optional decimal exponent (place value) of the * 41 * lowest_order decimal digit: produce a floating point * 42 * number. The number is 'generic' floating point: our * 43 * caller will encode it for a specific machine architecture. * 44 * * 45 * Assumptions * 46 * uses base (radix) 2 * 47 * this machine uses 2's complement binary integers * 48 * target flonums use " " " " * 49 * target flonums exponents fit in a long * 50 * * 51 \***********************************************************************/ 52 53 /* 54 55 Syntax: 56 57 <flonum> ::= <optional-sign> <decimal-number> <optional-exponent> 58 <optional-sign> ::= '+' | '-' | {empty} 59 <decimal-number> ::= <integer> 60 | <integer> <radix-character> 61 | <integer> <radix-character> <integer> 62 | <radix-character> <integer> 63 64 <optional-exponent> ::= {empty} 65 | <exponent-character> <optional-sign> <integer> 66 67 <integer> ::= <digit> | <digit> <integer> 68 <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' 69 <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"} 70 <radix-character> ::= {one character from "string_of_decimal_marks"} 71 72 */ 73 74 int 75 atof_generic (/* return pointer to just AFTER number we read. */ 76 char **address_of_string_pointer, 77 /* At most one per number. */ 78 const char *string_of_decimal_marks, 79 const char *string_of_decimal_exponent_marks, 80 FLONUM_TYPE *address_of_generic_floating_point_number) 81 { 82 int return_value; /* 0 means OK. */ 83 char *first_digit; 84 unsigned int number_of_digits_before_decimal; 85 unsigned int number_of_digits_after_decimal; 86 long decimal_exponent; 87 unsigned int number_of_digits_available; 88 char digits_sign_char; 89 90 /* 91 * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent. 92 * It would be simpler to modify the string, but we don't; just to be nice 93 * to caller. 94 * We need to know how many digits we have, so we can allocate space for 95 * the digits' value. 96 */ 97 98 char *p; 99 char c; 100 int seen_significant_digit; 101 102 #ifdef ASSUME_DECIMAL_MARK_IS_DOT 103 gas_assert (string_of_decimal_marks[0] == '.' 104 && string_of_decimal_marks[1] == 0); 105 #define IS_DECIMAL_MARK(c) ((c) == '.') 106 #else 107 #define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c))) 108 #endif 109 110 first_digit = *address_of_string_pointer; 111 c = *first_digit; 112 113 if (c == '-' || c == '+') 114 { 115 digits_sign_char = c; 116 first_digit++; 117 } 118 else 119 digits_sign_char = '+'; 120 121 switch (first_digit[0]) 122 { 123 case 's': 124 case 'S': 125 if (!strncasecmp ("snan", first_digit, 4)) 126 { 127 address_of_generic_floating_point_number->sign = 0; 128 address_of_generic_floating_point_number->exponent = 0; 129 address_of_generic_floating_point_number->leader = 130 address_of_generic_floating_point_number->low; 131 *address_of_string_pointer = first_digit + 4; 132 return 0; 133 } 134 break; 135 136 case 'q': 137 case 'Q': 138 if (!strncasecmp ("qnan", first_digit, 4)) 139 { 140 address_of_generic_floating_point_number->sign = 0; 141 address_of_generic_floating_point_number->exponent = 0; 142 address_of_generic_floating_point_number->leader = 143 address_of_generic_floating_point_number->low; 144 *address_of_string_pointer = first_digit + 4; 145 return 0; 146 } 147 break; 148 149 case 'n': 150 case 'N': 151 if (!strncasecmp ("nan", first_digit, 3)) 152 { 153 address_of_generic_floating_point_number->sign = 0; 154 address_of_generic_floating_point_number->exponent = 0; 155 address_of_generic_floating_point_number->leader = 156 address_of_generic_floating_point_number->low; 157 *address_of_string_pointer = first_digit + 3; 158 return 0; 159 } 160 break; 161 162 case 'i': 163 case 'I': 164 if (!strncasecmp ("inf", first_digit, 3)) 165 { 166 address_of_generic_floating_point_number->sign = 167 digits_sign_char == '+' ? 'P' : 'N'; 168 address_of_generic_floating_point_number->exponent = 0; 169 address_of_generic_floating_point_number->leader = 170 address_of_generic_floating_point_number->low; 171 172 first_digit += 3; 173 if (!strncasecmp ("inity", first_digit, 5)) 174 first_digit += 5; 175 176 *address_of_string_pointer = first_digit; 177 178 return 0; 179 } 180 break; 181 } 182 183 number_of_digits_before_decimal = 0; 184 number_of_digits_after_decimal = 0; 185 decimal_exponent = 0; 186 seen_significant_digit = 0; 187 for (p = first_digit; 188 (((c = *p) != '\0') 189 && (!c || !IS_DECIMAL_MARK (c)) 190 && (!c || !strchr (string_of_decimal_exponent_marks, c))); 191 p++) 192 { 193 if (ISDIGIT (c)) 194 { 195 if (seen_significant_digit || c > '0') 196 { 197 ++number_of_digits_before_decimal; 198 seen_significant_digit = 1; 199 } 200 else 201 { 202 first_digit++; 203 } 204 } 205 else 206 { 207 break; /* p -> char after pre-decimal digits. */ 208 } 209 } /* For each digit before decimal mark. */ 210 211 #ifndef OLD_FLOAT_READS 212 /* Ignore trailing 0's after the decimal point. The original code here 213 (ifdef'd out) does not do this, and numbers like 214 4.29496729600000000000e+09 (2**31) 215 come out inexact for some reason related to length of the digit 216 string. */ 217 218 /* The case number_of_digits_before_decimal = 0 is handled for 219 deleting zeros after decimal. In this case the decimal mark and 220 the first zero digits after decimal mark are skipped. */ 221 seen_significant_digit = 0; 222 signed long subtract_decimal_exponent = 0; 223 224 if (c && IS_DECIMAL_MARK (c)) 225 { 226 unsigned int zeros = 0; /* Length of current string of zeros. */ 227 228 if (number_of_digits_before_decimal == 0) 229 /* Skip decimal mark. */ 230 first_digit++; 231 232 for (p++; (c = *p) && ISDIGIT (c); p++) 233 { 234 if (c == '0') 235 { 236 if (number_of_digits_before_decimal == 0 237 && !seen_significant_digit) 238 { 239 /* Skip '0' and the decimal mark. */ 240 first_digit++; 241 subtract_decimal_exponent--; 242 } 243 else 244 zeros++; 245 } 246 else 247 { 248 seen_significant_digit = 1; 249 number_of_digits_after_decimal += 1 + zeros; 250 zeros = 0; 251 } 252 } 253 } 254 #else 255 if (c && IS_DECIMAL_MARK (c)) 256 { 257 for (p++; 258 (((c = *p) != '\0') 259 && (!c || !strchr (string_of_decimal_exponent_marks, c))); 260 p++) 261 { 262 if (ISDIGIT (c)) 263 { 264 /* This may be retracted below. */ 265 number_of_digits_after_decimal++; 266 267 if ( /* seen_significant_digit || */ c > '0') 268 { 269 seen_significant_digit = TRUE; 270 } 271 } 272 else 273 { 274 if (!seen_significant_digit) 275 { 276 number_of_digits_after_decimal = 0; 277 } 278 break; 279 } 280 } /* For each digit after decimal mark. */ 281 } 282 283 while (number_of_digits_after_decimal 284 && first_digit[number_of_digits_before_decimal 285 + number_of_digits_after_decimal] == '0') 286 --number_of_digits_after_decimal; 287 #endif 288 289 if (flag_m68k_mri) 290 { 291 while (c == '_') 292 c = *++p; 293 } 294 if (c && strchr (string_of_decimal_exponent_marks, c)) 295 { 296 char digits_exponent_sign_char; 297 298 c = *++p; 299 if (flag_m68k_mri) 300 { 301 while (c == '_') 302 c = *++p; 303 } 304 if (c && strchr ("+-", c)) 305 { 306 digits_exponent_sign_char = c; 307 c = *++p; 308 } 309 else 310 { 311 digits_exponent_sign_char = '+'; 312 } 313 314 for (; (c); c = *++p) 315 { 316 if (ISDIGIT (c)) 317 { 318 decimal_exponent = decimal_exponent * 10 + c - '0'; 319 /* 320 * BUG! If we overflow here, we lose! 321 */ 322 } 323 else 324 { 325 break; 326 } 327 } 328 329 if (digits_exponent_sign_char == '-') 330 { 331 decimal_exponent = -decimal_exponent; 332 } 333 } 334 335 #ifndef OLD_FLOAT_READS 336 /* Subtract_decimal_exponent != 0 when number_of_digits_before_decimal = 0 337 and first digit after decimal is '0'. */ 338 decimal_exponent += subtract_decimal_exponent; 339 #endif 340 341 *address_of_string_pointer = p; 342 343 number_of_digits_available = 344 number_of_digits_before_decimal + number_of_digits_after_decimal; 345 return_value = 0; 346 if (number_of_digits_available == 0) 347 { 348 address_of_generic_floating_point_number->exponent = 0; /* Not strictly necessary */ 349 address_of_generic_floating_point_number->leader 350 = -1 + address_of_generic_floating_point_number->low; 351 address_of_generic_floating_point_number->sign = digits_sign_char; 352 /* We have just concocted (+/-)0.0E0 */ 353 354 } 355 else 356 { 357 int count; /* Number of useful digits left to scan. */ 358 359 LITTLENUM_TYPE *temporary_binary_low = NULL; 360 LITTLENUM_TYPE *power_binary_low = NULL; 361 LITTLENUM_TYPE *digits_binary_low; 362 unsigned int precision; 363 unsigned int maximum_useful_digits; 364 unsigned int number_of_digits_to_use; 365 unsigned int more_than_enough_bits_for_digits; 366 unsigned int more_than_enough_littlenums_for_digits; 367 unsigned int size_of_digits_in_littlenums; 368 unsigned int size_of_digits_in_chars; 369 FLONUM_TYPE power_of_10_flonum; 370 FLONUM_TYPE digits_flonum; 371 372 precision = (address_of_generic_floating_point_number->high 373 - address_of_generic_floating_point_number->low 374 + 1); /* Number of destination littlenums. */ 375 376 /* precision includes two littlenums worth of guard bits, 377 so this gives us 10 decimal guard digits here. */ 378 maximum_useful_digits = (precision 379 * LITTLENUM_NUMBER_OF_BITS 380 * 1000000 / 3321928 381 + 1); /* round up. */ 382 383 if (number_of_digits_available > maximum_useful_digits) 384 { 385 number_of_digits_to_use = maximum_useful_digits; 386 } 387 else 388 { 389 number_of_digits_to_use = number_of_digits_available; 390 } 391 392 /* Cast these to SIGNED LONG first, otherwise, on systems with 393 LONG wider than INT (such as Alpha OSF/1), unsignedness may 394 cause unexpected results. */ 395 decimal_exponent += ((long) number_of_digits_before_decimal 396 - (long) number_of_digits_to_use); 397 398 more_than_enough_bits_for_digits 399 = (number_of_digits_to_use * 3321928 / 1000000 + 1); 400 401 more_than_enough_littlenums_for_digits 402 = (more_than_enough_bits_for_digits 403 / LITTLENUM_NUMBER_OF_BITS) 404 + 2; 405 406 /* Compute (digits) part. In "12.34E56" this is the "1234" part. 407 Arithmetic is exact here. If no digits are supplied then this 408 part is a 0 valued binary integer. Allocate room to build up 409 the binary number as littlenums. We want this memory to 410 disappear when we leave this function. Assume no alignment 411 problems => (room for n objects) == n * (room for 1 412 object). */ 413 414 size_of_digits_in_littlenums = more_than_enough_littlenums_for_digits; 415 size_of_digits_in_chars = size_of_digits_in_littlenums 416 * sizeof (LITTLENUM_TYPE); 417 418 digits_binary_low = (LITTLENUM_TYPE *) 419 xmalloc (size_of_digits_in_chars); 420 421 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars); 422 423 /* Digits_binary_low[] is allocated and zeroed. */ 424 425 /* 426 * Parse the decimal digits as if * digits_low was in the units position. 427 * Emit a binary number into digits_binary_low[]. 428 * 429 * Use a large-precision version of: 430 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit 431 */ 432 433 for (p = first_digit, count = number_of_digits_to_use; count; p++, --count) 434 { 435 c = *p; 436 if (ISDIGIT (c)) 437 { 438 /* 439 * Multiply by 10. Assume can never overflow. 440 * Add this digit to digits_binary_low[]. 441 */ 442 443 long carry; 444 LITTLENUM_TYPE *littlenum_pointer; 445 LITTLENUM_TYPE *littlenum_limit; 446 447 littlenum_limit = digits_binary_low 448 + more_than_enough_littlenums_for_digits 449 - 1; 450 451 carry = c - '0'; /* char -> binary */ 452 453 for (littlenum_pointer = digits_binary_low; 454 littlenum_pointer <= littlenum_limit; 455 littlenum_pointer++) 456 { 457 long work; 458 459 work = carry + 10 * (long) (*littlenum_pointer); 460 *littlenum_pointer = work & LITTLENUM_MASK; 461 carry = work >> LITTLENUM_NUMBER_OF_BITS; 462 } 463 464 if (carry != 0) 465 { 466 /* 467 * We have a GROSS internal error. 468 * This should never happen. 469 */ 470 as_fatal (_("failed sanity check")); 471 } 472 } 473 else 474 { 475 ++count; /* '.' doesn't alter digits used count. */ 476 } 477 } 478 479 /* 480 * Digits_binary_low[] properly encodes the value of the digits. 481 * Forget about any high-order littlenums that are 0. 482 */ 483 while (digits_binary_low[size_of_digits_in_littlenums - 1] == 0 484 && size_of_digits_in_littlenums >= 2) 485 size_of_digits_in_littlenums--; 486 487 digits_flonum.low = digits_binary_low; 488 digits_flonum.high = digits_binary_low + size_of_digits_in_littlenums - 1; 489 digits_flonum.leader = digits_flonum.high; 490 digits_flonum.exponent = 0; 491 /* 492 * The value of digits_flonum . sign should not be important. 493 * We have already decided the output's sign. 494 * We trust that the sign won't influence the other parts of the number! 495 * So we give it a value for these reasons: 496 * (1) courtesy to humans reading/debugging 497 * these numbers so they don't get excited about strange values 498 * (2) in future there may be more meaning attached to sign, 499 * and what was 500 * harmless noise may become disruptive, ill-conditioned (or worse) 501 * input. 502 */ 503 digits_flonum.sign = '+'; 504 505 { 506 /* 507 * Compute the mantissa (& exponent) of the power of 10. 508 * If successful, then multiply the power of 10 by the digits 509 * giving return_binary_mantissa and return_binary_exponent. 510 */ 511 512 int decimal_exponent_is_negative; 513 /* This refers to the "-56" in "12.34E-56". */ 514 /* FALSE: decimal_exponent is positive (or 0) */ 515 /* TRUE: decimal_exponent is negative */ 516 FLONUM_TYPE temporary_flonum; 517 unsigned int size_of_power_in_littlenums; 518 unsigned int size_of_power_in_chars; 519 520 size_of_power_in_littlenums = precision; 521 /* Precision has a built-in fudge factor so we get a few guard bits. */ 522 523 decimal_exponent_is_negative = decimal_exponent < 0; 524 if (decimal_exponent_is_negative) 525 { 526 decimal_exponent = -decimal_exponent; 527 } 528 529 /* From now on: the decimal exponent is > 0. Its sign is separate. */ 530 531 size_of_power_in_chars = size_of_power_in_littlenums 532 * sizeof (LITTLENUM_TYPE) + 2; 533 534 power_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars); 535 temporary_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars); 536 537 memset ((char *) power_binary_low, '\0', size_of_power_in_chars); 538 *power_binary_low = 1; 539 power_of_10_flonum.exponent = 0; 540 power_of_10_flonum.low = power_binary_low; 541 power_of_10_flonum.leader = power_binary_low; 542 power_of_10_flonum.high = power_binary_low + size_of_power_in_littlenums - 1; 543 power_of_10_flonum.sign = '+'; 544 temporary_flonum.low = temporary_binary_low; 545 temporary_flonum.high = temporary_binary_low + size_of_power_in_littlenums - 1; 546 /* 547 * (power) == 1. 548 * Space for temporary_flonum allocated. 549 */ 550 551 /* 552 * ... 553 * 554 * WHILE more bits 555 * DO find next bit (with place value) 556 * multiply into power mantissa 557 * OD 558 */ 559 { 560 int place_number_limit; 561 /* Any 10^(2^n) whose "n" exceeds this */ 562 /* value will fall off the end of */ 563 /* flonum_XXXX_powers_of_ten[]. */ 564 int place_number; 565 const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */ 566 567 place_number_limit = table_size_of_flonum_powers_of_ten; 568 569 multiplicand = (decimal_exponent_is_negative 570 ? flonum_negative_powers_of_ten 571 : flonum_positive_powers_of_ten); 572 573 for (place_number = 1;/* Place value of this bit of exponent. */ 574 decimal_exponent;/* Quit when no more 1 bits in exponent. */ 575 decimal_exponent >>= 1, place_number++) 576 { 577 if (decimal_exponent & 1) 578 { 579 if (place_number > place_number_limit) 580 { 581 /* The decimal exponent has a magnitude so great 582 that our tables can't help us fragment it. 583 Although this routine is in error because it 584 can't imagine a number that big, signal an 585 error as if it is the user's fault for 586 presenting such a big number. */ 587 return_value = ERROR_EXPONENT_OVERFLOW; 588 /* quit out of loop gracefully */ 589 decimal_exponent = 0; 590 } 591 else 592 { 593 #ifdef TRACE 594 printf ("before multiply, place_number = %d., power_of_10_flonum:\n", 595 place_number); 596 597 flonum_print (&power_of_10_flonum); 598 (void) putchar ('\n'); 599 #endif 600 #ifdef TRACE 601 printf ("multiplier:\n"); 602 flonum_print (multiplicand + place_number); 603 (void) putchar ('\n'); 604 #endif 605 flonum_multip (multiplicand + place_number, 606 &power_of_10_flonum, &temporary_flonum); 607 #ifdef TRACE 608 printf ("after multiply:\n"); 609 flonum_print (&temporary_flonum); 610 (void) putchar ('\n'); 611 #endif 612 flonum_copy (&temporary_flonum, &power_of_10_flonum); 613 #ifdef TRACE 614 printf ("after copy:\n"); 615 flonum_print (&power_of_10_flonum); 616 (void) putchar ('\n'); 617 #endif 618 } /* If this bit of decimal_exponent was computable.*/ 619 } /* If this bit of decimal_exponent was set. */ 620 } /* For each bit of binary representation of exponent */ 621 #ifdef TRACE 622 printf ("after computing power_of_10_flonum:\n"); 623 flonum_print (&power_of_10_flonum); 624 (void) putchar ('\n'); 625 #endif 626 } 627 } 628 629 /* 630 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent). 631 * It may be the number 1, in which case we don't NEED to multiply. 632 * 633 * Multiply (decimal digits) by power_of_10_flonum. 634 */ 635 636 flonum_multip (&power_of_10_flonum, &digits_flonum, address_of_generic_floating_point_number); 637 /* Assert sign of the number we made is '+'. */ 638 address_of_generic_floating_point_number->sign = digits_sign_char; 639 640 if (temporary_binary_low) 641 free (temporary_binary_low); 642 if (power_binary_low) 643 free (power_binary_low); 644 free (digits_binary_low); 645 } 646 return return_value; 647 } 648 649 #ifdef TRACE 650 static void 651 flonum_print (f) 652 const FLONUM_TYPE *f; 653 { 654 LITTLENUM_TYPE *lp; 655 char littlenum_format[10]; 656 sprintf (littlenum_format, " %%0%dx", sizeof (LITTLENUM_TYPE) * 2); 657 #define print_littlenum(LP) (printf (littlenum_format, LP)) 658 printf ("flonum @%p %c e%ld", f, f->sign, f->exponent); 659 if (f->low < f->high) 660 for (lp = f->high; lp >= f->low; lp--) 661 print_littlenum (*lp); 662 else 663 for (lp = f->low; lp <= f->high; lp++) 664 print_littlenum (*lp); 665 printf ("\n"); 666 fflush (stdout); 667 } 668 #endif 669 670 /* end of atof_generic.c */ 671