1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright (C) 1987-2021 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"
25 static void flonum_print (const FLONUM_TYPE
*);
28 #define ASSUME_DECIMAL_MARK_IS_DOT
30 /***********************************************************************\
32 * Given a string of decimal digits , with optional decimal *
33 * mark and optional decimal exponent (place value) of the *
34 * lowest_order decimal digit: produce a floating point *
35 * number. The number is 'generic' floating point: our *
36 * caller will encode it for a specific machine architecture. *
39 * uses base (radix) 2 *
40 * this machine uses 2's complement binary integers *
41 * target flonums use " " " " *
42 * target flonums exponents fit in a long *
44 \***********************************************************************/
50 <flonum> ::= <optional-sign> <decimal-number> <optional-exponent>
51 <optional-sign> ::= '+' | '-' | {empty}
52 <decimal-number> ::= <integer>
53 | <integer> <radix-character>
54 | <integer> <radix-character> <integer>
55 | <radix-character> <integer>
57 <optional-exponent> ::= {empty}
58 | <exponent-character> <optional-sign> <integer>
60 <integer> ::= <digit> | <digit> <integer>
61 <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
62 <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"}
63 <radix-character> ::= {one character from "string_of_decimal_marks"}
68 atof_generic (/* return pointer to just AFTER number we read. */
69 char **address_of_string_pointer
,
70 /* At most one per number. */
71 const char *string_of_decimal_marks
,
72 const char *string_of_decimal_exponent_marks
,
73 FLONUM_TYPE
*address_of_generic_floating_point_number
)
75 int return_value
; /* 0 means OK. */
77 unsigned int number_of_digits_before_decimal
;
78 unsigned int number_of_digits_after_decimal
;
79 long decimal_exponent
;
80 unsigned int number_of_digits_available
;
81 char digits_sign_char
;
84 * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
85 * It would be simpler to modify the string, but we don't; just to be nice
87 * We need to know how many digits we have, so we can allocate space for
93 int seen_significant_digit
;
95 #ifdef ASSUME_DECIMAL_MARK_IS_DOT
96 gas_assert (string_of_decimal_marks
[0] == '.'
97 && string_of_decimal_marks
[1] == 0);
98 #define IS_DECIMAL_MARK(c) ((c) == '.')
100 #define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c)))
103 first_digit
= *address_of_string_pointer
;
106 if (c
== '-' || c
== '+')
108 digits_sign_char
= c
;
112 digits_sign_char
= '+';
114 switch (first_digit
[0])
118 if (!strncasecmp ("nan", first_digit
, 3))
120 address_of_generic_floating_point_number
->sign
= 0;
121 address_of_generic_floating_point_number
->exponent
= 0;
122 address_of_generic_floating_point_number
->leader
=
123 address_of_generic_floating_point_number
->low
;
124 *address_of_string_pointer
= first_digit
+ 3;
131 if (!strncasecmp ("inf", first_digit
, 3))
133 address_of_generic_floating_point_number
->sign
=
134 digits_sign_char
== '+' ? 'P' : 'N';
135 address_of_generic_floating_point_number
->exponent
= 0;
136 address_of_generic_floating_point_number
->leader
=
137 address_of_generic_floating_point_number
->low
;
140 if (!strncasecmp ("inity", first_digit
, 5))
143 *address_of_string_pointer
= first_digit
;
150 number_of_digits_before_decimal
= 0;
151 number_of_digits_after_decimal
= 0;
152 decimal_exponent
= 0;
153 seen_significant_digit
= 0;
154 for (p
= first_digit
;
156 && (!c
|| !IS_DECIMAL_MARK (c
))
157 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
162 if (seen_significant_digit
|| c
> '0')
164 ++number_of_digits_before_decimal
;
165 seen_significant_digit
= 1;
174 break; /* p -> char after pre-decimal digits. */
176 } /* For each digit before decimal mark. */
178 #ifndef OLD_FLOAT_READS
179 /* Ignore trailing 0's after the decimal point. The original code here
180 (ifdef'd out) does not do this, and numbers like
181 4.29496729600000000000e+09 (2**31)
182 come out inexact for some reason related to length of the digit
185 /* The case number_of_digits_before_decimal = 0 is handled for
186 deleting zeros after decimal. In this case the decimal mark and
187 the first zero digits after decimal mark are skipped. */
188 seen_significant_digit
= 0;
189 signed long subtract_decimal_exponent
= 0;
191 if (c
&& IS_DECIMAL_MARK (c
))
193 unsigned int zeros
= 0; /* Length of current string of zeros. */
195 if (number_of_digits_before_decimal
== 0)
196 /* Skip decimal mark. */
199 for (p
++; (c
= *p
) && ISDIGIT (c
); p
++)
203 if (number_of_digits_before_decimal
== 0
204 && !seen_significant_digit
)
206 /* Skip '0' and the decimal mark. */
208 subtract_decimal_exponent
--;
215 seen_significant_digit
= 1;
216 number_of_digits_after_decimal
+= 1 + zeros
;
222 if (c
&& IS_DECIMAL_MARK (c
))
226 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
231 /* This may be retracted below. */
232 number_of_digits_after_decimal
++;
234 if ( /* seen_significant_digit || */ c
> '0')
236 seen_significant_digit
= true;
241 if (!seen_significant_digit
)
243 number_of_digits_after_decimal
= 0;
247 } /* For each digit after decimal mark. */
250 while (number_of_digits_after_decimal
251 && first_digit
[number_of_digits_before_decimal
252 + number_of_digits_after_decimal
] == '0')
253 --number_of_digits_after_decimal
;
261 if (c
&& strchr (string_of_decimal_exponent_marks
, c
))
263 char digits_exponent_sign_char
;
271 if (c
&& strchr ("+-", c
))
273 digits_exponent_sign_char
= c
;
278 digits_exponent_sign_char
= '+';
281 for (; (c
); c
= *++p
)
285 decimal_exponent
= decimal_exponent
* 10 + c
- '0';
287 * BUG! If we overflow here, we lose!
296 if (digits_exponent_sign_char
== '-')
298 decimal_exponent
= -decimal_exponent
;
302 #ifndef OLD_FLOAT_READS
303 /* Subtract_decimal_exponent != 0 when number_of_digits_before_decimal = 0
304 and first digit after decimal is '0'. */
305 decimal_exponent
+= subtract_decimal_exponent
;
308 *address_of_string_pointer
= p
;
310 number_of_digits_available
=
311 number_of_digits_before_decimal
+ number_of_digits_after_decimal
;
313 if (number_of_digits_available
== 0)
315 address_of_generic_floating_point_number
->exponent
= 0; /* Not strictly necessary */
316 address_of_generic_floating_point_number
->leader
317 = -1 + address_of_generic_floating_point_number
->low
;
318 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
319 /* We have just concocted (+/-)0.0E0 */
324 int count
; /* Number of useful digits left to scan. */
326 LITTLENUM_TYPE
*temporary_binary_low
= NULL
;
327 LITTLENUM_TYPE
*power_binary_low
= NULL
;
328 LITTLENUM_TYPE
*digits_binary_low
;
329 unsigned int precision
;
330 unsigned int maximum_useful_digits
;
331 unsigned int number_of_digits_to_use
;
332 unsigned int more_than_enough_bits_for_digits
;
333 unsigned int more_than_enough_littlenums_for_digits
;
334 unsigned int size_of_digits_in_littlenums
;
335 unsigned int size_of_digits_in_chars
;
336 FLONUM_TYPE power_of_10_flonum
;
337 FLONUM_TYPE digits_flonum
;
339 precision
= (address_of_generic_floating_point_number
->high
340 - address_of_generic_floating_point_number
->low
341 + 1); /* Number of destination littlenums. */
343 /* precision includes two littlenums worth of guard bits,
344 so this gives us 10 decimal guard digits here. */
345 maximum_useful_digits
= (precision
346 * LITTLENUM_NUMBER_OF_BITS
348 + 1); /* round up. */
350 if (number_of_digits_available
> maximum_useful_digits
)
352 number_of_digits_to_use
= maximum_useful_digits
;
356 number_of_digits_to_use
= number_of_digits_available
;
359 /* Cast these to SIGNED LONG first, otherwise, on systems with
360 LONG wider than INT (such as Alpha OSF/1), unsignedness may
361 cause unexpected results. */
362 decimal_exponent
+= ((long) number_of_digits_before_decimal
363 - (long) number_of_digits_to_use
);
365 more_than_enough_bits_for_digits
366 = (number_of_digits_to_use
* 3321928 / 1000000 + 1);
368 more_than_enough_littlenums_for_digits
369 = (more_than_enough_bits_for_digits
370 / LITTLENUM_NUMBER_OF_BITS
)
373 /* Compute (digits) part. In "12.34E56" this is the "1234" part.
374 Arithmetic is exact here. If no digits are supplied then this
375 part is a 0 valued binary integer. Allocate room to build up
376 the binary number as littlenums. We want this memory to
377 disappear when we leave this function. Assume no alignment
378 problems => (room for n objects) == n * (room for 1
381 size_of_digits_in_littlenums
= more_than_enough_littlenums_for_digits
;
382 size_of_digits_in_chars
= size_of_digits_in_littlenums
383 * sizeof (LITTLENUM_TYPE
);
385 digits_binary_low
= (LITTLENUM_TYPE
*)
386 xmalloc (size_of_digits_in_chars
);
388 memset ((char *) digits_binary_low
, '\0', size_of_digits_in_chars
);
390 /* Digits_binary_low[] is allocated and zeroed. */
393 * Parse the decimal digits as if * digits_low was in the units position.
394 * Emit a binary number into digits_binary_low[].
396 * Use a large-precision version of:
397 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
400 for (p
= first_digit
, count
= number_of_digits_to_use
; count
; p
++, --count
)
406 * Multiply by 10. Assume can never overflow.
407 * Add this digit to digits_binary_low[].
411 LITTLENUM_TYPE
*littlenum_pointer
;
412 LITTLENUM_TYPE
*littlenum_limit
;
414 littlenum_limit
= digits_binary_low
415 + more_than_enough_littlenums_for_digits
418 carry
= c
- '0'; /* char -> binary */
420 for (littlenum_pointer
= digits_binary_low
;
421 littlenum_pointer
<= littlenum_limit
;
426 work
= carry
+ 10 * (long) (*littlenum_pointer
);
427 *littlenum_pointer
= work
& LITTLENUM_MASK
;
428 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
434 * We have a GROSS internal error.
435 * This should never happen.
437 as_fatal (_("failed sanity check"));
442 ++count
; /* '.' doesn't alter digits used count. */
447 * Digits_binary_low[] properly encodes the value of the digits.
448 * Forget about any high-order littlenums that are 0.
450 while (digits_binary_low
[size_of_digits_in_littlenums
- 1] == 0
451 && size_of_digits_in_littlenums
>= 2)
452 size_of_digits_in_littlenums
--;
454 digits_flonum
.low
= digits_binary_low
;
455 digits_flonum
.high
= digits_binary_low
+ size_of_digits_in_littlenums
- 1;
456 digits_flonum
.leader
= digits_flonum
.high
;
457 digits_flonum
.exponent
= 0;
459 * The value of digits_flonum . sign should not be important.
460 * We have already decided the output's sign.
461 * We trust that the sign won't influence the other parts of the number!
462 * So we give it a value for these reasons:
463 * (1) courtesy to humans reading/debugging
464 * these numbers so they don't get excited about strange values
465 * (2) in future there may be more meaning attached to sign,
467 * harmless noise may become disruptive, ill-conditioned (or worse)
470 digits_flonum
.sign
= '+';
474 * Compute the mantissa (& exponent) of the power of 10.
475 * If successful, then multiply the power of 10 by the digits
476 * giving return_binary_mantissa and return_binary_exponent.
479 int decimal_exponent_is_negative
;
480 /* This refers to the "-56" in "12.34E-56". */
481 /* FALSE: decimal_exponent is positive (or 0) */
482 /* TRUE: decimal_exponent is negative */
483 FLONUM_TYPE temporary_flonum
;
484 unsigned int size_of_power_in_littlenums
;
485 unsigned int size_of_power_in_chars
;
487 size_of_power_in_littlenums
= precision
;
488 /* Precision has a built-in fudge factor so we get a few guard bits. */
490 decimal_exponent_is_negative
= decimal_exponent
< 0;
491 if (decimal_exponent_is_negative
)
493 decimal_exponent
= -decimal_exponent
;
496 /* From now on: the decimal exponent is > 0. Its sign is separate. */
498 size_of_power_in_chars
= size_of_power_in_littlenums
499 * sizeof (LITTLENUM_TYPE
) + 2;
501 power_binary_low
= (LITTLENUM_TYPE
*) xmalloc (size_of_power_in_chars
);
502 temporary_binary_low
= (LITTLENUM_TYPE
*) xmalloc (size_of_power_in_chars
);
504 memset ((char *) power_binary_low
, '\0', size_of_power_in_chars
);
505 *power_binary_low
= 1;
506 power_of_10_flonum
.exponent
= 0;
507 power_of_10_flonum
.low
= power_binary_low
;
508 power_of_10_flonum
.leader
= power_binary_low
;
509 power_of_10_flonum
.high
= power_binary_low
+ size_of_power_in_littlenums
- 1;
510 power_of_10_flonum
.sign
= '+';
511 temporary_flonum
.low
= temporary_binary_low
;
512 temporary_flonum
.high
= temporary_binary_low
+ size_of_power_in_littlenums
- 1;
515 * Space for temporary_flonum allocated.
522 * DO find next bit (with place value)
523 * multiply into power mantissa
527 int place_number_limit
;
528 /* Any 10^(2^n) whose "n" exceeds this */
529 /* value will fall off the end of */
530 /* flonum_XXXX_powers_of_ten[]. */
532 const FLONUM_TYPE
*multiplicand
; /* -> 10^(2^n) */
534 place_number_limit
= table_size_of_flonum_powers_of_ten
;
536 multiplicand
= (decimal_exponent_is_negative
537 ? flonum_negative_powers_of_ten
538 : flonum_positive_powers_of_ten
);
540 for (place_number
= 1;/* Place value of this bit of exponent. */
541 decimal_exponent
;/* Quit when no more 1 bits in exponent. */
542 decimal_exponent
>>= 1, place_number
++)
544 if (decimal_exponent
& 1)
546 if (place_number
> place_number_limit
)
548 /* The decimal exponent has a magnitude so great
549 that our tables can't help us fragment it.
550 Although this routine is in error because it
551 can't imagine a number that big, signal an
552 error as if it is the user's fault for
553 presenting such a big number. */
554 return_value
= ERROR_EXPONENT_OVERFLOW
;
555 /* quit out of loop gracefully */
556 decimal_exponent
= 0;
561 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
564 flonum_print (&power_of_10_flonum
);
565 (void) putchar ('\n');
568 printf ("multiplier:\n");
569 flonum_print (multiplicand
+ place_number
);
570 (void) putchar ('\n');
572 flonum_multip (multiplicand
+ place_number
,
573 &power_of_10_flonum
, &temporary_flonum
);
575 printf ("after multiply:\n");
576 flonum_print (&temporary_flonum
);
577 (void) putchar ('\n');
579 flonum_copy (&temporary_flonum
, &power_of_10_flonum
);
581 printf ("after copy:\n");
582 flonum_print (&power_of_10_flonum
);
583 (void) putchar ('\n');
585 } /* If this bit of decimal_exponent was computable.*/
586 } /* If this bit of decimal_exponent was set. */
587 } /* For each bit of binary representation of exponent */
589 printf ("after computing power_of_10_flonum:\n");
590 flonum_print (&power_of_10_flonum
);
591 (void) putchar ('\n');
597 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
598 * It may be the number 1, in which case we don't NEED to multiply.
600 * Multiply (decimal digits) by power_of_10_flonum.
603 flonum_multip (&power_of_10_flonum
, &digits_flonum
, address_of_generic_floating_point_number
);
604 /* Assert sign of the number we made is '+'. */
605 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
607 free (temporary_binary_low
);
608 free (power_binary_low
);
609 free (digits_binary_low
);
617 const FLONUM_TYPE
*f
;
620 char littlenum_format
[10];
621 sprintf (littlenum_format
, " %%0%dx", sizeof (LITTLENUM_TYPE
) * 2);
622 #define print_littlenum(LP) (printf (littlenum_format, LP))
623 printf ("flonum @%p %c e%ld", f
, f
->sign
, f
->exponent
);
624 if (f
->low
< f
->high
)
625 for (lp
= f
->high
; lp
>= f
->low
; lp
--)
626 print_littlenum (*lp
);
628 for (lp
= f
->low
; lp
<= f
->high
; lp
++)
629 print_littlenum (*lp
);
635 /* end of atof_generic.c */