_strtod.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. #include <stdlib.h>
  82. #include <string.h>
  83. #include <ctype.h>
  84. #include <errno.h>
  85. #include <limits.h>
  86. #include <float.h>
  87. #include <bits/uClibc_fpmax.h>
  88. #include <locale.h>
  89. #ifdef __UCLIBC_HAS_WCHAR__
  90. # include <wchar.h>
  91. # include <wctype.h>
  92. # include <bits/uClibc_uwchar.h>
  93. #endif
  94. /* Handle _STRTOD_HEXADECIMAL_FLOATS via uClibc config now. */
  95. #undef _STRTOD_HEXADECIMAL_FLOATS
  96. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  97. # define _STRTOD_HEXADECIMAL_FLOATS 1
  98. #endif
  99. /**********************************************************************/
  100. #undef _STRTOD_FPMAX
  101. #if FPMAX_TYPE == 3
  102. #define NEED_STRTOLD_WRAPPER
  103. #define NEED_STRTOD_WRAPPER
  104. #define NEED_STRTOF_WRAPPER
  105. #elif FPMAX_TYPE == 2
  106. #define NEED_STRTOD_WRAPPER
  107. #define NEED_STRTOF_WRAPPER
  108. #elif FPMAX_TYPE == 1
  109. #define NEED_STRTOF_WRAPPER
  110. #else
  111. #error unknown FPMAX_TYPE!
  112. #endif
  113. extern void __fp_range_check(__fpmax_t y, __fpmax_t x) attribute_hidden;
  114. /**********************************************************************/
  115. #ifdef _STRTOD_RESTRICT_DIGITS
  116. #define EXP_DENORM_ADJUST DECIMAL_DIG
  117. #define MAX_ALLOWED_EXP (DECIMAL_DIG + EXP_DENORM_ADJUST - FPMAX_MIN_10_EXP)
  118. #if MAX_ALLOWED_EXP > INT_MAX
  119. #error size assumption violated for MAX_ALLOWED_EXP
  120. #endif
  121. #else
  122. /* We want some excess if we're not restricting mantissa digits. */
  123. #define MAX_ALLOWED_EXP ((20 - FPMAX_MIN_10_EXP) * 2)
  124. #endif
  125. #if defined(_STRTOD_RESTRICT_DIGITS) || defined(_STRTOD_ENDPTR) || defined(_STRTOD_HEXADECIMAL_FLOATS)
  126. #undef _STRTOD_NEED_NUM_DIGITS
  127. #define _STRTOD_NEED_NUM_DIGITS 1
  128. #endif
  129. /**********************************************************************/
  130. #if defined(L___strtofpmax) || defined(L___strtofpmax_l) || defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  131. #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  132. #define __strtofpmax __wcstofpmax
  133. #define __strtofpmax_l __wcstofpmax_l
  134. #define Wchar wchar_t
  135. #ifdef __UCLIBC_DO_XLOCALE
  136. #define ISSPACE(C) iswspace_l((C), locale_arg)
  137. #else
  138. #define ISSPACE(C) iswspace((C))
  139. #endif
  140. #else /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
  141. #define Wchar char
  142. #ifdef __UCLIBC_DO_XLOCALE
  143. #define ISSPACE(C) isspace_l((C), locale_arg)
  144. #else
  145. #define ISSPACE(C) isspace((C))
  146. #endif
  147. #endif /* defined(L___wcstofpmax) || defined(L___wcstofpmax_l) */
  148. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  149. __fpmax_t attribute_hidden __strtofpmax(const Wchar *str, Wchar **endptr, int exponent_power)
  150. {
  151. return __strtofpmax_l(str, endptr, exponent_power, __UCLIBC_CURLOCALE);
  152. }
  153. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  154. __fpmax_t attribute_hidden __XL_NPP(__strtofpmax)(const Wchar *str, Wchar **endptr, int exponent_power
  155. __LOCALE_PARAM )
  156. {
  157. __fpmax_t number;
  158. __fpmax_t p_base = 10; /* Adjusted to 16 in the hex case. */
  159. Wchar *pos0;
  160. #ifdef _STRTOD_ENDPTR
  161. Wchar *pos1;
  162. #endif
  163. Wchar *pos = (Wchar *) str;
  164. int exponent_temp;
  165. int negative; /* A flag for the number, a multiplier for the exponent. */
  166. #ifdef _STRTOD_NEED_NUM_DIGITS
  167. int num_digits;
  168. #endif
  169. #ifdef __UCLIBC_HAS_LOCALE__
  170. #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  171. wchar_t decpt_wc = __LOCALE_PTR->decimal_point_wc;
  172. #else
  173. const char *decpt = __LOCALE_PTR->decimal_point;
  174. int decpt_len = __LOCALE_PTR->decimal_point_len;
  175. #endif
  176. #endif
  177. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  178. Wchar expchar = 'e';
  179. Wchar *poshex = NULL;
  180. __uint16_t is_mask = _ISdigit;
  181. #define EXPCHAR expchar
  182. #define IS_X_DIGIT(C) __isctype((C), is_mask)
  183. #else /* _STRTOD_HEXADECIMAL_FLOATS */
  184. #define EXPCHAR 'e'
  185. #define IS_X_DIGIT(C) isdigit((C))
  186. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  187. while (ISSPACE(*pos)) { /* Skip leading whitespace. */
  188. ++pos;
  189. }
  190. negative = 0;
  191. switch(*pos) { /* Handle optional sign. */
  192. case '-': negative = 1; /* Fall through to increment position. */
  193. case '+': ++pos;
  194. }
  195. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  196. if ((*pos == '0') && (((pos[1])|0x20) == 'x')) {
  197. poshex = ++pos; /* Save position of 'x' in case no digits */
  198. ++pos; /* and advance past it. */
  199. is_mask = _ISxdigit; /* Used by IS_X_DIGIT. */
  200. expchar = 'p'; /* Adjust exponent char. */
  201. p_base = 16; /* Adjust base multiplier. */
  202. }
  203. #endif
  204. number = 0.;
  205. #ifdef _STRTOD_NEED_NUM_DIGITS
  206. num_digits = -1;
  207. #endif
  208. /* exponent_power = 0; */
  209. pos0 = NULL;
  210. LOOP:
  211. while (IS_X_DIGIT(*pos)) { /* Process string of (hex) digits. */
  212. #ifdef _STRTOD_RESTRICT_DIGITS
  213. if (num_digits < 0) { /* First time through? */
  214. ++num_digits; /* We've now seen a digit. */
  215. }
  216. if (num_digits || (*pos != '0')) { /* Had/have nonzero. */
  217. ++num_digits;
  218. if (num_digits <= DECIMAL_DIG) { /* Is digit significant? */
  219. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  220. number = number * p_base
  221. + (isdigit(*pos)
  222. ? (*pos - '0')
  223. : (((*pos)|0x20) - ('a' - 10)));
  224. #else /* _STRTOD_HEXADECIMAL_FLOATS */
  225. number = number * p_base + (*pos - '0');
  226. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  227. }
  228. }
  229. #else /* _STRTOD_RESTRICT_DIGITS */
  230. #ifdef _STRTOD_NEED_NUM_DIGITS
  231. ++num_digits;
  232. #endif
  233. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  234. number = number * p_base
  235. + (isdigit(*pos)
  236. ? (*pos - '0')
  237. : (((*pos)|0x20) - ('a' - 10)));
  238. #else /* _STRTOD_HEXADECIMAL_FLOATS */
  239. number = number * p_base + (*pos - '0');
  240. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  241. #endif /* _STRTOD_RESTRICT_DIGITS */
  242. ++pos;
  243. }
  244. #ifdef __UCLIBC_HAS_LOCALE__
  245. #if defined(L___wcstofpmax) || defined(L___wcstofpmax_l)
  246. if (!pos0 && (*pos == decpt_wc)) { /* First decimal point? */
  247. pos0 = ++pos;
  248. goto LOOP;
  249. }
  250. #else
  251. if (!pos0 && !memcmp(pos, decpt, decpt_len)) { /* First decimal point? */
  252. pos0 = (pos += decpt_len);
  253. goto LOOP;
  254. }
  255. #endif
  256. #else /* __UCLIBC_HAS_LOCALE__ */
  257. if ((*pos == '.') && !pos0) { /* First decimal point? */
  258. pos0 = ++pos; /* Save position of decimal point */
  259. goto LOOP; /* and process rest of digits. */
  260. }
  261. #endif /* __UCLIBC_HAS_LOCALE__ */
  262. #ifdef _STRTOD_NEED_NUM_DIGITS
  263. if (num_digits<0) { /* Must have at least one digit. */
  264. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  265. if (poshex) { /* Back up to '0' in '0x' prefix. */
  266. pos = poshex;
  267. goto DONE;
  268. }
  269. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  270. #ifdef _STRTOD_NAN_INF_STRINGS
  271. if (!pos0) { /* No decimal point, so check for inf/nan. */
  272. /* Note: nan is the first string so 'number = i/0.;' works. */
  273. static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0";
  274. int i = 0;
  275. do {
  276. /* Unfortunately, we have no memcasecmp(). */
  277. int j = 0;
  278. /* | 0x20 is a cheap lowercasing (valid for ASCII letters and numbers only) */
  279. while ((pos[j] | 0x20) == nan_inf_str[i+1+j]) {
  280. ++j;
  281. if (!nan_inf_str[i+1+j]) {
  282. number = i / 0.;
  283. if (negative) { /* Correct for sign. */
  284. number = -number;
  285. }
  286. pos += nan_inf_str[i] - 2;
  287. goto DONE;
  288. }
  289. }
  290. i += nan_inf_str[i];
  291. } while (nan_inf_str[i]);
  292. }
  293. #endif /* STRTOD_NAN_INF_STRINGS */
  294. #ifdef _STRTOD_ENDPTR
  295. pos = (Wchar *) str;
  296. #endif
  297. goto DONE;
  298. }
  299. #endif /* _STRTOD_NEED_NUM_DIGITS */
  300. #ifdef _STRTOD_RESTRICT_DIGITS
  301. if (num_digits > DECIMAL_DIG) { /* Adjust exponent for skipped digits. */
  302. exponent_power += num_digits - DECIMAL_DIG;
  303. }
  304. #endif
  305. if (pos0) {
  306. exponent_power += pos0 - pos; /* Adjust exponent for decimal point. */
  307. }
  308. #ifdef _STRTOD_HEXADECIMAL_FLOATS
  309. if (poshex) {
  310. exponent_power *= 4; /* Above is 2**4, but below is 2. */
  311. p_base = 2;
  312. }
  313. #endif /* _STRTOD_HEXADECIMAL_FLOATS */
  314. if (negative) { /* Correct for sign. */
  315. number = -number;
  316. }
  317. /* process an exponent string */
  318. if (((*pos)|0x20) == EXPCHAR) {
  319. #ifdef _STRTOD_ENDPTR
  320. pos1 = pos;
  321. #endif
  322. negative = 1;
  323. switch(*++pos) { /* Handle optional sign. */
  324. case '-': negative = -1; /* Fall through to increment pos. */
  325. case '+': ++pos;
  326. }
  327. pos0 = pos;
  328. exponent_temp = 0;
  329. while (isdigit(*pos)) { /* Process string of digits. */
  330. #ifdef _STRTOD_RESTRICT_EXP
  331. if (exponent_temp < MAX_ALLOWED_EXP) { /* Avoid overflow. */
  332. exponent_temp = exponent_temp * 10 + (*pos - '0');
  333. }
  334. #else
  335. exponent_temp = exponent_temp * 10 + (*pos - '0');
  336. #endif
  337. ++pos;
  338. }
  339. #ifdef _STRTOD_ENDPTR
  340. if (pos == pos0) { /* No digits? */
  341. pos = pos1; /* Back up to {e|E}/{p|P}. */
  342. } /* else */
  343. #endif
  344. exponent_power += negative * exponent_temp;
  345. }
  346. #ifdef _STRTOD_ZERO_CHECK
  347. if (number == 0.) {
  348. goto DONE;
  349. }
  350. #endif
  351. /* scale the result */
  352. #ifdef _STRTOD_LOG_SCALING
  353. exponent_temp = exponent_power;
  354. if (exponent_temp < 0) {
  355. exponent_temp = -exponent_temp;
  356. }
  357. while (exponent_temp) {
  358. if (exponent_temp & 1) {
  359. if (exponent_power < 0) {
  360. /* Warning... caluclating a factor for the exponent and
  361. * then dividing could easily be faster. But doing so
  362. * might cause problems when dealing with denormals. */
  363. number /= p_base;
  364. } else {
  365. number *= p_base;
  366. }
  367. }
  368. exponent_temp >>= 1;
  369. p_base *= p_base;
  370. }
  371. #else /* _STRTOD_LOG_SCALING */
  372. while (exponent_power) {
  373. if (exponent_power < 0) {
  374. number /= p_base;
  375. exponent_power++;
  376. } else {
  377. number *= p_base;
  378. exponent_power--;
  379. }
  380. }
  381. #endif /* _STRTOD_LOG_SCALING */
  382. #ifdef _STRTOD_ERRNO
  383. if (__FPMAX_ZERO_OR_INF_CHECK(number)) {
  384. __set_errno(ERANGE);
  385. }
  386. #endif
  387. DONE:
  388. #ifdef _STRTOD_ENDPTR
  389. if (endptr) {
  390. *endptr = pos;
  391. }
  392. #endif
  393. return number;
  394. }
  395. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  396. #endif
  397. /**********************************************************************/
  398. #ifdef L___fp_range_check
  399. #if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)
  400. void attribute_hidden __fp_range_check(__fpmax_t y, __fpmax_t x)
  401. {
  402. if (__FPMAX_ZERO_OR_INF_CHECK(y) /* y is 0 or +/- infinity */
  403. && (y != 0) /* y is not 0 (could have x>0, y==0 if underflow) */
  404. && !__FPMAX_ZERO_OR_INF_CHECK(x) /* x is not 0 or +/- infinity */
  405. ) {
  406. __set_errno(ERANGE); /* Then x is not in y's range. */
  407. }
  408. }
  409. #endif
  410. #endif
  411. /**********************************************************************/
  412. #if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)
  413. #if defined(NEED_STRTOF_WRAPPER)
  414. #if defined(L_wcstof) || defined(L_wcstof_l)
  415. #define strtof wcstof
  416. #define strtof_l wcstof_l
  417. #define __strtofpmax __wcstofpmax
  418. #define __strtofpmax_l __wcstofpmax_l
  419. #define Wchar wchar_t
  420. #else
  421. #define Wchar char
  422. #endif
  423. float __XL_NPP(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM )
  424. {
  425. #if FPMAX_TYPE == 1
  426. return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  427. #else
  428. __fpmax_t x;
  429. float y;
  430. x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  431. y = (float) x;
  432. __fp_range_check(y, x);
  433. return y;
  434. #endif
  435. }
  436. #endif
  437. #endif
  438. /**********************************************************************/
  439. #if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)
  440. #if defined(NEED_STRTOD_WRAPPER)
  441. #if defined(L_wcstod) || defined(L_wcstod_l)
  442. #define strtod wcstod
  443. #define strtod_l wcstod_l
  444. #define __strtofpmax __wcstofpmax
  445. #define __strtofpmax_l __wcstofpmax_l
  446. #define Wchar wchar_t
  447. #else
  448. #define Wchar char
  449. #endif
  450. double __XL_NPP(strtod)(const Wchar *__restrict str,
  451. Wchar **__restrict endptr __LOCALE_PARAM )
  452. {
  453. #if FPMAX_TYPE == 2
  454. return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  455. #else
  456. __fpmax_t x;
  457. double y;
  458. x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  459. y = (double) x;
  460. __fp_range_check(y, x);
  461. return y;
  462. #endif
  463. }
  464. #ifdef L_strtod
  465. libc_hidden_def(strtod)
  466. #endif
  467. #endif
  468. #endif
  469. /**********************************************************************/
  470. #if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)
  471. #if defined(NEED_STRTOLD_WRAPPER)
  472. #if defined(L_wcstold) || defined(L_wcstold_l)
  473. #define strtold wcstold
  474. #define strtold_l wcstold_l
  475. #define __strtofpmax __wcstofpmax
  476. #define __strtofpmax_l __wcstofpmax_l
  477. #define Wchar wchar_t
  478. #else
  479. #define Wchar char
  480. #endif
  481. long double __XL_NPP(strtold) (const Wchar *str, Wchar **endptr __LOCALE_PARAM )
  482. {
  483. #if FPMAX_TYPE == 3
  484. return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  485. #else
  486. __fpmax_t x;
  487. long double y;
  488. x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );
  489. y = (long double) x;
  490. __fp_range_check(y, x);
  491. return y;
  492. #endif
  493. }
  494. #endif
  495. #endif
  496. /**********************************************************************/