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