1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright (C) 1987-2022 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])
120 if (!strncasecmp ("nan", first_digit
+ 1, 3))
122 address_of_generic_floating_point_number
->sign
=
123 digits_sign_char
== '+' ? TOUPPER (first_digit
[0])
124 : TOLOWER (first_digit
[0]);
125 address_of_generic_floating_point_number
->exponent
= 0;
126 address_of_generic_floating_point_number
->leader
=
127 address_of_generic_floating_point_number
->low
;
128 *address_of_string_pointer
= first_digit
+ 4;
135 if (!strncasecmp ("nan", first_digit
, 3))
137 address_of_generic_floating_point_number
->sign
=
138 digits_sign_char
== '+' ? 0 : 'q';
139 address_of_generic_floating_point_number
->exponent
= 0;
140 address_of_generic_floating_point_number
->leader
=
141 address_of_generic_floating_point_number
->low
;
142 *address_of_string_pointer
= first_digit
+ 3;
149 if (!strncasecmp ("inf", first_digit
, 3))
151 address_of_generic_floating_point_number
->sign
=
152 digits_sign_char
== '+' ? 'P' : 'N';
153 address_of_generic_floating_point_number
->exponent
= 0;
154 address_of_generic_floating_point_number
->leader
=
155 address_of_generic_floating_point_number
->low
;
158 if (!strncasecmp ("inity", first_digit
, 5))
161 *address_of_string_pointer
= first_digit
;
168 number_of_digits_before_decimal
= 0;
169 number_of_digits_after_decimal
= 0;
170 decimal_exponent
= 0;
171 seen_significant_digit
= 0;
172 for (p
= first_digit
;
174 && (!c
|| !IS_DECIMAL_MARK (c
))
175 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
180 if (seen_significant_digit
|| c
> '0')
182 ++number_of_digits_before_decimal
;
183 seen_significant_digit
= 1;
192 break; /* p -> char after pre-decimal digits. */
194 } /* For each digit before decimal mark. */
196 #ifndef OLD_FLOAT_READS
197 /* Ignore trailing 0's after the decimal point. The original code here
198 (ifdef'd out) does not do this, and numbers like
199 4.29496729600000000000e+09 (2**31)
200 come out inexact for some reason related to length of the digit
203 /* The case number_of_digits_before_decimal = 0 is handled for
204 deleting zeros after decimal. In this case the decimal mark and
205 the first zero digits after decimal mark are skipped. */
206 seen_significant_digit
= 0;
207 signed long subtract_decimal_exponent
= 0;
209 if (c
&& IS_DECIMAL_MARK (c
))
211 unsigned int zeros
= 0; /* Length of current string of zeros. */
213 if (number_of_digits_before_decimal
== 0)
214 /* Skip decimal mark. */
217 for (p
++; (c
= *p
) && ISDIGIT (c
); p
++)
221 if (number_of_digits_before_decimal
== 0
222 && !seen_significant_digit
)
224 /* Skip '0' and the decimal mark. */
226 subtract_decimal_exponent
--;
233 seen_significant_digit
= 1;
234 number_of_digits_after_decimal
+= 1 + zeros
;
240 if (c
&& IS_DECIMAL_MARK (c
))
244 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
249 /* This may be retracted below. */
250 number_of_digits_after_decimal
++;
252 if ( /* seen_significant_digit || */ c
> '0')
254 seen_significant_digit
= true;
259 if (!seen_significant_digit
)
261 number_of_digits_after_decimal
= 0;
265 } /* For each digit after decimal mark. */
268 while (number_of_digits_after_decimal
269 && first_digit
[number_of_digits_before_decimal
270 + number_of_digits_after_decimal
] == '0')
271 --number_of_digits_after_decimal
;
279 if (c
&& strchr (string_of_decimal_exponent_marks
, c
))
281 char digits_exponent_sign_char
;
289 if (c
&& strchr ("+-", c
))
291 digits_exponent_sign_char
= c
;
296 digits_exponent_sign_char
= '+';
299 for (; (c
); c
= *++p
)
303 decimal_exponent
= decimal_exponent
* 10 + c
- '0';
305 * BUG! If we overflow here, we lose!
314 if (digits_exponent_sign_char
== '-')
316 decimal_exponent
= -decimal_exponent
;
320 #ifndef OLD_FLOAT_READS
321 /* Subtract_decimal_exponent != 0 when number_of_digits_before_decimal = 0
322 and first digit after decimal is '0'. */
323 decimal_exponent
+= subtract_decimal_exponent
;
326 *address_of_string_pointer
= p
;
328 number_of_digits_available
=
329 number_of_digits_before_decimal
+ number_of_digits_after_decimal
;
331 if (number_of_digits_available
== 0)
333 address_of_generic_floating_point_number
->exponent
= 0; /* Not strictly necessary */
334 address_of_generic_floating_point_number
->leader
335 = -1 + address_of_generic_floating_point_number
->low
;
336 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
337 /* We have just concocted (+/-)0.0E0 */
342 int count
; /* Number of useful digits left to scan. */
344 LITTLENUM_TYPE
*temporary_binary_low
= NULL
;
345 LITTLENUM_TYPE
*power_binary_low
= NULL
;
346 LITTLENUM_TYPE
*digits_binary_low
;
347 unsigned int precision
;
348 unsigned int maximum_useful_digits
;
349 unsigned int number_of_digits_to_use
;
350 unsigned int more_than_enough_bits_for_digits
;
351 unsigned int more_than_enough_littlenums_for_digits
;
352 unsigned int size_of_digits_in_littlenums
;
353 unsigned int size_of_digits_in_chars
;
354 FLONUM_TYPE power_of_10_flonum
;
355 FLONUM_TYPE digits_flonum
;
357 precision
= (address_of_generic_floating_point_number
->high
358 - address_of_generic_floating_point_number
->low
359 + 1); /* Number of destination littlenums. */
361 /* precision includes two littlenums worth of guard bits,
362 so this gives us 10 decimal guard digits here. */
363 maximum_useful_digits
= (precision
364 * LITTLENUM_NUMBER_OF_BITS
366 + 1); /* round up. */
368 if (number_of_digits_available
> maximum_useful_digits
)
370 number_of_digits_to_use
= maximum_useful_digits
;
374 number_of_digits_to_use
= number_of_digits_available
;
377 /* Cast these to SIGNED LONG first, otherwise, on systems with
378 LONG wider than INT (such as Alpha OSF/1), unsignedness may
379 cause unexpected results. */
380 decimal_exponent
+= ((long) number_of_digits_before_decimal
381 - (long) number_of_digits_to_use
);
383 more_than_enough_bits_for_digits
384 = (number_of_digits_to_use
* 3321928 / 1000000 + 1);
386 more_than_enough_littlenums_for_digits
387 = (more_than_enough_bits_for_digits
388 / LITTLENUM_NUMBER_OF_BITS
)
391 /* Compute (digits) part. In "12.34E56" this is the "1234" part.
392 Arithmetic is exact here. If no digits are supplied then this
393 part is a 0 valued binary integer. Allocate room to build up
394 the binary number as littlenums. We want this memory to
395 disappear when we leave this function. Assume no alignment
396 problems => (room for n objects) == n * (room for 1
399 size_of_digits_in_littlenums
= more_than_enough_littlenums_for_digits
;
400 size_of_digits_in_chars
= size_of_digits_in_littlenums
401 * sizeof (LITTLENUM_TYPE
);
403 digits_binary_low
= (LITTLENUM_TYPE
*)
404 xmalloc (size_of_digits_in_chars
);
406 memset ((char *) digits_binary_low
, '\0', size_of_digits_in_chars
);
408 /* Digits_binary_low[] is allocated and zeroed. */
411 * Parse the decimal digits as if * digits_low was in the units position.
412 * Emit a binary number into digits_binary_low[].
414 * Use a large-precision version of:
415 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
418 for (p
= first_digit
, count
= number_of_digits_to_use
; count
; p
++, --count
)
424 * Multiply by 10. Assume can never overflow.
425 * Add this digit to digits_binary_low[].
429 LITTLENUM_TYPE
*littlenum_pointer
;
430 LITTLENUM_TYPE
*littlenum_limit
;
432 littlenum_limit
= digits_binary_low
433 + more_than_enough_littlenums_for_digits
436 carry
= c
- '0'; /* char -> binary */
438 for (littlenum_pointer
= digits_binary_low
;
439 littlenum_pointer
<= littlenum_limit
;
444 work
= carry
+ 10 * (long) (*littlenum_pointer
);
445 *littlenum_pointer
= work
& LITTLENUM_MASK
;
446 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
452 * We have a GROSS internal error.
453 * This should never happen.
455 as_fatal (_("failed sanity check"));
460 ++count
; /* '.' doesn't alter digits used count. */
465 * Digits_binary_low[] properly encodes the value of the digits.
466 * Forget about any high-order littlenums that are 0.
468 while (digits_binary_low
[size_of_digits_in_littlenums
- 1] == 0
469 && size_of_digits_in_littlenums
>= 2)
470 size_of_digits_in_littlenums
--;
472 digits_flonum
.low
= digits_binary_low
;
473 digits_flonum
.high
= digits_binary_low
+ size_of_digits_in_littlenums
- 1;
474 digits_flonum
.leader
= digits_flonum
.high
;
475 digits_flonum
.exponent
= 0;
477 * The value of digits_flonum . sign should not be important.
478 * We have already decided the output's sign.
479 * We trust that the sign won't influence the other parts of the number!
480 * So we give it a value for these reasons:
481 * (1) courtesy to humans reading/debugging
482 * these numbers so they don't get excited about strange values
483 * (2) in future there may be more meaning attached to sign,
485 * harmless noise may become disruptive, ill-conditioned (or worse)
488 digits_flonum
.sign
= '+';
492 * Compute the mantissa (& exponent) of the power of 10.
493 * If successful, then multiply the power of 10 by the digits
494 * giving return_binary_mantissa and return_binary_exponent.
497 int decimal_exponent_is_negative
;
498 /* This refers to the "-56" in "12.34E-56". */
499 /* FALSE: decimal_exponent is positive (or 0) */
500 /* TRUE: decimal_exponent is negative */
501 FLONUM_TYPE temporary_flonum
;
502 unsigned int size_of_power_in_littlenums
;
503 unsigned int size_of_power_in_chars
;
505 size_of_power_in_littlenums
= precision
;
506 /* Precision has a built-in fudge factor so we get a few guard bits. */
508 decimal_exponent_is_negative
= decimal_exponent
< 0;
509 if (decimal_exponent_is_negative
)
511 decimal_exponent
= -decimal_exponent
;
514 /* From now on: the decimal exponent is > 0. Its sign is separate. */
516 size_of_power_in_chars
= size_of_power_in_littlenums
517 * sizeof (LITTLENUM_TYPE
) + 2;
519 power_binary_low
= (LITTLENUM_TYPE
*) xmalloc (size_of_power_in_chars
);
520 temporary_binary_low
= (LITTLENUM_TYPE
*) xmalloc (size_of_power_in_chars
);
522 memset ((char *) power_binary_low
, '\0', size_of_power_in_chars
);
523 *power_binary_low
= 1;
524 power_of_10_flonum
.exponent
= 0;
525 power_of_10_flonum
.low
= power_binary_low
;
526 power_of_10_flonum
.leader
= power_binary_low
;
527 power_of_10_flonum
.high
= power_binary_low
+ size_of_power_in_littlenums
- 1;
528 power_of_10_flonum
.sign
= '+';
529 temporary_flonum
.low
= temporary_binary_low
;
530 temporary_flonum
.high
= temporary_binary_low
+ size_of_power_in_littlenums
- 1;
533 * Space for temporary_flonum allocated.
540 * DO find next bit (with place value)
541 * multiply into power mantissa
545 int place_number_limit
;
546 /* Any 10^(2^n) whose "n" exceeds this */
547 /* value will fall off the end of */
548 /* flonum_XXXX_powers_of_ten[]. */
550 const FLONUM_TYPE
*multiplicand
; /* -> 10^(2^n) */
552 place_number_limit
= table_size_of_flonum_powers_of_ten
;
554 multiplicand
= (decimal_exponent_is_negative
555 ? flonum_negative_powers_of_ten
556 : flonum_positive_powers_of_ten
);
558 for (place_number
= 1;/* Place value of this bit of exponent. */
559 decimal_exponent
;/* Quit when no more 1 bits in exponent. */
560 decimal_exponent
>>= 1, place_number
++)
562 if (decimal_exponent
& 1)
564 if (place_number
> place_number_limit
)
566 /* The decimal exponent has a magnitude so great
567 that our tables can't help us fragment it.
568 Although this routine is in error because it
569 can't imagine a number that big, signal an
570 error as if it is the user's fault for
571 presenting such a big number. */
572 return_value
= ERROR_EXPONENT_OVERFLOW
;
573 /* quit out of loop gracefully */
574 decimal_exponent
= 0;
579 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
582 flonum_print (&power_of_10_flonum
);
583 (void) putchar ('\n');
586 printf ("multiplier:\n");
587 flonum_print (multiplicand
+ place_number
);
588 (void) putchar ('\n');
590 flonum_multip (multiplicand
+ place_number
,
591 &power_of_10_flonum
, &temporary_flonum
);
593 printf ("after multiply:\n");
594 flonum_print (&temporary_flonum
);
595 (void) putchar ('\n');
597 flonum_copy (&temporary_flonum
, &power_of_10_flonum
);
599 printf ("after copy:\n");
600 flonum_print (&power_of_10_flonum
);
601 (void) putchar ('\n');
603 } /* If this bit of decimal_exponent was computable.*/
604 } /* If this bit of decimal_exponent was set. */
605 } /* For each bit of binary representation of exponent */
607 printf ("after computing power_of_10_flonum:\n");
608 flonum_print (&power_of_10_flonum
);
609 (void) putchar ('\n');
615 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
616 * It may be the number 1, in which case we don't NEED to multiply.
618 * Multiply (decimal digits) by power_of_10_flonum.
621 flonum_multip (&power_of_10_flonum
, &digits_flonum
, address_of_generic_floating_point_number
);
622 /* Assert sign of the number we made is '+'. */
623 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
625 free (temporary_binary_low
);
626 free (power_binary_low
);
627 free (digits_binary_low
);
635 const FLONUM_TYPE
*f
;
638 char littlenum_format
[10];
639 sprintf (littlenum_format
, " %%0%dx", sizeof (LITTLENUM_TYPE
) * 2);
640 #define print_littlenum(LP) (printf (littlenum_format, LP))
641 printf ("flonum @%p %c e%ld", f
, f
->sign
, f
->exponent
);
642 if (f
->low
< f
->high
)
643 for (lp
= f
->high
; lp
>= f
->low
; lp
--)
644 print_littlenum (*lp
);
646 for (lp
= f
->low
; lp
<= f
->high
; lp
++)
647 print_littlenum (*lp
);
653 /* end of atof_generic.c */