_strtod.c 15 KB

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