]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/strtod_l.c
Include errno.h. Change main() to do_test(). Define TEST_FUNCTION. Include test...
[thirdparty/glibc.git] / stdlib / strtod_l.c
1 /* Convert string representing a number to float value, using given locale.
2 Copyright (C) 1997,1998,2002,2004,2005,2006,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
21
22 #include <xlocale.h>
23
24 extern double ____strtod_l_internal (const char *, char **, int, __locale_t);
25 extern unsigned long long int ____strtoull_l_internal (const char *, char **,
26 int, int, __locale_t);
27
28 /* Configuration part. These macros are defined by `strtold.c',
29 `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
30 `long double' and `float' versions of the reader. */
31 #ifndef FLOAT
32 # include <math_ldbl_opt.h>
33 # define FLOAT double
34 # define FLT DBL
35 # ifdef USE_WIDE_CHAR
36 # define STRTOF wcstod_l
37 # define __STRTOF __wcstod_l
38 # else
39 # define STRTOF strtod_l
40 # define __STRTOF __strtod_l
41 # endif
42 # define MPN2FLOAT __mpn_construct_double
43 # define FLOAT_HUGE_VAL HUGE_VAL
44 # define SET_MANTISSA(flt, mant) \
45 do { union ieee754_double u; \
46 u.d = (flt); \
47 if ((mant & 0xfffffffffffffULL) == 0) \
48 mant = 0x8000000000000ULL; \
49 u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff; \
50 u.ieee.mantissa1 = (mant) & 0xffffffff; \
51 (flt) = u.d; \
52 } while (0)
53 #endif
54 /* End of configuration part. */
55 \f
56 #include <ctype.h>
57 #include <errno.h>
58 #include <float.h>
59 #include <ieee754.h>
60 #include "../locale/localeinfo.h"
61 #include <locale.h>
62 #include <math.h>
63 #include <stdlib.h>
64 #include <string.h>
65
66 /* The gmp headers need some configuration frobs. */
67 #define HAVE_ALLOCA 1
68
69 /* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
70 and _LONG_LONG_LIMB in it can take effect into gmp.h. */
71 #include <gmp-mparam.h>
72 #include <gmp.h>
73 #include "gmp-impl.h"
74 #include "longlong.h"
75 #include "fpioconst.h"
76
77 #define NDEBUG 1
78 #include <assert.h>
79
80
81 /* We use this code for the extended locale handling where the
82 function gets as an additional argument the locale which has to be
83 used. To access the values we have to redefine the _NL_CURRENT and
84 _NL_CURRENT_WORD macros. */
85 #undef _NL_CURRENT
86 #define _NL_CURRENT(category, item) \
87 (current->values[_NL_ITEM_INDEX (item)].string)
88 #undef _NL_CURRENT_WORD
89 #define _NL_CURRENT_WORD(category, item) \
90 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
91
92 #if defined _LIBC || defined HAVE_WCHAR_H
93 # include <wchar.h>
94 #endif
95
96 #ifdef USE_WIDE_CHAR
97 # include <wctype.h>
98 # define STRING_TYPE wchar_t
99 # define CHAR_TYPE wint_t
100 # define L_(Ch) L##Ch
101 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
102 # define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
103 # define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
104 # define TOLOWER(Ch) __towlower_l ((Ch), loc)
105 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
106 # define STRNCASECMP(S1, S2, N) \
107 __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
108 # define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
109 #else
110 # define STRING_TYPE char
111 # define CHAR_TYPE char
112 # define L_(Ch) Ch
113 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
114 # define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
115 # define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
116 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
117 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
118 # define STRNCASECMP(S1, S2, N) \
119 __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
120 # define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
121 #endif
122
123
124 /* Constants we need from float.h; select the set for the FLOAT precision. */
125 #define MANT_DIG PASTE(FLT,_MANT_DIG)
126 #define DIG PASTE(FLT,_DIG)
127 #define MAX_EXP PASTE(FLT,_MAX_EXP)
128 #define MIN_EXP PASTE(FLT,_MIN_EXP)
129 #define MAX_10_EXP PASTE(FLT,_MAX_10_EXP)
130 #define MIN_10_EXP PASTE(FLT,_MIN_10_EXP)
131
132 /* Extra macros required to get FLT expanded before the pasting. */
133 #define PASTE(a,b) PASTE1(a,b)
134 #define PASTE1(a,b) a##b
135
136 /* Function to construct a floating point number from an MP integer
137 containing the fraction bits, a base 2 exponent, and a sign flag. */
138 extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);
139 \f
140 /* Definitions according to limb size used. */
141 #if BITS_PER_MP_LIMB == 32
142 # define MAX_DIG_PER_LIMB 9
143 # define MAX_FAC_PER_LIMB 1000000000UL
144 #elif BITS_PER_MP_LIMB == 64
145 # define MAX_DIG_PER_LIMB 19
146 # define MAX_FAC_PER_LIMB 10000000000000000000ULL
147 #else
148 # error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
149 #endif
150
151
152 /* Local data structure. */
153 static const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1] =
154 { 0, 10, 100,
155 1000, 10000, 100000L,
156 1000000L, 10000000L, 100000000L,
157 1000000000L
158 #if BITS_PER_MP_LIMB > 32
159 , 10000000000ULL, 100000000000ULL,
160 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
161 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL,
162 1000000000000000000ULL, 10000000000000000000ULL
163 #endif
164 #if BITS_PER_MP_LIMB > 64
165 #error "Need to expand tens_in_limb table to" MAX_DIG_PER_LIMB
166 #endif
167 };
168 \f
169 #ifndef howmany
170 #define howmany(x,y) (((x)+((y)-1))/(y))
171 #endif
172 #define SWAP(x, y) ({ typeof(x) _tmp = x; x = y; y = _tmp; })
173
174 #define NDIG (MAX_10_EXP - MIN_10_EXP + 2 * MANT_DIG)
175 #define HEXNDIG ((MAX_EXP - MIN_EXP + 7) / 8 + 2 * MANT_DIG)
176 #define RETURN_LIMB_SIZE howmany (MANT_DIG, BITS_PER_MP_LIMB)
177
178 #define RETURN(val,end) \
179 do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end); \
180 return val; } while (0)
181
182 /* Maximum size necessary for mpn integers to hold floating point numbers. */
183 #define MPNSIZE (howmany (MAX_EXP + 2 * MANT_DIG, BITS_PER_MP_LIMB) \
184 + 2)
185 /* Declare an mpn integer variable that big. */
186 #define MPN_VAR(name) mp_limb_t name[MPNSIZE]; mp_size_t name##size
187 /* Copy an mpn integer value. */
188 #define MPN_ASSIGN(dst, src) \
189 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
190
191
192 /* Return a floating point number of the needed type according to the given
193 multi-precision number after possible rounding. */
194 static FLOAT
195 round_and_return (mp_limb_t *retval, int exponent, int negative,
196 mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
197 {
198 if (exponent < MIN_EXP - 1)
199 {
200 mp_size_t shift = MIN_EXP - 1 - exponent;
201
202 if (shift > MANT_DIG)
203 {
204 __set_errno (EDOM);
205 return 0.0;
206 }
207
208 more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
209 if (shift == MANT_DIG)
210 /* This is a special case to handle the very seldom case where
211 the mantissa will be empty after the shift. */
212 {
213 int i;
214
215 round_limb = retval[RETURN_LIMB_SIZE - 1];
216 round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
217 for (i = 0; i < RETURN_LIMB_SIZE; ++i)
218 more_bits |= retval[i] != 0;
219 MPN_ZERO (retval, RETURN_LIMB_SIZE);
220 }
221 else if (shift >= BITS_PER_MP_LIMB)
222 {
223 int i;
224
225 round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
226 round_bit = (shift - 1) % BITS_PER_MP_LIMB;
227 for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
228 more_bits |= retval[i] != 0;
229 more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
230 != 0);
231
232 (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
233 RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
234 shift % BITS_PER_MP_LIMB);
235 MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
236 shift / BITS_PER_MP_LIMB);
237 }
238 else if (shift > 0)
239 {
240 round_limb = retval[0];
241 round_bit = shift - 1;
242 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
243 }
244 /* This is a hook for the m68k long double format, where the
245 exponent bias is the same for normalized and denormalized
246 numbers. */
247 #ifndef DENORM_EXP
248 # define DENORM_EXP (MIN_EXP - 2)
249 #endif
250 exponent = DENORM_EXP;
251 }
252
253 if ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
254 && (more_bits || (retval[0] & 1) != 0
255 || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
256 {
257 mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);
258
259 if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy) ||
260 ((MANT_DIG % BITS_PER_MP_LIMB) != 0 &&
261 (retval[RETURN_LIMB_SIZE - 1]
262 & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
263 {
264 ++exponent;
265 (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
266 retval[RETURN_LIMB_SIZE - 1]
267 |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
268 }
269 else if (exponent == DENORM_EXP
270 && (retval[RETURN_LIMB_SIZE - 1]
271 & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
272 != 0)
273 /* The number was denormalized but now normalized. */
274 exponent = MIN_EXP - 1;
275 }
276
277 if (exponent > MAX_EXP)
278 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
279
280 return MPN2FLOAT (retval, exponent, negative);
281 }
282
283
284 /* Read a multi-precision integer starting at STR with exactly DIGCNT digits
285 into N. Return the size of the number limbs in NSIZE at the first
286 character od the string that is not part of the integer as the function
287 value. If the EXPONENT is small enough to be taken as an additional
288 factor for the resulting number (see code) multiply by it. */
289 static const STRING_TYPE *
290 str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
291 int *exponent
292 #ifndef USE_WIDE_CHAR
293 , const char *decimal, size_t decimal_len, const char *thousands
294 #endif
295
296 )
297 {
298 /* Number of digits for actual limb. */
299 int cnt = 0;
300 mp_limb_t low = 0;
301 mp_limb_t start;
302
303 *nsize = 0;
304 assert (digcnt > 0);
305 do
306 {
307 if (cnt == MAX_DIG_PER_LIMB)
308 {
309 if (*nsize == 0)
310 {
311 n[0] = low;
312 *nsize = 1;
313 }
314 else
315 {
316 mp_limb_t cy;
317 cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
318 cy += __mpn_add_1 (n, n, *nsize, low);
319 if (cy != 0)
320 {
321 n[*nsize] = cy;
322 ++(*nsize);
323 }
324 }
325 cnt = 0;
326 low = 0;
327 }
328
329 /* There might be thousands separators or radix characters in
330 the string. But these all can be ignored because we know the
331 format of the number is correct and we have an exact number
332 of characters to read. */
333 #ifdef USE_WIDE_CHAR
334 if (*str < L'0' || *str > L'9')
335 ++str;
336 #else
337 if (*str < '0' || *str > '9')
338 {
339 int inner = 0;
340 if (thousands != NULL && *str == *thousands
341 && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
342 if (thousands[inner] != str[inner])
343 break;
344 thousands[inner] == '\0'; }))
345 str += inner;
346 else
347 str += decimal_len;
348 }
349 #endif
350 low = low * 10 + *str++ - L_('0');
351 ++cnt;
352 }
353 while (--digcnt > 0);
354
355 if (*exponent > 0 && cnt + *exponent <= MAX_DIG_PER_LIMB)
356 {
357 low *= _tens_in_limb[*exponent];
358 start = _tens_in_limb[cnt + *exponent];
359 *exponent = 0;
360 }
361 else
362 start = _tens_in_limb[cnt];
363
364 if (*nsize == 0)
365 {
366 n[0] = low;
367 *nsize = 1;
368 }
369 else
370 {
371 mp_limb_t cy;
372 cy = __mpn_mul_1 (n, n, *nsize, start);
373 cy += __mpn_add_1 (n, n, *nsize, low);
374 if (cy != 0)
375 n[(*nsize)++] = cy;
376 }
377
378 return str;
379 }
380
381
382 /* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
383 with the COUNT most significant bits of LIMB.
384
385 Tege doesn't like this function so I have to write it here myself. :)
386 --drepper */
387 static inline void
388 __attribute ((always_inline))
389 __mpn_lshift_1 (mp_limb_t *ptr, mp_size_t size, unsigned int count,
390 mp_limb_t limb)
391 {
392 if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)
393 {
394 /* Optimize the case of shifting by exactly a word:
395 just copy words, with no actual bit-shifting. */
396 mp_size_t i;
397 for (i = size - 1; i > 0; --i)
398 ptr[i] = ptr[i - 1];
399 ptr[0] = limb;
400 }
401 else
402 {
403 (void) __mpn_lshift (ptr, ptr, size, count);
404 ptr[0] |= limb >> (BITS_PER_MP_LIMB - count);
405 }
406 }
407
408
409 #define INTERNAL(x) INTERNAL1(x)
410 #define INTERNAL1(x) __##x##_internal
411 #ifndef ____STRTOF_INTERNAL
412 # define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
413 #endif
414
415 /* This file defines a function to check for correct grouping. */
416 #include "grouping.h"
417
418
419 /* Return a floating point number with the value of the given string NPTR.
420 Set *ENDPTR to the character after the last used one. If the number is
421 smaller than the smallest representable number, set `errno' to ERANGE and
422 return 0.0. If the number is too big to be represented, set `errno' to
423 ERANGE and return HUGE_VAL with the appropriate sign. */
424 FLOAT
425 ____STRTOF_INTERNAL (nptr, endptr, group, loc)
426 const STRING_TYPE *nptr;
427 STRING_TYPE **endptr;
428 int group;
429 __locale_t loc;
430 {
431 int negative; /* The sign of the number. */
432 MPN_VAR (num); /* MP representation of the number. */
433 int exponent; /* Exponent of the number. */
434
435 /* Numbers starting `0X' or `0x' have to be processed with base 16. */
436 int base = 10;
437
438 /* When we have to compute fractional digits we form a fraction with a
439 second multi-precision number (and we sometimes need a second for
440 temporary results). */
441 MPN_VAR (den);
442
443 /* Representation for the return value. */
444 mp_limb_t retval[RETURN_LIMB_SIZE];
445 /* Number of bits currently in result value. */
446 int bits;
447
448 /* Running pointer after the last character processed in the string. */
449 const STRING_TYPE *cp, *tp;
450 /* Start of significant part of the number. */
451 const STRING_TYPE *startp, *start_of_digits;
452 /* Points at the character following the integer and fractional digits. */
453 const STRING_TYPE *expp;
454 /* Total number of digit and number of digits in integer part. */
455 int dig_no, int_no, lead_zero;
456 /* Contains the last character read. */
457 CHAR_TYPE c;
458
459 /* We should get wint_t from <stddef.h>, but not all GCC versions define it
460 there. So define it ourselves if it remains undefined. */
461 #ifndef _WINT_T
462 typedef unsigned int wint_t;
463 #endif
464 /* The radix character of the current locale. */
465 #ifdef USE_WIDE_CHAR
466 wchar_t decimal;
467 #else
468 const char *decimal;
469 size_t decimal_len;
470 #endif
471 /* The thousands character of the current locale. */
472 #ifdef USE_WIDE_CHAR
473 wchar_t thousands = L'\0';
474 #else
475 const char *thousands = NULL;
476 #endif
477 /* The numeric grouping specification of the current locale,
478 in the format described in <locale.h>. */
479 const char *grouping;
480 /* Used in several places. */
481 int cnt;
482
483 struct locale_data *current = loc->__locales[LC_NUMERIC];
484
485 if (group)
486 {
487 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
488 if (*grouping <= 0 || *grouping == CHAR_MAX)
489 grouping = NULL;
490 else
491 {
492 /* Figure out the thousands separator character. */
493 #ifdef USE_WIDE_CHAR
494 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
495 _NL_NUMERIC_THOUSANDS_SEP_WC);
496 if (thousands == L'\0')
497 grouping = NULL;
498 #else
499 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
500 if (*thousands == '\0')
501 {
502 thousands = NULL;
503 grouping = NULL;
504 }
505 #endif
506 }
507 }
508 else
509 grouping = NULL;
510
511 /* Find the locale's decimal point character. */
512 #ifdef USE_WIDE_CHAR
513 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
514 assert (decimal != L'\0');
515 # define decimal_len 1
516 #else
517 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
518 decimal_len = strlen (decimal);
519 assert (decimal_len > 0);
520 #endif
521
522 /* Prepare number representation. */
523 exponent = 0;
524 negative = 0;
525 bits = 0;
526
527 /* Parse string to get maximal legal prefix. We need the number of
528 characters of the integer part, the fractional part and the exponent. */
529 cp = nptr - 1;
530 /* Ignore leading white space. */
531 do
532 c = *++cp;
533 while (ISSPACE (c));
534
535 /* Get sign of the result. */
536 if (c == L_('-'))
537 {
538 negative = 1;
539 c = *++cp;
540 }
541 else if (c == L_('+'))
542 c = *++cp;
543
544 /* Return 0.0 if no legal string is found.
545 No character is used even if a sign was found. */
546 #ifdef USE_WIDE_CHAR
547 if (c == (wint_t) decimal
548 && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
549 {
550 /* We accept it. This funny construct is here only to indent
551 the code directly. */
552 }
553 #else
554 for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
555 if (cp[cnt] != decimal[cnt])
556 break;
557 if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
558 {
559 /* We accept it. This funny construct is here only to indent
560 the code directly. */
561 }
562 #endif
563 else if (c < L_('0') || c > L_('9'))
564 {
565 /* Check for `INF' or `INFINITY'. */
566 if (TOLOWER_C (c) == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
567 {
568 /* Return +/- infinity. */
569 if (endptr != NULL)
570 *endptr = (STRING_TYPE *)
571 (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
572 ? 8 : 3));
573
574 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
575 }
576
577 if (TOLOWER_C (c) == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
578 {
579 /* Return NaN. */
580 FLOAT retval = NAN;
581
582 cp += 3;
583
584 /* Match `(n-char-sequence-digit)'. */
585 if (*cp == L_('('))
586 {
587 const STRING_TYPE *startp = cp;
588 do
589 ++cp;
590 while ((*cp >= L_('0') && *cp <= L_('9'))
591 || (TOLOWER (*cp) >= L_('a') && TOLOWER (*cp) <= L_('z'))
592 || *cp == L_('_'));
593
594 if (*cp != L_(')'))
595 /* The closing brace is missing. Only match the NAN
596 part. */
597 cp = startp;
598 else
599 {
600 /* This is a system-dependent way to specify the
601 bitmask used for the NaN. We expect it to be
602 a number which is put in the mantissa of the
603 number. */
604 STRING_TYPE *endp;
605 unsigned long long int mant;
606
607 mant = STRTOULL (startp + 1, &endp, 0);
608 if (endp == cp)
609 SET_MANTISSA (retval, mant);
610 }
611 }
612
613 if (endptr != NULL)
614 *endptr = (STRING_TYPE *) cp;
615
616 return retval;
617 }
618
619 /* It is really a text we do not recognize. */
620 RETURN (0.0, nptr);
621 }
622
623 /* First look whether we are faced with a hexadecimal number. */
624 if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
625 {
626 /* Okay, it is a hexa-decimal number. Remember this and skip
627 the characters. BTW: hexadecimal numbers must not be
628 grouped. */
629 base = 16;
630 cp += 2;
631 c = *cp;
632 grouping = NULL;
633 }
634
635 /* Record the start of the digits, in case we will check their grouping. */
636 start_of_digits = startp = cp;
637
638 /* Ignore leading zeroes. This helps us to avoid useless computations. */
639 #ifdef USE_WIDE_CHAR
640 while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
641 c = *++cp;
642 #else
643 if (thousands == NULL)
644 while (c == '0')
645 c = *++cp;
646 else
647 {
648 /* We also have the multibyte thousands string. */
649 while (1)
650 {
651 if (c != '0')
652 {
653 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
654 if (thousands[cnt] != cp[cnt])
655 break;
656 if (thousands[cnt] != '\0')
657 break;
658 cp += cnt - 1;
659 }
660 c = *++cp;
661 }
662 }
663 #endif
664
665 /* If no other digit but a '0' is found the result is 0.0.
666 Return current read pointer. */
667 if (!((c >= L_('0') && c <= L_('9'))
668 || (base == 16 && ((CHAR_TYPE) TOLOWER (c) >= L_('a')
669 && (CHAR_TYPE) TOLOWER (c) <= L_('f')))
670 || (
671 #ifdef USE_WIDE_CHAR
672 c == (wint_t) decimal
673 #else
674 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
675 if (decimal[cnt] != cp[cnt])
676 break;
677 decimal[cnt] == '\0'; })
678 #endif
679 /* '0x.' alone is not a valid hexadecimal number.
680 '.' alone is not valid either, but that has been checked
681 already earlier. */
682 && (base != 16
683 || cp != start_of_digits
684 || (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
685 || ((CHAR_TYPE) TOLOWER (cp[decimal_len]) >= L_('a')
686 && (CHAR_TYPE) TOLOWER (cp[decimal_len]) <= L_('f'))))
687 || (base == 16 && (cp != start_of_digits
688 && (CHAR_TYPE) TOLOWER (c) == L_('p')))
689 || (base != 16 && (CHAR_TYPE) TOLOWER (c) == L_('e'))))
690 {
691 #ifdef USE_WIDE_CHAR
692 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
693 grouping);
694 #else
695 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
696 grouping);
697 #endif
698 /* If TP is at the start of the digits, there was no correctly
699 grouped prefix of the string; so no number found. */
700 RETURN (0.0, tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
701 }
702
703 /* Remember first significant digit and read following characters until the
704 decimal point, exponent character or any non-FP number character. */
705 startp = cp;
706 dig_no = 0;
707 while (1)
708 {
709 if ((c >= L_('0') && c <= L_('9'))
710 || (base == 16 && (wint_t) TOLOWER (c) >= L_('a')
711 && (wint_t) TOLOWER (c) <= L_('f')))
712 ++dig_no;
713 else
714 {
715 #ifdef USE_WIDE_CHAR
716 if ((wint_t) thousands == L'\0' || c != (wint_t) thousands)
717 /* Not a digit or separator: end of the integer part. */
718 break;
719 #else
720 if (thousands == NULL)
721 break;
722 else
723 {
724 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
725 if (thousands[cnt] != cp[cnt])
726 break;
727 if (thousands[cnt] != '\0')
728 break;
729 cp += cnt - 1;
730 }
731 #endif
732 }
733 c = *++cp;
734 }
735
736 if (grouping && cp > start_of_digits)
737 {
738 /* Check the grouping of the digits. */
739 #ifdef USE_WIDE_CHAR
740 tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
741 grouping);
742 #else
743 tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
744 grouping);
745 #endif
746 if (cp != tp)
747 {
748 /* Less than the entire string was correctly grouped. */
749
750 if (tp == start_of_digits)
751 /* No valid group of numbers at all: no valid number. */
752 RETURN (0.0, nptr);
753
754 if (tp < startp)
755 /* The number is validly grouped, but consists
756 only of zeroes. The whole value is zero. */
757 RETURN (0.0, tp);
758
759 /* Recompute DIG_NO so we won't read more digits than
760 are properly grouped. */
761 cp = tp;
762 dig_no = 0;
763 for (tp = startp; tp < cp; ++tp)
764 if (*tp >= L_('0') && *tp <= L_('9'))
765 ++dig_no;
766
767 int_no = dig_no;
768 lead_zero = 0;
769
770 goto number_parsed;
771 }
772 }
773
774 /* We have the number of digits in the integer part. Whether these
775 are all or any is really a fractional digit will be decided
776 later. */
777 int_no = dig_no;
778 lead_zero = int_no == 0 ? -1 : 0;
779
780 /* Read the fractional digits. A special case are the 'american
781 style' numbers like `16.' i.e. with decimal point but without
782 trailing digits. */
783 if (
784 #ifdef USE_WIDE_CHAR
785 c == (wint_t) decimal
786 #else
787 ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
788 if (decimal[cnt] != cp[cnt])
789 break;
790 decimal[cnt] == '\0'; })
791 #endif
792 )
793 {
794 cp += decimal_len;
795 c = *cp;
796 while ((c >= L_('0') && c <= L_('9')) ||
797 (base == 16 && TOLOWER (c) >= L_('a') && TOLOWER (c) <= L_('f')))
798 {
799 if (c != L_('0') && lead_zero == -1)
800 lead_zero = dig_no - int_no;
801 ++dig_no;
802 c = *++cp;
803 }
804 }
805
806 /* Remember start of exponent (if any). */
807 expp = cp;
808
809 /* Read exponent. */
810 if ((base == 16 && TOLOWER (c) == L_('p'))
811 || (base != 16 && TOLOWER (c) == L_('e')))
812 {
813 int exp_negative = 0;
814
815 c = *++cp;
816 if (c == L_('-'))
817 {
818 exp_negative = 1;
819 c = *++cp;
820 }
821 else if (c == L_('+'))
822 c = *++cp;
823
824 if (c >= L_('0') && c <= L_('9'))
825 {
826 int exp_limit;
827
828 /* Get the exponent limit. */
829 if (base == 16)
830 exp_limit = (exp_negative ?
831 -MIN_EXP + MANT_DIG + 4 * int_no :
832 MAX_EXP - 4 * int_no + 4 * lead_zero + 3);
833 else
834 exp_limit = (exp_negative ?
835 -MIN_10_EXP + MANT_DIG + int_no :
836 MAX_10_EXP - int_no + lead_zero + 1);
837
838 do
839 {
840 exponent *= 10;
841 exponent += c - L_('0');
842
843 if (exponent > exp_limit)
844 /* The exponent is too large/small to represent a valid
845 number. */
846 {
847 FLOAT result;
848
849 /* We have to take care for special situation: a joker
850 might have written "0.0e100000" which is in fact
851 zero. */
852 if (lead_zero == -1)
853 result = negative ? -0.0 : 0.0;
854 else
855 {
856 /* Overflow or underflow. */
857 __set_errno (ERANGE);
858 result = (exp_negative ? 0.0 :
859 negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL);
860 }
861
862 /* Accept all following digits as part of the exponent. */
863 do
864 ++cp;
865 while (*cp >= L_('0') && *cp <= L_('9'));
866
867 RETURN (result, cp);
868 /* NOTREACHED */
869 }
870
871 c = *++cp;
872 }
873 while (c >= L_('0') && c <= L_('9'));
874
875 if (exp_negative)
876 exponent = -exponent;
877 }
878 else
879 cp = expp;
880 }
881
882 /* We don't want to have to work with trailing zeroes after the radix. */
883 if (dig_no > int_no)
884 {
885 while (expp[-1] == L_('0'))
886 {
887 --expp;
888 --dig_no;
889 }
890 assert (dig_no >= int_no);
891 }
892
893 if (dig_no == int_no && dig_no > 0 && exponent < 0)
894 do
895 {
896 while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
897 --expp;
898
899 if (expp[-1] != L_('0'))
900 break;
901
902 --expp;
903 --dig_no;
904 --int_no;
905 exponent += base == 16 ? 4 : 1;
906 }
907 while (dig_no > 0 && exponent < 0);
908
909 number_parsed:
910
911 /* The whole string is parsed. Store the address of the next character. */
912 if (endptr)
913 *endptr = (STRING_TYPE *) cp;
914
915 if (dig_no == 0)
916 return negative ? -0.0 : 0.0;
917
918 if (lead_zero)
919 {
920 /* Find the decimal point */
921 #ifdef USE_WIDE_CHAR
922 while (*startp != decimal)
923 ++startp;
924 #else
925 while (1)
926 {
927 if (*startp == decimal[0])
928 {
929 for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
930 if (decimal[cnt] != startp[cnt])
931 break;
932 if (decimal[cnt] == '\0')
933 break;
934 }
935 ++startp;
936 }
937 #endif
938 startp += lead_zero + decimal_len;
939 exponent -= base == 16 ? 4 * lead_zero : lead_zero;
940 dig_no -= lead_zero;
941 }
942
943 /* If the BASE is 16 we can use a simpler algorithm. */
944 if (base == 16)
945 {
946 static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
947 4, 4, 4, 4, 4, 4, 4, 4 };
948 int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
949 int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
950 mp_limb_t val;
951
952 while (!ISXDIGIT (*startp))
953 ++startp;
954 while (*startp == L_('0'))
955 ++startp;
956 if (ISDIGIT (*startp))
957 val = *startp++ - L_('0');
958 else
959 val = 10 + TOLOWER (*startp++) - L_('a');
960 bits = nbits[val];
961 /* We cannot have a leading zero. */
962 assert (bits != 0);
963
964 if (pos + 1 >= 4 || pos + 1 >= bits)
965 {
966 /* We don't have to care for wrapping. This is the normal
967 case so we add the first clause in the `if' expression as
968 an optimization. It is a compile-time constant and so does
969 not cost anything. */
970 retval[idx] = val << (pos - bits + 1);
971 pos -= bits;
972 }
973 else
974 {
975 retval[idx--] = val >> (bits - pos - 1);
976 retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
977 pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
978 }
979
980 /* Adjust the exponent for the bits we are shifting in. */
981 exponent += bits - 1 + (int_no - 1) * 4;
982
983 while (--dig_no > 0 && idx >= 0)
984 {
985 if (!ISXDIGIT (*startp))
986 startp += decimal_len;
987 if (ISDIGIT (*startp))
988 val = *startp++ - L_('0');
989 else
990 val = 10 + TOLOWER (*startp++) - L_('a');
991
992 if (pos + 1 >= 4)
993 {
994 retval[idx] |= val << (pos - 4 + 1);
995 pos -= 4;
996 }
997 else
998 {
999 retval[idx--] |= val >> (4 - pos - 1);
1000 val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
1001 if (idx < 0)
1002 return round_and_return (retval, exponent, negative, val,
1003 BITS_PER_MP_LIMB - 1, dig_no > 0);
1004
1005 retval[idx] = val;
1006 pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
1007 }
1008 }
1009
1010 /* We ran out of digits. */
1011 MPN_ZERO (retval, idx);
1012
1013 return round_and_return (retval, exponent, negative, 0, 0, 0);
1014 }
1015
1016 /* Now we have the number of digits in total and the integer digits as well
1017 as the exponent and its sign. We can decide whether the read digits are
1018 really integer digits or belong to the fractional part; i.e. we normalize
1019 123e-2 to 1.23. */
1020 {
1021 register int incr = (exponent < 0 ? MAX (-int_no, exponent)
1022 : MIN (dig_no - int_no, exponent));
1023 int_no += incr;
1024 exponent -= incr;
1025 }
1026
1027 if (int_no + exponent > MAX_10_EXP + 1)
1028 {
1029 __set_errno (ERANGE);
1030 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1031 }
1032
1033 if (exponent < MIN_10_EXP - (DIG + 1))
1034 {
1035 __set_errno (ERANGE);
1036 return 0.0;
1037 }
1038
1039 if (int_no > 0)
1040 {
1041 /* Read the integer part as a multi-precision number to NUM. */
1042 startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
1043 #ifndef USE_WIDE_CHAR
1044 , decimal, decimal_len, thousands
1045 #endif
1046 );
1047
1048 if (exponent > 0)
1049 {
1050 /* We now multiply the gained number by the given power of ten. */
1051 mp_limb_t *psrc = num;
1052 mp_limb_t *pdest = den;
1053 int expbit = 1;
1054 const struct mp_power *ttab = &_fpioconst_pow10[0];
1055
1056 do
1057 {
1058 if ((exponent & expbit) != 0)
1059 {
1060 size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
1061 mp_limb_t cy;
1062 exponent ^= expbit;
1063
1064 /* FIXME: not the whole multiplication has to be
1065 done. If we have the needed number of bits we
1066 only need the information whether more non-zero
1067 bits follow. */
1068 if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
1069 cy = __mpn_mul (pdest, psrc, numsize,
1070 &__tens[ttab->arrayoff
1071 + _FPIO_CONST_OFFSET],
1072 size);
1073 else
1074 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1075 + _FPIO_CONST_OFFSET],
1076 size, psrc, numsize);
1077 numsize += size;
1078 if (cy == 0)
1079 --numsize;
1080 (void) SWAP (psrc, pdest);
1081 }
1082 expbit <<= 1;
1083 ++ttab;
1084 }
1085 while (exponent != 0);
1086
1087 if (psrc == den)
1088 memcpy (num, den, numsize * sizeof (mp_limb_t));
1089 }
1090
1091 /* Determine how many bits of the result we already have. */
1092 count_leading_zeros (bits, num[numsize - 1]);
1093 bits = numsize * BITS_PER_MP_LIMB - bits;
1094
1095 /* Now we know the exponent of the number in base two.
1096 Check it against the maximum possible exponent. */
1097 if (bits > MAX_EXP)
1098 {
1099 __set_errno (ERANGE);
1100 return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
1101 }
1102
1103 /* We have already the first BITS bits of the result. Together with
1104 the information whether more non-zero bits follow this is enough
1105 to determine the result. */
1106 if (bits > MANT_DIG)
1107 {
1108 int i;
1109 const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
1110 const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
1111 const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
1112 : least_idx;
1113 const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
1114 : least_bit - 1;
1115
1116 if (least_bit == 0)
1117 memcpy (retval, &num[least_idx],
1118 RETURN_LIMB_SIZE * sizeof (mp_limb_t));
1119 else
1120 {
1121 for (i = least_idx; i < numsize - 1; ++i)
1122 retval[i - least_idx] = (num[i] >> least_bit)
1123 | (num[i + 1]
1124 << (BITS_PER_MP_LIMB - least_bit));
1125 if (i - least_idx < RETURN_LIMB_SIZE)
1126 retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
1127 }
1128
1129 /* Check whether any limb beside the ones in RETVAL are non-zero. */
1130 for (i = 0; num[i] == 0; ++i)
1131 ;
1132
1133 return round_and_return (retval, bits - 1, negative,
1134 num[round_idx], round_bit,
1135 int_no < dig_no || i < round_idx);
1136 /* NOTREACHED */
1137 }
1138 else if (dig_no == int_no)
1139 {
1140 const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
1141 const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;
1142
1143 if (target_bit == is_bit)
1144 {
1145 memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
1146 numsize * sizeof (mp_limb_t));
1147 /* FIXME: the following loop can be avoided if we assume a
1148 maximal MANT_DIG value. */
1149 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1150 }
1151 else if (target_bit > is_bit)
1152 {
1153 (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
1154 num, numsize, target_bit - is_bit);
1155 /* FIXME: the following loop can be avoided if we assume a
1156 maximal MANT_DIG value. */
1157 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
1158 }
1159 else
1160 {
1161 mp_limb_t cy;
1162 assert (numsize < RETURN_LIMB_SIZE);
1163
1164 cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
1165 num, numsize, is_bit - target_bit);
1166 retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
1167 /* FIXME: the following loop can be avoided if we assume a
1168 maximal MANT_DIG value. */
1169 MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
1170 }
1171
1172 return round_and_return (retval, bits - 1, negative, 0, 0, 0);
1173 /* NOTREACHED */
1174 }
1175
1176 /* Store the bits we already have. */
1177 memcpy (retval, num, numsize * sizeof (mp_limb_t));
1178 #if RETURN_LIMB_SIZE > 1
1179 if (numsize < RETURN_LIMB_SIZE)
1180 # if RETURN_LIMB_SIZE == 2
1181 retval[numsize] = 0;
1182 # else
1183 MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
1184 # endif
1185 #endif
1186 }
1187
1188 /* We have to compute at least some of the fractional digits. */
1189 {
1190 /* We construct a fraction and the result of the division gives us
1191 the needed digits. The denominator is 1.0 multiplied by the
1192 exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
1193 123e-6 gives 123 / 1000000. */
1194
1195 int expbit;
1196 int neg_exp;
1197 int more_bits;
1198 mp_limb_t cy;
1199 mp_limb_t *psrc = den;
1200 mp_limb_t *pdest = num;
1201 const struct mp_power *ttab = &_fpioconst_pow10[0];
1202
1203 assert (dig_no > int_no && exponent <= 0);
1204
1205
1206 /* For the fractional part we need not process too many digits. One
1207 decimal digits gives us log_2(10) ~ 3.32 bits. If we now compute
1208 ceil(BITS / 3) =: N
1209 digits we should have enough bits for the result. The remaining
1210 decimal digits give us the information that more bits are following.
1211 This can be used while rounding. (Two added as a safety margin.) */
1212 if (dig_no - int_no > (MANT_DIG - bits + 2) / 3 + 2)
1213 {
1214 dig_no = int_no + (MANT_DIG - bits + 2) / 3 + 2;
1215 more_bits = 1;
1216 }
1217 else
1218 more_bits = 0;
1219
1220 neg_exp = dig_no - int_no - exponent;
1221
1222 /* Construct the denominator. */
1223 densize = 0;
1224 expbit = 1;
1225 do
1226 {
1227 if ((neg_exp & expbit) != 0)
1228 {
1229 mp_limb_t cy;
1230 neg_exp ^= expbit;
1231
1232 if (densize == 0)
1233 {
1234 densize = ttab->arraysize - _FPIO_CONST_OFFSET;
1235 memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
1236 densize * sizeof (mp_limb_t));
1237 }
1238 else
1239 {
1240 cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
1241 + _FPIO_CONST_OFFSET],
1242 ttab->arraysize - _FPIO_CONST_OFFSET,
1243 psrc, densize);
1244 densize += ttab->arraysize - _FPIO_CONST_OFFSET;
1245 if (cy == 0)
1246 --densize;
1247 (void) SWAP (psrc, pdest);
1248 }
1249 }
1250 expbit <<= 1;
1251 ++ttab;
1252 }
1253 while (neg_exp != 0);
1254
1255 if (psrc == num)
1256 memcpy (den, num, densize * sizeof (mp_limb_t));
1257
1258 /* Read the fractional digits from the string. */
1259 (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
1260 #ifndef USE_WIDE_CHAR
1261 , decimal, decimal_len, thousands
1262 #endif
1263 );
1264
1265 /* We now have to shift both numbers so that the highest bit in the
1266 denominator is set. In the same process we copy the numerator to
1267 a high place in the array so that the division constructs the wanted
1268 digits. This is done by a "quasi fix point" number representation.
1269
1270 num: ddddddddddd . 0000000000000000000000
1271 |--- m ---|
1272 den: ddddddddddd n >= m
1273 |--- n ---|
1274 */
1275
1276 count_leading_zeros (cnt, den[densize - 1]);
1277
1278 if (cnt > 0)
1279 {
1280 /* Don't call `mpn_shift' with a count of zero since the specification
1281 does not allow this. */
1282 (void) __mpn_lshift (den, den, densize, cnt);
1283 cy = __mpn_lshift (num, num, numsize, cnt);
1284 if (cy != 0)
1285 num[numsize++] = cy;
1286 }
1287
1288 /* Now we are ready for the division. But it is not necessary to
1289 do a full multi-precision division because we only need a small
1290 number of bits for the result. So we do not use __mpn_divmod
1291 here but instead do the division here by hand and stop whenever
1292 the needed number of bits is reached. The code itself comes
1293 from the GNU MP Library by Torbj\"orn Granlund. */
1294
1295 exponent = bits;
1296
1297 switch (densize)
1298 {
1299 case 1:
1300 {
1301 mp_limb_t d, n, quot;
1302 int used = 0;
1303
1304 n = num[0];
1305 d = den[0];
1306 assert (numsize == 1 && n < d);
1307
1308 do
1309 {
1310 udiv_qrnnd (quot, n, n, 0, d);
1311
1312 #define got_limb \
1313 if (bits == 0) \
1314 { \
1315 register int cnt; \
1316 if (quot == 0) \
1317 cnt = BITS_PER_MP_LIMB; \
1318 else \
1319 count_leading_zeros (cnt, quot); \
1320 exponent -= cnt; \
1321 if (BITS_PER_MP_LIMB - cnt > MANT_DIG) \
1322 { \
1323 used = MANT_DIG + cnt; \
1324 retval[0] = quot >> (BITS_PER_MP_LIMB - used); \
1325 bits = MANT_DIG + 1; \
1326 } \
1327 else \
1328 { \
1329 /* Note that we only clear the second element. */ \
1330 /* The conditional is determined at compile time. */ \
1331 if (RETURN_LIMB_SIZE > 1) \
1332 retval[1] = 0; \
1333 retval[0] = quot; \
1334 bits = -cnt; \
1335 } \
1336 } \
1337 else if (bits + BITS_PER_MP_LIMB <= MANT_DIG) \
1338 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB, \
1339 quot); \
1340 else \
1341 { \
1342 used = MANT_DIG - bits; \
1343 if (used > 0) \
1344 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot); \
1345 } \
1346 bits += BITS_PER_MP_LIMB
1347
1348 got_limb;
1349 }
1350 while (bits <= MANT_DIG);
1351
1352 return round_and_return (retval, exponent - 1, negative,
1353 quot, BITS_PER_MP_LIMB - 1 - used,
1354 more_bits || n != 0);
1355 }
1356 case 2:
1357 {
1358 mp_limb_t d0, d1, n0, n1;
1359 mp_limb_t quot = 0;
1360 int used = 0;
1361
1362 d0 = den[0];
1363 d1 = den[1];
1364
1365 if (numsize < densize)
1366 {
1367 if (num[0] >= d1)
1368 {
1369 /* The numerator of the number occupies fewer bits than
1370 the denominator but the one limb is bigger than the
1371 high limb of the numerator. */
1372 n1 = 0;
1373 n0 = num[0];
1374 }
1375 else
1376 {
1377 if (bits <= 0)
1378 exponent -= BITS_PER_MP_LIMB;
1379 else
1380 {
1381 if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
1382 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1383 BITS_PER_MP_LIMB, 0);
1384 else
1385 {
1386 used = MANT_DIG - bits;
1387 if (used > 0)
1388 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1389 }
1390 bits += BITS_PER_MP_LIMB;
1391 }
1392 n1 = num[0];
1393 n0 = 0;
1394 }
1395 }
1396 else
1397 {
1398 n1 = num[1];
1399 n0 = num[0];
1400 }
1401
1402 while (bits <= MANT_DIG)
1403 {
1404 mp_limb_t r;
1405
1406 if (n1 == d1)
1407 {
1408 /* QUOT should be either 111..111 or 111..110. We need
1409 special treatment of this rare case as normal division
1410 would give overflow. */
1411 quot = ~(mp_limb_t) 0;
1412
1413 r = n0 + d1;
1414 if (r < d1) /* Carry in the addition? */
1415 {
1416 add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
1417 goto have_quot;
1418 }
1419 n1 = d0 - (d0 != 0);
1420 n0 = -d0;
1421 }
1422 else
1423 {
1424 udiv_qrnnd (quot, r, n1, n0, d1);
1425 umul_ppmm (n1, n0, d0, quot);
1426 }
1427
1428 q_test:
1429 if (n1 > r || (n1 == r && n0 > 0))
1430 {
1431 /* The estimated QUOT was too large. */
1432 --quot;
1433
1434 sub_ddmmss (n1, n0, n1, n0, 0, d0);
1435 r += d1;
1436 if (r >= d1) /* If not carry, test QUOT again. */
1437 goto q_test;
1438 }
1439 sub_ddmmss (n1, n0, r, 0, n1, n0);
1440
1441 have_quot:
1442 got_limb;
1443 }
1444
1445 return round_and_return (retval, exponent - 1, negative,
1446 quot, BITS_PER_MP_LIMB - 1 - used,
1447 more_bits || n1 != 0 || n0 != 0);
1448 }
1449 default:
1450 {
1451 int i;
1452 mp_limb_t cy, dX, d1, n0, n1;
1453 mp_limb_t quot = 0;
1454 int used = 0;
1455
1456 dX = den[densize - 1];
1457 d1 = den[densize - 2];
1458
1459 /* The division does not work if the upper limb of the two-limb
1460 numerator is greater than the denominator. */
1461 if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
1462 num[numsize++] = 0;
1463
1464 if (numsize < densize)
1465 {
1466 mp_size_t empty = densize - numsize;
1467 register int i;
1468
1469 if (bits <= 0)
1470 exponent -= empty * BITS_PER_MP_LIMB;
1471 else
1472 {
1473 if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
1474 {
1475 /* We make a difference here because the compiler
1476 cannot optimize the `else' case that good and
1477 this reflects all currently used FLOAT types
1478 and GMP implementations. */
1479 #if RETURN_LIMB_SIZE <= 2
1480 assert (empty == 1);
1481 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
1482 BITS_PER_MP_LIMB, 0);
1483 #else
1484 for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
1485 retval[i] = retval[i - empty];
1486 while (i >= 0)
1487 retval[i--] = 0;
1488 #endif
1489 }
1490 else
1491 {
1492 used = MANT_DIG - bits;
1493 if (used >= BITS_PER_MP_LIMB)
1494 {
1495 register int i;
1496 (void) __mpn_lshift (&retval[used
1497 / BITS_PER_MP_LIMB],
1498 retval, RETURN_LIMB_SIZE,
1499 used % BITS_PER_MP_LIMB);
1500 for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
1501 retval[i] = 0;
1502 }
1503 else if (used > 0)
1504 __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
1505 }
1506 bits += empty * BITS_PER_MP_LIMB;
1507 }
1508 for (i = numsize; i > 0; --i)
1509 num[i + empty] = num[i - 1];
1510 MPN_ZERO (num, empty + 1);
1511 }
1512 else
1513 {
1514 int i;
1515 assert (numsize == densize);
1516 for (i = numsize; i > 0; --i)
1517 num[i] = num[i - 1];
1518 }
1519
1520 den[densize] = 0;
1521 n0 = num[densize];
1522
1523 while (bits <= MANT_DIG)
1524 {
1525 if (n0 == dX)
1526 /* This might over-estimate QUOT, but it's probably not
1527 worth the extra code here to find out. */
1528 quot = ~(mp_limb_t) 0;
1529 else
1530 {
1531 mp_limb_t r;
1532
1533 udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
1534 umul_ppmm (n1, n0, d1, quot);
1535
1536 while (n1 > r || (n1 == r && n0 > num[densize - 2]))
1537 {
1538 --quot;
1539 r += dX;
1540 if (r < dX) /* I.e. "carry in previous addition?" */
1541 break;
1542 n1 -= n0 < d1;
1543 n0 -= d1;
1544 }
1545 }
1546
1547 /* Possible optimization: We already have (q * n0) and (1 * n1)
1548 after the calculation of QUOT. Taking advantage of this, we
1549 could make this loop make two iterations less. */
1550
1551 cy = __mpn_submul_1 (num, den, densize + 1, quot);
1552
1553 if (num[densize] != cy)
1554 {
1555 cy = __mpn_add_n (num, num, den, densize);
1556 assert (cy != 0);
1557 --quot;
1558 }
1559 n0 = num[densize] = num[densize - 1];
1560 for (i = densize - 1; i > 0; --i)
1561 num[i] = num[i - 1];
1562
1563 got_limb;
1564 }
1565
1566 for (i = densize; num[i] == 0 && i >= 0; --i)
1567 ;
1568 return round_and_return (retval, exponent - 1, negative,
1569 quot, BITS_PER_MP_LIMB - 1 - used,
1570 more_bits || i >= 0);
1571 }
1572 }
1573 }
1574
1575 /* NOTREACHED */
1576 }
1577 #if defined _LIBC && !defined USE_WIDE_CHAR
1578 libc_hidden_def (____STRTOF_INTERNAL)
1579 #endif
1580 \f
1581 /* External user entry point. */
1582
1583 FLOAT
1584 #ifdef weak_function
1585 weak_function
1586 #endif
1587 __STRTOF (nptr, endptr, loc)
1588 const STRING_TYPE *nptr;
1589 STRING_TYPE **endptr;
1590 __locale_t loc;
1591 {
1592 return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
1593 }
1594 weak_alias (__STRTOF, STRTOF)
1595
1596 #ifdef LONG_DOUBLE_COMPAT
1597 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
1598 # ifdef USE_WIDE_CHAR
1599 compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
1600 # else
1601 compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
1602 # endif
1603 # endif
1604 # if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
1605 # ifdef USE_WIDE_CHAR
1606 compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
1607 # else
1608 compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
1609 # endif
1610 # endif
1611 #endif