gas: drop TC_ADDRESS_BYTES conditionals
[deliverable/binutils-gdb.git] / gas / atof-generic.c
CommitLineData
252b5132 1/* atof_generic.c - turn a string of digits into a Flonum
250d07de 2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
252b5132
RH
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
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
ec2655a6
NC
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.
252b5132
RH
15
16 You should have received a copy of the GNU General Public License
e49bc11e 17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
252b5132 21#include "as.h"
3882b010 22#include "safe-ctype.h"
252b5132 23
252b5132 24#ifdef TRACE
73ee5e4c 25static void flonum_print (const FLONUM_TYPE *);
252b5132
RH
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
67int
73ee5e4c
KH
68atof_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)
252b5132 74{
e49bc11e 75 int return_value; /* 0 means OK. */
252b5132
RH
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
9c2799c2 96 gas_assert (string_of_decimal_marks[0] == '.'
252b5132
RH
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 {
3882b010 160 if (ISDIGIT (c))
252b5132
RH
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 {
e49bc11e 174 break; /* p -> char after pre-decimal digits. */
252b5132 175 }
e49bc11e 176 } /* For each digit before decimal mark. */
252b5132
RH
177
178#ifndef OLD_FLOAT_READS
179 /* Ignore trailing 0's after the decimal point. The original code here
a3197745
BG
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
252b5132
RH
191 if (c && IS_DECIMAL_MARK (c))
192 {
a3197745
BG
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++;
252b5132 198
3882b010 199 for (p++; (c = *p) && ISDIGIT (c); p++)
252b5132
RH
200 {
201 if (c == '0')
202 {
a3197745
BG
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++;
252b5132
RH
212 }
213 else
214 {
a3197745 215 seen_significant_digit = 1;
252b5132
RH
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 {
3882b010 229 if (ISDIGIT (c))
252b5132 230 {
e49bc11e 231 /* This may be retracted below. */
252b5132
RH
232 number_of_digits_after_decimal++;
233
234 if ( /* seen_significant_digit || */ c > '0')
235 {
5b7c81bd 236 seen_significant_digit = true;
252b5132
RH
237 }
238 }
239 else
240 {
241 if (!seen_significant_digit)
242 {
243 number_of_digits_after_decimal = 0;
244 }
245 break;
246 }
e49bc11e 247 } /* For each digit after decimal mark. */
252b5132
RH
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 {
3882b010 283 if (ISDIGIT (c))
252b5132
RH
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
a3197745
BG
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
252b5132
RH
308 *address_of_string_pointer = p;
309
252b5132
RH
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 {
e49bc11e 324 int count; /* Number of useful digits left to scan. */
252b5132 325
e1fa0163
NC
326 LITTLENUM_TYPE *temporary_binary_low = NULL;
327 LITTLENUM_TYPE *power_binary_low = NULL;
252b5132
RH
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
e49bc11e 341 + 1); /* Number of destination littlenums. */
252b5132 342
94c9b9db
AM
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. */
252b5132
RH
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
252b5132
RH
365 more_than_enough_bits_for_digits
366 = (number_of_digits_to_use * 3321928 / 1000000 + 1);
252b5132
RH
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 *)
e1fa0163 386 xmalloc (size_of_digits_in_chars);
252b5132
RH
387
388 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
389
e49bc11e 390 /* Digits_binary_low[] is allocated and zeroed. */
252b5132
RH
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;
3882b010 403 if (ISDIGIT (c))
252b5132
RH
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 */
0e389e77 437 as_fatal (_("failed sanity check"));
252b5132
RH
438 }
439 }
440 else
441 {
e49bc11e 442 ++count; /* '.' doesn't alter digits used count. */
252b5132
RH
443 }
444 }
445
252b5132
RH
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 /*
33eaf5de 474 * Compute the mantissa (& exponent) of the power of 10.
47eebc20 475 * If successful, then multiply the power of 10 by the digits
252b5132
RH
476 * giving return_binary_mantissa and return_binary_exponent.
477 */
478
252b5132 479 int decimal_exponent_is_negative;
e49bc11e 480 /* This refers to the "-56" in "12.34E-56". */
252b5132
RH
481 /* FALSE: decimal_exponent is positive (or 0) */
482 /* TRUE: decimal_exponent is negative */
483 FLONUM_TYPE temporary_flonum;
252b5132
RH
484 unsigned int size_of_power_in_littlenums;
485 unsigned int size_of_power_in_chars;
486
487 size_of_power_in_littlenums = precision;
e49bc11e 488 /* Precision has a built-in fudge factor so we get a few guard bits. */
252b5132
RH
489
490 decimal_exponent_is_negative = decimal_exponent < 0;
491 if (decimal_exponent_is_negative)
492 {
493 decimal_exponent = -decimal_exponent;
494 }
495
e49bc11e 496 /* From now on: the decimal exponent is > 0. Its sign is separate. */
252b5132
RH
497
498 size_of_power_in_chars = size_of_power_in_littlenums
499 * sizeof (LITTLENUM_TYPE) + 2;
500
e1fa0163
NC
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
252b5132
RH
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 */
e49bc11e 530 /* flonum_XXXX_powers_of_ten[]. */
252b5132
RH
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
e49bc11e
KH
540 for (place_number = 1;/* Place value of this bit of exponent. */
541 decimal_exponent;/* Quit when no more 1 bits in exponent. */
252b5132
RH
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.*/
e49bc11e 586 } /* If this bit of decimal_exponent was set. */
252b5132
RH
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 }
252b5132
RH
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);
e49bc11e 604 /* Assert sign of the number we made is '+'. */
252b5132
RH
605 address_of_generic_floating_point_number->sign = digits_sign_char;
606
9fbb53c7
AM
607 free (temporary_binary_low);
608 free (power_binary_low);
e1fa0163 609 free (digits_binary_low);
252b5132
RH
610 }
611 return return_value;
612}
613
614#ifdef TRACE
615static void
616flonum_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.934218 seconds and 4 git commands to generate.