This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / gas / atof-generic.c
1 /* atof_generic.c - turn a string of digits into a Flonum
2 Copyright (C) 1987, 1990, 1991, 1992 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 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public 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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <ctype.h>
21 #include <string.h>
22
23 #include "as.h"
24
25 #ifdef __GNUC__
26 #define alloca __builtin_alloca
27 #else
28 #ifdef sparc
29 #include <alloca.h>
30 #endif
31 #endif
32
33 #ifndef FALSE
34 #define FALSE (0)
35 #endif
36 #ifndef TRUE
37 #define TRUE (1)
38 #endif
39
40 /***********************************************************************\
41 * *
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. *
47 * *
48 * Assumptions *
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 *
53 * *
54 \***********************************************************************/
55
56 /*
57
58 Syntax:
59
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>
66
67 <optional-exponent> ::= {empty}
68 | <exponent-character> <optional-sign> <integer>
69
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"}
74
75 */
76
77 int
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;
88 {
89 int return_value; /* 0 means OK. */
90 char *first_digit;
91 /* char *last_digit; JF unused */
92 int number_of_digits_before_decimal;
93 int number_of_digits_after_decimal;
94 long decimal_exponent;
95 int number_of_digits_available;
96 char digits_sign_char;
97
98 /*
99 * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
100 * It would be simpler to modify the string, but we don't; just to be nice
101 * to caller.
102 * We need to know how many digits we have, so we can allocate space for
103 * the digits' value.
104 */
105
106 char *p;
107 char c;
108 int seen_significant_digit;
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 if ((first_digit[0] == 'n' || first_digit[0] == 'N')
122 && (first_digit[1] == 'a' || first_digit[1] == 'A')
123 && (first_digit[2] == 'n' || first_digit[2] == 'N'))
124 {
125 address_of_generic_floating_point_number->sign = 0;
126 address_of_generic_floating_point_number->exponent = 0;
127 address_of_generic_floating_point_number->leader =
128 address_of_generic_floating_point_number->low;
129 *address_of_string_pointer = first_digit + 3;
130 return 0;
131 }
132
133 if ((first_digit[0] == 'i' || first_digit[0] == 'I')
134 && (first_digit[1] == 'n' || first_digit[1] == 'N')
135 && (first_digit[2] == 'f' || first_digit[2] == 'F'))
136 {
137 address_of_generic_floating_point_number->sign =
138 digits_sign_char == '+' ? 'P' : 'N';
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
143 if ((first_digit[3] == 'i'
144 || first_digit[3] == 'I')
145 && (first_digit[4] == 'n'
146 || first_digit[4] == 'N')
147 && (first_digit[5] == 'i'
148 || first_digit[5] == 'I')
149 && (first_digit[6] == 't'
150 || first_digit[6] == 'T')
151 && (first_digit[7] == 'y'
152 || first_digit[7] == 'Y'))
153 {
154 *address_of_string_pointer = first_digit + 8;
155 }
156 else
157 {
158 *address_of_string_pointer = first_digit + 3;
159 }
160 return 0;
161 }
162
163 number_of_digits_before_decimal = 0;
164 number_of_digits_after_decimal = 0;
165 decimal_exponent = 0;
166 seen_significant_digit = 0;
167 for (p = first_digit;
168 (((c = *p) != '\0')
169 && (!c || !strchr (string_of_decimal_marks, c))
170 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
171 p++)
172 {
173 if (isdigit (c))
174 {
175 if (seen_significant_digit || c > '0')
176 {
177 ++number_of_digits_before_decimal;
178 seen_significant_digit = 1;
179 }
180 else
181 {
182 first_digit++;
183 }
184 }
185 else
186 {
187 break; /* p -> char after pre-decimal digits. */
188 }
189 } /* For each digit before decimal mark. */
190
191 #ifndef OLD_FLOAT_READS
192 /* Ignore trailing 0's after the decimal point. The original code here
193 * (ifdef'd out) does not do this, and numbers like
194 * 4.29496729600000000000e+09 (2**31)
195 * come out inexact for some reason related to length of the digit
196 * string.
197 */
198 if (c && strchr (string_of_decimal_marks, c))
199 {
200 int zeros = 0; /* Length of current string of zeros */
201
202 for (p++; (c = *p) && isdigit (c); p++)
203 {
204 if (c == '0')
205 {
206 zeros++;
207 }
208 else
209 {
210 number_of_digits_after_decimal += 1 + zeros;
211 zeros = 0;
212 }
213 }
214 }
215 #else
216 if (c && strchr (string_of_decimal_marks, c))
217 {
218 for (p++;
219 (((c = *p) != '\0')
220 && (!c || !strchr (string_of_decimal_exponent_marks, c)));
221 p++)
222 {
223 if (isdigit (c))
224 {
225 /* This may be retracted below. */
226 number_of_digits_after_decimal++;
227
228 if ( /* seen_significant_digit || */ c > '0')
229 {
230 seen_significant_digit = TRUE;
231 }
232 }
233 else
234 {
235 if (!seen_significant_digit)
236 {
237 number_of_digits_after_decimal = 0;
238 }
239 break;
240 }
241 } /* For each digit after decimal mark. */
242 }
243
244 while (number_of_digits_after_decimal
245 && first_digit[number_of_digits_before_decimal
246 + number_of_digits_after_decimal] == '0')
247 --number_of_digits_after_decimal;
248 #endif
249
250 if (c && strchr (string_of_decimal_exponent_marks, c))
251 {
252 char digits_exponent_sign_char;
253
254 c = *++p;
255 if (c && strchr ("+-", c))
256 {
257 digits_exponent_sign_char = c;
258 c = *++p;
259 }
260 else
261 {
262 digits_exponent_sign_char = '+';
263 }
264
265 for (; (c); c = *++p)
266 {
267 if (isdigit (c))
268 {
269 decimal_exponent = decimal_exponent * 10 + c - '0';
270 /*
271 * BUG! If we overflow here, we lose!
272 */
273 }
274 else
275 {
276 break;
277 }
278 }
279
280 if (digits_exponent_sign_char == '-')
281 {
282 decimal_exponent = -decimal_exponent;
283 }
284 }
285
286 *address_of_string_pointer = p;
287
288
289
290 number_of_digits_available =
291 number_of_digits_before_decimal + number_of_digits_after_decimal;
292 return_value = 0;
293 if (number_of_digits_available == 0)
294 {
295 address_of_generic_floating_point_number->exponent = 0; /* Not strictly necessary */
296 address_of_generic_floating_point_number->leader
297 = -1 + address_of_generic_floating_point_number->low;
298 address_of_generic_floating_point_number->sign = digits_sign_char;
299 /* We have just concocted (+/-)0.0E0 */
300
301 }
302 else
303 {
304 int count; /* Number of useful digits left to scan. */
305
306 LITTLENUM_TYPE *digits_binary_low;
307 unsigned int precision;
308 unsigned int maximum_useful_digits;
309 unsigned int number_of_digits_to_use;
310 unsigned int more_than_enough_bits_for_digits;
311 unsigned int more_than_enough_littlenums_for_digits;
312 unsigned int size_of_digits_in_littlenums;
313 unsigned int size_of_digits_in_chars;
314 FLONUM_TYPE power_of_10_flonum;
315 FLONUM_TYPE digits_flonum;
316
317 precision = (address_of_generic_floating_point_number->high
318 - address_of_generic_floating_point_number->low
319 + 1); /* Number of destination littlenums. */
320
321 /* Includes guard bits (two littlenums worth) */
322 #if 0 /* The integer version below is very close, and it doesn't
323 require floating point support (which is currently buggy on
324 the Alpha). */
325 maximum_useful_digits = (((double) (precision - 2))
326 * ((double) (LITTLENUM_NUMBER_OF_BITS))
327 / (LOG_TO_BASE_2_OF_10))
328 + 2; /* 2 :: guard digits. */
329 #else
330 maximum_useful_digits = (((precision - 2))
331 * ( (LITTLENUM_NUMBER_OF_BITS))
332 * 1000000 / 3321928)
333 + 2; /* 2 :: guard digits. */
334 #endif
335
336 if (number_of_digits_available > maximum_useful_digits)
337 {
338 number_of_digits_to_use = maximum_useful_digits;
339 }
340 else
341 {
342 number_of_digits_to_use = number_of_digits_available;
343 }
344
345 /* Cast these to SIGNED LONG first, otherwise, on systems with
346 LONG wider than INT (such as Alpha OSF/1), unsignedness may
347 cause unexpected results. */
348 decimal_exponent += ((long) number_of_digits_before_decimal
349 - (long) number_of_digits_to_use);
350
351 more_than_enough_bits_for_digits
352 = ((((double) number_of_digits_to_use) * LOG_TO_BASE_2_OF_10) + 1);
353
354 more_than_enough_littlenums_for_digits
355 = (more_than_enough_bits_for_digits
356 / LITTLENUM_NUMBER_OF_BITS)
357 + 2;
358
359 /* Compute (digits) part. In "12.34E56" this is the "1234" part.
360 Arithmetic is exact here. If no digits are supplied then this
361 part is a 0 valued binary integer. Allocate room to build up
362 the binary number as littlenums. We want this memory to
363 disappear when we leave this function. Assume no alignment
364 problems => (room for n objects) == n * (room for 1
365 object). */
366
367 size_of_digits_in_littlenums = more_than_enough_littlenums_for_digits;
368 size_of_digits_in_chars = size_of_digits_in_littlenums
369 * sizeof (LITTLENUM_TYPE);
370
371 digits_binary_low = (LITTLENUM_TYPE *)
372 alloca (size_of_digits_in_chars);
373
374 memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
375
376 /* Digits_binary_low[] is allocated and zeroed. */
377
378 /*
379 * Parse the decimal digits as if * digits_low was in the units position.
380 * Emit a binary number into digits_binary_low[].
381 *
382 * Use a large-precision version of:
383 * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
384 */
385
386 for (p = first_digit, count = number_of_digits_to_use; count; p++, --count)
387 {
388 c = *p;
389 if (isdigit (c))
390 {
391 /*
392 * Multiply by 10. Assume can never overflow.
393 * Add this digit to digits_binary_low[].
394 */
395
396 long carry;
397 LITTLENUM_TYPE *littlenum_pointer;
398 LITTLENUM_TYPE *littlenum_limit;
399
400 littlenum_limit = digits_binary_low
401 + more_than_enough_littlenums_for_digits
402 - 1;
403
404 carry = c - '0'; /* char -> binary */
405
406 for (littlenum_pointer = digits_binary_low;
407 littlenum_pointer <= littlenum_limit;
408 littlenum_pointer++)
409 {
410 long work;
411
412 work = carry + 10 * (long) (*littlenum_pointer);
413 *littlenum_pointer = work & LITTLENUM_MASK;
414 carry = work >> LITTLENUM_NUMBER_OF_BITS;
415 }
416
417 if (carry != 0)
418 {
419 /*
420 * We have a GROSS internal error.
421 * This should never happen.
422 */
423 as_fatal ("failed sanity check.");
424 }
425 }
426 else
427 {
428 ++count; /* '.' doesn't alter digits used count. */
429 }
430 }
431
432
433 /*
434 * Digits_binary_low[] properly encodes the value of the digits.
435 * Forget about any high-order littlenums that are 0.
436 */
437 while (digits_binary_low[size_of_digits_in_littlenums - 1] == 0
438 && size_of_digits_in_littlenums >= 2)
439 size_of_digits_in_littlenums--;
440
441 digits_flonum.low = digits_binary_low;
442 digits_flonum.high = digits_binary_low + size_of_digits_in_littlenums - 1;
443 digits_flonum.leader = digits_flonum.high;
444 digits_flonum.exponent = 0;
445 /*
446 * The value of digits_flonum . sign should not be important.
447 * We have already decided the output's sign.
448 * We trust that the sign won't influence the other parts of the number!
449 * So we give it a value for these reasons:
450 * (1) courtesy to humans reading/debugging
451 * these numbers so they don't get excited about strange values
452 * (2) in future there may be more meaning attached to sign,
453 * and what was
454 * harmless noise may become disruptive, ill-conditioned (or worse)
455 * input.
456 */
457 digits_flonum.sign = '+';
458
459 {
460 /*
461 * Compute the mantssa (& exponent) of the power of 10.
462 * If sucessful, then multiply the power of 10 by the digits
463 * giving return_binary_mantissa and return_binary_exponent.
464 */
465
466 LITTLENUM_TYPE *power_binary_low;
467 int decimal_exponent_is_negative;
468 /* This refers to the "-56" in "12.34E-56". */
469 /* FALSE: decimal_exponent is positive (or 0) */
470 /* TRUE: decimal_exponent is negative */
471 FLONUM_TYPE temporary_flonum;
472 LITTLENUM_TYPE *temporary_binary_low;
473 unsigned int size_of_power_in_littlenums;
474 unsigned int size_of_power_in_chars;
475
476 size_of_power_in_littlenums = precision;
477 /* Precision has a built-in fudge factor so we get a few guard bits. */
478
479 decimal_exponent_is_negative = decimal_exponent < 0;
480 if (decimal_exponent_is_negative)
481 {
482 decimal_exponent = -decimal_exponent;
483 }
484
485 /* From now on: the decimal exponent is > 0. Its sign is seperate. */
486
487 size_of_power_in_chars = size_of_power_in_littlenums
488 * sizeof (LITTLENUM_TYPE) + 2;
489
490 power_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars);
491 temporary_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars);
492 memset ((char *) power_binary_low, '\0', size_of_power_in_chars);
493 *power_binary_low = 1;
494 power_of_10_flonum.exponent = 0;
495 power_of_10_flonum.low = power_binary_low;
496 power_of_10_flonum.leader = power_binary_low;
497 power_of_10_flonum.high = power_binary_low + size_of_power_in_littlenums - 1;
498 power_of_10_flonum.sign = '+';
499 temporary_flonum.low = temporary_binary_low;
500 temporary_flonum.high = temporary_binary_low + size_of_power_in_littlenums - 1;
501 /*
502 * (power) == 1.
503 * Space for temporary_flonum allocated.
504 */
505
506 /*
507 * ...
508 *
509 * WHILE more bits
510 * DO find next bit (with place value)
511 * multiply into power mantissa
512 * OD
513 */
514 {
515 int place_number_limit;
516 /* Any 10^(2^n) whose "n" exceeds this */
517 /* value will fall off the end of */
518 /* flonum_XXXX_powers_of_ten[]. */
519 int place_number;
520 const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */
521
522 place_number_limit = table_size_of_flonum_powers_of_ten;
523
524 multiplicand = (decimal_exponent_is_negative
525 ? flonum_negative_powers_of_ten
526 : flonum_positive_powers_of_ten);
527
528 for (place_number = 1;/* Place value of this bit of exponent. */
529 decimal_exponent;/* Quit when no more 1 bits in exponent. */
530 decimal_exponent >>= 1, place_number++)
531 {
532 if (decimal_exponent & 1)
533 {
534 if (place_number > place_number_limit)
535 {
536 /* The decimal exponent has a magnitude so great
537 that our tables can't help us fragment it.
538 Although this routine is in error because it
539 can't imagine a number that big, signal an
540 error as if it is the user's fault for
541 presenting such a big number. */
542 return_value = ERROR_EXPONENT_OVERFLOW;
543 /* quit out of loop gracefully */
544 decimal_exponent = 0;
545 }
546 else
547 {
548 #ifdef TRACE
549 printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
550 place_number);
551
552 flonum_print (&power_of_10_flonum);
553 (void) putchar ('\n');
554 #endif
555 flonum_multip (multiplicand + place_number,
556 &power_of_10_flonum, &temporary_flonum);
557 flonum_copy (&temporary_flonum, &power_of_10_flonum);
558 } /* If this bit of decimal_exponent was computable.*/
559 } /* If this bit of decimal_exponent was set. */
560 } /* For each bit of binary representation of exponent */
561 #ifdef TRACE
562 printf (" after computing power_of_10_flonum: ");
563 flonum_print (&power_of_10_flonum);
564 (void) putchar ('\n');
565 #endif
566 }
567
568 }
569
570 /*
571 * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
572 * It may be the number 1, in which case we don't NEED to multiply.
573 *
574 * Multiply (decimal digits) by power_of_10_flonum.
575 */
576
577 flonum_multip (&power_of_10_flonum, &digits_flonum, address_of_generic_floating_point_number);
578 /* Assert sign of the number we made is '+'. */
579 address_of_generic_floating_point_number->sign = digits_sign_char;
580
581 }
582 return return_value;
583 }
584
585 /* end of atof_generic.c */
This page took 0.042291 seconds and 5 git commands to generate.