1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 static void flonum_print
PARAMS ((const FLONUM_TYPE
*));
38 #define ASSUME_DECIMAL_MARK_IS_DOT
40 /***********************************************************************\
42 * Given a string of decimal digits , with optional decimal *
43 * mark and optional decimal exponent (place value) of the *
44 * lowest_order decimal digit: produce a floating point *
45 * number. The number is 'generic' floating point: our *
46 * caller will encode it for a specific machine architecture. *
49 * uses base (radix) 2 *
50 * this machine uses 2's complement binary integers *
51 * target flonums use " " " " *
52 * target flonums exponents fit in a long *
54 \***********************************************************************/
60 <flonum> ::= <optional-sign> <decimal-number> <optional-exponent>
61 <optional-sign> ::= '+' | '-' | {empty}
62 <decimal-number> ::= <integer>
63 | <integer> <radix-character>
64 | <integer> <radix-character> <integer>
65 | <radix-character> <integer>
67 <optional-exponent> ::= {empty}
68 | <exponent-character> <optional-sign> <integer>
70 <integer> ::= <digit> | <digit> <integer>
71 <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
72 <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"}
73 <radix-character> ::= {one character from "string_of_decimal_marks"}
78 atof_generic (address_of_string_pointer
,
79 string_of_decimal_marks
,
80 string_of_decimal_exponent_marks
,
81 address_of_generic_floating_point_number
)
82 /* return pointer to just AFTER number we read. */
83 char **address_of_string_pointer
;
84 /* At most one per number. */
85 const char *string_of_decimal_marks
;
86 const char *string_of_decimal_exponent_marks
;
87 FLONUM_TYPE
*address_of_generic_floating_point_number
;
89 int return_value
; /* 0 means OK. */
91 unsigned int number_of_digits_before_decimal
;
92 unsigned int number_of_digits_after_decimal
;
93 long decimal_exponent
;
94 unsigned int number_of_digits_available
;
95 char digits_sign_char
;
98 * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
99 * It would be simpler to modify the string, but we don't; just to be nice
101 * We need to know how many digits we have, so we can allocate space for
107 int seen_significant_digit
;
109 #ifdef ASSUME_DECIMAL_MARK_IS_DOT
110 assert (string_of_decimal_marks
[0] == '.'
111 && string_of_decimal_marks
[1] == 0);
112 #define IS_DECIMAL_MARK(c) ((c) == '.')
114 #define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c)))
117 first_digit
= *address_of_string_pointer
;
120 if (c
== '-' || c
== '+')
122 digits_sign_char
= c
;
126 digits_sign_char
= '+';
128 switch (first_digit
[0])
132 if (!strncasecmp ("nan", first_digit
, 3))
134 address_of_generic_floating_point_number
->sign
= 0;
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
;
138 *address_of_string_pointer
= first_digit
+ 3;
145 if (!strncasecmp ("inf", first_digit
, 3))
147 address_of_generic_floating_point_number
->sign
=
148 digits_sign_char
== '+' ? 'P' : 'N';
149 address_of_generic_floating_point_number
->exponent
= 0;
150 address_of_generic_floating_point_number
->leader
=
151 address_of_generic_floating_point_number
->low
;
154 if (!strncasecmp ("inity", first_digit
, 5))
157 *address_of_string_pointer
= first_digit
;
164 number_of_digits_before_decimal
= 0;
165 number_of_digits_after_decimal
= 0;
166 decimal_exponent
= 0;
167 seen_significant_digit
= 0;
168 for (p
= first_digit
;
170 && (!c
|| !IS_DECIMAL_MARK (c
))
171 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
174 if (isdigit ((unsigned char) c
))
176 if (seen_significant_digit
|| c
> '0')
178 ++number_of_digits_before_decimal
;
179 seen_significant_digit
= 1;
188 break; /* p -> char after pre-decimal digits. */
190 } /* For each digit before decimal mark. */
192 #ifndef OLD_FLOAT_READS
193 /* Ignore trailing 0's after the decimal point. The original code here
194 * (ifdef'd out) does not do this, and numbers like
195 * 4.29496729600000000000e+09 (2**31)
196 * come out inexact for some reason related to length of the digit
199 if (c
&& IS_DECIMAL_MARK (c
))
201 unsigned int zeros
= 0; /* Length of current string of zeros */
203 for (p
++; (c
= *p
) && isdigit ((unsigned char) c
); p
++)
211 number_of_digits_after_decimal
+= 1 + zeros
;
217 if (c
&& IS_DECIMAL_MARK (c
))
221 && (!c
|| !strchr (string_of_decimal_exponent_marks
, c
)));
224 if (isdigit ((unsigned char) c
))
226 /* This may be retracted below. */
227 number_of_digits_after_decimal
++;
229 if ( /* seen_significant_digit || */ c
> '0')
231 seen_significant_digit
= TRUE
;
236 if (!seen_significant_digit
)
238 number_of_digits_after_decimal
= 0;
242 } /* For each digit after decimal mark. */
245 while (number_of_digits_after_decimal
246 && first_digit
[number_of_digits_before_decimal
247 + number_of_digits_after_decimal
] == '0')
248 --number_of_digits_after_decimal
;
256 if (c
&& strchr (string_of_decimal_exponent_marks
, c
))
258 char digits_exponent_sign_char
;
266 if (c
&& strchr ("+-", c
))
268 digits_exponent_sign_char
= c
;
273 digits_exponent_sign_char
= '+';
276 for (; (c
); c
= *++p
)
278 if (isdigit ((unsigned char) c
))
280 decimal_exponent
= decimal_exponent
* 10 + c
- '0';
282 * BUG! If we overflow here, we lose!
291 if (digits_exponent_sign_char
== '-')
293 decimal_exponent
= -decimal_exponent
;
297 *address_of_string_pointer
= p
;
299 number_of_digits_available
=
300 number_of_digits_before_decimal
+ number_of_digits_after_decimal
;
302 if (number_of_digits_available
== 0)
304 address_of_generic_floating_point_number
->exponent
= 0; /* Not strictly necessary */
305 address_of_generic_floating_point_number
->leader
306 = -1 + address_of_generic_floating_point_number
->low
;
307 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
308 /* We have just concocted (+/-)0.0E0 */
313 int count
; /* Number of useful digits left to scan. */
315 LITTLENUM_TYPE
*digits_binary_low
;
316 unsigned int precision
;
317 unsigned int maximum_useful_digits
;
318 unsigned int number_of_digits_to_use
;
319 unsigned int more_than_enough_bits_for_digits
;
320 unsigned int more_than_enough_littlenums_for_digits
;
321 unsigned int size_of_digits_in_littlenums
;
322 unsigned int size_of_digits_in_chars
;
323 FLONUM_TYPE power_of_10_flonum
;
324 FLONUM_TYPE digits_flonum
;
326 precision
= (address_of_generic_floating_point_number
->high
327 - address_of_generic_floating_point_number
->low
328 + 1); /* Number of destination littlenums. */
330 /* Includes guard bits (two littlenums worth) */
331 #if 0 /* The integer version below is very close, and it doesn't
332 require floating point support (which is currently buggy on
334 maximum_useful_digits
= (((double) (precision
- 2))
335 * ((double) (LITTLENUM_NUMBER_OF_BITS
))
336 / (LOG_TO_BASE_2_OF_10
))
337 + 2; /* 2 :: guard digits. */
339 maximum_useful_digits
= (((precision
- 2))
340 * ( (LITTLENUM_NUMBER_OF_BITS
))
342 + 2; /* 2 :: guard digits. */
345 if (number_of_digits_available
> maximum_useful_digits
)
347 number_of_digits_to_use
= maximum_useful_digits
;
351 number_of_digits_to_use
= number_of_digits_available
;
354 /* Cast these to SIGNED LONG first, otherwise, on systems with
355 LONG wider than INT (such as Alpha OSF/1), unsignedness may
356 cause unexpected results. */
357 decimal_exponent
+= ((long) number_of_digits_before_decimal
358 - (long) number_of_digits_to_use
);
361 more_than_enough_bits_for_digits
362 = ((((double) number_of_digits_to_use
) * LOG_TO_BASE_2_OF_10
) + 1);
364 more_than_enough_bits_for_digits
365 = (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 alloca (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
)
403 if (isdigit ((unsigned char) c
))
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 mantssa (& exponent) of the power of 10.
475 * If sucessful, then multiply the power of 10 by the digits
476 * giving return_binary_mantissa and return_binary_exponent.
479 LITTLENUM_TYPE
*power_binary_low
;
480 int decimal_exponent_is_negative
;
481 /* This refers to the "-56" in "12.34E-56". */
482 /* FALSE: decimal_exponent is positive (or 0) */
483 /* TRUE: decimal_exponent is negative */
484 FLONUM_TYPE temporary_flonum
;
485 LITTLENUM_TYPE
*temporary_binary_low
;
486 unsigned int size_of_power_in_littlenums
;
487 unsigned int size_of_power_in_chars
;
489 size_of_power_in_littlenums
= precision
;
490 /* Precision has a built-in fudge factor so we get a few guard bits. */
492 decimal_exponent_is_negative
= decimal_exponent
< 0;
493 if (decimal_exponent_is_negative
)
495 decimal_exponent
= -decimal_exponent
;
498 /* From now on: the decimal exponent is > 0. Its sign is separate. */
500 size_of_power_in_chars
= size_of_power_in_littlenums
501 * sizeof (LITTLENUM_TYPE
) + 2;
503 power_binary_low
= (LITTLENUM_TYPE
*) alloca (size_of_power_in_chars
);
504 temporary_binary_low
= (LITTLENUM_TYPE
*) alloca (size_of_power_in_chars
);
505 memset ((char *) power_binary_low
, '\0', size_of_power_in_chars
);
506 *power_binary_low
= 1;
507 power_of_10_flonum
.exponent
= 0;
508 power_of_10_flonum
.low
= power_binary_low
;
509 power_of_10_flonum
.leader
= power_binary_low
;
510 power_of_10_flonum
.high
= power_binary_low
+ size_of_power_in_littlenums
- 1;
511 power_of_10_flonum
.sign
= '+';
512 temporary_flonum
.low
= temporary_binary_low
;
513 temporary_flonum
.high
= temporary_binary_low
+ size_of_power_in_littlenums
- 1;
516 * Space for temporary_flonum allocated.
523 * DO find next bit (with place value)
524 * multiply into power mantissa
528 int place_number_limit
;
529 /* Any 10^(2^n) whose "n" exceeds this */
530 /* value will fall off the end of */
531 /* flonum_XXXX_powers_of_ten[]. */
533 const FLONUM_TYPE
*multiplicand
; /* -> 10^(2^n) */
535 place_number_limit
= table_size_of_flonum_powers_of_ten
;
537 multiplicand
= (decimal_exponent_is_negative
538 ? flonum_negative_powers_of_ten
539 : flonum_positive_powers_of_ten
);
541 for (place_number
= 1;/* Place value of this bit of exponent. */
542 decimal_exponent
;/* Quit when no more 1 bits in exponent. */
543 decimal_exponent
>>= 1, place_number
++)
545 if (decimal_exponent
& 1)
547 if (place_number
> place_number_limit
)
549 /* The decimal exponent has a magnitude so great
550 that our tables can't help us fragment it.
551 Although this routine is in error because it
552 can't imagine a number that big, signal an
553 error as if it is the user's fault for
554 presenting such a big number. */
555 return_value
= ERROR_EXPONENT_OVERFLOW
;
556 /* quit out of loop gracefully */
557 decimal_exponent
= 0;
562 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
565 flonum_print (&power_of_10_flonum
);
566 (void) putchar ('\n');
569 printf ("multiplier:\n");
570 flonum_print (multiplicand
+ place_number
);
571 (void) putchar ('\n');
573 flonum_multip (multiplicand
+ place_number
,
574 &power_of_10_flonum
, &temporary_flonum
);
576 printf ("after multiply:\n");
577 flonum_print (&temporary_flonum
);
578 (void) putchar ('\n');
580 flonum_copy (&temporary_flonum
, &power_of_10_flonum
);
582 printf ("after copy:\n");
583 flonum_print (&power_of_10_flonum
);
584 (void) putchar ('\n');
586 } /* If this bit of decimal_exponent was computable.*/
587 } /* If this bit of decimal_exponent was set. */
588 } /* For each bit of binary representation of exponent */
590 printf ("after computing power_of_10_flonum:\n");
591 flonum_print (&power_of_10_flonum
);
592 (void) putchar ('\n');
599 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
600 * It may be the number 1, in which case we don't NEED to multiply.
602 * Multiply (decimal digits) by power_of_10_flonum.
605 flonum_multip (&power_of_10_flonum
, &digits_flonum
, address_of_generic_floating_point_number
);
606 /* Assert sign of the number we made is '+'. */
607 address_of_generic_floating_point_number
->sign
= digits_sign_char
;
616 const FLONUM_TYPE
*f
;
619 char littlenum_format
[10];
620 sprintf (littlenum_format
, " %%0%dx", sizeof (LITTLENUM_TYPE
) * 2);
621 #define print_littlenum(LP) (printf (littlenum_format, LP))
622 printf ("flonum @%p %c e%ld", f
, f
->sign
, f
->exponent
);
623 if (f
->low
< f
->high
)
624 for (lp
= f
->high
; lp
>= f
->low
; lp
--)
625 print_littlenum (*lp
);
627 for (lp
= f
->low
; lp
<= f
->high
; lp
++)
628 print_littlenum (*lp
);
634 /* end of atof_generic.c */