c3f818d9489cea4f53044b385b34cb8289a5e1cc
[binutils-gdb.git] / gas / atof-generic.c
1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright (C) 1987-2022 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 #ifdef TRACE
25 static void flonum_print (const FLONUM_TYPE *);
26 #endif
27
28 #define ASSUME_DECIMAL_MARK_IS_DOT
29
30 /***********************************************************************\
31 * *
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. *
37 * *
38 * Assumptions *
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 *
43 * *
44 \***********************************************************************/
45
46 /*
47
48 Syntax:
49
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>
56
57 <optional-exponent> ::= {empty}
58 | <exponent-character> <optional-sign> <integer>
59
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"}
64
65 */
66
67 int
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)
74 {
75 int return_value; /* 0 means OK. */
76 char *first_digit;
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;
82
83 /*
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
86 * to caller.
87 * We need to know how many digits we have, so we can allocate space for
88 * the digits' value.
89 */
90
91 char *p;
92 char c;
93 int seen_significant_digit;
94
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) == '.')
99 #else
100 #define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c)))
101 #endif
102
103 first_digit = *address_of_string_pointer;
104 c = *first_digit;
105
106 if (c == '-' || c == '+')
107 {
108 digits_sign_char = c;
109 first_digit++;
110 }
111 else
112 digits_sign_char = '+';
113
114 switch (first_digit[0])
115 {
116 case 's':
117 case 'S':
118 case 'q':
119 case 'Q':
120 if (!strncasecmp ("nan", first_digit + 1, 3))
121 {
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;
129 return 0;
130 }
131 break;
132
133 case 'n':
134 case 'N':
135 if (!strncasecmp ("nan", first_digit, 3))
136 {
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;
143 return 0;
144 }
145 break;
146
147 case 'i':
148 case 'I':
149 if (!strncasecmp ("inf", first_digit, 3))
150 {
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;
156
157 first_digit += 3;
158 if (!strncasecmp ("inity", first_digit, 5))
159 first_digit += 5;
160
161 *address_of_string_pointer = first_digit;
162
163 return 0;
164 }
165 break;
166 }
167
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;
173 (((c = *p) != '\0')
174 && (!c || !IS_DECIMAL_MARK (c))
175 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
176 p++)
177 {
178 if (ISDIGIT (c))
179 {
180 if (seen_significant_digit || c > '0')
181 {
182 ++number_of_digits_before_decimal;
183 seen_significant_digit = 1;
184 }
185 else
186 {
187 first_digit++;
188 }
189 }
190 else
191 {
192 break; /* p -> char after pre-decimal digits. */
193 }
194 } /* For each digit before decimal mark. */
195
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
201 string. */
202
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;
208
209 if (c && IS_DECIMAL_MARK (c))
210 {
211 unsigned int zeros = 0; /* Length of current string of zeros. */
212
213 if (number_of_digits_before_decimal == 0)
214 /* Skip decimal mark. */
215 first_digit++;
216
217 for (p++; (c = *p) && ISDIGIT (c); p++)
218 {
219 if (c == '0')
220 {
221 if (number_of_digits_before_decimal == 0
222 && !seen_significant_digit)
223 {
224 /* Skip '0' and the decimal mark. */
225 first_digit++;
226 subtract_decimal_exponent--;
227 }
228 else
229 zeros++;
230 }
231 else
232 {
233 seen_significant_digit = 1;
234 number_of_digits_after_decimal += 1 + zeros;
235 zeros = 0;
236 }
237 }
238 }
239 #else
240 if (c && IS_DECIMAL_MARK (c))
241 {
242 for (p++;
243 (((c = *p) != '\0')
244 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
245 p++)
246 {
247 if (ISDIGIT (c))
248 {
249 /* This may be retracted below. */
250 number_of_digits_after_decimal++;
251
252 if ( /* seen_significant_digit || */ c > '0')
253 {
254 seen_significant_digit = true;
255 }
256 }
257 else
258 {
259 if (!seen_significant_digit)
260 {
261 number_of_digits_after_decimal = 0;
262 }
263 break;
264 }
265 } /* For each digit after decimal mark. */
266 }
267
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;
272 #endif
273
274 if (flag_m68k_mri)
275 {
276 while (c == '_')
277 c = *++p;
278 }
279 if (c && strchr (string_of_decimal_exponent_marks, c))
280 {
281 char digits_exponent_sign_char;
282
283 c = *++p;
284 if (flag_m68k_mri)
285 {
286 while (c == '_')
287 c = *++p;
288 }
289 if (c && strchr ("+-", c))
290 {
291 digits_exponent_sign_char = c;
292 c = *++p;
293 }
294 else
295 {
296 digits_exponent_sign_char = '+';
297 }
298
299 for (; (c); c = *++p)
300 {
301 if (ISDIGIT (c))
302 {
303 decimal_exponent = decimal_exponent * 10 + c - '0';
304 /*
305 * BUG! If we overflow here, we lose!
306 */
307 }
308 else
309 {
310 break;
311 }
312 }
313
314 if (digits_exponent_sign_char == '-')
315 {
316 decimal_exponent = -decimal_exponent;
317 }
318 }
319
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;
324 #endif
325
326 *address_of_string_pointer = p;
327
328 number_of_digits_available =
329 number_of_digits_before_decimal + number_of_digits_after_decimal;
330 return_value = 0;
331 if (number_of_digits_available == 0)
332 {
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 */
338
339 }
340 else
341 {
342 int count; /* Number of useful digits left to scan. */
343
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;
356
357 precision = (address_of_generic_floating_point_number->high
358 - address_of_generic_floating_point_number->low
359 + 1); /* Number of destination littlenums. */
360
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
365 * 1000000 / 3321928
366 + 1); /* round up. */
367
368 if (number_of_digits_available > maximum_useful_digits)
369 {
370 number_of_digits_to_use = maximum_useful_digits;
371 }
372 else
373 {
374 number_of_digits_to_use = number_of_digits_available;
375 }
376
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);
382
383 more_than_enough_bits_for_digits
384 = (number_of_digits_to_use * 3321928 / 1000000 + 1);
385
386 more_than_enough_littlenums_for_digits
387 = (more_than_enough_bits_for_digits
388 / LITTLENUM_NUMBER_OF_BITS)
389 + 2;
390
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
397 object). */
398
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);
402
403 digits_binary_low = (LITTLENUM_TYPE *)
404 xmalloc (size_of_digits_in_chars);
405
406 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
407
408 /* Digits_binary_low[] is allocated and zeroed. */
409
410 /*
411 * Parse the decimal digits as if * digits_low was in the units position.
412 * Emit a binary number into digits_binary_low[].
413 *
414 * Use a large-precision version of:
415 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
416 */
417
418 for (p = first_digit, count = number_of_digits_to_use; count; p++, --count)
419 {
420 c = *p;
421 if (ISDIGIT (c))
422 {
423 /*
424 * Multiply by 10. Assume can never overflow.
425 * Add this digit to digits_binary_low[].
426 */
427
428 long carry;
429 LITTLENUM_TYPE *littlenum_pointer;
430 LITTLENUM_TYPE *littlenum_limit;
431
432 littlenum_limit = digits_binary_low
433 + more_than_enough_littlenums_for_digits
434 - 1;
435
436 carry = c - '0'; /* char -> binary */
437
438 for (littlenum_pointer = digits_binary_low;
439 littlenum_pointer <= littlenum_limit;
440 littlenum_pointer++)
441 {
442 long work;
443
444 work = carry + 10 * (long) (*littlenum_pointer);
445 *littlenum_pointer = work & LITTLENUM_MASK;
446 carry = work >> LITTLENUM_NUMBER_OF_BITS;
447 }
448
449 if (carry != 0)
450 {
451 /*
452 * We have a GROSS internal error.
453 * This should never happen.
454 */
455 as_fatal (_("failed sanity check"));
456 }
457 }
458 else
459 {
460 ++count; /* '.' doesn't alter digits used count. */
461 }
462 }
463
464 /*
465 * Digits_binary_low[] properly encodes the value of the digits.
466 * Forget about any high-order littlenums that are 0.
467 */
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--;
471
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;
476 /*
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,
484 * and what was
485 * harmless noise may become disruptive, ill-conditioned (or worse)
486 * input.
487 */
488 digits_flonum.sign = '+';
489
490 {
491 /*
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.
495 */
496
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;
504
505 size_of_power_in_littlenums = precision;
506 /* Precision has a built-in fudge factor so we get a few guard bits. */
507
508 decimal_exponent_is_negative = decimal_exponent < 0;
509 if (decimal_exponent_is_negative)
510 {
511 decimal_exponent = -decimal_exponent;
512 }
513
514 /* From now on: the decimal exponent is > 0. Its sign is separate. */
515
516 size_of_power_in_chars = size_of_power_in_littlenums
517 * sizeof (LITTLENUM_TYPE) + 2;
518
519 power_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars);
520 temporary_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars);
521
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;
531 /*
532 * (power) == 1.
533 * Space for temporary_flonum allocated.
534 */
535
536 /*
537 * ...
538 *
539 * WHILE more bits
540 * DO find next bit (with place value)
541 * multiply into power mantissa
542 * OD
543 */
544 {
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[]. */
549 int place_number;
550 const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */
551
552 place_number_limit = table_size_of_flonum_powers_of_ten;
553
554 multiplicand = (decimal_exponent_is_negative
555 ? flonum_negative_powers_of_ten
556 : flonum_positive_powers_of_ten);
557
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++)
561 {
562 if (decimal_exponent & 1)
563 {
564 if (place_number > place_number_limit)
565 {
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;
575 }
576 else
577 {
578 #ifdef TRACE
579 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
580 place_number);
581
582 flonum_print (&power_of_10_flonum);
583 (void) putchar ('\n');
584 #endif
585 #ifdef TRACE
586 printf ("multiplier:\n");
587 flonum_print (multiplicand + place_number);
588 (void) putchar ('\n');
589 #endif
590 flonum_multip (multiplicand + place_number,
591 &power_of_10_flonum, &temporary_flonum);
592 #ifdef TRACE
593 printf ("after multiply:\n");
594 flonum_print (&temporary_flonum);
595 (void) putchar ('\n');
596 #endif
597 flonum_copy (&temporary_flonum, &power_of_10_flonum);
598 #ifdef TRACE
599 printf ("after copy:\n");
600 flonum_print (&power_of_10_flonum);
601 (void) putchar ('\n');
602 #endif
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 */
606 #ifdef TRACE
607 printf ("after computing power_of_10_flonum:\n");
608 flonum_print (&power_of_10_flonum);
609 (void) putchar ('\n');
610 #endif
611 }
612 }
613
614 /*
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.
617 *
618 * Multiply (decimal digits) by power_of_10_flonum.
619 */
620
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;
624
625 free (temporary_binary_low);
626 free (power_binary_low);
627 free (digits_binary_low);
628 }
629 return return_value;
630 }
631
632 #ifdef TRACE
633 static void
634 flonum_print (f)
635 const FLONUM_TYPE *f;
636 {
637 LITTLENUM_TYPE *lp;
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);
645 else
646 for (lp = f->low; lp <= f->high; lp++)
647 print_littlenum (*lp);
648 printf ("\n");
649 fflush (stdout);
650 }
651 #endif
652
653 /* end of atof_generic.c */