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