_strtod.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (C) 2000-2005 Manuel Novoa III
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. /* Notes:
  7. *
  8. * The primary objective of this implementation was minimal size and
  9. * portablility, while providing robustness and resonable accuracy.
  10. *
  11. * This implementation depends on IEEE floating point behavior and expects
  12. * to be able to generate +/- infinity as a result.
  13. *
  14. * There are a number of compile-time options below.
  15. */
  16. /* July 27, 2003
  17. *
  18. * General cleanup and some minor size optimizations.
  19. * Change implementation to support __strtofpmax() rather than strtod().
  20. * Now all the strto{floating pt}() funcs are implemented in terms of
  21. * of the internal __strtofpmax() function.
  22. * Support "nan", "inf", and "infinity" strings (case-insensitive).
  23. * Support hexadecimal floating point notation.
  24. * Support wchar variants.
  25. * Support xlocale variants.
  26. *
  27. * TODO:
  28. *
  29. * Consider accumulating blocks of digits in longs to save floating pt mults.
  30. * This would likely be much better on anything that only supported floats
  31. * where DECIMAL_DIG == 9. Actually, if floats have FLT_MAX_10_EXP == 38,
  32. * we could calculate almost all the exponent multipliers (p_base) in
  33. * long arithmetic as well.
  34. */
  35. /**********************************************************************/
  36. /* OPTIONS */
  37. /**********************************************************************/
  38. /* Defined if we want to recognize "nan", "inf", and "infinity". (C99) */
  39. #define _STRTOD_NAN_INF_STRINGS 1
  40. /* Defined if we want support hexadecimal floating point notation. (C99) */
  41. /* Note! Now controlled by uClibc configuration. See below. */
  42. #define _STRTOD_HEXADECIMAL_FLOATS 1
  43. /* Defined if we want to scale with a O(log2(exp)) multiplications.
  44. * This is generally a good thing to do unless you are really tight
  45. * on space and do not expect to convert values of large magnitude. */
  46. #define _STRTOD_LOG_SCALING 1
  47. /* WARNING!!! WARNING!!! WARNING!!! WARNING!!! WARNING!!!
  48. *
  49. * Clearing any of the options below this point is not advised (or tested).
  50. *
  51. * WARNING!!! WARNING!!! WARNING!!! WARNING!!! WARNING!!! */
  52. /* Defined if we want strtod to set errno appropriately. */
  53. /* NOTE: Implies all options below. */
  54. #define _STRTOD_ERRNO 1
  55. /* Defined if we want support for the endptr arg. */
  56. /* Implied by _STRTOD_ERRNO. */
  57. #define _STRTOD_ENDPTR 1
  58. /* Defined if we want to prevent overflow in accumulating the exponent. */
  59. /* Implied by _STRTOD_ERRNO. */
  60. #define _STRTOD_RESTRICT_EXP 1
  61. /* Defined if we want to process mantissa digits more intelligently. */
  62. /* Implied by _STRTOD_ERRNO. */
  63. #define _STRTOD_RESTRICT_DIGITS 1
  64. /* Defined if we want to skip scaling 0 for the exponent. */
  65. /* Implied by _STRTOD_ERRNO. */
  66. #define _STRTOD_ZERO_CHECK 1
  67. /**********************************************************************/
  68. /* Don't change anything that follows. */
  69. /**********************************************************************/
  70. #ifdef _STRTOD_ERRNO
  71. #undef _STRTOD_ENDPTR
  72. #undef _STRTOD_RESTRICT_EXP
  73. #undef _STRTOD_RESTRICT_DIGITS
  74. #undef _STRTOD_ZERO_CHECK
  75. #define _STRTOD_ENDPTR 1
  76. #define _STRTOD_RESTRICT_EXP 1
  77. #define _STRTOD_RESTRICT_DIGITS 1
  78. #define _STRTOD_ZERO_CHECK 1
  79. #endif
  80. /**********************************************************************/
  81. #define _ISOC99_SOURCE 1
  82. #include <stdlib.h>
  83. #include <string.h>
  84. #include <ctype.h>
  85. #include <errno.h>
  86. #include <limits.h>
  87. #include <float.h>
  88. #include <bits/uClibc_fpmax.h>
  89. #include <locale.h>
  90. #ifdef __UCLIBC_HAS_WCHAR__
  91. # include <wchar.h>
  92. # include <wctype.h>
  93. # include <bits/uClibc_uwchar.h>
  94. #endif
  95. #ifdef __UCLIBC_HAS_XLOCALE__
  96. # include <xlocale.h>
  97. #endif
  98. /* Handle _STRTOD_HEXADECIMAL_FLOATS via uClibc config now. */
  99. #undef _STRTOD_HEXADECIMAL_FLOATS
  100. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  101. # define _STRTOD_HEXADECIMAL_FLOATS 1
  102. #endif
  103. /**********************************************************************/
  104. #undef _STRTOD_FPMAX
  105. #if FPMAX_TYPE == 3
  106. #define NEED_STRTOLD_WRAPPER
  107. #define NEED_STRTOD_WRAPPER
  108. #define NEED_STRTOF_WRAPPER
  109. #elif FPMAX_TYPE == 2
  110. #define NEED_STRTOD_WRAPPER
  111. #define NEED_STRTOF_WRAPPER
  112. #elif FPMAX_TYPE == 1
  113. #define NEED_STRTOF_WRAPPER
  114. #else
  115. #error unknown FPMAX_TYPE!
  116. #endif
  117. extern void __fp_range_check(__fpmax_t y, __fpmax_t x) attribute_hidden;
  118. /**********************************************************************/
  119. #ifdef _STRTOD_RESTRICT_DIGITS
  120. #define EXP_DENORM_ADJUST DECIMAL_DIG
  121. #define MAX_ALLOWED_EXP (DECIMAL_DIG + EXP_DENORM_ADJUST - FPMAX_MIN_10_EXP)
  122. #if MAX_ALLOWED_EXP > INT_MAX
  123. #error size assumption violated for MAX_ALLOWED_EXP
  124. #endif
  125. #else
  126. /* We want some excess if we're not restricting mantissa digits. */
  127. #define MAX_ALLOWED_EXP ((20 - FPMAX_MIN_10_EXP) * 2)
  128. #endif
  129. #if defined(_STRTOD_RESTRICT_DIGITS) || defined(_STRTOD_ENDPTR) || defined(_STRTOD_HEXADECIMAL_FLOATS)
  130. #undef _STRTOD_NEED_NUM_DIGITS
  131. #define _STRTOD_NEED_NUM_DIGITS 1
  132. #endif
  133. /**********************************************************************/
  134. #if defined(L___strtofpmax) || defined(L___strtofpmax_l) || defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  135. #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  136. #define __strtofpmax __wcstofpmax
  137. #define __strtofpmax_l __wcstofpmax_l
  138. #define Wchar wchar_t
  139. #ifdef __UCLIBC_DO_XLOCALE
  140. #define ISSPACE(C) iswspace_l((C), locale_arg)
  141. #else
  142. #define ISSPACE(C) iswspace((C))
  143. #endif
  144. #else /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
  145. #define Wchar char
  146. #ifdef __UCLIBC_DO_XLOCALE
  147. #define ISSPACE(C) isspace_l((C), locale_arg)
  148. #else
  149. #define ISSPACE(C) isspace((C))
  150. #endif
  151. #endif /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
  152. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  153. __fpmax_t attribute_hidden __strtofpmax(const Wchar *str, Wchar **endptr, int exponent_power)
  154. {
  155. return __strtofpmax_l(str, endptr, exponent_power, __UCLIBC_CURLOCALE);
  156. }
  157. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  158. __fpmax_t attribute_hidden __XL_NPP(__strtofpmax)(const Wchar *str, Wchar **endptr, int exponent_power
  159. __LOCALE_PARAM )
  160. {
  161. __fpmax_t number;
  162. __fpmax_t p_base = 10; /* Adjusted to 16 in the hex case. */
  163. Wchar *pos0;
  164. #ifdef _STRTOD_ENDPTR
  165. Wchar *pos1;
  166. #endif
  167. Wchar *pos = (Wchar *) str;
  168. int exponent_temp;
  169. int negative; /* A flag for the number, a multiplier for the exponent. */
  170. #ifdef _STRTOD_NEED_NUM_DIGITS
  171. int num_digits;
  172. #endif
  173. #ifdef __UCLIBC_HAS_LOCALE__
  174. #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  175. wchar_t decpt_wc = __LOCALE_PTR->decimal_point_wc;
  176. #else
  177. const char *decpt = __LOCALE_PTR->decimal_point;
  178. int decpt_len = __LOCALE_PTR->decimal_point_len;
  179. #endif
  180. #endif
  181. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  182. Wchar expchar = 'e';
  183. Wchar *poshex = NULL;
  184. __uint16_t is_mask = _ISdigit;
  185. #define EXPCHAR expchar
  186. #define IS_X_DIGIT(C) __isctype((C), is_mask)
  187. #else /* _STRTOD_HEXADECIMAL_FLOATS */
  188. #define EXPCHAR 'e'
  189. #define IS_X_DIGIT(C) isdigit((C))
  190. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  191. while (ISSPACE(*pos)) { /* Skip leading whitespace. */
  192. ++pos;
  193. }
  194. negative = 0;
  195. switch(*pos) { /* Handle optional sign. */
  196. case '-': negative = 1; /* Fall through to increment position. */
  197. case '+': ++pos;
  198. }
  199. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  200. if ((*pos == '0') && (((pos[1])|0x20) == 'x')) {
  201. poshex = ++pos; /* Save position of 'x' in case no digits */
  202. ++pos; /* and advance past it. */
  203. is_mask = _ISxdigit; /* Used by IS_X_DIGIT. */
  204. expchar = 'p'; /* Adjust exponent char. */
  205. p_base = 16; /* Adjust base multiplier. */
  206. }
  207. #endif
  208. number = 0.;
  209. #ifdef _STRTOD_NEED_NUM_DIGITS
  210. num_digits = -1;
  211. #endif
  212. /* exponent_power = 0; */
  213. pos0 = NULL;
  214. LOOP:
  215. while (IS_X_DIGIT(*pos)) { /* Process string of (hex) digits. */
  216. #ifdef _STRTOD_RESTRICT_DIGITS
  217. if (num_digits < 0) { /* First time through? */
  218. ++num_digits; /* We've now seen a digit. */
  219. }
  220. if (num_digits || (*pos != '0')) { /* Had/have nonzero. */
  221. ++num_digits;
  222. if (num_digits <= DECIMAL_DIG) { /* Is digit significant? */
  223. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  224. number = number * p_base
  225. + (isdigit(*pos)
  226. ? (*pos - '0')
  227. : (((*pos)|0x20) - ('a' - 10)));
  228. #else /* _STRTOD_HEXADECIMAL_FLOATS */
  229. number = number * p_base + (*pos - '0');
  230. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  231. }
  232. }
  233. #else /* _STRTOD_RESTRICT_DIGITS */
  234. #ifdef _STRTOD_NEED_NUM_DIGITS
  235. ++num_digits;
  236. #endif
  237. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  238. number = number * p_base
  239. + (isdigit(*pos)
  240. ? (*pos - '0')
  241. : (((*pos)|0x20) - ('a' - 10)));
  242. #else /* _STRTOD_HEXADECIMAL_FLOATS */
  243. number = number * p_base + (*pos - '0');
  244. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  245. #endif /* _STRTOD_RESTRICT_DIGITS */
  246. ++pos;
  247. }
  248. #ifdef __UCLIBC_HAS_LOCALE__
  249. #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  250. if (!pos0 && (*pos == decpt_wc)) { /* First decimal point? */
  251. pos0 = ++pos;
  252. goto LOOP;
  253. }
  254. #else
  255. if (!pos0 && !memcmp(pos, decpt, decpt_len)) { /* First decimal point? */
  256. pos0 = (pos += decpt_len);
  257. goto LOOP;
  258. }
  259. #endif
  260. #else /* __UCLIBC_HAS_LOCALE__ */
  261. if ((*pos == '.') && !pos0) { /* First decimal point? */
  262. pos0 = ++pos; /* Save position of decimal point */
  263. goto LOOP; /* and process rest of digits. */
  264. }
  265. #endif /* __UCLIBC_HAS_LOCALE__ */
  266. #ifdef _STRTOD_NEED_NUM_DIGITS
  267. if (num_digits<0) { /* Must have at least one digit. */
  268. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  269. if (poshex) { /* Back up to '0' in '0x' prefix. */
  270. pos = poshex;
  271. goto DONE;
  272. }
  273. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  274. #ifdef _STRTOD_NAN_INF_STRINGS
  275. if (!pos0) { /* No decimal point, so check for inf/nan. */
  276. /* Note: nan is the first string so 'number = i/0.;' works. */
  277. static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0";
  278. int i = 0;
  279. do {
  280. /* Unfortunately, we have no memcasecmp(). */
  281. int j = 0;
  282. /* | 0x20 is a cheap lowercasing (valid for ASCII letters and numbers only) */
  283. while ((pos[j] | 0x20) == nan_inf_str[i+1+j]) {
  284. ++j;
  285. if (!nan_inf_str[i+1+j]) {
  286. number = i / 0.;
  287. if (negative) { /* Correct for sign. */
  288. number = -number;
  289. }
  290. pos += nan_inf_str[i] - 2;
  291. goto DONE;
  292. }
  293. }
  294. i += nan_inf_str[i];
  295. } while (nan_inf_str[i]);
  296. }
  297. #endif /* STRTOD_NAN_INF_STRINGS */
  298. #ifdef _STRTOD_ENDPTR
  299. pos = (Wchar *) str;
  300. #endif
  301. goto DONE;
  302. }
  303. #endif /* _STRTOD_NEED_NUM_DIGITS */
  304. #ifdef _STRTOD_RESTRICT_DIGITS
  305. if (num_digits > DECIMAL_DIG) { /* Adjust exponent for skipped digits. */
  306. exponent_power += num_digits - DECIMAL_DIG;
  307. }
  308. #endif
  309. if (pos0) {
  310. exponent_power += pos0 - pos; /* Adjust exponent for decimal point. */
  311. }
  312. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  313. if (poshex) {
  314. exponent_power *= 4; /* Above is 2**4, but below is 2. */
  315. p_base = 2;
  316. }
  317. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  318. if (negative) { /* Correct for sign. */
  319. number = -number;
  320. }
  321. /* process an exponent string */
  322. if (((*pos)|0x20) == EXPCHAR) {
  323. #ifdef _STRTOD_ENDPTR
  324. pos1 = pos;
  325. #endif
  326. negative = 1;
  327. switch(*++pos) { /* Handle optional sign. */
  328. case '-': negative = -1; /* Fall through to increment pos. */
  329. case '+': ++pos;
  330. }
  331. pos0 = pos;
  332. exponent_temp = 0;
  333. while (isdigit(*pos)) { /* Process string of digits. */
  334. #ifdef _STRTOD_RESTRICT_EXP
  335. if (exponent_temp < MAX_ALLOWED_EXP) { /* Avoid overflow. */
  336. exponent_temp = exponent_temp * 10 + (*pos - '0');
  337. }
  338. #else
  339. exponent_temp = exponent_temp * 10 + (*pos - '0');
  340. #endif
  341. ++pos;
  342. }
  343. #ifdef _STRTOD_ENDPTR
  344. if (pos == pos0) { /* No digits? */
  345. pos = pos1; /* Back up to {e|E}/{p|P}. */
  346. } /* else */
  347. #endif
  348. exponent_power += negative * exponent_temp;
  349. }
  350. #ifdef _STRTOD_ZERO_CHECK
  351. if (number == 0.) {
  352. goto DONE;
  353. }
  354. #endif
  355. /* scale the result */
  356. #ifdef _STRTOD_LOG_SCALING
  357. exponent_temp = exponent_power;
  358. if (exponent_temp < 0) {
  359. exponent_temp = -exponent_temp;
  360. }
  361. while (exponent_temp) {
  362. if (exponent_temp & 1) {
  363. if (exponent_power < 0) {
  364. /* Warning... caluclating a factor for the exponent and
  365. * then dividing could easily be faster. But doing so
  366. * might cause problems when dealing with denormals. */
  367. number /= p_base;
  368. } else {
  369. number *= p_base;
  370. }
  371. }
  372. exponent_temp >>= 1;
  373. p_base *= p_base;
  374. }
  375. #else /* _STRTOD_LOG_SCALING */
  376. while (exponent_power) {
  377. if (exponent_power < 0) {
  378. number /= p_base;
  379. exponent_power++;
  380. } else {
  381. number *= p_base;
  382. exponent_power--;
  383. }
  384. }
  385. #endif /* _STRTOD_LOG_SCALING */
  386. #ifdef _STRTOD_ERRNO
  387. if (__FPMAX_ZERO_OR_INF_CHECK(number)) {
  388. __set_errno(ERANGE);
  389. }
  390. #endif
  391. DONE:
  392. #ifdef _STRTOD_ENDPTR
  393. if (endptr) {
  394. *endptr = pos;
  395. }
  396. #endif
  397. return number;
  398. }
  399. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  400. #endif
  401. /**********************************************************************/
  402. #ifdef L___fp_range_check
  403. #if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)
  404. void attribute_hidden __fp_range_check(__fpmax_t y, __fpmax_t x)
  405. {
  406. if (__FPMAX_ZERO_OR_INF_CHECK(y) /* y is 0 or +/- infinity */
  407. && (y != 0) /* y is not 0 (could have x>0, y==0 if underflow) */
  408. && !__FPMAX_ZERO_OR_INF_CHECK(x) /* x is not 0 or +/- infinity */
  409. ) {
  410. __set_errno(ERANGE); /* Then x is not in y's range. */
  411. }
  412. }
  413. #endif
  414. #endif
  415. /**********************************************************************/
  416. #if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)
  417. #if defined(NEED_STRTOF_WRAPPER)
  418. #if defined(L_wcstof) || defined(L_wcstof_l)
  419. #define strtof wcstof
  420. #define strtof_l wcstof_l
  421. #define __strtofpmax __wcstofpmax
  422. #define __strtofpmax_l __wcstofpmax_l
  423. #define Wchar wchar_t
  424. #else
  425. #define Wchar char
  426. #endif
  427. libc_hidden_proto(__XL_NPP(strtof))
  428. float __XL_NPP(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
  429. {
  430. #if FPMAX_TYPE == 1
  431. return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  432. #else
  433. __fpmax_t x;
  434. float y;
  435. x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  436. y = (float) x;
  437. __fp_range_check(y, x);
  438. return y;
  439. #endif
  440. }
  441. libc_hidden_def(__XL_NPP(strtof))
  442. #endif
  443. #endif
  444. /**********************************************************************/
  445. #if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)
  446. #if defined(NEED_STRTOD_WRAPPER)
  447. #if defined(L_wcstod) || defined(L_wcstod_l)
  448. #define strtod wcstod
  449. #define strtod_l wcstod_l
  450. #define __strtofpmax __wcstofpmax
  451. #define __strtofpmax_l __wcstofpmax_l
  452. #define Wchar wchar_t
  453. #else
  454. #define Wchar char
  455. #endif
  456. libc_hidden_proto(__XL_NPP(strtod))
  457. double __XL_NPP(strtod)(const Wchar *__restrict str,
  458. Wchar **__restrict endptr __LOCALE_PARAM )
  459. {
  460. #if FPMAX_TYPE == 2
  461. return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  462. #else
  463. __fpmax_t x;
  464. double y;
  465. x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  466. y = (double) x;
  467. __fp_range_check(y, x);
  468. return y;
  469. #endif
  470. }
  471. libc_hidden_def(__XL_NPP(strtod))
  472. #endif
  473. #endif
  474. /**********************************************************************/
  475. #if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)
  476. #if defined(NEED_STRTOLD_WRAPPER)
  477. #if defined(L_wcstold) || defined(L_wcstold_l)
  478. #define strtold wcstold
  479. #define strtold_l wcstold_l
  480. #define __strtofpmax __wcstofpmax
  481. #define __strtofpmax_l __wcstofpmax_l
  482. #define Wchar wchar_t
  483. #else
  484. #define Wchar char
  485. #endif
  486. libc_hidden_proto(__XL_NPP(strtold))
  487. long double __XL_NPP(strtold) (const Wchar *str, Wchar **endptr __LOCALE_PARAM )
  488. {
  489. #if FPMAX_TYPE == 3
  490. return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  491. #else
  492. __fpmax_t x;
  493. long double y;
  494. x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  495. y = (long double) x;
  496. __fp_range_check(y, x);
  497. return y;
  498. #endif
  499. }
  500. libc_hidden_def(__XL_NPP(strtold))
  501. #endif
  502. #endif
  503. /**********************************************************************/