strtod_l.c 47 KB

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