1 /* atof_generic.c - turn a string of digits into a Flonum 2 Copyright (C) 1987-2015 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 if (c && IS_DECIMAL_MARK (c)) 219 { 220 unsigned int zeros = 0; /* Length of current string of zeros */ 221 222 for (p++; (c = *p) && ISDIGIT (c); p++) 223 { 224 if (c == '0') 225 { 226 zeros++; 227 } 228 else 229 { 230 number_of_digits_after_decimal += 1 + zeros; 231 zeros = 0; 232 } 233 } 234 } 235 #else 236 if (c && IS_DECIMAL_MARK (c)) 237 { 238 for (p++; 239 (((c = *p) != '\0') 240 && (!c || !strchr (string_of_decimal_exponent_marks, c))); 241 p++) 242 { 243 if (ISDIGIT (c)) 244 { 245 /* This may be retracted below. */ 246 number_of_digits_after_decimal++; 247 248 if ( /* seen_significant_digit || */ c > '0') 249 { 250 seen_significant_digit = TRUE; 251 } 252 } 253 else 254 { 255 if (!seen_significant_digit) 256 { 257 number_of_digits_after_decimal = 0; 258 } 259 break; 260 } 261 } /* For each digit after decimal mark. */ 262 } 263 264 while (number_of_digits_after_decimal 265 && first_digit[number_of_digits_before_decimal 266 + number_of_digits_after_decimal] == '0') 267 --number_of_digits_after_decimal; 268 #endif 269 270 if (flag_m68k_mri) 271 { 272 while (c == '_') 273 c = *++p; 274 } 275 if (c && strchr (string_of_decimal_exponent_marks, c)) 276 { 277 char digits_exponent_sign_char; 278 279 c = *++p; 280 if (flag_m68k_mri) 281 { 282 while (c == '_') 283 c = *++p; 284 } 285 if (c && strchr ("+-", c)) 286 { 287 digits_exponent_sign_char = c; 288 c = *++p; 289 } 290 else 291 { 292 digits_exponent_sign_char = '+'; 293 } 294 295 for (; (c); c = *++p) 296 { 297 if (ISDIGIT (c)) 298 { 299 decimal_exponent = decimal_exponent * 10 + c - '0'; 300 /* 301 * BUG! If we overflow here, we lose! 302 */ 303 } 304 else 305 { 306 break; 307 } 308 } 309 310 if (digits_exponent_sign_char == '-') 311 { 312 decimal_exponent = -decimal_exponent; 313 } 314 } 315 316 *address_of_string_pointer = p; 317 318 number_of_digits_available = 319 number_of_digits_before_decimal + number_of_digits_after_decimal; 320 return_value = 0; 321 if (number_of_digits_available == 0) 322 { 323 address_of_generic_floating_point_number->exponent = 0; /* Not strictly necessary */ 324 address_of_generic_floating_point_number->leader 325 = -1 + address_of_generic_floating_point_number->low; 326 address_of_generic_floating_point_number->sign = digits_sign_char; 327 /* We have just concocted (+/-)0.0E0 */ 328 329 } 330 else 331 { 332 int count; /* Number of useful digits left to scan. */ 333 334 LITTLENUM_TYPE *digits_binary_low; 335 unsigned int precision; 336 unsigned int maximum_useful_digits; 337 unsigned int number_of_digits_to_use; 338 unsigned int more_than_enough_bits_for_digits; 339 unsigned int more_than_enough_littlenums_for_digits; 340 unsigned int size_of_digits_in_littlenums; 341 unsigned int size_of_digits_in_chars; 342 FLONUM_TYPE power_of_10_flonum; 343 FLONUM_TYPE digits_flonum; 344 345 precision = (address_of_generic_floating_point_number->high 346 - address_of_generic_floating_point_number->low 347 + 1); /* Number of destination littlenums. */ 348 349 /* Includes guard bits (two littlenums worth) */ 350 maximum_useful_digits = (((precision - 2)) 351 * ( (LITTLENUM_NUMBER_OF_BITS)) 352 * 1000000 / 3321928) 353 + 2; /* 2 :: guard digits. */ 354 355 if (number_of_digits_available > maximum_useful_digits) 356 { 357 number_of_digits_to_use = maximum_useful_digits; 358 } 359 else 360 { 361 number_of_digits_to_use = number_of_digits_available; 362 } 363 364 /* Cast these to SIGNED LONG first, otherwise, on systems with 365 LONG wider than INT (such as Alpha OSF/1), unsignedness may 366 cause unexpected results. */ 367 decimal_exponent += ((long) number_of_digits_before_decimal 368 - (long) number_of_digits_to_use); 369 370 more_than_enough_bits_for_digits 371 = (number_of_digits_to_use * 3321928 / 1000000 + 1); 372 373 more_than_enough_littlenums_for_digits 374 = (more_than_enough_bits_for_digits 375 / LITTLENUM_NUMBER_OF_BITS) 376 + 2; 377 378 /* Compute (digits) part. In "12.34E56" this is the "1234" part. 379 Arithmetic is exact here. If no digits are supplied then this 380 part is a 0 valued binary integer. Allocate room to build up 381 the binary number as littlenums. We want this memory to 382 disappear when we leave this function. Assume no alignment 383 problems => (room for n objects) == n * (room for 1 384 object). */ 385 386 size_of_digits_in_littlenums = more_than_enough_littlenums_for_digits; 387 size_of_digits_in_chars = size_of_digits_in_littlenums 388 * sizeof (LITTLENUM_TYPE); 389 390 digits_binary_low = (LITTLENUM_TYPE *) 391 alloca (size_of_digits_in_chars); 392 393 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars); 394 395 /* Digits_binary_low[] is allocated and zeroed. */ 396 397 /* 398 * Parse the decimal digits as if * digits_low was in the units position. 399 * Emit a binary number into digits_binary_low[]. 400 * 401 * Use a large-precision version of: 402 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit 403 */ 404 405 for (p = first_digit, count = number_of_digits_to_use; count; p++, --count) 406 { 407 c = *p; 408 if (ISDIGIT (c)) 409 { 410 /* 411 * Multiply by 10. Assume can never overflow. 412 * Add this digit to digits_binary_low[]. 413 */ 414 415 long carry; 416 LITTLENUM_TYPE *littlenum_pointer; 417 LITTLENUM_TYPE *littlenum_limit; 418 419 littlenum_limit = digits_binary_low 420 + more_than_enough_littlenums_for_digits 421 - 1; 422 423 carry = c - '0'; /* char -> binary */ 424 425 for (littlenum_pointer = digits_binary_low; 426 littlenum_pointer <= littlenum_limit; 427 littlenum_pointer++) 428 { 429 long work; 430 431 work = carry + 10 * (long) (*littlenum_pointer); 432 *littlenum_pointer = work & LITTLENUM_MASK; 433 carry = work >> LITTLENUM_NUMBER_OF_BITS; 434 } 435 436 if (carry != 0) 437 { 438 /* 439 * We have a GROSS internal error. 440 * This should never happen. 441 */ 442 as_fatal (_("failed sanity check")); 443 } 444 } 445 else 446 { 447 ++count; /* '.' doesn't alter digits used count. */ 448 } 449 } 450 451 /* 452 * Digits_binary_low[] properly encodes the value of the digits. 453 * Forget about any high-order littlenums that are 0. 454 */ 455 while (digits_binary_low[size_of_digits_in_littlenums - 1] == 0 456 && size_of_digits_in_littlenums >= 2) 457 size_of_digits_in_littlenums--; 458 459 digits_flonum.low = digits_binary_low; 460 digits_flonum.high = digits_binary_low + size_of_digits_in_littlenums - 1; 461 digits_flonum.leader = digits_flonum.high; 462 digits_flonum.exponent = 0; 463 /* 464 * The value of digits_flonum . sign should not be important. 465 * We have already decided the output's sign. 466 * We trust that the sign won't influence the other parts of the number! 467 * So we give it a value for these reasons: 468 * (1) courtesy to humans reading/debugging 469 * these numbers so they don't get excited about strange values 470 * (2) in future there may be more meaning attached to sign, 471 * and what was 472 * harmless noise may become disruptive, ill-conditioned (or worse) 473 * input. 474 */ 475 digits_flonum.sign = '+'; 476 477 { 478 /* 479 * Compute the mantssa (& exponent) of the power of 10. 480 * If successful, then multiply the power of 10 by the digits 481 * giving return_binary_mantissa and return_binary_exponent. 482 */ 483 484 LITTLENUM_TYPE *power_binary_low; 485 int decimal_exponent_is_negative; 486 /* This refers to the "-56" in "12.34E-56". */ 487 /* FALSE: decimal_exponent is positive (or 0) */ 488 /* TRUE: decimal_exponent is negative */ 489 FLONUM_TYPE temporary_flonum; 490 LITTLENUM_TYPE *temporary_binary_low; 491 unsigned int size_of_power_in_littlenums; 492 unsigned int size_of_power_in_chars; 493 494 size_of_power_in_littlenums = precision; 495 /* Precision has a built-in fudge factor so we get a few guard bits. */ 496 497 decimal_exponent_is_negative = decimal_exponent < 0; 498 if (decimal_exponent_is_negative) 499 { 500 decimal_exponent = -decimal_exponent; 501 } 502 503 /* From now on: the decimal exponent is > 0. Its sign is separate. */ 504 505 size_of_power_in_chars = size_of_power_in_littlenums 506 * sizeof (LITTLENUM_TYPE) + 2; 507 508 power_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars); 509 temporary_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars); 510 memset ((char *) power_binary_low, '\0', size_of_power_in_chars); 511 *power_binary_low = 1; 512 power_of_10_flonum.exponent = 0; 513 power_of_10_flonum.low = power_binary_low; 514 power_of_10_flonum.leader = power_binary_low; 515 power_of_10_flonum.high = power_binary_low + size_of_power_in_littlenums - 1; 516 power_of_10_flonum.sign = '+'; 517 temporary_flonum.low = temporary_binary_low; 518 temporary_flonum.high = temporary_binary_low + size_of_power_in_littlenums - 1; 519 /* 520 * (power) == 1. 521 * Space for temporary_flonum allocated. 522 */ 523 524 /* 525 * ... 526 * 527 * WHILE more bits 528 * DO find next bit (with place value) 529 * multiply into power mantissa 530 * OD 531 */ 532 { 533 int place_number_limit; 534 /* Any 10^(2^n) whose "n" exceeds this */ 535 /* value will fall off the end of */ 536 /* flonum_XXXX_powers_of_ten[]. */ 537 int place_number; 538 const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */ 539 540 place_number_limit = table_size_of_flonum_powers_of_ten; 541 542 multiplicand = (decimal_exponent_is_negative 543 ? flonum_negative_powers_of_ten 544 : flonum_positive_powers_of_ten); 545 546 for (place_number = 1;/* Place value of this bit of exponent. */ 547 decimal_exponent;/* Quit when no more 1 bits in exponent. */ 548 decimal_exponent >>= 1, place_number++) 549 { 550 if (decimal_exponent & 1) 551 { 552 if (place_number > place_number_limit) 553 { 554 /* The decimal exponent has a magnitude so great 555 that our tables can't help us fragment it. 556 Although this routine is in error because it 557 can't imagine a number that big, signal an 558 error as if it is the user's fault for 559 presenting such a big number. */ 560 return_value = ERROR_EXPONENT_OVERFLOW; 561 /* quit out of loop gracefully */ 562 decimal_exponent = 0; 563 } 564 else 565 { 566 #ifdef TRACE 567 printf ("before multiply, place_number = %d., power_of_10_flonum:\n", 568 place_number); 569 570 flonum_print (&power_of_10_flonum); 571 (void) putchar ('\n'); 572 #endif 573 #ifdef TRACE 574 printf ("multiplier:\n"); 575 flonum_print (multiplicand + place_number); 576 (void) putchar ('\n'); 577 #endif 578 flonum_multip (multiplicand + place_number, 579 &power_of_10_flonum, &temporary_flonum); 580 #ifdef TRACE 581 printf ("after multiply:\n"); 582 flonum_print (&temporary_flonum); 583 (void) putchar ('\n'); 584 #endif 585 flonum_copy (&temporary_flonum, &power_of_10_flonum); 586 #ifdef TRACE 587 printf ("after copy:\n"); 588 flonum_print (&power_of_10_flonum); 589 (void) putchar ('\n'); 590 #endif 591 } /* If this bit of decimal_exponent was computable.*/ 592 } /* If this bit of decimal_exponent was set. */ 593 } /* For each bit of binary representation of exponent */ 594 #ifdef TRACE 595 printf ("after computing power_of_10_flonum:\n"); 596 flonum_print (&power_of_10_flonum); 597 (void) putchar ('\n'); 598 #endif 599 } 600 601 } 602 603 /* 604 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent). 605 * It may be the number 1, in which case we don't NEED to multiply. 606 * 607 * Multiply (decimal digits) by power_of_10_flonum. 608 */ 609 610 flonum_multip (&power_of_10_flonum, &digits_flonum, address_of_generic_floating_point_number); 611 /* Assert sign of the number we made is '+'. */ 612 address_of_generic_floating_point_number->sign = digits_sign_char; 613 614 } 615 return return_value; 616 } 617 618 #ifdef TRACE 619 static void 620 flonum_print (f) 621 const FLONUM_TYPE *f; 622 { 623 LITTLENUM_TYPE *lp; 624 char littlenum_format[10]; 625 sprintf (littlenum_format, " %%0%dx", sizeof (LITTLENUM_TYPE) * 2); 626 #define print_littlenum(LP) (printf (littlenum_format, LP)) 627 printf ("flonum @%p %c e%ld", f, f->sign, f->exponent); 628 if (f->low < f->high) 629 for (lp = f->high; lp >= f->low; lp--) 630 print_littlenum (*lp); 631 else 632 for (lp = f->low; lp <= f->high; lp++) 633 print_littlenum (*lp); 634 printf ("\n"); 635 fflush (stdout); 636 } 637 #endif 638 639 /* end of atof_generic.c */ 640