gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gas / atof-generic.c
CommitLineData
252b5132 1/* atof_generic.c - turn a string of digits into a Flonum
b3adc24a 2 Copyright (C) 1987-2020 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
RH
23
24#ifndef FALSE
25#define FALSE (0)
26#endif
27#ifndef TRUE
28#define TRUE (1)
29#endif
30
31#ifdef TRACE
73ee5e4c 32static void flonum_print (const FLONUM_TYPE *);
252b5132
RH
33#endif
34
35#define ASSUME_DECIMAL_MARK_IS_DOT
36
37/***********************************************************************\
38 * *
39 * Given a string of decimal digits , with optional decimal *
40 * mark and optional decimal exponent (place value) of the *
41 * lowest_order decimal digit: produce a floating point *
42 * number. The number is 'generic' floating point: our *
43 * caller will encode it for a specific machine architecture. *
44 * *
45 * Assumptions *
46 * uses base (radix) 2 *
47 * this machine uses 2's complement binary integers *
48 * target flonums use " " " " *
49 * target flonums exponents fit in a long *
50 * *
51 \***********************************************************************/
52
53/*
54
55 Syntax:
56
57 <flonum> ::= <optional-sign> <decimal-number> <optional-exponent>
58 <optional-sign> ::= '+' | '-' | {empty}
59 <decimal-number> ::= <integer>
60 | <integer> <radix-character>
61 | <integer> <radix-character> <integer>
62 | <radix-character> <integer>
63
64 <optional-exponent> ::= {empty}
65 | <exponent-character> <optional-sign> <integer>
66
67 <integer> ::= <digit> | <digit> <integer>
68 <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
69 <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"}
70 <radix-character> ::= {one character from "string_of_decimal_marks"}
71
72 */
73
74int
73ee5e4c
KH
75atof_generic (/* return pointer to just AFTER number we read. */
76 char **address_of_string_pointer,
77 /* At most one per number. */
78 const char *string_of_decimal_marks,
79 const char *string_of_decimal_exponent_marks,
80 FLONUM_TYPE *address_of_generic_floating_point_number)
252b5132 81{
e49bc11e 82 int return_value; /* 0 means OK. */
252b5132
RH
83 char *first_digit;
84 unsigned int number_of_digits_before_decimal;
85 unsigned int number_of_digits_after_decimal;
86 long decimal_exponent;
87 unsigned int number_of_digits_available;
88 char digits_sign_char;
89
90 /*
91 * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
92 * It would be simpler to modify the string, but we don't; just to be nice
93 * to caller.
94 * We need to know how many digits we have, so we can allocate space for
95 * the digits' value.
96 */
97
98 char *p;
99 char c;
100 int seen_significant_digit;
101
102#ifdef ASSUME_DECIMAL_MARK_IS_DOT
9c2799c2 103 gas_assert (string_of_decimal_marks[0] == '.'
252b5132
RH
104 && string_of_decimal_marks[1] == 0);
105#define IS_DECIMAL_MARK(c) ((c) == '.')
106#else
107#define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c)))
108#endif
109
110 first_digit = *address_of_string_pointer;
111 c = *first_digit;
112
113 if (c == '-' || c == '+')
114 {
115 digits_sign_char = c;
116 first_digit++;
117 }
118 else
119 digits_sign_char = '+';
120
121 switch (first_digit[0])
122 {
123 case 'n':
124 case 'N':
125 if (!strncasecmp ("nan", first_digit, 3))
126 {
127 address_of_generic_floating_point_number->sign = 0;
128 address_of_generic_floating_point_number->exponent = 0;
129 address_of_generic_floating_point_number->leader =
130 address_of_generic_floating_point_number->low;
131 *address_of_string_pointer = first_digit + 3;
132 return 0;
133 }
134 break;
135
136 case 'i':
137 case 'I':
138 if (!strncasecmp ("inf", first_digit, 3))
139 {
140 address_of_generic_floating_point_number->sign =
141 digits_sign_char == '+' ? 'P' : 'N';
142 address_of_generic_floating_point_number->exponent = 0;
143 address_of_generic_floating_point_number->leader =
144 address_of_generic_floating_point_number->low;
145
146 first_digit += 3;
147 if (!strncasecmp ("inity", first_digit, 5))
148 first_digit += 5;
149
150 *address_of_string_pointer = first_digit;
151
152 return 0;
153 }
154 break;
155 }
156
157 number_of_digits_before_decimal = 0;
158 number_of_digits_after_decimal = 0;
159 decimal_exponent = 0;
160 seen_significant_digit = 0;
161 for (p = first_digit;
162 (((c = *p) != '\0')
163 && (!c || !IS_DECIMAL_MARK (c))
164 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
165 p++)
166 {
3882b010 167 if (ISDIGIT (c))
252b5132
RH
168 {
169 if (seen_significant_digit || c > '0')
170 {
171 ++number_of_digits_before_decimal;
172 seen_significant_digit = 1;
173 }
174 else
175 {
176 first_digit++;
177 }
178 }
179 else
180 {
e49bc11e 181 break; /* p -> char after pre-decimal digits. */
252b5132 182 }
e49bc11e 183 } /* For each digit before decimal mark. */
252b5132
RH
184
185#ifndef OLD_FLOAT_READS
186 /* Ignore trailing 0's after the decimal point. The original code here
a3197745
BG
187 (ifdef'd out) does not do this, and numbers like
188 4.29496729600000000000e+09 (2**31)
189 come out inexact for some reason related to length of the digit
190 string. */
191
192 /* The case number_of_digits_before_decimal = 0 is handled for
193 deleting zeros after decimal. In this case the decimal mark and
194 the first zero digits after decimal mark are skipped. */
195 seen_significant_digit = 0;
196 signed long subtract_decimal_exponent = 0;
197
252b5132
RH
198 if (c && IS_DECIMAL_MARK (c))
199 {
a3197745
BG
200 unsigned int zeros = 0; /* Length of current string of zeros. */
201
202 if (number_of_digits_before_decimal == 0)
203 /* Skip decimal mark. */
204 first_digit++;
252b5132 205
3882b010 206 for (p++; (c = *p) && ISDIGIT (c); p++)
252b5132
RH
207 {
208 if (c == '0')
209 {
a3197745
BG
210 if (number_of_digits_before_decimal == 0
211 && !seen_significant_digit)
212 {
213 /* Skip '0' and the decimal mark. */
214 first_digit++;
215 subtract_decimal_exponent--;
216 }
217 else
218 zeros++;
252b5132
RH
219 }
220 else
221 {
a3197745 222 seen_significant_digit = 1;
252b5132
RH
223 number_of_digits_after_decimal += 1 + zeros;
224 zeros = 0;
225 }
226 }
227 }
228#else
229 if (c && IS_DECIMAL_MARK (c))
230 {
231 for (p++;
232 (((c = *p) != '\0')
233 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
234 p++)
235 {
3882b010 236 if (ISDIGIT (c))
252b5132 237 {
e49bc11e 238 /* This may be retracted below. */
252b5132
RH
239 number_of_digits_after_decimal++;
240
241 if ( /* seen_significant_digit || */ c > '0')
242 {
243 seen_significant_digit = TRUE;
244 }
245 }
246 else
247 {
248 if (!seen_significant_digit)
249 {
250 number_of_digits_after_decimal = 0;
251 }
252 break;
253 }
e49bc11e 254 } /* For each digit after decimal mark. */
252b5132
RH
255 }
256
257 while (number_of_digits_after_decimal
258 && first_digit[number_of_digits_before_decimal
259 + number_of_digits_after_decimal] == '0')
260 --number_of_digits_after_decimal;
261#endif
262
263 if (flag_m68k_mri)
264 {
265 while (c == '_')
266 c = *++p;
267 }
268 if (c && strchr (string_of_decimal_exponent_marks, c))
269 {
270 char digits_exponent_sign_char;
271
272 c = *++p;
273 if (flag_m68k_mri)
274 {
275 while (c == '_')
276 c = *++p;
277 }
278 if (c && strchr ("+-", c))
279 {
280 digits_exponent_sign_char = c;
281 c = *++p;
282 }
283 else
284 {
285 digits_exponent_sign_char = '+';
286 }
287
288 for (; (c); c = *++p)
289 {
3882b010 290 if (ISDIGIT (c))
252b5132
RH
291 {
292 decimal_exponent = decimal_exponent * 10 + c - '0';
293 /*
294 * BUG! If we overflow here, we lose!
295 */
296 }
297 else
298 {
299 break;
300 }
301 }
302
303 if (digits_exponent_sign_char == '-')
304 {
305 decimal_exponent = -decimal_exponent;
306 }
307 }
308
a3197745
BG
309#ifndef OLD_FLOAT_READS
310 /* Subtract_decimal_exponent != 0 when number_of_digits_before_decimal = 0
311 and first digit after decimal is '0'. */
312 decimal_exponent += subtract_decimal_exponent;
313#endif
314
252b5132
RH
315 *address_of_string_pointer = p;
316
252b5132
RH
317 number_of_digits_available =
318 number_of_digits_before_decimal + number_of_digits_after_decimal;
319 return_value = 0;
320 if (number_of_digits_available == 0)
321 {
322 address_of_generic_floating_point_number->exponent = 0; /* Not strictly necessary */
323 address_of_generic_floating_point_number->leader
324 = -1 + address_of_generic_floating_point_number->low;
325 address_of_generic_floating_point_number->sign = digits_sign_char;
326 /* We have just concocted (+/-)0.0E0 */
327
328 }
329 else
330 {
e49bc11e 331 int count; /* Number of useful digits left to scan. */
252b5132 332
e1fa0163
NC
333 LITTLENUM_TYPE *temporary_binary_low = NULL;
334 LITTLENUM_TYPE *power_binary_low = NULL;
252b5132
RH
335 LITTLENUM_TYPE *digits_binary_low;
336 unsigned int precision;
337 unsigned int maximum_useful_digits;
338 unsigned int number_of_digits_to_use;
339 unsigned int more_than_enough_bits_for_digits;
340 unsigned int more_than_enough_littlenums_for_digits;
341 unsigned int size_of_digits_in_littlenums;
342 unsigned int size_of_digits_in_chars;
343 FLONUM_TYPE power_of_10_flonum;
344 FLONUM_TYPE digits_flonum;
345
346 precision = (address_of_generic_floating_point_number->high
347 - address_of_generic_floating_point_number->low
e49bc11e 348 + 1); /* Number of destination littlenums. */
252b5132 349
94c9b9db
AM
350 /* precision includes two littlenums worth of guard bits,
351 so this gives us 10 decimal guard digits here. */
352 maximum_useful_digits = (precision
353 * LITTLENUM_NUMBER_OF_BITS
354 * 1000000 / 3321928
355 + 1); /* round up. */
252b5132
RH
356
357 if (number_of_digits_available > maximum_useful_digits)
358 {
359 number_of_digits_to_use = maximum_useful_digits;
360 }
361 else
362 {
363 number_of_digits_to_use = number_of_digits_available;
364 }
365
366 /* Cast these to SIGNED LONG first, otherwise, on systems with
367 LONG wider than INT (such as Alpha OSF/1), unsignedness may
368 cause unexpected results. */
369 decimal_exponent += ((long) number_of_digits_before_decimal
370 - (long) number_of_digits_to_use);
371
252b5132
RH
372 more_than_enough_bits_for_digits
373 = (number_of_digits_to_use * 3321928 / 1000000 + 1);
252b5132
RH
374
375 more_than_enough_littlenums_for_digits
376 = (more_than_enough_bits_for_digits
377 / LITTLENUM_NUMBER_OF_BITS)
378 + 2;
379
380 /* Compute (digits) part. In "12.34E56" this is the "1234" part.
381 Arithmetic is exact here. If no digits are supplied then this
382 part is a 0 valued binary integer. Allocate room to build up
383 the binary number as littlenums. We want this memory to
384 disappear when we leave this function. Assume no alignment
385 problems => (room for n objects) == n * (room for 1
386 object). */
387
388 size_of_digits_in_littlenums = more_than_enough_littlenums_for_digits;
389 size_of_digits_in_chars = size_of_digits_in_littlenums
390 * sizeof (LITTLENUM_TYPE);
391
392 digits_binary_low = (LITTLENUM_TYPE *)
e1fa0163 393 xmalloc (size_of_digits_in_chars);
252b5132
RH
394
395 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
396
e49bc11e 397 /* Digits_binary_low[] is allocated and zeroed. */
252b5132
RH
398
399 /*
400 * Parse the decimal digits as if * digits_low was in the units position.
401 * Emit a binary number into digits_binary_low[].
402 *
403 * Use a large-precision version of:
404 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
405 */
406
407 for (p = first_digit, count = number_of_digits_to_use; count; p++, --count)
408 {
409 c = *p;
3882b010 410 if (ISDIGIT (c))
252b5132
RH
411 {
412 /*
413 * Multiply by 10. Assume can never overflow.
414 * Add this digit to digits_binary_low[].
415 */
416
417 long carry;
418 LITTLENUM_TYPE *littlenum_pointer;
419 LITTLENUM_TYPE *littlenum_limit;
420
421 littlenum_limit = digits_binary_low
422 + more_than_enough_littlenums_for_digits
423 - 1;
424
425 carry = c - '0'; /* char -> binary */
426
427 for (littlenum_pointer = digits_binary_low;
428 littlenum_pointer <= littlenum_limit;
429 littlenum_pointer++)
430 {
431 long work;
432
433 work = carry + 10 * (long) (*littlenum_pointer);
434 *littlenum_pointer = work & LITTLENUM_MASK;
435 carry = work >> LITTLENUM_NUMBER_OF_BITS;
436 }
437
438 if (carry != 0)
439 {
440 /*
441 * We have a GROSS internal error.
442 * This should never happen.
443 */
0e389e77 444 as_fatal (_("failed sanity check"));
252b5132
RH
445 }
446 }
447 else
448 {
e49bc11e 449 ++count; /* '.' doesn't alter digits used count. */
252b5132
RH
450 }
451 }
452
252b5132
RH
453 /*
454 * Digits_binary_low[] properly encodes the value of the digits.
455 * Forget about any high-order littlenums that are 0.
456 */
457 while (digits_binary_low[size_of_digits_in_littlenums - 1] == 0
458 && size_of_digits_in_littlenums >= 2)
459 size_of_digits_in_littlenums--;
460
461 digits_flonum.low = digits_binary_low;
462 digits_flonum.high = digits_binary_low + size_of_digits_in_littlenums - 1;
463 digits_flonum.leader = digits_flonum.high;
464 digits_flonum.exponent = 0;
465 /*
466 * The value of digits_flonum . sign should not be important.
467 * We have already decided the output's sign.
468 * We trust that the sign won't influence the other parts of the number!
469 * So we give it a value for these reasons:
470 * (1) courtesy to humans reading/debugging
471 * these numbers so they don't get excited about strange values
472 * (2) in future there may be more meaning attached to sign,
473 * and what was
474 * harmless noise may become disruptive, ill-conditioned (or worse)
475 * input.
476 */
477 digits_flonum.sign = '+';
478
479 {
480 /*
33eaf5de 481 * Compute the mantissa (& exponent) of the power of 10.
47eebc20 482 * If successful, then multiply the power of 10 by the digits
252b5132
RH
483 * giving return_binary_mantissa and return_binary_exponent.
484 */
485
252b5132 486 int decimal_exponent_is_negative;
e49bc11e 487 /* This refers to the "-56" in "12.34E-56". */
252b5132
RH
488 /* FALSE: decimal_exponent is positive (or 0) */
489 /* TRUE: decimal_exponent is negative */
490 FLONUM_TYPE temporary_flonum;
252b5132
RH
491 unsigned int size_of_power_in_littlenums;
492 unsigned int size_of_power_in_chars;
493
494 size_of_power_in_littlenums = precision;
e49bc11e 495 /* Precision has a built-in fudge factor so we get a few guard bits. */
252b5132
RH
496
497 decimal_exponent_is_negative = decimal_exponent < 0;
498 if (decimal_exponent_is_negative)
499 {
500 decimal_exponent = -decimal_exponent;
501 }
502
e49bc11e 503 /* From now on: the decimal exponent is > 0. Its sign is separate. */
252b5132
RH
504
505 size_of_power_in_chars = size_of_power_in_littlenums
506 * sizeof (LITTLENUM_TYPE) + 2;
507
e1fa0163
NC
508 power_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars);
509 temporary_binary_low = (LITTLENUM_TYPE *) xmalloc (size_of_power_in_chars);
510
252b5132
RH
511 memset ((char *) power_binary_low, '\0', size_of_power_in_chars);
512 *power_binary_low = 1;
513 power_of_10_flonum.exponent = 0;
514 power_of_10_flonum.low = power_binary_low;
515 power_of_10_flonum.leader = power_binary_low;
516 power_of_10_flonum.high = power_binary_low + size_of_power_in_littlenums - 1;
517 power_of_10_flonum.sign = '+';
518 temporary_flonum.low = temporary_binary_low;
519 temporary_flonum.high = temporary_binary_low + size_of_power_in_littlenums - 1;
520 /*
521 * (power) == 1.
522 * Space for temporary_flonum allocated.
523 */
524
525 /*
526 * ...
527 *
528 * WHILE more bits
529 * DO find next bit (with place value)
530 * multiply into power mantissa
531 * OD
532 */
533 {
534 int place_number_limit;
535 /* Any 10^(2^n) whose "n" exceeds this */
536 /* value will fall off the end of */
e49bc11e 537 /* flonum_XXXX_powers_of_ten[]. */
252b5132
RH
538 int place_number;
539 const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */
540
541 place_number_limit = table_size_of_flonum_powers_of_ten;
542
543 multiplicand = (decimal_exponent_is_negative
544 ? flonum_negative_powers_of_ten
545 : flonum_positive_powers_of_ten);
546
e49bc11e
KH
547 for (place_number = 1;/* Place value of this bit of exponent. */
548 decimal_exponent;/* Quit when no more 1 bits in exponent. */
252b5132
RH
549 decimal_exponent >>= 1, place_number++)
550 {
551 if (decimal_exponent & 1)
552 {
553 if (place_number > place_number_limit)
554 {
555 /* The decimal exponent has a magnitude so great
556 that our tables can't help us fragment it.
557 Although this routine is in error because it
558 can't imagine a number that big, signal an
559 error as if it is the user's fault for
560 presenting such a big number. */
561 return_value = ERROR_EXPONENT_OVERFLOW;
562 /* quit out of loop gracefully */
563 decimal_exponent = 0;
564 }
565 else
566 {
567#ifdef TRACE
568 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
569 place_number);
570
571 flonum_print (&power_of_10_flonum);
572 (void) putchar ('\n');
573#endif
574#ifdef TRACE
575 printf ("multiplier:\n");
576 flonum_print (multiplicand + place_number);
577 (void) putchar ('\n');
578#endif
579 flonum_multip (multiplicand + place_number,
580 &power_of_10_flonum, &temporary_flonum);
581#ifdef TRACE
582 printf ("after multiply:\n");
583 flonum_print (&temporary_flonum);
584 (void) putchar ('\n');
585#endif
586 flonum_copy (&temporary_flonum, &power_of_10_flonum);
587#ifdef TRACE
588 printf ("after copy:\n");
589 flonum_print (&power_of_10_flonum);
590 (void) putchar ('\n');
591#endif
592 } /* If this bit of decimal_exponent was computable.*/
e49bc11e 593 } /* If this bit of decimal_exponent was set. */
252b5132
RH
594 } /* For each bit of binary representation of exponent */
595#ifdef TRACE
596 printf ("after computing power_of_10_flonum:\n");
597 flonum_print (&power_of_10_flonum);
598 (void) putchar ('\n');
599#endif
600 }
252b5132
RH
601 }
602
603 /*
604 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
605 * It may be the number 1, in which case we don't NEED to multiply.
606 *
607 * Multiply (decimal digits) by power_of_10_flonum.
608 */
609
610 flonum_multip (&power_of_10_flonum, &digits_flonum, address_of_generic_floating_point_number);
e49bc11e 611 /* Assert sign of the number we made is '+'. */
252b5132
RH
612 address_of_generic_floating_point_number->sign = digits_sign_char;
613
9fbb53c7
AM
614 free (temporary_binary_low);
615 free (power_binary_low);
e1fa0163 616 free (digits_binary_low);
252b5132
RH
617 }
618 return return_value;
619}
620
621#ifdef TRACE
622static void
623flonum_print (f)
624 const FLONUM_TYPE *f;
625{
626 LITTLENUM_TYPE *lp;
627 char littlenum_format[10];
628 sprintf (littlenum_format, " %%0%dx", sizeof (LITTLENUM_TYPE) * 2);
629#define print_littlenum(LP) (printf (littlenum_format, LP))
630 printf ("flonum @%p %c e%ld", f, f->sign, f->exponent);
631 if (f->low < f->high)
632 for (lp = f->high; lp >= f->low; lp--)
633 print_littlenum (*lp);
634 else
635 for (lp = f->low; lp <= f->high; lp++)
636 print_littlenum (*lp);
637 printf ("\n");
638 fflush (stdout);
639}
640#endif
641
642/* end of atof_generic.c */
This page took 0.876226 seconds and 4 git commands to generate.