gas: handle csect in bss section for XCOFF
[deliverable/binutils-gdb.git] / gas / atof-generic.c
1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright (C) 1987-2021 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 'n':
117 case 'N':
118 if (!strncasecmp ("nan", first_digit, 3))
119 {
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;
125 return 0;
126 }
127 break;
128
129 case 'i':
130 case 'I':
131 if (!strncasecmp ("inf", first_digit, 3))
132 {
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;
138
139 first_digit += 3;
140 if (!strncasecmp ("inity", first_digit, 5))
141 first_digit += 5;
142
143 *address_of_string_pointer = first_digit;
144
145 return 0;
146 }
147 break;
148 }
149
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;
155 (((c = *p) != '\0')
156 && (!c || !IS_DECIMAL_MARK (c))
157 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
158 p++)
159 {
160 if (ISDIGIT (c))
161 {
162 if (seen_significant_digit || c > '0')
163 {
164 ++number_of_digits_before_decimal;
165 seen_significant_digit = 1;
166 }
167 else
168 {
169 first_digit++;
170 }
171 }
172 else
173 {
174 break; /* p -> char after pre-decimal digits. */
175 }
176 } /* For each digit before decimal mark. */
177
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
183 string. */
184
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;
190
191 if (c && IS_DECIMAL_MARK (c))
192 {
193 unsigned int zeros = 0; /* Length of current string of zeros. */
194
195 if (number_of_digits_before_decimal == 0)
196 /* Skip decimal mark. */
197 first_digit++;
198
199 for (p++; (c = *p) && ISDIGIT (c); p++)
200 {
201 if (c == '0')
202 {
203 if (number_of_digits_before_decimal == 0
204 && !seen_significant_digit)
205 {
206 /* Skip '0' and the decimal mark. */
207 first_digit++;
208 subtract_decimal_exponent--;
209 }
210 else
211 zeros++;
212 }
213 else
214 {
215 seen_significant_digit = 1;
216 number_of_digits_after_decimal += 1 + zeros;
217 zeros = 0;
218 }
219 }
220 }
221 #else
222 if (c && IS_DECIMAL_MARK (c))
223 {
224 for (p++;
225 (((c = *p) != '\0')
226 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
227 p++)
228 {
229 if (ISDIGIT (c))
230 {
231 /* This may be retracted below. */
232 number_of_digits_after_decimal++;
233
234 if ( /* seen_significant_digit || */ c > '0')
235 {
236 seen_significant_digit = true;
237 }
238 }
239 else
240 {
241 if (!seen_significant_digit)
242 {
243 number_of_digits_after_decimal = 0;
244 }
245 break;
246 }
247 } /* For each digit after decimal mark. */
248 }
249
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;
254 #endif
255
256 if (flag_m68k_mri)
257 {
258 while (c == '_')
259 c = *++p;
260 }
261 if (c && strchr (string_of_decimal_exponent_marks, c))
262 {
263 char digits_exponent_sign_char;
264
265 c = *++p;
266 if (flag_m68k_mri)
267 {
268 while (c == '_')
269 c = *++p;
270 }
271 if (c && strchr ("+-", c))
272 {
273 digits_exponent_sign_char = c;
274 c = *++p;
275 }
276 else
277 {
278 digits_exponent_sign_char = '+';
279 }
280
281 for (; (c); c = *++p)
282 {
283 if (ISDIGIT (c))
284 {
285 decimal_exponent = decimal_exponent * 10 + c - '0';
286 /*
287 * BUG! If we overflow here, we lose!
288 */
289 }
290 else
291 {
292 break;
293 }
294 }
295
296 if (digits_exponent_sign_char == '-')
297 {
298 decimal_exponent = -decimal_exponent;
299 }
300 }
301
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;
306 #endif
307
308 *address_of_string_pointer = p;
309
310 number_of_digits_available =
311 number_of_digits_before_decimal + number_of_digits_after_decimal;
312 return_value = 0;
313 if (number_of_digits_available == 0)
314 {
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 */
320
321 }
322 else
323 {
324 int count; /* Number of useful digits left to scan. */
325
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;
338
339 precision = (address_of_generic_floating_point_number->high
340 - address_of_generic_floating_point_number->low
341 + 1); /* Number of destination littlenums. */
342
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
347 * 1000000 / 3321928
348 + 1); /* round up. */
349
350 if (number_of_digits_available > maximum_useful_digits)
351 {
352 number_of_digits_to_use = maximum_useful_digits;
353 }
354 else
355 {
356 number_of_digits_to_use = number_of_digits_available;
357 }
358
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);
364
365 more_than_enough_bits_for_digits
366 = (number_of_digits_to_use * 3321928 / 1000000 + 1);
367
368 more_than_enough_littlenums_for_digits
369 = (more_than_enough_bits_for_digits
370 / LITTLENUM_NUMBER_OF_BITS)
371 + 2;
372
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
379 object). */
380
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);
384
385 digits_binary_low = (LITTLENUM_TYPE *)
386 xmalloc (size_of_digits_in_chars);
387
388 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
389
390 /* Digits_binary_low[] is allocated and zeroed. */
391
392 /*
393 * Parse the decimal digits as if * digits_low was in the units position.
394 * Emit a binary number into digits_binary_low[].
395 *
396 * Use a large-precision version of:
397 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
398 */
399
400 for (p = first_digit, count = number_of_digits_to_use; count; p++, --count)
401 {
402 c = *p;
403 if (ISDIGIT (c))
404 {
405 /*
406 * Multiply by 10. Assume can never overflow.
407 * Add this digit to digits_binary_low[].
408 */
409
410 long carry;
411 LITTLENUM_TYPE *littlenum_pointer;
412 LITTLENUM_TYPE *littlenum_limit;
413
414 littlenum_limit = digits_binary_low
415 + more_than_enough_littlenums_for_digits
416 - 1;
417
418 carry = c - '0'; /* char -> binary */
419
420 for (littlenum_pointer = digits_binary_low;
421 littlenum_pointer <= littlenum_limit;
422 littlenum_pointer++)
423 {
424 long work;
425
426 work = carry + 10 * (long) (*littlenum_pointer);
427 *littlenum_pointer = work & LITTLENUM_MASK;
428 carry = work >> LITTLENUM_NUMBER_OF_BITS;
429 }
430
431 if (carry != 0)
432 {
433 /*
434 * We have a GROSS internal error.
435 * This should never happen.
436 */
437 as_fatal (_("failed sanity check"));
438 }
439 }
440 else
441 {
442 ++count; /* '.' doesn't alter digits used count. */
443 }
444 }
445
446 /*
447 * Digits_binary_low[] properly encodes the value of the digits.
448 * Forget about any high-order littlenums that are 0.
449 */
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--;
453
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;
458 /*
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,
466 * and what was
467 * harmless noise may become disruptive, ill-conditioned (or worse)
468 * input.
469 */
470 digits_flonum.sign = '+';
471
472 {
473 /*
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.
477 */
478
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;
486
487 size_of_power_in_littlenums = precision;
488 /* Precision has a built-in fudge factor so we get a few guard bits. */
489
490 decimal_exponent_is_negative = decimal_exponent < 0;
491 if (decimal_exponent_is_negative)
492 {
493 decimal_exponent = -decimal_exponent;
494 }
495
496 /* From now on: the decimal exponent is > 0. Its sign is separate. */
497
498 size_of_power_in_chars = size_of_power_in_littlenums
499 * sizeof (LITTLENUM_TYPE) + 2;
500
501 power_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars);
502 temporary_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars);
503
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;
513 /*
514 * (power) == 1.
515 * Space for temporary_flonum allocated.
516 */
517
518 /*
519 * ...
520 *
521 * WHILE more bits
522 * DO find next bit (with place value)
523 * multiply into power mantissa
524 * OD
525 */
526 {
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[]. */
531 int place_number;
532 const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */
533
534 place_number_limit = table_size_of_flonum_powers_of_ten;
535
536 multiplicand = (decimal_exponent_is_negative
537 ? flonum_negative_powers_of_ten
538 : flonum_positive_powers_of_ten);
539
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++)
543 {
544 if (decimal_exponent & 1)
545 {
546 if (place_number > place_number_limit)
547 {
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;
557 }
558 else
559 {
560 #ifdef TRACE
561 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
562 place_number);
563
564 flonum_print (&power_of_10_flonum);
565 (void) putchar ('\n');
566 #endif
567 #ifdef TRACE
568 printf ("multiplier:\n");
569 flonum_print (multiplicand + place_number);
570 (void) putchar ('\n');
571 #endif
572 flonum_multip (multiplicand + place_number,
573 &power_of_10_flonum, &temporary_flonum);
574 #ifdef TRACE
575 printf ("after multiply:\n");
576 flonum_print (&temporary_flonum);
577 (void) putchar ('\n');
578 #endif
579 flonum_copy (&temporary_flonum, &power_of_10_flonum);
580 #ifdef TRACE
581 printf ("after copy:\n");
582 flonum_print (&power_of_10_flonum);
583 (void) putchar ('\n');
584 #endif
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 */
588 #ifdef TRACE
589 printf ("after computing power_of_10_flonum:\n");
590 flonum_print (&power_of_10_flonum);
591 (void) putchar ('\n');
592 #endif
593 }
594 }
595
596 /*
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.
599 *
600 * Multiply (decimal digits) by power_of_10_flonum.
601 */
602
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;
606
607 free (temporary_binary_low);
608 free (power_binary_low);
609 free (digits_binary_low);
610 }
611 return return_value;
612 }
613
614 #ifdef TRACE
615 static void
616 flonum_print (f)
617 const FLONUM_TYPE *f;
618 {
619 LITTLENUM_TYPE *lp;
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);
627 else
628 for (lp = f->low; lp <= f->high; lp++)
629 print_littlenum (*lp);
630 printf ("\n");
631 fflush (stdout);
632 }
633 #endif
634
635 /* end of atof_generic.c */
This page took 0.043163 seconds and 4 git commands to generate.