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