Wed Aug 9 14:25:35 1995 Miles Bader <miles@geech.gnu.ai.mit.edu>
[glibc.git] / stdlib / strtod.c
blob8afacfff14dbde352eefd0c10e50157ace07fa78
1 /* Read decimal floating point numbers.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 /* Configuration part. These macros are defined by `strtold.c' and `strtof.c'
23 to produce the `long double' and `float' versions of the reader. */
24 #ifndef FLOAT
25 #define FLOAT double
26 #define FLT DBL
27 #define STRTOF strtod
28 #define MPN2FLOAT __mpn_construct_double
29 #define FLOAT_HUGE_VAL HUGE_VAL
30 #endif
31 /* End of configuration part. */
33 #include <ctype.h>
34 #include <errno.h>
35 #include <float.h>
36 #include "../locale/localeinfo.h"
37 #include <math.h>
38 #include <stdlib.h>
39 #include "gmp.h"
40 #include "gmp-impl.h"
41 #include <gmp-mparam.h>
42 #include "longlong.h"
43 #include "fpioconst.h"
45 #define NDEBUG 1
46 #include <assert.h>
49 /* Constants we need from float.h; select the set for the FLOAT precision. */
50 #define MANT_DIG PASTE(FLT,_MANT_DIG)
51 #define DIG PASTE(FLT,_DIG)
52 #define MAX_EXP PASTE(FLT,_MAX_EXP)
53 #define MIN_EXP PASTE(FLT,_MIN_EXP)
54 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
55 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
57 /* Extra macros required to get FLT expanded before the pasting. */
58 #define PASTE(a,b) PASTE1(a,b)
59 #define PASTE1(a,b) a##b
61 /* Function to construct a floating point number from an MP integer
62 containing the fraction bits, a base 2 exponent, and a sign flag. */
63 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
65 /* Definitions according to limb size used. */
66 #if BITS_PER_MP_LIMB == 32
67 # define MAX_DIG_PER_LIMB 9
68 # define MAX_FAC_PER_LIMB 1000000000L
69 #elif BITS_PER_MP_LIMB == 64
70 # define MAX_DIG_PER_LIMB 19
71 # define MAX_FAC_PER_LIMB 10000000000000000000L
72 #else
73 # error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
74 #endif
77 /* Local data structure. */
78 static const mp_limb _tens_in_limb[MAX_DIG_PER_LIMB + 1] =
79 { 0, 10, 100,
80 1000, 10000, 100000,
81 1000000, 10000000, 100000000,
82 1000000000
83 #if BITS_PER_MP_LIMB > 32
84 , 10000000000, 100000000000,
85 1000000000000, 10000000000000, 100000000000000,
86 1000000000000000, 10000000000000000, 100000000000000000,
87 1000000000000000000, 10000000000000000000
88 #endif
89 #if BITS_PER_MP_LIMB > 64
90 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
91 #endif
94 #ifndef howmany
95 #define howmany(x,y) (((x)+((y)-1))/(y))
96 #endif
97 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
99 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
100 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
102 #define RETURN(val,end) \
103 do { if (endptr != 0) *endptr = (char *) (end); return val; } while (0)
105 /* Maximum size necessary for mpn integers to hold floating point numbers. */
106 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
107 + 2)
108 /* Declare an mpn integer variable that big. */
109 #define MPN_VAR(name) mp_limb name[MPNSIZE]; mp_size_t name##size
110 /* Copy an mpn integer value. */
111 #define MPN_ASSIGN(dst, src) \
112 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb))
115 /* Return a floating point number of the needed type according to the given
116 multi-precision number after possible rounding. */
117 static inline FLOAT
118 round_and_return (mp_limb *retval, int exponent, int negative,
119 mp_limb round_limb, mp_size_t round_bit, int more_bits)
121 if (exponent < MIN_EXP - 1)
123 mp_size_t shift = MIN_EXP - 1 - exponent;
125 if (shift > MANT_DIG)
127 errno = EDOM;
128 return 0.0;
131 more_bits |= (round_limb & ((1 << round_bit) - 1)) != 0;
132 if (shift == MANT_DIG)
133 /* This is a special case to handle the very seldom case where
134 the mantissa will be empty after the shift. */
136 int i;
138 round_limb = retval[RETURN_LIMB_SIZE - 1];
139 round_bit = BITS_PER_MP_LIMB - 1;
140 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
141 more_bits |= retval[i] != 0;
142 MPN_ZERO (retval, RETURN_LIMB_SIZE);
144 else if (shift >= BITS_PER_MP_LIMB)
146 int i;
148 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
149 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
150 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
151 more_bits |= retval[i] != 0;
152 more_bits |= (round_limb & ((1 << round_bit) - 1)) != 0;
154 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
155 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
156 shift % BITS_PER_MP_LIMB);
157 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
158 shift / BITS_PER_MP_LIMB);
160 else if (shift > 0)
162 round_limb = retval[0];
163 round_bit = shift - 1;
164 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
166 exponent = MIN_EXP - 2;
169 if ((round_limb & (1 << round_bit)) != 0
170 && (more_bits || (retval[0] & 1) != 0
171 || (round_limb & ((1 << round_bit) - 1)) != 0))
173 mp_limb cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
175 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
176 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
177 (retval[RETURN_LIMB_SIZE - 1]
178 & (1 << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
180 ++exponent;
181 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
182 retval[RETURN_LIMB_SIZE - 1] |= 1 << ((MANT_DIG - 1)
183 % BITS_PER_MP_LIMB);
185 else if (exponent == MIN_EXP - 2
186 && (retval[RETURN_LIMB_SIZE - 1]
187 & (1 << ((MANT_DIG - 1) % BITS_PER_MP_LIMB))) != 0)
188 /* The number was denormalized but now normalized. */
189 exponent = MIN_EXP - 1;
192 if (exponent > MAX_EXP)
193 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
195 return MPN2FLOAT (retval, exponent, negative);
199 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
200 into N. Return the size of the number limbs in NSIZE at the first
201 character od the string that is not part of the integer as the function
202 value. If the EXPONENT is small enough to be taken as an additional
203 factor for the resulting number (see code) multiply by it. */
204 static inline const char *
205 str_to_mpn (const char *str, int digcnt, mp_limb *n, mp_size_t *nsize,
206 int *exponent)
208 /* Number of digits for actual limb. */
209 int cnt = 0;
210 mp_limb low = 0;
211 mp_limb base;
213 *nsize = 0;
214 assert (digcnt > 0);
217 if (cnt == MAX_DIG_PER_LIMB)
219 if (*nsize == 0)
220 n[0] = low;
221 else
223 mp_limb cy;
224 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
225 cy += __mpn_add_1 (n, n, *nsize, low);
226 if (cy != 0)
227 n[*nsize] = cy;
229 ++(*nsize);
230 cnt = 0;
231 low = 0;
234 /* There might be thousands separators or radix characters in the string.
235 But these all can be ignored because we know the format of the number
236 is correct and we have an exact number of characters to read. */
237 while (!isdigit (*str))
238 ++str;
239 low = low * 10 + *str++ - '0';
240 ++cnt;
242 while (--digcnt > 0);
244 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
246 low *= _tens_in_limb[*exponent];
247 base = _tens_in_limb[cnt + *exponent];
248 *exponent = 0;
250 else
251 base = _tens_in_limb[cnt];
253 if (*nsize == 0)
255 n[0] = low;
256 *nsize = 1;
258 else
260 mp_limb cy;
261 cy = __mpn_mul_1 (n, n, *nsize, base);
262 cy += __mpn_add_1 (n, n, *nsize, low);
263 if (cy != 0)
264 n[(*nsize)++] = cy;
266 return str;
270 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
271 with the COUNT most significant bits of LIMB.
273 Tege doesn't like this function so I have to write it here myself. :)
274 --drepper */
275 static inline void
276 __mpn_lshift_1 (mp_limb *ptr, mp_size_t size, unsigned int count, mp_limb limb)
278 if (count == BITS_PER_MP_LIMB)
280 /* Optimize the case of shifting by exactly a word:
281 just copy words, with no actual bit-shifting. */
282 mp_size_t i;
283 for (i = size - 1; i > 0; --i)
284 ptr[i] = ptr[i - 1];
285 ptr[0] = limb;
287 else
289 (void) __mpn_lshift (ptr, ptr, size, count);
290 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
295 #define INTERNAL(x) INTERNAL1(x)
296 #define INTERNAL1(x) __##x##_internal
298 /* This file defines a function to check for correct grouping. */
299 #include "grouping.h"
302 /* Return a floating point number with the value of the given string NPTR.
303 Set *ENDPTR to the character after the last used one. If the number is
304 smaller than the smallest representable number, set `errno' to ERANGE and
305 return 0.0. If the number is too big to be represented, set `errno' to
306 ERANGE and return HUGE_VAL with the approriate sign. */
307 FLOAT
308 INTERNAL (STRTOF) (nptr, endptr, group)
309 const char *nptr;
310 char **endptr;
311 int group;
313 int negative; /* The sign of the number. */
314 MPN_VAR (num); /* MP representation of the number. */
315 int exponent; /* Exponent of the number. */
317 /* When we have to compute fractional digits we form a fraction with a
318 second multi-precision number (and we sometimes need a second for
319 temporary results). */
320 MPN_VAR (den);
322 /* Representation for the return value. */
323 mp_limb retval[RETURN_LIMB_SIZE];
324 /* Number of bits currently in result value. */
325 int bits;
327 /* Running pointer after the last character processed in the string. */
328 const char *cp, *tp;
329 /* Start of significant part of the number. */
330 const char *startp, *start_of_digits;
331 /* Points at the character following the integer and fractional digits. */
332 const char *expp;
333 /* Total number of digit and number of digits in integer part. */
334 int dig_no, int_no, lead_zero;
335 /* Contains the last character read. */
336 char c;
338 /* The radix character of the current locale. */
339 wchar_t decimal;
340 /* The thousands character of the current locale. */
341 wchar_t thousands;
342 /* The numeric grouping specification of the current locale,
343 in the format described in <locale.h>. */
344 const char *grouping;
346 if (group)
348 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
349 if (*grouping <= 0 || *grouping == CHAR_MAX)
350 grouping = NULL;
351 else
353 /* Figure out the thousands separator character. */
354 if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
355 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
356 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
357 if (thousands == L'\0')
358 grouping = NULL;
361 else
362 grouping = NULL;
364 /* Find the locale's decimal point character. */
365 if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
366 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0)
367 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
370 /* Prepare number representation. */
371 exponent = 0;
372 negative = 0;
373 bits = 0;
375 /* Parse string to get maximal legal prefix. We need the number of
376 characters of the interger part, the fractional part and the exponent. */
377 cp = nptr - 1;
378 /* Ignore leading white space. */
380 c = *++cp;
381 while (isspace (c));
383 /* Get sign of the result. */
384 if (c == '-')
386 negative = 1;
387 c = *++cp;
389 else if (c == '+')
390 c = *++cp;
392 /* Return 0.0 if no legal string is found.
393 No character is used even if a sign was found. */
394 if (!isdigit (c) && (c != decimal || !isdigit (cp[1])))
395 RETURN (0.0, nptr);
397 /* Record the start of the digits, in case we will check their grouping. */
398 start_of_digits = startp = cp;
400 /* Ignore leading zeroes. This helps us to avoid useless computations. */
401 while (c == '0' || (thousands != L'\0' && c == thousands))
402 c = *++cp;
404 /* If no other digit but a '0' is found the result is 0.0.
405 Return current read pointer. */
406 if (!isdigit (c) && c != decimal)
408 tp = correctly_grouped_prefix (start_of_digits, cp, thousands, grouping);
409 /* If TP is at the start of the digits, there was no correctly
410 grouped prefix of the string; so no number found. */
411 RETURN (0.0, tp == start_of_digits ? nptr : tp);
414 /* Remember first significant digit and read following characters until the
415 decimal point, exponent character or any non-FP number character. */
416 startp = cp;
417 dig_no = 0;
418 while (dig_no < NDIG ||
419 /* If parsing grouping info, keep going past useful digits
420 so we can check all the grouping separators. */
421 grouping)
423 if (isdigit (c))
424 ++dig_no;
425 else if (thousands == L'\0' || c != thousands)
426 /* Not a digit or separator: end of the integer part. */
427 break;
428 c = *++cp;
431 if (grouping && dig_no > 0)
433 /* Check the grouping of the digits. */
434 tp = correctly_grouped_prefix (start_of_digits, cp, thousands, grouping);
435 if (cp != tp)
437 /* Less than the entire string was correctly grouped. */
439 if (tp == start_of_digits)
440 /* No valid group of numbers at all: no valid number. */
441 RETURN (0.0, nptr);
443 if (tp < startp)
444 /* The number is validly grouped, but consists
445 only of zeroes. The whole value is zero. */
446 RETURN (0.0, tp);
448 /* Recompute DIG_NO so we won't read more digits than
449 are properly grouped. */
450 cp = tp;
451 dig_no = 0;
452 for (tp = startp; tp < cp; ++tp)
453 if (isdigit (*tp))
454 ++dig_no;
456 int_no = dig_no;
457 lead_zero = 0;
459 goto number_parsed;
463 if (dig_no >= NDIG)
464 /* Too many digits to be representable. Assigning this to EXPONENT
465 allows us to read the full number but return HUGE_VAL after parsing. */
466 exponent = MAX_10_EXP;
468 /* We have the number digits in the integer part. Whether these are all or
469 any is really a fractional digit will be decided later. */
470 int_no = dig_no;
471 lead_zero = int_no == 0 ? -1 : 0;
473 /* Read the fractional digits. */
474 if (c == decimal)
476 if (isdigit (cp[1]))
478 c = *++cp;
481 if (c != '0' && lead_zero == -1)
482 lead_zero = dig_no - int_no;
483 ++dig_no;
484 c = *++cp;
486 while (isdigit (c));
490 /* Remember start of exponent (if any). */
491 expp = cp;
493 /* Read exponent. */
494 if (tolower (c) == 'e')
496 int exp_negative = 0;
498 c = *++cp;
499 if (c == '-')
501 exp_negative = 1;
502 c = *++cp;
504 else if (c == '+')
505 c = *++cp;
507 if (isdigit (c))
511 if ((!exp_negative && exponent * 10 + int_no > MAX_10_EXP)
512 || (exp_negative
513 && exponent * 10 + int_no > -MIN_10_EXP + MANT_DIG))
514 /* The exponent is too large/small to represent a valid
515 number. */
517 FLOAT retval;
519 /* Overflow or underflow. */
520 errno = ERANGE;
521 retval = (exp_negative ? 0.0 :
522 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
524 /* Accept all following digits as part of the exponent. */
526 ++cp;
527 while (isdigit (*cp));
529 RETURN (retval, cp);
530 /* NOTREACHED */
533 exponent *= 10;
534 exponent += c - '0';
535 c = *++cp;
537 while (isdigit (c));
539 else
540 cp = expp;
542 if (exp_negative)
543 exponent = -exponent;
546 /* We don't want to have to work with trailing zeroes after the radix. */
547 if (dig_no > int_no)
549 while (expp[-1] == '0')
551 --expp;
552 --dig_no;
554 assert (dig_no >= int_no);
557 number_parsed:
559 /* The whole string is parsed. Store the address of the next character. */
560 if (endptr)
561 *endptr = (char *) cp;
563 if (dig_no == 0)
564 return 0.0;
566 /* Now we have the number of digits in total and the integer digits as well
567 as the exponent and its sign. We can decide whether the read digits are
568 really integer digits or belong to the fractional part; i.e. we normalize
569 123e-2 to 1.23. */
571 register int incr = exponent < 0 ? MAX (-int_no, exponent)
572 : MIN (dig_no - int_no, exponent);
573 int_no += incr;
574 exponent -= incr;
577 if (int_no + exponent > MAX_10_EXP + 1)
579 errno = ERANGE;
580 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
583 if (exponent - MAX(0, lead_zero) < MIN_10_EXP - (DIG + 1))
585 errno = ERANGE;
586 return 0.0;
589 if (int_no > 0)
591 /* Read the integer part as a multi-precision number to NUM. */
592 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent);
594 if (exponent > 0)
596 /* We now multiply the gained number by the given power of ten. */
597 mp_limb *psrc = num;
598 mp_limb *pdest = den;
599 int expbit = 1;
600 const struct mp_power *ttab = &_fpioconst_pow10[0];
604 if ((exponent & expbit) != 0)
606 mp_limb cy;
607 exponent ^= expbit;
609 /* FIXME: not the whole multiplication has to be done.
610 If we have the needed number of bits we only need the
611 information whether more non-zero bits follow. */
612 if (numsize >= ttab->arraysize - 2)
613 cy = __mpn_mul (pdest, psrc, numsize,
614 &ttab->array[2], ttab->arraysize - 2);
615 else
616 cy = __mpn_mul (pdest, &ttab->array[2],
617 ttab->arraysize - 2,
618 psrc, numsize);
619 numsize += ttab->arraysize - 2;
620 if (cy == 0)
621 --numsize;
622 SWAP (psrc, pdest);
624 expbit <<= 1;
625 ++ttab;
627 while (exponent != 0);
629 if (psrc == den)
630 memcpy (num, den, numsize * sizeof (mp_limb));
633 /* Determine how many bits of the result we already have. */
634 count_leading_zeros (bits, num[numsize - 1]);
635 bits = numsize * BITS_PER_MP_LIMB - bits;
637 /* Now we know the exponent of the number in base two.
638 Check it against the maximum possible exponent. */
639 if (bits > MAX_EXP)
641 errno = ERANGE;
642 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
645 /* We have already the first BITS bits of the result. Together with
646 the information whether more non-zero bits follow this is enough
647 to determine the result. */
648 if (bits > MANT_DIG)
650 int i;
651 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
652 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
653 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
654 : least_idx;
655 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
656 : least_bit - 1;
658 if (least_bit == 0)
659 memcpy (retval, &num[least_idx],
660 RETURN_LIMB_SIZE * sizeof (mp_limb));
661 else
663 for (i = least_idx; i < numsize - 1; ++i)
664 retval[i - least_idx] = (num[i] >> least_bit)
665 | (num[i + 1]
666 << (BITS_PER_MP_LIMB - least_bit));
667 if (i - least_idx < RETURN_LIMB_SIZE)
668 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
671 /* Check whether any limb beside the ones in RETVAL are non-zero. */
672 for (i = 0; num[i] == 0; ++i)
675 return round_and_return (retval, bits - 1, negative,
676 num[round_idx], round_bit,
677 int_no < dig_no || i < round_idx);
678 /* NOTREACHED */
680 else if (dig_no == int_no)
682 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
683 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
685 if (target_bit == is_bit)
687 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
688 numsize * sizeof (mp_limb));
689 /* FIXME: the following loop can be avoided if we assume a
690 maximal MANT_DIG value. */
691 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
693 else if (target_bit > is_bit)
695 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
696 num, numsize, target_bit - is_bit);
697 /* FIXME: the following loop can be avoided if we assume a
698 maximal MANT_DIG value. */
699 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
701 else
703 mp_limb cy;
704 assert (numsize < RETURN_LIMB_SIZE);
706 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
707 num, numsize, is_bit - target_bit);
708 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
709 /* FIXME: the following loop can be avoided if we assume a
710 maximal MANT_DIG value. */
711 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
714 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
715 /* NOTREACHED */
718 /* Store the bits we already have. */
719 memcpy (retval, num, numsize * sizeof (mp_limb));
720 #if RETURN_LIMB_SIZE > 1
721 if (numsize < RETURN_LIMB_SIZE)
722 retval[numsize] = 0;
723 #endif
726 /* We have to compute at least some of the fractional digits. */
728 /* We construct a fraction and the result of the division gives us
729 the needed digits. The denominator is 1.0 multiplied by the
730 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
731 123e6 gives 123 / 1000000. */
733 int expbit;
734 int cnt;
735 int neg_exp;
736 int more_bits;
737 mp_limb cy;
738 mp_limb *psrc = den;
739 mp_limb *pdest = num;
740 const struct mp_power *ttab = &_fpioconst_pow10[0];
742 assert (dig_no > int_no && exponent <= 0);
745 /* For the fractional part we need not process too much digits. One
746 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
747 ceil(BITS / 3) =: N
748 digits we should have enough bits for the result. The remaining
749 decimal digits give us the information that more bits are following.
750 This can be used while rounding. (One added as a safety margin.) */
751 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 1)
753 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 1;
754 more_bits = 1;
756 else
757 more_bits = 0;
759 neg_exp = dig_no - int_no - exponent;
761 /* Construct the denominator. */
762 densize = 0;
763 expbit = 1;
766 if ((neg_exp & expbit) != 0)
768 mp_limb cy;
769 neg_exp ^= expbit;
771 if (densize == 0)
772 memcpy (psrc, &ttab->array[2],
773 (densize = ttab->arraysize - 2) * sizeof (mp_limb));
774 else
776 cy = __mpn_mul (pdest, &ttab->array[2], ttab->arraysize - 2,
777 psrc, densize);
778 densize += ttab->arraysize - 2;
779 if (cy == 0)
780 --densize;
781 SWAP (psrc, pdest);
784 expbit <<= 1;
785 ++ttab;
787 while (neg_exp != 0);
789 if (psrc == num)
790 memcpy (den, num, densize * sizeof (mp_limb));
792 /* Read the fractional digits from the string. */
793 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent);
796 /* We now have to shift both numbers so that the highest bit in the
797 denominator is set. In the same process we copy the numerator to
798 a high place in the array so that the division constructs the wanted
799 digits. This is done by a "quasi fix point" number representation.
801 num: ddddddddddd . 0000000000000000000000
802 |--- m ---|
803 den: ddddddddddd n >= m
804 |--- n ---|
807 count_leading_zeros (cnt, den[densize - 1]);
809 (void) __mpn_lshift (den, den, densize, cnt);
810 cy = __mpn_lshift (num, num, numsize, cnt);
811 if (cy != 0)
812 num[numsize++] = cy;
814 /* Now we are ready for the division. But it is not necessary to
815 do a full multi-precision division because we only need a small
816 number of bits for the result. So we do not use __mpn_divmod
817 here but instead do the division here by hand and stop whenever
818 the needed number of bits is reached. The code itself comes
819 from the GNU MP Library by Torbj\"orn Granlund. */
821 exponent = bits;
823 switch (densize)
825 case 1:
827 mp_limb d, n, quot;
828 int used = 0;
830 n = num[0];
831 d = den[0];
832 assert (numsize == 1 && n < d);
836 udiv_qrnnd (quot, n, n, 0, d);
838 #define got_limb \
839 if (bits == 0) \
841 register int cnt; \
842 if (quot == 0) \
843 cnt = BITS_PER_MP_LIMB; \
844 else \
845 count_leading_zeros (cnt, quot); \
846 exponent -= cnt; \
847 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
849 used = MANT_DIG + cnt; \
850 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
851 bits = MANT_DIG + 1; \
853 else \
855 /* Note that we only clear the second element. */ \
856 /* The conditional is determined at compile time. */ \
857 if (RETURN_LIMB_SIZE > 1) \
858 retval[1] = 0; \
859 retval[0] = quot; \
860 bits = -cnt; \
863 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
864 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
865 quot); \
866 else \
868 used = MANT_DIG - bits; \
869 if (used > 0) \
870 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
872 bits += BITS_PER_MP_LIMB
874 got_limb;
876 while (bits <= MANT_DIG);
878 return round_and_return (retval, exponent - 1, negative,
879 quot, BITS_PER_MP_LIMB - 1 - used,
880 more_bits || n != 0);
882 case 2:
884 mp_limb d0, d1, n0, n1;
885 mp_limb quot = 0;
886 int used = 0;
888 d0 = den[0];
889 d1 = den[1];
891 if (numsize < densize)
893 if (bits <= 0)
894 exponent -= BITS_PER_MP_LIMB;
895 else
897 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
898 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
899 BITS_PER_MP_LIMB, 0);
900 else
902 used = MANT_DIG - bits;
903 if (used > 0)
904 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
906 bits += BITS_PER_MP_LIMB;
908 n1 = num[0];
909 n0 = 0;
911 else
913 n1 = num[1];
914 n0 = num[0];
917 while (bits <= MANT_DIG)
919 mp_limb r;
921 if (n1 == d1)
923 /* QUOT should be either 111..111 or 111..110. We need
924 special treatment of this rare case as normal division
925 would give overflow. */
926 quot = ~(mp_limb) 0;
928 r = n0 + d1;
929 if (r < d1) /* Carry in the addition? */
931 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
932 goto have_quot;
934 n1 = d0 - (d0 != 0);
935 n0 = -d0;
937 else
939 udiv_qrnnd (quot, r, n1, n0, d1);
940 umul_ppmm (n1, n0, d0, quot);
943 q_test:
944 if (n1 > r || (n1 == r && n0 > 0))
946 /* The estimated QUOT was too large. */
947 --quot;
949 sub_ddmmss (n1, n0, n1, n0, 0, d0);
950 r += d1;
951 if (r >= d1) /* If not carry, test QUOT again. */
952 goto q_test;
954 sub_ddmmss (n1, n0, r, 0, n1, n0);
956 have_quot:
957 got_limb;
960 return round_and_return (retval, exponent - 1, negative,
961 quot, BITS_PER_MP_LIMB - 1 - used,
962 more_bits || n1 != 0 || n0 != 0);
964 default:
966 int i;
967 mp_limb cy, dX, d1, n0, n1;
968 mp_limb quot = 0;
969 int used = 0;
971 dX = den[densize - 1];
972 d1 = den[densize - 2];
974 /* The division does not work if the upper limb of the two-limb
975 numerator is greater than the denominator. */
976 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
977 num[numsize++] = 0;
979 if (numsize < densize)
981 mp_size_t empty = densize - numsize;
983 if (bits <= 0)
985 register int i;
986 for (i = numsize; i > 0; --i)
987 num[i + empty] = num[i - 1];
988 MPN_ZERO (num, empty + 1);
989 exponent -= empty * BITS_PER_MP_LIMB;
991 else
993 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
995 /* We make a difference here because the compiler
996 cannot optimize the `else' case that good and
997 this reflects all currently used FLOAT types
998 and GMP implementations. */
999 register int i;
1000 #if RETURN_LIMB_SIZE <= 2
1001 assert (empty == 1);
1002 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1003 BITS_PER_MP_LIMB, 0);
1004 #else
1005 for (i = RETURN_LIMB_SIZE; i > empty; --i)
1006 retval[i] = retval[i - empty];
1007 #endif
1008 retval[1] = 0;
1009 for (i = numsize; i > 0; --i)
1010 num[i + empty] = num[i - 1];
1011 MPN_ZERO (num, empty + 1);
1013 else
1015 used = MANT_DIG - bits;
1016 if (used >= BITS_PER_MP_LIMB)
1018 register int i;
1019 (void) __mpn_lshift (&retval[used
1020 / BITS_PER_MP_LIMB],
1021 retval, RETURN_LIMB_SIZE,
1022 used % BITS_PER_MP_LIMB);
1023 for (i = used / BITS_PER_MP_LIMB; i >= 0; --i)
1024 retval[i] = 0;
1026 else if (used > 0)
1027 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1029 bits += empty * BITS_PER_MP_LIMB;
1032 else
1034 int i;
1035 assert (numsize == densize);
1036 for (i = numsize; i > 0; --i)
1037 num[i] = num[i - 1];
1040 den[densize] = 0;
1041 n0 = num[densize];
1043 while (bits <= MANT_DIG)
1045 if (n0 == dX)
1046 /* This might over-estimate QUOT, but it's probably not
1047 worth the extra code here to find out. */
1048 quot = ~(mp_limb) 0;
1049 else
1051 mp_limb r;
1053 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1054 umul_ppmm (n1, n0, d1, quot);
1056 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1058 --quot;
1059 r += dX;
1060 if (r < dX) /* I.e. "carry in previous addition?" */
1061 break;
1062 n1 -= n0 < d1;
1063 n0 -= d1;
1067 /* Possible optimization: We already have (q * n0) and (1 * n1)
1068 after the calculation of QUOT. Taking advantage of this, we
1069 could make this loop make two iterations less. */
1071 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1073 if (num[densize] != cy)
1075 cy = __mpn_add_n (num, num, den, densize);
1076 assert (cy != 0);
1077 --quot;
1079 n0 = num[densize] = num[densize - 1];
1080 for (i = densize - 1; i > 0; --i)
1081 num[i] = num[i - 1];
1083 got_limb;
1086 for (i = densize; num[i] == 0 && i >= 0; --i)
1088 return round_and_return (retval, exponent - 1, negative,
1089 quot, BITS_PER_MP_LIMB - 1 - used,
1090 more_bits || i >= 0);
1095 /* NOTREACHED */
1098 /* External user entry point. */
1100 #define weak_this(x) weak_symbol(x)
1101 weak_this (STRTOF)
1103 FLOAT
1104 STRTOF (nptr, endptr)
1105 const char *nptr;
1106 char **endptr;
1108 return INTERNAL (STRTOF) (nptr, endptr, 0);