1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright (C) 1987-2023 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 3, or (at your option)
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.
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
22 #include "safe-ctype.h"
26 static void flonum_print (const FLONUM_TYPE
*);
29 #define ASSUME_DECIMAL_MARK_IS_DOT
31 /***********************************************************************\
33 * Given a string of decimal digits , with optional decimal *
34 * mark and optional decimal exponent (place value) of the *
35 * lowest_order decimal digit: produce a floating point *
36 * number. The number is 'generic' floating point: our *
37 * caller will encode it for a specific machine architecture. *
40 * uses base (radix) 2 *
41 * this machine uses 2's complement binary integers *
42 * target flonums use " " " " *
43 * target flonums exponents fit in a long *
45 \***********************************************************************/
51 <flonum> ::= <optional-sign> <decimal-number> <optional-exponent>
52 <optional-sign> ::= '+' | '-' | {empty}
53 <decimal-number> ::= <integer>
54 | <integer> <radix-character>
55 | <integer> <radix-character> <integer>
56 | <radix-character> <integer>
58 <optional-exponent> ::= {empty}
59 | <exponent-character> <optional-sign> <integer>
61 <integer> ::= <digit> | <digit> <integer>
62 <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
63 <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"}
64 <radix-character> ::= {one character from "string_of_decimal_marks"}
69 atof_generic (/* return pointer to just AFTER number we read. */
70 char **address_of_string_pointer
,
71 /* At most one per number. */
72 const char *string_of_decimal_marks
,
73 const char *string_of_decimal_exponent_marks
,
74 FLONUM_TYPE
*address_of_generic_floating_point_number
)
76 int return_value
= 0; /* 0 means OK. */
78 unsigned int number_of_digits_before_decimal
;
79 unsigned int number_of_digits_after_decimal
;
80 unsigned long decimal_exponent
;
81 unsigned int number_of_digits_available
;
82 char digits_sign_char
;
85 * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
86 * It would be simpler to modify the string, but we don't; just to be nice
88 * We need to know how many digits we have, so we can allocate space for
94 int seen_significant_digit
;
96 #ifdef ASSUME_DECIMAL_MARK_IS_DOT
97 gas_assert (string_of_decimal_marks
[0] == '.'
98 && string_of_decimal_marks
[1] == 0);
99 #define IS_DECIMAL_MARK(c) ((c) == '.')
101 #define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c)))
104 first_digit
= *address_of_string_pointer
;
107 if (c
== '-' || c
== '+')
109 digits_sign_char
= c
;
113 digits_sign_char
= '+';
115 switch (first_digit
[0])
121 if (!strncasecmp ("nan", first_digit
+ 1, 3))
123 address_of_generic_floating_point_number
->sign
=
124 digits_sign_char
== '+' ? TOUPPER (first_digit
[0])
125 : TOLOWER (first_digit
[0]);
126 address_of_generic_floating_point_number
->exponent
= 0;
127 address_of_generic_floating_point_number
->leader
=
128 address_of_generic_floating_point_number
->low
;
129 *address_of_string_pointer
= first_digit
+ 4;
136 if (!strncasecmp ("nan", first_digit
, 3))
138 address_of_generic_floating_point_number
->sign
=
139 digits_sign_char
== '+' ? 0 : 'q';
140 address_of_generic_floating_point_number
->exponent
= 0;
141 address_of_generic_floating_point_number
->leader
=
142 address_of_generic_floating_point_number
->low
;
143 *address_of_string_pointer
= first_digit
+ 3;
150 if (!strncasecmp ("inf", first_digit
, 3))
152 address_of_generic_floating_point_number
->sign
=
153 digits_sign_char
== '+' ? 'P' : 'N';
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
;
159 if (!strncasecmp ("inity", first_digit
, 5))
162 *address_of_string_pointer
= first_digit
;
169 number_of_digits_before_decimal
= 0;
170 number_of_digits_after_decimal
= 0;
171 decimal_exponent
= 0;
172 seen_significant_digit
= 0;
173 for (p
= first_digit
;
175 && (!c
|| !IS_DECIMAL_MARK (c
))
176 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
181 if (seen_significant_digit
|| c
> '0')
183 ++number_of_digits_before_decimal
;
184 seen_significant_digit
= 1;
193 break; /* p -> char after pre-decimal digits. */
195 } /* For each digit before decimal mark. */
197 #ifndef OLD_FLOAT_READS
198 /* Ignore trailing 0's after the decimal point. The original code here
199 (ifdef'd out) does not do this, and numbers like
200 4.29496729600000000000e+09 (2**31)
201 come out inexact for some reason related to length of the digit
204 /* The case number_of_digits_before_decimal = 0 is handled for
205 deleting zeros after decimal. In this case the decimal mark and
206 the first zero digits after decimal mark are skipped. */
207 seen_significant_digit
= 0;
208 unsigned long subtract_decimal_exponent
= 0;
210 if (c
&& IS_DECIMAL_MARK (c
))
212 unsigned int zeros
= 0; /* Length of current string of zeros. */
214 if (number_of_digits_before_decimal
== 0)
215 /* Skip decimal mark. */
218 for (p
++; (c
= *p
) && ISDIGIT (c
); p
++)
222 if (number_of_digits_before_decimal
== 0
223 && !seen_significant_digit
)
225 /* Skip '0' and the decimal mark. */
227 subtract_decimal_exponent
--;
234 seen_significant_digit
= 1;
235 number_of_digits_after_decimal
+= 1 + zeros
;
241 if (c
&& IS_DECIMAL_MARK (c
))
245 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
250 /* This may be retracted below. */
251 number_of_digits_after_decimal
++;
253 if ( /* seen_significant_digit || */ c
> '0')
255 seen_significant_digit
= true;
260 if (!seen_significant_digit
)
262 number_of_digits_after_decimal
= 0;
266 } /* For each digit after decimal mark. */
269 while (number_of_digits_after_decimal
270 && first_digit
[number_of_digits_before_decimal
271 + number_of_digits_after_decimal
] == '0')
272 --number_of_digits_after_decimal
;
280 if (c
&& strchr (string_of_decimal_exponent_marks
, c
))
282 char digits_exponent_sign_char
;
290 if (c
&& strchr ("+-", c
))
292 digits_exponent_sign_char
= c
;
297 digits_exponent_sign_char
= '+';
300 for (; (c
); c
= *++p
)
304 if (decimal_exponent
> LONG_MAX
/ 10
305 || (decimal_exponent
== LONG_MAX
/ 10
306 && c
> '0' + (char) (LONG_MAX
- LONG_MAX
/ 10 * 10)))
307 return_value
= ERROR_EXPONENT_OVERFLOW
;
308 decimal_exponent
= decimal_exponent
* 10 + c
- '0';
316 if (digits_exponent_sign_char
== '-')
318 decimal_exponent
= -decimal_exponent
;
322 #ifndef OLD_FLOAT_READS
323 /* Subtract_decimal_exponent != 0 when number_of_digits_before_decimal = 0
324 and first digit after decimal is '0'. */
325 decimal_exponent
+= subtract_decimal_exponent
;
328 *address_of_string_pointer
= p
;
330 number_of_digits_available
=
331 number_of_digits_before_decimal
+ number_of_digits_after_decimal
;
332 if (number_of_digits_available
== 0)
334 address_of_generic_floating_point_number
->exponent
= 0; /* Not strictly necessary */
335 address_of_generic_floating_point_number
->leader
336 = -1 + address_of_generic_floating_point_number
->low
;
337 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
338 /* We have just concocted (+/-)0.0E0 */
343 int count
; /* Number of useful digits left to scan. */
345 LITTLENUM_TYPE
*temporary_binary_low
= NULL
;
346 LITTLENUM_TYPE
*power_binary_low
= NULL
;
347 LITTLENUM_TYPE
*digits_binary_low
;
348 unsigned int precision
;
349 unsigned int maximum_useful_digits
;
350 unsigned int number_of_digits_to_use
;
351 unsigned int more_than_enough_bits_for_digits
;
352 unsigned int more_than_enough_littlenums_for_digits
;
353 unsigned int size_of_digits_in_littlenums
;
354 unsigned int size_of_digits_in_chars
;
355 FLONUM_TYPE power_of_10_flonum
;
356 FLONUM_TYPE digits_flonum
;
358 precision
= (address_of_generic_floating_point_number
->high
359 - address_of_generic_floating_point_number
->low
360 + 1); /* Number of destination littlenums. */
362 /* precision includes two littlenums worth of guard bits,
363 so this gives us 10 decimal guard digits here. */
364 maximum_useful_digits
= (precision
365 * LITTLENUM_NUMBER_OF_BITS
367 + 1); /* round up. */
369 if (number_of_digits_available
> maximum_useful_digits
)
371 number_of_digits_to_use
= maximum_useful_digits
;
375 number_of_digits_to_use
= number_of_digits_available
;
378 /* Cast these to SIGNED LONG first, otherwise, on systems with
379 LONG wider than INT (such as Alpha OSF/1), unsignedness may
380 cause unexpected results. */
381 decimal_exponent
+= ((long) number_of_digits_before_decimal
382 - (long) number_of_digits_to_use
);
384 more_than_enough_bits_for_digits
385 = (number_of_digits_to_use
* 3321928 / 1000000 + 1);
387 more_than_enough_littlenums_for_digits
388 = (more_than_enough_bits_for_digits
389 / LITTLENUM_NUMBER_OF_BITS
)
392 /* Compute (digits) part. In "12.34E56" this is the "1234" part.
393 Arithmetic is exact here. If no digits are supplied then this
394 part is a 0 valued binary integer. Allocate room to build up
395 the binary number as littlenums. We want this memory to
396 disappear when we leave this function. Assume no alignment
397 problems => (room for n objects) == n * (room for 1
400 size_of_digits_in_littlenums
= more_than_enough_littlenums_for_digits
;
401 size_of_digits_in_chars
= size_of_digits_in_littlenums
402 * sizeof (LITTLENUM_TYPE
);
404 digits_binary_low
= (LITTLENUM_TYPE
*)
405 xmalloc (size_of_digits_in_chars
);
407 memset ((char *) digits_binary_low
, '\0', size_of_digits_in_chars
);
409 /* Digits_binary_low[] is allocated and zeroed. */
412 * Parse the decimal digits as if * digits_low was in the units position.
413 * Emit a binary number into digits_binary_low[].
415 * Use a large-precision version of:
416 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
419 for (p
= first_digit
, count
= number_of_digits_to_use
; count
; p
++, --count
)
425 * Multiply by 10. Assume can never overflow.
426 * Add this digit to digits_binary_low[].
430 LITTLENUM_TYPE
*littlenum_pointer
;
431 LITTLENUM_TYPE
*littlenum_limit
;
433 littlenum_limit
= digits_binary_low
434 + more_than_enough_littlenums_for_digits
437 carry
= c
- '0'; /* char -> binary */
439 for (littlenum_pointer
= digits_binary_low
;
440 littlenum_pointer
<= littlenum_limit
;
445 work
= carry
+ 10 * (long) (*littlenum_pointer
);
446 *littlenum_pointer
= work
& LITTLENUM_MASK
;
447 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
453 * We have a GROSS internal error.
454 * This should never happen.
456 as_fatal (_("failed sanity check"));
461 ++count
; /* '.' doesn't alter digits used count. */
466 * Digits_binary_low[] properly encodes the value of the digits.
467 * Forget about any high-order littlenums that are 0.
469 while (digits_binary_low
[size_of_digits_in_littlenums
- 1] == 0
470 && size_of_digits_in_littlenums
>= 2)
471 size_of_digits_in_littlenums
--;
473 digits_flonum
.low
= digits_binary_low
;
474 digits_flonum
.high
= digits_binary_low
+ size_of_digits_in_littlenums
- 1;
475 digits_flonum
.leader
= digits_flonum
.high
;
476 digits_flonum
.exponent
= 0;
478 * The value of digits_flonum . sign should not be important.
479 * We have already decided the output's sign.
480 * We trust that the sign won't influence the other parts of the number!
481 * So we give it a value for these reasons:
482 * (1) courtesy to humans reading/debugging
483 * these numbers so they don't get excited about strange values
484 * (2) in future there may be more meaning attached to sign,
486 * harmless noise may become disruptive, ill-conditioned (or worse)
489 digits_flonum
.sign
= '+';
493 * Compute the mantissa (& exponent) of the power of 10.
494 * If successful, then multiply the power of 10 by the digits
495 * giving return_binary_mantissa and return_binary_exponent.
498 int decimal_exponent_is_negative
;
499 /* This refers to the "-56" in "12.34E-56". */
500 /* FALSE: decimal_exponent is positive (or 0) */
501 /* TRUE: decimal_exponent is negative */
502 FLONUM_TYPE temporary_flonum
;
503 unsigned int size_of_power_in_littlenums
;
504 unsigned int size_of_power_in_chars
;
506 size_of_power_in_littlenums
= precision
;
507 /* Precision has a built-in fudge factor so we get a few guard bits. */
509 decimal_exponent_is_negative
= (long) decimal_exponent
< 0;
510 if (decimal_exponent_is_negative
)
512 decimal_exponent
= -decimal_exponent
;
515 /* From now on: the decimal exponent is > 0. Its sign is separate. */
517 size_of_power_in_chars
= size_of_power_in_littlenums
518 * sizeof (LITTLENUM_TYPE
) + 2;
520 power_binary_low
= (LITTLENUM_TYPE
*) xmalloc (size_of_power_in_chars
);
521 temporary_binary_low
= (LITTLENUM_TYPE
*) xmalloc (size_of_power_in_chars
);
523 memset ((char *) power_binary_low
, '\0', size_of_power_in_chars
);
524 *power_binary_low
= 1;
525 power_of_10_flonum
.exponent
= 0;
526 power_of_10_flonum
.low
= power_binary_low
;
527 power_of_10_flonum
.leader
= power_binary_low
;
528 power_of_10_flonum
.high
= power_binary_low
+ size_of_power_in_littlenums
- 1;
529 power_of_10_flonum
.sign
= '+';
530 temporary_flonum
.low
= temporary_binary_low
;
531 temporary_flonum
.high
= temporary_binary_low
+ size_of_power_in_littlenums
- 1;
534 * Space for temporary_flonum allocated.
541 * DO find next bit (with place value)
542 * multiply into power mantissa
546 int place_number_limit
;
547 /* Any 10^(2^n) whose "n" exceeds this */
548 /* value will fall off the end of */
549 /* flonum_XXXX_powers_of_ten[]. */
551 const FLONUM_TYPE
*multiplicand
; /* -> 10^(2^n) */
553 place_number_limit
= table_size_of_flonum_powers_of_ten
;
555 multiplicand
= (decimal_exponent_is_negative
556 ? flonum_negative_powers_of_ten
557 : flonum_positive_powers_of_ten
);
559 for (place_number
= 1;/* Place value of this bit of exponent. */
560 decimal_exponent
;/* Quit when no more 1 bits in exponent. */
561 decimal_exponent
>>= 1, place_number
++)
563 if (decimal_exponent
& 1)
565 if (place_number
> place_number_limit
)
567 /* The decimal exponent has a magnitude so great
568 that our tables can't help us fragment it.
569 Although this routine is in error because it
570 can't imagine a number that big, signal an
571 error as if it is the user's fault for
572 presenting such a big number. */
573 return_value
= ERROR_EXPONENT_OVERFLOW
;
574 /* quit out of loop gracefully */
575 decimal_exponent
= 0;
580 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
583 flonum_print (&power_of_10_flonum
);
584 (void) putchar ('\n');
587 printf ("multiplier:\n");
588 flonum_print (multiplicand
+ place_number
);
589 (void) putchar ('\n');
591 flonum_multip (multiplicand
+ place_number
,
592 &power_of_10_flonum
, &temporary_flonum
);
594 printf ("after multiply:\n");
595 flonum_print (&temporary_flonum
);
596 (void) putchar ('\n');
598 flonum_copy (&temporary_flonum
, &power_of_10_flonum
);
600 printf ("after copy:\n");
601 flonum_print (&power_of_10_flonum
);
602 (void) putchar ('\n');
604 } /* If this bit of decimal_exponent was computable.*/
605 } /* If this bit of decimal_exponent was set. */
606 } /* For each bit of binary representation of exponent */
608 printf ("after computing power_of_10_flonum:\n");
609 flonum_print (&power_of_10_flonum
);
610 (void) putchar ('\n');
616 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
617 * It may be the number 1, in which case we don't NEED to multiply.
619 * Multiply (decimal digits) by power_of_10_flonum.
622 flonum_multip (&power_of_10_flonum
, &digits_flonum
, address_of_generic_floating_point_number
);
623 /* Assert sign of the number we made is '+'. */
624 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
626 free (temporary_binary_low
);
627 free (power_binary_low
);
628 free (digits_binary_low
);
636 const FLONUM_TYPE
*f
;
639 char littlenum_format
[10];
640 sprintf (littlenum_format
, " %%0%dx", sizeof (LITTLENUM_TYPE
) * 2);
641 #define print_littlenum(LP) (printf (littlenum_format, LP))
642 printf ("flonum @%p %c e%ld", f
, f
->sign
, f
->exponent
);
643 if (f
->low
< f
->high
)
644 for (lp
= f
->high
; lp
>= f
->low
; lp
--)
645 print_littlenum (*lp
);
647 for (lp
= f
->low
; lp
<= f
->high
; lp
++)
648 print_littlenum (*lp
);
654 /* end of atof_generic.c */