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