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