This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / numeric.c
CommitLineData
98994639
HS
1/* numeric.c
2 *
663f364b 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
1129b882 4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
98994639
HS
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
11/*
4ac71550
TC
12 * "That only makes eleven (plus one mislaid) and not fourteen,
13 * unless wizards count differently to other people." --Beorn
14 *
15 * [p.115 of _The Hobbit_: "Queer Lodgings"]
98994639
HS
16 */
17
ccfc67b7 18/*
7fefc6c1 19
166f8a29
DM
20This file contains all the stuff needed by perl for manipulating numeric
21values, including such things as replacements for the OS's atof() function
22
ccfc67b7
JH
23*/
24
98994639
HS
25#include "EXTERN.h"
26#define PERL_IN_NUMERIC_C
27#include "perl.h"
28
9ec8aea5
KW
29#ifdef Perl_strtod
30
31PERL_STATIC_INLINE NV
32S_strtod(pTHX_ const char * const s, char ** e)
33{
34 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
35 NV result;
36
37 STORE_LC_NUMERIC_SET_TO_NEEDED();
38
39# ifdef USE_QUADMATH
40
41 result = strtoflt128(s, e);
42
43# elif defined(HAS_STRTOLD) && defined(HAS_LONG_DOUBLE) \
44 && defined(USE_LONG_DOUBLE)
45# if defined(__MINGW64_VERSION_MAJOR)
46 /***********************************************
47 We are unable to use strtold because of
48 https://sourceforge.net/p/mingw-w64/bugs/711/
49 &
50 https://sourceforge.net/p/mingw-w64/bugs/725/
51
52 but __mingw_strtold is fine.
53 ***********************************************/
54
55 result = __mingw_strtold(s, e);
56
57# else
58
59 result = strtold(s, e);
60
61# endif
62# elif defined(HAS_STRTOD)
63
64 result = strtod(s, e);
65
02fd9d54
KW
66# else
67# error No strtod() equivalent found
9ec8aea5
KW
68# endif
69
70 RESTORE_LC_NUMERIC();
71
72 return result;
73}
74
75#endif /* #ifdef Perl_strtod */
76
77/*
78
79=for apidoc my_strtod
80
81This function is equivalent to the libc strtod() function, and is available
82even on platforms that lack plain strtod(). Its return value is the best
83available precision depending on platform capabilities and F<Configure>
84options.
85
86It properly handles the locale radix character, meaning it expects a dot except
87when called from within the scope of S<C<use locale>>, in which case the radix
88character should be that specified by the current locale.
89
e7a3fd45 90The synonym Strtod() may be used instead.
9ec8aea5
KW
91
92=cut
93
94*/
95
96NV
0f3d8cd4 97Perl_my_strtod(const char * const s, char **e)
9ec8aea5
KW
98{
99 dTHX;
100
101 PERL_ARGS_ASSERT_MY_STRTOD;
102
103#ifdef Perl_strtod
104
105 return S_strtod(aTHX_ s, e);
106
107#else
108
109 {
110 NV result;
b4603d09 111 char * end_ptr;
9ec8aea5 112
b4603d09 113 end_ptr = my_atof2(s, &result);
9ec8aea5 114 if (e) {
b4603d09 115 *e = end_ptr;
9ec8aea5
KW
116 }
117
b4603d09 118 if (! end_ptr) {
9ec8aea5
KW
119 result = 0.0;
120 }
121
122 return result;
123 }
124
125#endif
126
127}
128
129
98994639 130U32
ddeaf645 131Perl_cast_ulong(NV f)
98994639
HS
132{
133 if (f < 0.0)
134 return f < I32_MIN ? (U32) I32_MIN : (U32)(I32) f;
135 if (f < U32_MAX_P1) {
136#if CASTFLAGS & 2
137 if (f < U32_MAX_P1_HALF)
138 return (U32) f;
139 f -= U32_MAX_P1_HALF;
071db91b 140 return ((U32) f) | (1 + (U32_MAX >> 1));
98994639
HS
141#else
142 return (U32) f;
143#endif
144 }
145 return f > 0 ? U32_MAX : 0 /* NaN */;
146}
147
148I32
ddeaf645 149Perl_cast_i32(NV f)
98994639
HS
150{
151 if (f < I32_MAX_P1)
152 return f < I32_MIN ? I32_MIN : (I32) f;
153 if (f < U32_MAX_P1) {
154#if CASTFLAGS & 2
155 if (f < U32_MAX_P1_HALF)
156 return (I32)(U32) f;
157 f -= U32_MAX_P1_HALF;
071db91b 158 return (I32)(((U32) f) | (1 + (U32_MAX >> 1)));
98994639
HS
159#else
160 return (I32)(U32) f;
161#endif
162 }
163 return f > 0 ? (I32)U32_MAX : 0 /* NaN */;
164}
165
166IV
ddeaf645 167Perl_cast_iv(NV f)
98994639
HS
168{
169 if (f < IV_MAX_P1)
170 return f < IV_MIN ? IV_MIN : (IV) f;
171 if (f < UV_MAX_P1) {
172#if CASTFLAGS & 2
173 /* For future flexibility allowing for sizeof(UV) >= sizeof(IV) */
174 if (f < UV_MAX_P1_HALF)
175 return (IV)(UV) f;
176 f -= UV_MAX_P1_HALF;
071db91b 177 return (IV)(((UV) f) | (1 + (UV_MAX >> 1)));
98994639
HS
178#else
179 return (IV)(UV) f;
180#endif
181 }
182 return f > 0 ? (IV)UV_MAX : 0 /* NaN */;
183}
184
185UV
ddeaf645 186Perl_cast_uv(NV f)
98994639
HS
187{
188 if (f < 0.0)
189 return f < IV_MIN ? (UV) IV_MIN : (UV)(IV) f;
190 if (f < UV_MAX_P1) {
191#if CASTFLAGS & 2
192 if (f < UV_MAX_P1_HALF)
193 return (UV) f;
194 f -= UV_MAX_P1_HALF;
071db91b 195 return ((UV) f) | (1 + (UV_MAX >> 1));
98994639
HS
196#else
197 return (UV) f;
198#endif
199 }
200 return f > 0 ? UV_MAX : 0 /* NaN */;
201}
202
53305cf1
NC
203/*
204=for apidoc grok_bin
98994639 205
53305cf1
NC
206converts a string representing a binary number to numeric form.
207
6974a337
KW
208On entry C<start> and C<*len_p> give the string to scan, C<*flags> gives
209conversion flags, and C<result> should be C<NULL> or a pointer to an NV. The
210scan stops at the end of the string, or at just before the first invalid
211character. Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>,
212encountering an invalid character (except NUL) will also trigger a warning. On
213return C<*len_p> is set to the length of the scanned string, and C<*flags>
214gives output flags.
53305cf1 215
7fc63493 216If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
796b6530
KW
217and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_bin>
218returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
6974a337
KW
219and writes an approximation of the correct value into C<*result> (which is an
220NV; or the approximation is discarded if C<result> is NULL).
53305cf1 221
796b6530 222The binary number may optionally be prefixed with C<"0b"> or C<"b"> unless
6974a337
KW
223C<PERL_SCAN_DISALLOW_PREFIX> is set in C<*flags> on entry.
224
225If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then any or all pairs of
226digits may be separated from each other by a single underscore; also a single
227leading underscore is accepted.
53305cf1 228
ceea512d
KW
229=for apidoc Amnh||PERL_SCAN_ALLOW_UNDERSCORES
230=for apidoc Amnh||PERL_SCAN_DISALLOW_PREFIX
231=for apidoc Amnh||PERL_SCAN_GREATER_THAN_UV_MAX
232=for apidoc Amnh||PERL_SCAN_SILENT_ILLDIGIT
ceea512d 233
53305cf1 234=cut
02470786
KW
235
236Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE
237which suppresses any message for non-portable numbers that are still valid
238on this platform.
53305cf1
NC
239 */
240
241UV
7918f24d
NC
242Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
243{
7918f24d
NC
244 PERL_ARGS_ASSERT_GROK_BIN;
245
bcfb98ec 246 return grok_bin(start, len_p, flags, result);
98994639
HS
247}
248
53305cf1
NC
249/*
250=for apidoc grok_hex
251
252converts a string representing a hex number to numeric form.
253
2d7f6611 254On entry C<start> and C<*len_p> give the string to scan, C<*flags> gives
6974a337
KW
255conversion flags, and C<result> should be C<NULL> or a pointer to an NV. The
256scan stops at the end of the string, or at just before the first invalid
257character. Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>,
258encountering an invalid character (except NUL) will also trigger a warning. On
259return C<*len_p> is set to the length of the scanned string, and C<*flags>
260gives output flags.
53305cf1 261
796b6530
KW
262If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
263and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_hex>
264returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
6974a337
KW
265and writes an approximation of the correct value into C<*result> (which is an
266NV; or the approximation is discarded if C<result> is NULL).
53305cf1 267
796b6530 268The hex number may optionally be prefixed with C<"0x"> or C<"x"> unless
6974a337
KW
269C<PERL_SCAN_DISALLOW_PREFIX> is set in C<*flags> on entry.
270
271If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then any or all pairs of
272digits may be separated from each other by a single underscore; also a single
273leading underscore is accepted.
53305cf1
NC
274
275=cut
02470786 276
2e046c5b 277Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE>
baf48926 278which suppresses any message for non-portable numbers, but which are valid
3f8c4d74 279on this platform. But, C<*flags> will have the corresponding flag bit set.
53305cf1
NC
280 */
281
282UV
7918f24d
NC
283Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
284{
bcfb98ec
KW
285 PERL_ARGS_ASSERT_GROK_HEX;
286
287 return grok_hex(start, len_p, flags, result);
288}
289
d05c9ddb
KW
290/*
291=for apidoc grok_oct
292
293converts a string representing an octal number to numeric form.
294
6974a337
KW
295On entry C<start> and C<*len_p> give the string to scan, C<*flags> gives
296conversion flags, and C<result> should be C<NULL> or a pointer to an NV. The
297scan stops at the end of the string, or at just before the first invalid
298character. Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>,
299encountering an invalid character (except NUL) will also trigger a warning. On
300return C<*len_p> is set to the length of the scanned string, and C<*flags>
301gives output flags.
d05c9ddb
KW
302
303If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
304and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_oct>
305returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
6974a337
KW
306and writes an approximation of the correct value into C<*result> (which is an
307NV; or the approximation is discarded if C<result> is NULL).
308
309If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then any or all pairs of
310digits may be separated from each other by a single underscore; also a single
311leading underscore is accepted.
d05c9ddb 312
a3815e44 313The C<PERL_SCAN_DISALLOW_PREFIX> flag is always treated as being set for
6974a337 314this function.
d05c9ddb
KW
315
316=cut
317
318Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE>
319which suppresses any message for non-portable numbers, but which are valid
320on this platform.
321 */
322
323UV
324Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
325{
326 PERL_ARGS_ASSERT_GROK_OCT;
327
328 return grok_oct(start, len_p, flags, result);
329}
330
c969ff22
KW
331STATIC void
332S_output_non_portable(pTHX_ const U8 base)
333{
334 /* Display the proper message for a number in the given input base not
335 * fitting in 32 bits */
336 const char * which = (base == 2)
337 ? "Binary number > 0b11111111111111111111111111111111"
338 : (base == 8)
339 ? "Octal number > 037777777777"
340 : "Hexadecimal number > 0xffffffff";
341
342 PERL_ARGS_ASSERT_OUTPUT_NON_PORTABLE;
343
344 /* Also there are listings for the other two. That's because, since they
345 * are the first word, it would be hard for a user to find them there
346 * starting with a %s */
347 /* diag_listed_as: Hexadecimal number > 0xffffffff non-portable */
348 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE), "%s non-portable", which);
349}
350
bcfb98ec
KW
351UV
352Perl_grok_bin_oct_hex(pTHX_ const char *start,
353 STRLEN *len_p,
354 I32 *flags,
355 NV *result,
c969ff22 356 const unsigned shift, /* 1 for binary; 3 for octal;
bcfb98ec 357 4 for hex */
c969ff22
KW
358 const U8 class_bit,
359 const char prefix
360 )
361
bcfb98ec 362{
c969ff22
KW
363 const char *s0 = start;
364 const char *s;
53305cf1 365 STRLEN len = *len_p;
c969ff22 366 STRLEN bytes_so_far; /* How many real digits have been processed */
53305cf1
NC
367 UV value = 0;
368 NV value_nv = 0;
c969ff22
KW
369 const PERL_UINT_FAST8_T base = 1 << shift; /* 2, 8, or 16 */
370 const UV max_div= UV_MAX / base; /* Value above which, the next digit
371 processed would overflow */
4b24f703
KW
372 const I32 input_flags = *flags;
373 const bool allow_underscores =
374 cBOOL(input_flags & PERL_SCAN_ALLOW_UNDERSCORES);
c969ff22 375 bool overflowed = FALSE;
cddf31e4
KW
376
377 /* In overflows, this keeps track of how much to multiply the overflowed NV
378 * by as we continue to parse the remaining digits */
efc92487 379 NV factor = 0;
c969ff22
KW
380
381 /* This function unifies the core of grok_bin, grok_oct, and grok_hex. It
382 * is optimized for hex conversion. For example, it uses XDIGIT_VALUE to
383 * find the numeric value of a digit. That requires more instructions than
384 * OCTAL_VALUE would, but gives the same result for the narrowed range of
385 * octal digits; same for binary. If it were ever critical to squeeze more
386 * performance from this, the function could become grok_hex, and a regen
387 * perl script could scan it and write out two edited copies for the other
388 * two functions. That would improve the performance of all three
389 * somewhat. Besides eliminating XDIGIT_VALUE for the other two, extra
390 * parameters are now passed to this to avoid conditionals. Those could
391 * become declared consts, like:
392 * const U8 base = 16;
393 * const U8 base = 8;
394 * ...
395 */
98994639 396
bcfb98ec
KW
397 PERL_ARGS_ASSERT_GROK_BIN_OCT_HEX;
398
399 ASSUME(inRANGE(shift, 1, 4) && shift != 2);
7918f24d 400
4b24f703
KW
401 /* Clear output flags; unlikely to find a problem that sets them */
402 *flags = 0;
403
c969ff22 404 if (!(input_flags & PERL_SCAN_DISALLOW_PREFIX)) {
bcfb98ec
KW
405
406 /* strip off leading b or 0b; x or 0x.
407 for compatibility silently suffer "b" and "0b" as valid binary; "x"
408 and "0x" as valid hex numbers. */
a4c04bdc 409 if (len >= 1) {
c969ff22
KW
410 if (isALPHA_FOLD_EQ(s0[0], prefix)) {
411 s0++;
a4c04bdc
NC
412 len--;
413 }
c969ff22
KW
414 else if (len >= 2 && s0[0] == '0' && (isALPHA_FOLD_EQ(s0[1], prefix))) {
415 s0+=2;
a4c04bdc
NC
416 len-=2;
417 }
418 }
98994639
HS
419 }
420
c969ff22
KW
421 s = s0; /* s0 potentially advanced from 'start' */
422
b2fffc9a 423 /* Unroll the loop so that the first 8 digits are branchless except for the
b9abeb0c 424 * switch. A ninth hex one overflows a 32 bit word. */
c969ff22
KW
425 switch (len) {
426 case 0:
427 return 0;
428 default:
6eb62d23 429 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
430 value = (value << shift) | XDIGIT_VALUE(*s);
431 s++;
432 /* FALLTHROUGH */
b2fffc9a 433 case 7:
6eb62d23 434 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
b2fffc9a
KW
435 value = (value << shift) | XDIGIT_VALUE(*s);
436 s++;
437 /* FALLTHROUGH */
c969ff22 438 case 6:
6eb62d23 439 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
440 value = (value << shift) | XDIGIT_VALUE(*s);
441 s++;
442 /* FALLTHROUGH */
443 case 5:
6eb62d23 444 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
445 value = (value << shift) | XDIGIT_VALUE(*s);
446 s++;
447 /* FALLTHROUGH */
448 case 4:
6eb62d23 449 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
450 value = (value << shift) | XDIGIT_VALUE(*s);
451 s++;
452 /* FALLTHROUGH */
453 case 3:
6eb62d23 454 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
455 value = (value << shift) | XDIGIT_VALUE(*s);
456 s++;
457 /* FALLTHROUGH */
458 case 2:
6eb62d23 459 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
460 value = (value << shift) | XDIGIT_VALUE(*s);
461 s++;
462 /* FALLTHROUGH */
463 case 1:
6eb62d23 464 if (UNLIKELY(! generic_isCC_(*s, class_bit))) break;
c969ff22
KW
465 value = (value << shift) | XDIGIT_VALUE(*s);
466
b2fffc9a 467 if (LIKELY(len <= 8)) {
c969ff22
KW
468 return value;
469 }
470
471 s++;
472 break;
473 }
474
475 bytes_so_far = s - s0;
476 factor = shift << bytes_so_far;
477 len -= bytes_so_far;
478
479 for (; len--; s++) {
6eb62d23 480 if (generic_isCC_(*s, class_bit)) {
53305cf1
NC
481 /* Write it in this wonky order with a goto to attempt to get the
482 compiler to make the common case integer-only loop pretty tight.
35e5392d
KW
483 With gcc seems to be much straighter code than old scan_hex.
484 (khw suspects that adding a LIKELY() just above would do the
485 same thing) */
53305cf1 486 redo:
ebf83045
KW
487 if (LIKELY(value <= max_div)) {
488 value = (value << shift) | XDIGIT_VALUE(*s);
489 /* Note XDIGIT_VALUE() is branchless, works on binary
490 * and octal as well, so can be used here, without
491 * slowing those down */
efc92487 492 factor *= 1 << shift;
ebf83045
KW
493 continue;
494 }
cddf31e4
KW
495
496 /* Bah. We are about to overflow. Instead, add the unoverflowed
497 * value to an NV that contains an approximation to the correct
498 * value. Each time through the loop we have increased 'factor' so
499 * that it gives how much the current approximation needs to
500 * effectively be shifted to make room for this new value */
efc92487 501 value_nv *= factor;
cddf31e4
KW
502 value_nv += (NV) value;
503
504 /* Then we keep accumulating digits, until all are parsed. We
505 * start over using the current input value. This will be added to
506 * 'value_nv' eventually, either when all digits are gone, or we
507 * have overflowed this fresh start. */
508 value = XDIGIT_VALUE(*s);
509 factor = 1 << shift;
510
c969ff22
KW
511 if (! overflowed) {
512 overflowed = TRUE;
99a25d63
KW
513 if ( ! (input_flags & PERL_SCAN_SILENT_OVERFLOW)
514 && ckWARN_d(WARN_OVERFLOW))
515 {
516 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
bcfb98ec
KW
517 "Integer overflow in %s number",
518 (base == 16) ? "hexadecimal"
519 : (base == 2)
520 ? "binary"
521 : "octal");
99a25d63 522 }
53305cf1 523 }
53305cf1
NC
524 continue;
525 }
ebf83045 526
bcfb98ec
KW
527 if ( *s == '_'
528 && len
529 && allow_underscores
6eb62d23 530 && generic_isCC_(s[1], class_bit)
99a25d63
KW
531
532 /* Don't allow a leading underscore if the only-medial bit is
533 * set */
534 && ( LIKELY(s > s0)
535 || UNLIKELY((input_flags & PERL_SCAN_ALLOW_MEDIAL_UNDERSCORES)
536 != PERL_SCAN_ALLOW_MEDIAL_UNDERSCORES)))
bcfb98ec
KW
537 {
538 --len;
539 ++s;
540 goto redo;
541 }
ebf83045 542
3f8c4d74
KW
543 if (*s) {
544 if ( ! (input_flags & PERL_SCAN_SILENT_ILLDIGIT)
17d6187b
KW
545 && ckWARN(WARN_DIGIT))
546 {
547 if (base != 8) {
548 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
549 "Illegal %s digit '%c' ignored",
550 ((base == 2)
551 ? "binary"
552 : "hexadecimal"),
553 *s);
554 }
555 else if (isDIGIT(*s)) { /* octal base */
556
557 /* Allow \octal to work the DWIM way (that is, stop
558 * scanning as soon as non-octal characters are seen,
559 * complain only if someone seems to want to use the digits
560 * eight and nine. Since we know it is not octal, then if
561 * isDIGIT, must be an 8 or 9). */
562 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
bcfb98ec 563 "Illegal octal digit '%c' ignored", *s);
3f8c4d74
KW
564 }
565 }
566
567 if (input_flags & PERL_SCAN_NOTIFY_ILLDIGIT) {
568 *flags |= PERL_SCAN_NOTIFY_ILLDIGIT;
bcfb98ec
KW
569 }
570 }
ebf83045 571
53305cf1
NC
572 break;
573 }
19c1206d 574
c969ff22 575 *len_p = s - start;
cddf31e4 576
c969ff22 577 if (LIKELY(! overflowed)) {
53305cf1 578#if UVSIZE > 4
c969ff22 579 if ( UNLIKELY(value > 0xffffffff)
4b24f703 580 && ! (input_flags & PERL_SCAN_SILENT_NON_PORTABLE))
c969ff22
KW
581 {
582 output_non_portable(base);
3f8c4d74 583 *flags |= PERL_SCAN_SILENT_NON_PORTABLE;
c969ff22 584 }
53305cf1 585#endif
c969ff22 586 return value;
53305cf1 587 }
bcfb98ec 588
c969ff22 589 /* Overflowed: Calculate the final overflow approximation */
efc92487 590 value_nv *= factor;
c969ff22 591 value_nv += (NV) value;
cddf31e4 592
c969ff22 593 output_non_portable(base);
ebf83045 594
3f8c4d74
KW
595 *flags |= PERL_SCAN_GREATER_THAN_UV_MAX
596 | PERL_SCAN_SILENT_NON_PORTABLE;
53305cf1
NC
597 if (result)
598 *result = value_nv;
599 return UV_MAX;
600}
601
602/*
53305cf1
NC
603=for apidoc scan_bin
604
72d33970 605For backwards compatibility. Use C<grok_bin> instead.
53305cf1
NC
606
607=for apidoc scan_hex
608
72d33970 609For backwards compatibility. Use C<grok_hex> instead.
53305cf1
NC
610
611=for apidoc scan_oct
612
72d33970 613For backwards compatibility. Use C<grok_oct> instead.
53305cf1
NC
614
615=cut
616 */
617
618NV
73d840c0 619Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
53305cf1
NC
620{
621 NV rnv;
622 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
73d840c0 623 const UV ruv = grok_bin (start, &len, &flags, &rnv);
53305cf1 624
7918f24d
NC
625 PERL_ARGS_ASSERT_SCAN_BIN;
626
53305cf1
NC
627 *retlen = len;
628 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
629}
630
631NV
73d840c0 632Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
53305cf1
NC
633{
634 NV rnv;
635 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
73d840c0 636 const UV ruv = grok_oct (start, &len, &flags, &rnv);
53305cf1 637
7918f24d
NC
638 PERL_ARGS_ASSERT_SCAN_OCT;
639
53305cf1
NC
640 *retlen = len;
641 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
642}
643
644NV
73d840c0 645Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
53305cf1
NC
646{
647 NV rnv;
648 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
73d840c0 649 const UV ruv = grok_hex (start, &len, &flags, &rnv);
53305cf1 650
7918f24d
NC
651 PERL_ARGS_ASSERT_SCAN_HEX;
652
53305cf1
NC
653 *retlen = len;
654 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
98994639
HS
655}
656
657/*
658=for apidoc grok_numeric_radix
659
660Scan and skip for a numeric decimal separator (radix).
661
662=cut
663 */
664bool
665Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
666{
7918f24d
NC
667 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
668
7ea85fa8
KW
669#ifdef USE_LOCALE_NUMERIC
670
d6ded950 671 if (IN_LC(LC_NUMERIC)) {
f0dafd73
KW
672 STRLEN len;
673 char * radix;
674 bool matches_radix = FALSE;
67d796ae 675 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
f0dafd73 676
a1395eaf 677 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
f0dafd73
KW
678
679 radix = SvPV(PL_numeric_radix_sv, len);
680 radix = savepvn(radix, len);
681
21431899 682 RESTORE_LC_NUMERIC();
f0dafd73
KW
683
684 if (*sp + len <= send) {
685 matches_radix = memEQ(*sp, radix, len);
686 }
687
688 Safefree(radix);
689
690 if (matches_radix) {
691 *sp += len;
692 return TRUE;
693 }
98994639 694 }
f0dafd73 695
98994639 696#endif
7918f24d 697
f0dafd73
KW
698 /* always try "." if numeric radix didn't match because
699 * we may have data from different locales mixed */
98994639
HS
700 if (*sp < send && **sp == '.') {
701 ++*sp;
702 return TRUE;
703 }
f0dafd73 704
98994639
HS
705 return FALSE;
706}
707
569f27e5 708/*
ff4eb398
JH
709=for apidoc grok_infnan
710
796b6530 711Helper for C<grok_number()>, accepts various ways of spelling "infinity"
ff4eb398
JH
712or "not a number", and returns one of the following flag combinations:
713
5962c2f6 714 IS_NUMBER_INFINITY
ff4eb398 715 IS_NUMBER_NAN
5962c2f6 716 IS_NUMBER_INFINITY | IS_NUMBER_NEG
ff4eb398
JH
717 IS_NUMBER_NAN | IS_NUMBER_NEG
718 0
719
796b6530 720possibly |-ed with C<IS_NUMBER_TRAILING>.
b489e20f 721
796b6530 722If an infinity or a not-a-number is recognized, C<*sp> will point to
62bdc035 723one byte past the end of the recognized string. If the recognition fails,
796b6530 724zero is returned, and C<*sp> will not move.
ff4eb398 725
44eb6c28
KW
726=for apidoc Amnh|bool|IS_NUMBER_GREATER_THAN_UV_MAX
727=for apidoc Amnh|bool|IS_NUMBER_INFINITY
728=for apidoc Amnh|bool|IS_NUMBER_IN_UV
729=for apidoc Amnh|bool|IS_NUMBER_NAN
730=for apidoc Amnh|bool|IS_NUMBER_NEG
731=for apidoc Amnh|bool|IS_NUMBER_NOT_INT
ceea512d 732
ff4eb398
JH
733=cut
734*/
735
736int
3823048b 737Perl_grok_infnan(pTHX_ const char** sp, const char* send)
ff4eb398
JH
738{
739 const char* s = *sp;
740 int flags = 0;
a5dc2484 741#if defined(NV_INF) || defined(NV_NAN)
62bdc035 742 bool odh = FALSE; /* one-dot-hash: 1.#INF */
ff4eb398
JH
743
744 PERL_ARGS_ASSERT_GROK_INFNAN;
745
8c12dc63
JH
746 if (*s == '+') {
747 s++; if (s == send) return 0;
748 }
749 else if (*s == '-') {
ff4eb398
JH
750 flags |= IS_NUMBER_NEG; /* Yes, -NaN happens. Incorrect but happens. */
751 s++; if (s == send) return 0;
752 }
753
754 if (*s == '1') {
62bdc035
JH
755 /* Visual C: 1.#SNAN, -1.#QNAN, 1#INF, 1.#IND (maybe also 1.#NAN)
756 * Let's keep the dot optional. */
ff4eb398
JH
757 s++; if (s == send) return 0;
758 if (*s == '.') {
759 s++; if (s == send) return 0;
760 }
761 if (*s == '#') {
762 s++; if (s == send) return 0;
763 } else
764 return 0;
e855f543 765 odh = TRUE;
ff4eb398
JH
766 }
767
305b8651 768 if (isALPHA_FOLD_EQ(*s, 'I')) {
62bdc035
JH
769 /* INF or IND (1.#IND is "indeterminate", a certain type of NAN) */
770
305b8651 771 s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return 0;
ff4eb398 772 s++; if (s == send) return 0;
305b8651 773 if (isALPHA_FOLD_EQ(*s, 'F')) {
7294e9f9
HS
774 flags |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
775 *sp = ++s;
b8974fcb 776 if (s < send && (isALPHA_FOLD_EQ(*s, 'I'))) {
7294e9f9
HS
777 int trail = flags | IS_NUMBER_TRAILING;
778 s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return trail;
779 s++; if (s == send || isALPHA_FOLD_NE(*s, 'I')) return trail;
780 s++; if (s == send || isALPHA_FOLD_NE(*s, 'T')) return trail;
781 s++; if (s == send || isALPHA_FOLD_NE(*s, 'Y')) return trail;
782 *sp = ++s;
b8974fcb 783 } else if (odh) {
bbd86075 784 while (s < send && *s == '0') { /* 1.#INF00 */
b8974fcb
JH
785 s++;
786 }
3396ed30 787 }
6e5a616f 788 goto ok_check_space;
ff4eb398 789 }
e855f543 790 else if (isALPHA_FOLD_EQ(*s, 'D') && odh) { /* 1.#IND */
ff4eb398
JH
791 s++;
792 flags |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
bbd86075 793 while (s < send && *s == '0') { /* 1.#IND00 */
fae4db12
JH
794 s++;
795 }
6e5a616f 796 goto ok_check_space;
ff4eb398
JH
797 } else
798 return 0;
ff4eb398
JH
799 }
800 else {
62bdc035 801 /* Maybe NAN of some sort */
3823048b
JH
802
803 if (isALPHA_FOLD_EQ(*s, 'S') || isALPHA_FOLD_EQ(*s, 'Q')) {
804 /* snan, qNaN */
805 /* XXX do something with the snan/qnan difference */
806 s++; if (s == send) return 0;
807 }
808
809 if (isALPHA_FOLD_EQ(*s, 'N')) {
810 s++; if (s == send || isALPHA_FOLD_NE(*s, 'A')) return 0;
811 s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return 0;
3823048b 812 flags |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
7294e9f9
HS
813 *sp = ++s;
814
dca9f615
KW
815 if (s == send) {
816 return flags;
817 }
3823048b
JH
818
819 /* NaN can be followed by various stuff (NaNQ, NaNS), but
820 * there are also multiple different NaN values, and some
821 * implementations output the "payload" values,
822 * e.g. NaN123, NAN(abc), while some legacy implementations
823 * have weird stuff like NaN%. */
824 if (isALPHA_FOLD_EQ(*s, 'q') ||
825 isALPHA_FOLD_EQ(*s, 's')) {
826 /* "nanq" or "nans" are ok, though generating
827 * these portably is tricky. */
7294e9f9 828 *sp = ++s;
81d11450
KW
829 if (s == send) {
830 return flags;
831 }
3823048b
JH
832 }
833 if (*s == '(') {
834 /* C99 style "nan(123)" or Perlish equivalent "nan($uv)". */
835 const char *t;
7294e9f9 836 int trail = flags | IS_NUMBER_TRAILING;
3823048b 837 s++;
7294e9f9 838 if (s == send) { return trail; }
3823048b
JH
839 t = s + 1;
840 while (t < send && *t && *t != ')') {
841 t++;
842 }
7294e9f9 843 if (t == send) { return trail; }
3823048b
JH
844 if (*t == ')') {
845 int nantype;
846 UV nanval;
847 if (s[0] == '0' && s + 2 < t &&
848 isALPHA_FOLD_EQ(s[1], 'x') &&
849 isXDIGIT(s[2])) {
850 STRLEN len = t - s;
851 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
852 nanval = grok_hex(s, &len, &flags, NULL);
853 if ((flags & PERL_SCAN_GREATER_THAN_UV_MAX)) {
854 nantype = 0;
855 } else {
856 nantype = IS_NUMBER_IN_UV;
857 }
858 s += len;
859 } else if (s[0] == '0' && s + 2 < t &&
860 isALPHA_FOLD_EQ(s[1], 'b') &&
861 (s[2] == '0' || s[2] == '1')) {
862 STRLEN len = t - s;
863 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
864 nanval = grok_bin(s, &len, &flags, NULL);
865 if ((flags & PERL_SCAN_GREATER_THAN_UV_MAX)) {
866 nantype = 0;
867 } else {
868 nantype = IS_NUMBER_IN_UV;
869 }
870 s += len;
871 } else {
872 const char *u;
873 nantype =
874 grok_number_flags(s, t - s, &nanval,
875 PERL_SCAN_TRAILING |
876 PERL_SCAN_ALLOW_UNDERSCORES);
877 /* Unfortunately grok_number_flags() doesn't
878 * tell how far we got and the ')' will always
879 * be "trailing", so we need to double-check
880 * whether we had something dubious. */
881 for (u = s; u < t; u++) {
6e5a616f 882 if (!isDIGIT(*u))
3823048b 883 break;
3823048b
JH
884 }
885 s = u;
886 }
887
888 /* XXX Doesn't do octal: nan("0123").
889 * Probably not a big loss. */
890
7294e9f9
HS
891 /* XXX the nanval is currently unused, that is,
892 * not inserted as the NaN payload of the NV.
893 * But the above code already parses the C99
894 * nan(...) format. See below, and see also
895 * the nan() in POSIX.xs.
896 *
897 * Certain configuration combinations where
898 * NVSIZE is greater than UVSIZE mean that
899 * a single UV cannot contain all the possible
900 * NaN payload bits. There would need to be
901 * some more generic syntax than "nan($uv)".
902 *
903 * Issues to keep in mind:
904 *
905 * (1) In most common cases there would
906 * not be an integral number of bytes that
907 * could be set, only a certain number of bits.
908 * For example for the common case of
909 * NVSIZE == UVSIZE == 8 there is room for 52
910 * bits in the payload, but the most significant
911 * bit is commonly reserved for the
912 * signaling/quiet bit, leaving 51 bits.
913 * Furthermore, the C99 nan() is supposed
914 * to generate quiet NaNs, so it is doubtful
915 * whether it should be able to generate
916 * signaling NaNs. For the x86 80-bit doubles
917 * (if building a long double Perl) there would
918 * be 62 bits (s/q bit being the 63rd).
919 *
920 * (2) Endianness of the payload bits. If the
921 * payload is specified as an UV, the low-order
922 * bits of the UV are naturally little-endianed
923 * (rightmost) bits of the payload. The endianness
924 * of UVs and NVs can be different. */
925
3823048b
JH
926 if ((nantype & IS_NUMBER_NOT_INT) ||
927 !(nantype && IS_NUMBER_IN_UV)) {
7294e9f9
HS
928 /* treat "NaN(invalid)" the same as "NaNgarbage" */
929 return trail;
3823048b 930 }
7294e9f9 931 else {
6e5a616f
HS
932 /* allow whitespace between valid payload and ')' */
933 while (s < t && isSPACE(*s))
934 s++;
935 /* but on anything else treat the whole '(...)' chunk
936 * as trailing garbage */
937 if (s < t)
938 return trail;
939 s = t + 1;
940 goto ok_check_space;
3823048b
JH
941 }
942 } else {
943 /* Looked like nan(...), but no close paren. */
7294e9f9 944 return trail;
3823048b
JH
945 }
946 } else {
7294e9f9
HS
947 /* Note that we here implicitly accept (parse as
948 * "nan", but with warnings) also any other weird
949 * trailing stuff for "nan". In the above we just
950 * check that if we got the C99-style "nan(...)",
951 * the "..." looks sane.
952 * If in future we accept more ways of specifying
953 * the nan payload, the accepting would happen around
954 * here. */
6e5a616f 955 goto ok_check_space;
3823048b 956 }
3823048b
JH
957 }
958 else
959 return 0;
ff4eb398 960 }
1bd2ffc2 961 NOT_REACHED; /* NOTREACHED */
6e5a616f
HS
962
963 /* We parsed something valid, s points after it, flags describes it */
964 ok_check_space:
965 while (s < send && isSPACE(*s))
966 s++;
967 *sp = s;
968 return flags | (s < send ? IS_NUMBER_TRAILING : 0);
ff4eb398 969
a5dc2484
JH
970#else
971 PERL_UNUSED_ARG(send);
a1fe7cea
JH
972 *sp = s;
973 return flags;
7294e9f9 974#endif /* #if defined(NV_INF) || defined(NV_NAN) */
ff4eb398
JH
975}
976
13393a5e 977/*
3823048b 978=for apidoc grok_number_flags
13393a5e
JH
979
980Recognise (or not) a number. The type of the number is returned
981(0 if unrecognised), otherwise it is a bit-ORed combination of
796b6530
KW
982C<IS_NUMBER_IN_UV>, C<IS_NUMBER_GREATER_THAN_UV_MAX>, C<IS_NUMBER_NOT_INT>,
983C<IS_NUMBER_NEG>, C<IS_NUMBER_INFINITY>, C<IS_NUMBER_NAN> (defined in perl.h).
984
985If the value of the number can fit in a UV, it is returned in C<*valuep>.
986C<IS_NUMBER_IN_UV> will be set to indicate that C<*valuep> is valid, C<IS_NUMBER_IN_UV>
987will never be set unless C<*valuep> is valid, but C<*valuep> may have been assigned
988to during processing even though C<IS_NUMBER_IN_UV> is not set on return.
989If C<valuep> is C<NULL>, C<IS_NUMBER_IN_UV> will be set for the same cases as when
990C<valuep> is non-C<NULL>, but no actual assignment (or SEGV) will occur.
991
992C<IS_NUMBER_NOT_INT> will be set with C<IS_NUMBER_IN_UV> if trailing decimals were
993seen (in which case C<*valuep> gives the true value truncated to an integer), and
994C<IS_NUMBER_NEG> if the number is negative (in which case C<*valuep> holds the
d0f6d176 995absolute value). C<IS_NUMBER_IN_UV> is not set if C<e> notation was used or the
13393a5e
JH
996number is larger than a UV.
997
998C<flags> allows only C<PERL_SCAN_TRAILING>, which allows for trailing
999non-numeric text on an otherwise successful I<grok>, setting
1000C<IS_NUMBER_TRAILING> on the result.
1001
0b929024
KW
1002=for apidoc Amnh||PERL_SCAN_TRAILING
1003
13393a5e
JH
1004=for apidoc grok_number
1005
796b6530 1006Identical to C<grok_number_flags()> with C<flags> set to zero.
13393a5e
JH
1007
1008=cut
1009 */
1010int
1011Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
1012{
1013 PERL_ARGS_ASSERT_GROK_NUMBER;
1014
1015 return grok_number_flags(pv, len, valuep, 0);
1016}
1017
945b524a
JH
1018static const UV uv_max_div_10 = UV_MAX / 10;
1019static const U8 uv_max_mod_10 = UV_MAX % 10;
1020
3f7602fa 1021int
3823048b 1022Perl_grok_number_flags(pTHX_ const char *pv, STRLEN len, UV *valuep, U32 flags)
3f7602fa 1023{
60939fb8 1024 const char *s = pv;
c4420975 1025 const char * const send = pv + len;
ae776a2c 1026 const char *d;
60939fb8 1027 int numtype = 0;
60939fb8 1028
3823048b 1029 PERL_ARGS_ASSERT_GROK_NUMBER_FLAGS;
7918f24d 1030
129ccace
KW
1031 if (UNLIKELY(isSPACE(*s))) {
1032 s++;
1033 while (s < send) {
1034 if (LIKELY(! isSPACE(*s))) goto non_space;
1035 s++;
1036 }
1037 return 0;
1038 non_space: ;
60939fb8 1039 }
60939fb8 1040
129ccace
KW
1041 /* See if signed. This assumes it is more likely to be unsigned, so
1042 * penalizes signed by an extra conditional; rewarding unsigned by one fewer
1043 * (because we detect '+' and '-' with a single test and then add a
1044 * conditional to determine which) */
1045 if (UNLIKELY((*s & ~('+' ^ '-')) == ('+' & '-') )) {
1046
1047 /* Here, on ASCII platforms, *s is one of: 0x29 = ')', 2B = '+', 2D = '-',
1048 * 2F = '/'. That is, it is either a sign, or a character that doesn't
1049 * belong in a number at all (unless it's a radix character in a weird
1050 * locale). Given this, it's far more likely to be a minus than the
1051 * others. (On EBCDIC it is one of 42, 44, 46, 48, 4A, 4C, 4E, (not 40
17d6187b
KW
1052 * because can't be a space) 60, 62, 64, 66, 68, 6A, 6C, 6E. Again,
1053 * only potentially a weird radix character, or 4E='+', or 60='-') */
129ccace
KW
1054 if (LIKELY(*s == '-')) {
1055 s++;
1056 numtype = IS_NUMBER_NEG;
1057 }
1058 else if (LIKELY(*s == '+'))
1059 s++;
1060 else /* Can't just return failure here, as it could be a weird radix
1061 character */
1062 goto done_sign;
1063
1064 if (UNLIKELY(s == send))
1065 return 0;
1066 done_sign: ;
1067 }
60939fb8 1068
ae776a2c 1069 /* The first digit (after optional sign): note that might
8c12dc63 1070 * also point to "infinity" or "nan", or "1.#INF". */
ae776a2c
JH
1071 d = s;
1072
8c12dc63 1073 /* next must be digit or the radix separator or beginning of infinity/nan */
129ccace 1074 if (LIKELY(isDIGIT(*s))) {
60939fb8
NC
1075 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
1076 overflow. */
129ccace
KW
1077 UV value = *s - '0'; /* Process this first (perhaps only) digit */
1078 int digit;
1079
1080 s++;
1081
1082 switch(send - s) {
1083 default: /* 8 or more remaining characters */
1084 digit = *s - '0';
1085 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1086 value = value * 10 + digit;
1087 s++;
1088 /* FALLTHROUGH */
1089 case 7:
1090 digit = *s - '0';
1091 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1092 value = value * 10 + digit;
1093 s++;
1094 /* FALLTHROUGH */
1095 case 6:
1096 digit = *s - '0';
1097 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1098 value = value * 10 + digit;
1099 s++;
1100 /* FALLTHROUGH */
1101 case 5:
1102 digit = *s - '0';
1103 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1104 value = value * 10 + digit;
1105 s++;
1106 /* FALLTHROUGH */
1107 case 4:
1108 digit = *s - '0';
1109 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1110 value = value * 10 + digit;
1111 s++;
1112 /* FALLTHROUGH */
1113 case 3:
1114 digit = *s - '0';
1115 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1116 value = value * 10 + digit;
1117 s++;
1118 /* FALLTHROUGH */
1119 case 2:
1120 digit = *s - '0';
1121 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
1122 value = value * 10 + digit;
1123 s++;
1124 /* FALLTHROUGH */
1125 case 1:
1126 digit = *s - '0';
1127 if (UNLIKELY(! inRANGE(digit, 0, 9))) break;
60939fb8 1128 value = value * 10 + digit;
129ccace
KW
1129 s++;
1130 /* FALLTHROUGH */
1131 case 0: /* This case means the string consists of just the one
1132 digit we already have processed */
1133
1134 /* If we got here by falling through other than the default: case, we
1135 * have processed the whole string, and know it consists entirely of
1136 * digits, and can't have overflowed. */
1137 if (s >= send) {
1138 if (valuep)
1139 *valuep = value;
1140 return numtype|IS_NUMBER_IN_UV;
1141 }
1142
1143 /* Here, there are extra characters beyond the first 9 digits. Use a
1144 * loop to accumulate any remaining digits, until we get a non-digit or
1145 * would overflow. Note that leading zeros could cause us to get here
1146 * without being close to overflowing.
1147 *
1148 * (The conditional 's >= send' above could be eliminated by making the
1149 * default: in the switch to instead be 'case 8:', and process longer
1150 * strings separately by using the loop below. This would penalize
1151 * these inputs by the extra instructions needed for looping. That
1152 * could be eliminated by copying the unwound code from above to handle
1153 * the firt 9 digits of these. khw didn't think this saving of a
1154 * single conditional was worth it.) */
1155 do {
1156 digit = *s - '0';
1157 if (! inRANGE(digit, 0, 9)) goto mantissa_done;
1158 if ( value < uv_max_div_10
1159 || ( value == uv_max_div_10
1160 && digit <= uv_max_mod_10))
1161 {
60939fb8 1162 value = value * 10 + digit;
129ccace 1163 s++;
60939fb8 1164 }
129ccace
KW
1165 else { /* value would overflow. skip the remaining digits, don't
1166 worry about setting *valuep. */
1167 do {
1168 s++;
1169 } while (s < send && isDIGIT(*s));
1170 numtype |=
1171 IS_NUMBER_GREATER_THAN_UV_MAX;
1172 goto skip_value;
1173 }
1174 } while (s < send);
1175 } /* End switch on input length */
1176
1177 mantissa_done:
60939fb8
NC
1178 numtype |= IS_NUMBER_IN_UV;
1179 if (valuep)
1180 *valuep = value;
1181
1182 skip_value:
1183 if (GROK_NUMERIC_RADIX(&s, send)) {
1184 numtype |= IS_NUMBER_NOT_INT;
1185 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
1186 s++;
98994639 1187 }
129ccace 1188 } /* End of *s is a digit */
60939fb8
NC
1189 else if (GROK_NUMERIC_RADIX(&s, send)) {
1190 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
1191 /* no digits before the radix means we need digits after it */
1192 if (s < send && isDIGIT(*s)) {
1193 do {
1194 s++;
1195 } while (s < send && isDIGIT(*s));
1196 if (valuep) {
1197 /* integer approximation is valid - it's 0. */
1198 *valuep = 0;
1199 }
98994639 1200 }
60939fb8 1201 else
ae776a2c 1202 return 0;
ff4eb398 1203 }
60939fb8 1204
129ccace 1205 if (LIKELY(s > d) && s < send) {
60939fb8 1206 /* we can have an optional exponent part */
129ccace 1207 if (UNLIKELY(isALPHA_FOLD_EQ(*s, 'e'))) {
60939fb8
NC
1208 s++;
1209 if (s < send && (*s == '-' || *s == '+'))
1210 s++;
1211 if (s < send && isDIGIT(*s)) {
1212 do {
1213 s++;
1214 } while (s < send && isDIGIT(*s));
1215 }
3f7602fa
TC
1216 else if (flags & PERL_SCAN_TRAILING)
1217 return numtype | IS_NUMBER_TRAILING;
60939fb8 1218 else
3f7602fa
TC
1219 return 0;
1220
1221 /* The only flag we keep is sign. Blow away any "it's UV" */
1222 numtype &= IS_NUMBER_NEG;
1223 numtype |= IS_NUMBER_NOT_INT;
60939fb8
NC
1224 }
1225 }
129ccace
KW
1226
1227 while (s < send) {
1228 if (LIKELY(! isSPACE(*s))) goto end_space;
60939fb8 1229 s++;
129ccace
KW
1230 }
1231 return numtype;
1232
1233 end_space:
1234
1235 if (UNLIKELY(memEQs(pv, len, "0 but true"))) {
60939fb8
NC
1236 if (valuep)
1237 *valuep = 0;
1238 return IS_NUMBER_IN_UV;
1239 }
129ccace 1240
8c12dc63 1241 /* We could be e.g. at "Inf" or "NaN", or at the "#" of "1.#INF". */
129ccace 1242 if ((s + 2 < send) && UNLIKELY(memCHRs("inqs#", toFOLD(*s)))) {
8c12dc63
JH
1243 /* Really detect inf/nan. Start at d, not s, since the above
1244 * code might have already consumed the "1." or "1". */
7eff3d39 1245 const int infnan = Perl_grok_infnan(aTHX_ &d, send);
7294e9f9
HS
1246
1247 if ((infnan & IS_NUMBER_TRAILING) && !(flags & PERL_SCAN_TRAILING)) {
1248 return 0;
1249 }
8c12dc63
JH
1250 if ((infnan & IS_NUMBER_INFINITY)) {
1251 return (numtype | infnan); /* Keep sign for infinity. */
1252 }
1253 else if ((infnan & IS_NUMBER_NAN)) {
1254 return (numtype | infnan) & ~IS_NUMBER_NEG; /* Clear sign for nan. */
1255 }
1256 }
3f7602fa
TC
1257 else if (flags & PERL_SCAN_TRAILING) {
1258 return numtype | IS_NUMBER_TRAILING;
1259 }
1260
60939fb8 1261 return 0;
98994639
HS
1262}
1263
6313e544 1264/*
5d4a52b5 1265=for apidoc grok_atoUV
6313e544 1266
5d4a52b5 1267parse a string, looking for a decimal unsigned integer.
338aa8b0 1268
5d4a52b5
KW
1269On entry, C<pv> points to the beginning of the string;
1270C<valptr> points to a UV that will receive the converted value, if found;
1271C<endptr> is either NULL or points to a variable that points to one byte
1272beyond the point in C<pv> that this routine should examine.
1273If C<endptr> is NULL, C<pv> is assumed to be NUL-terminated.
f4379102 1274
5d4a52b5
KW
1275Returns FALSE if C<pv> doesn't represent a valid unsigned integer value (with
1276no leading zeros). Otherwise it returns TRUE, and sets C<*valptr> to that
1277value.
6313e544 1278
5d4a52b5
KW
1279If you constrain the portion of C<pv> that is looked at by this function (by
1280passing a non-NULL C<endptr>), and if the intial bytes of that portion form a
1281valid value, it will return TRUE, setting C<*endptr> to the byte following the
1282final digit of the value. But if there is no constraint at what's looked at,
b9abeb0c
KW
1283all of C<pv> must be valid in order for TRUE to be returned. C<*endptr> is
1284unchanged from its value on input if FALSE is returned;
6313e544 1285
5d4a52b5 1286The only characters this accepts are the decimal digits '0'..'9'.
338aa8b0 1287
5d4a52b5
KW
1288As opposed to L<atoi(3)> or L<strtol(3)>, C<grok_atoUV> does NOT allow optional
1289leading whitespace, nor negative inputs. If such features are required, the
1290calling code needs to explicitly implement those.
6313e544 1291
5d4a52b5
KW
1292Note that this function returns FALSE for inputs that would overflow a UV,
1293or have leading zeros. Thus a single C<0> is accepted, but not C<00> nor
1294C<01>, C<002>, I<etc>.
1295
1296Background: C<atoi> has severe problems with illegal inputs, it cannot be
d62b8c6a 1297used for incremental parsing, and therefore should be avoided
5d4a52b5 1298C<atoi> and C<strtol> are also affected by locale settings, which can also be
d62b8c6a
JH
1299seen as a bug (global state controlled by user environment).
1300
238217e5
JK
1301=cut
1302
6313e544
JH
1303*/
1304
22ff3130
HS
1305bool
1306Perl_grok_atoUV(const char *pv, UV *valptr, const char** endptr)
6313e544
JH
1307{
1308 const char* s = pv;
1309 const char** eptr;
1310 const char* end2; /* Used in case endptr is NULL. */
22ff3130 1311 UV val = 0; /* The parsed value. */
6313e544 1312
22ff3130 1313 PERL_ARGS_ASSERT_GROK_ATOUV;
6313e544 1314
5d4a52b5
KW
1315 if (endptr) {
1316 eptr = endptr;
1317 }
1318 else {
1319 end2 = s + strlen(s);
1320 eptr = &end2;
1321 }
1322
1323 if ( *eptr <= s
1324 || ! isDIGIT(*s))
1325 {
1326 return FALSE;
1327 }
1328
97d95d46
KW
1329 /* Single-digit inputs are quite common. */
1330 val = *s++ - '0';
1331 if (s < *eptr && isDIGIT(*s)) {
1332 /* Fail on extra leading zeros. */
1333 if (val == 0)
1334 return FALSE;
1335 while (s < *eptr && isDIGIT(*s)) {
1336 /* This could be unrolled like in grok_number(), but
1337 * the expected uses of this are not speed-needy, and
1338 * unlikely to need full 64-bitness. */
1339 const U8 digit = *s++ - '0';
1340 if (val < uv_max_div_10 ||
1341 (val == uv_max_div_10 && digit <= uv_max_mod_10)) {
1342 val = val * 10 + digit;
1343 } else {
22ff3130 1344 return FALSE;
6313e544
JH
1345 }
1346 }
97d95d46
KW
1347 }
1348
5d4a52b5
KW
1349 if (endptr == NULL) {
1350 if (*s) {
1351 return FALSE; /* If endptr is NULL, no trailing non-digits allowed. */
1352 }
1353 }
1354 else {
1355 *endptr = s;
75feedba 1356 }
97d95d46 1357
22ff3130
HS
1358 *valptr = val;
1359 return TRUE;
6313e544
JH
1360}
1361
ce6f496d 1362#ifndef Perl_strtod
4801ca72 1363STATIC NV
98994639
HS
1364S_mulexp10(NV value, I32 exponent)
1365{
1366 NV result = 1.0;
1367 NV power = 10.0;
1368 bool negative = 0;
1369 I32 bit;
1370
1371 if (exponent == 0)
1604cfb0 1372 return value;
659c4b96 1373 if (value == 0)
1604cfb0 1374 return (NV)0;
87032ba1 1375
24866caa 1376 /* On OpenVMS VAX we by default use the D_FLOAT double format,
67597c89 1377 * and that format does not have *easy* capabilities [1] for
19c1206d
KW
1378 * overflowing doubles 'silently' as IEEE fp does. We also need
1379 * to support G_FLOAT on both VAX and Alpha, and though the exponent
1380 * range is much larger than D_FLOAT it still doesn't do silent
1381 * overflow. Therefore we need to detect early whether we would
1382 * overflow (this is the behaviour of the native string-to-float
24866caa 1383 * conversion routines, and therefore of native applications, too).
67597c89 1384 *
24866caa
CB
1385 * [1] Trying to establish a condition handler to trap floating point
1386 * exceptions is not a good idea. */
87032ba1
JH
1387
1388 /* In UNICOS and in certain Cray models (such as T90) there is no
1389 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
1390 * There is something you can do if you are willing to use some
1391 * inline assembler: the instruction is called DFI-- but that will
1392 * disable *all* floating point interrupts, a little bit too large
1393 * a hammer. Therefore we need to catch potential overflows before
1394 * it's too late. */
353813d9 1395
a7157111 1396#if ((defined(VMS) && !defined(_IEEE_FP)) || defined(_UNICOS) || defined(DOUBLE_IS_VAX_FLOAT)) && defined(NV_MAX_10_EXP)
353813d9 1397 STMT_START {
1604cfb0
MS
1398 const NV exp_v = log10(value);
1399 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
1400 return NV_MAX;
1401 if (exponent < 0) {
1402 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
1403 return 0.0;
1404 while (-exponent >= NV_MAX_10_EXP) {
1405 /* combination does not overflow, but 10^(-exponent) does */
1406 value /= 10;
1407 ++exponent;
1408 }
1409 }
353813d9 1410 } STMT_END;
87032ba1
JH
1411#endif
1412
353813d9 1413 if (exponent < 0) {
1604cfb0
MS
1414 negative = 1;
1415 exponent = -exponent;
b27804d8
DM
1416#ifdef NV_MAX_10_EXP
1417 /* for something like 1234 x 10^-309, the action of calculating
1418 * the intermediate value 10^309 then returning 1234 / (10^309)
1419 * will fail, since 10^309 becomes infinity. In this case try to
1420 * refactor it as 123 / (10^308) etc.
1421 */
1422 while (value && exponent > NV_MAX_10_EXP) {
1423 exponent--;
1424 value /= 10;
1425 }
48853916
JH
1426 if (value == 0.0)
1427 return value;
b27804d8 1428#endif
353813d9 1429 }
c62e754c
JH
1430#if defined(__osf__)
1431 /* Even with cc -ieee + ieee_set_fp_control(IEEE_TRAP_ENABLE_INV)
1432 * Tru64 fp behavior on inf/nan is somewhat broken. Another way
1433 * to do this would be ieee_set_fp_control(IEEE_TRAP_ENABLE_OVF)
1434 * but that breaks another set of infnan.t tests. */
1435# define FP_OVERFLOWS_TO_ZERO
1436#endif
98994639 1437 for (bit = 1; exponent; bit <<= 1) {
1604cfb0
MS
1438 if (exponent & bit) {
1439 exponent ^= bit;
1440 result *= power;
c62e754c
JH
1441#ifdef FP_OVERFLOWS_TO_ZERO
1442 if (result == 0)
a7157111 1443# ifdef NV_INF
c62e754c 1444 return value < 0 ? -NV_INF : NV_INF;
a7157111
JH
1445# else
1446 return value < 0 ? -FLT_MAX : FLT_MAX;
1447# endif
c62e754c 1448#endif
1604cfb0
MS
1449 /* Floating point exceptions are supposed to be turned off,
1450 * but if we're obviously done, don't risk another iteration.
1451 */
1452 if (exponent == 0) break;
1453 }
1454 power *= power;
98994639
HS
1455 }
1456 return negative ? value / result : value * result;
1457}
ce6f496d 1458#endif /* #ifndef Perl_strtod */
98994639 1459
ce6f496d 1460#ifdef Perl_strtod
b93d1309 1461# define ATOF(s, x) my_atof2(s, &x)
f7b64c80 1462#else
b93d1309 1463# define ATOF(s, x) Perl_atof2(s, x)
f7b64c80 1464#endif
b93d1309 1465
98994639
HS
1466NV
1467Perl_my_atof(pTHX_ const char* s)
1468{
b1de2493
KW
1469
1470/*
1471=for apidoc my_atof
1472
1473L<C<atof>(3)>, but properly works with Perl locale handling, accepting a dot
1474radix character always, but also the current locale's radix character if and
1475only if called from within the lexical scope of a Perl C<use locale> statement.
1476
1477N.B. C<s> must be NUL terminated.
1478
1479=cut
1480*/
f720c878 1481
98994639 1482 NV x = 0.0;
9eda1ea6
KW
1483
1484 PERL_ARGS_ASSERT_MY_ATOF;
1485
b93d1309 1486#if ! defined(USE_LOCALE_NUMERIC)
9eda1ea6 1487
b93d1309 1488 ATOF(s, x);
9eda1ea6
KW
1489
1490#else
7918f24d 1491
a2287a13 1492 {
67d796ae
KW
1493 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
1494 STORE_LC_NUMERIC_SET_TO_NEEDED();
fdf55d20
KW
1495 if (! (PL_numeric_radix_sv && IN_LC(LC_NUMERIC))) {
1496 ATOF(s,x);
1497 }
1498 else {
19c1206d 1499
e4850248
KW
1500 /* Look through the string for the first thing that looks like a
1501 * decimal point: either the value in the current locale or the
1502 * standard fallback of '.'. The one which appears earliest in the
1503 * input string is the one that we should have atof look for. Note
1504 * that we have to determine this beforehand because on some
1505 * systems, Perl_atof2 is just a wrapper around the system's atof.
1506 * */
1ae85f6c
KW
1507 const char * const standard_pos = strchr(s, '.');
1508 const char * const local_pos
1509 = strstr(s, SvPV_nolen(PL_numeric_radix_sv));
1510 const bool use_standard_radix
1511 = standard_pos && (!local_pos || standard_pos < local_pos);
78787052 1512
665873e9 1513 if (use_standard_radix) {
e4850248 1514 SET_NUMERIC_STANDARD();
665873e9
KW
1515 LOCK_LC_NUMERIC_STANDARD();
1516 }
78787052 1517
b93d1309 1518 ATOF(s,x);
78787052 1519
665873e9
KW
1520 if (use_standard_radix) {
1521 UNLOCK_LC_NUMERIC_STANDARD();
67d796ae 1522 SET_NUMERIC_UNDERLYING();
665873e9 1523 }
e4850248 1524 }
a2287a13
KW
1525 RESTORE_LC_NUMERIC();
1526 }
9eda1ea6 1527
98994639 1528#endif
9eda1ea6 1529
98994639
HS
1530 return x;
1531}
1532
a7157111 1533#if defined(NV_INF) || defined(NV_NAN)
3c81f0b3 1534
829757a4 1535static char*
5563f457 1536S_my_atof_infnan(pTHX_ const char* s, bool negative, const char* send, NV* value)
829757a4
JH
1537{
1538 const char *p0 = negative ? s - 1 : s;
1539 const char *p = p0;
7eff3d39 1540 const int infnan = grok_infnan(&p, send);
7294e9f9
HS
1541 /* We act like PERL_SCAN_TRAILING here to permit trailing garbage,
1542 * it is not clear if that is desirable.
1543 */
829757a4
JH
1544 if (infnan && p != p0) {
1545 /* If we can generate inf/nan directly, let's do so. */
1546#ifdef NV_INF
1547 if ((infnan & IS_NUMBER_INFINITY)) {
3823048b 1548 *value = (infnan & IS_NUMBER_NEG) ? -NV_INF: NV_INF;
829757a4
JH
1549 return (char*)p;
1550 }
1551#endif
1552#ifdef NV_NAN
1553 if ((infnan & IS_NUMBER_NAN)) {
3823048b 1554 *value = NV_NAN;
829757a4
JH
1555 return (char*)p;
1556 }
1557#endif
1558#ifdef Perl_strtod
68611e6f 1559 /* If still here, we didn't have either NV_INF or NV_NAN,
829757a4
JH
1560 * and can try falling back to native strtod/strtold.
1561 *
1562 * The native interface might not recognize all the possible
1563 * inf/nan strings Perl recognizes. What we can try
1564 * is to try faking the input. We will try inf/-inf/nan
1565 * as the most promising/portable input. */
1566 {
6d37e916 1567 const char* fake = "silence compiler warning";
829757a4
JH
1568 char* endp;
1569 NV nv;
a7157111 1570#ifdef NV_INF
829757a4
JH
1571 if ((infnan & IS_NUMBER_INFINITY)) {
1572 fake = ((infnan & IS_NUMBER_NEG)) ? "-inf" : "inf";
1573 }
a7157111
JH
1574#endif
1575#ifdef NV_NAN
1576 if ((infnan & IS_NUMBER_NAN)) {
829757a4
JH
1577 fake = "nan";
1578 }
a7157111 1579#endif
6d37e916 1580 assert(strNE(fake, "silence compiler warning"));
9ec8aea5 1581 nv = S_strtod(aTHX_ fake, &endp);
829757a4 1582 if (fake != endp) {
a7157111 1583#ifdef NV_INF
829757a4 1584 if ((infnan & IS_NUMBER_INFINITY)) {
a7157111 1585# ifdef Perl_isinf
829757a4
JH
1586 if (Perl_isinf(nv))
1587 *value = nv;
a7157111 1588# else
829757a4
JH
1589 /* last resort, may generate SIGFPE */
1590 *value = Perl_exp((NV)1e9);
1591 if ((infnan & IS_NUMBER_NEG))
1592 *value = -*value;
a7157111 1593# endif
829757a4
JH
1594 return (char*)p; /* p, not endp */
1595 }
a7157111
JH
1596#endif
1597#ifdef NV_NAN
1598 if ((infnan & IS_NUMBER_NAN)) {
1599# ifdef Perl_isnan
829757a4
JH
1600 if (Perl_isnan(nv))
1601 *value = nv;
a7157111 1602# else
829757a4
JH
1603 /* last resort, may generate SIGFPE */
1604 *value = Perl_log((NV)-1.0);
a7157111 1605# endif
829757a4 1606 return (char*)p; /* p, not endp */
a7157111 1607#endif
829757a4
JH
1608 }
1609 }
1610 }
1611#endif /* #ifdef Perl_strtod */
1612 }
1613 return NULL;
1614}
1615
a7157111
JH
1616#endif /* if defined(NV_INF) || defined(NV_NAN) */
1617
98994639
HS
1618char*
1619Perl_my_atof2(pTHX_ const char* orig, NV* value)
1620{
6928bedc
KW
1621 PERL_ARGS_ASSERT_MY_ATOF2;
1622 return my_atof3(orig, value, 0);
1623}
1624
1625char*
16411967 1626Perl_my_atof3(pTHX_ const char* orig, NV* value, const STRLEN len)
6928bedc 1627{
e1ec3a88 1628 const char* s = orig;
a4eca1d4 1629 NV result[3] = {0.0, 0.0, 0.0};
ce6f496d 1630#if defined(USE_PERL_ATOF) || defined(Perl_strtod)
6928bedc
KW
1631 const char* send = s + ((len != 0)
1632 ? len
1633 : strlen(orig)); /* one past the last */
a4eca1d4 1634#endif
ce6f496d 1635#if defined(USE_PERL_ATOF) && !defined(Perl_strtod)
0b8a8310 1636 bool negative = 0;
a4eca1d4 1637 UV accumulator[2] = {0,0}; /* before/after dp */
8194bf88 1638 bool seen_digit = 0;
20f6aaab
AS
1639 I32 exp_adjust[2] = {0,0};
1640 I32 exp_acc[2] = {-1, -1};
1641 /* the current exponent adjust for the accumulators */
98994639 1642 I32 exponent = 0;
8194bf88 1643 I32 seen_dp = 0;
20f6aaab
AS
1644 I32 digit = 0;
1645 I32 old_digit = 0;
8194bf88 1646 I32 sig_digits = 0; /* noof significant digits seen so far */
a4eca1d4 1647#endif
8194bf88 1648
ce6f496d 1649#if defined(USE_PERL_ATOF) || defined(Perl_strtod)
6928bedc 1650 PERL_ARGS_ASSERT_MY_ATOF3;
7918f24d 1651
a4eca1d4 1652 /* leading whitespace */
6928bedc 1653 while (s < send && isSPACE(*s))
1604cfb0 1654 ++s;
a4eca1d4 1655
e79f3c06
TK
1656# if defined(NV_INF) || defined(NV_NAN)
1657 {
1658 char* endp;
1659 if ((endp = S_my_atof_infnan(aTHX_ s, FALSE, send, value)))
1660 return endp;
1661 }
1662# endif
1663
a4eca1d4
JH
1664 /* sign */
1665 switch (*s) {
1604cfb0 1666 case '-':
0b8a8310 1667# if !defined(Perl_strtod)
1604cfb0 1668 negative = 1;
0b8a8310 1669# endif
1604cfb0
MS
1670 /* FALLTHROUGH */
1671 case '+':
1672 ++s;
a4eca1d4
JH
1673 }
1674#endif
1675
ce6f496d 1676#ifdef Perl_strtod
a4eca1d4
JH
1677 {
1678 char* endp;
d94e901a
KW
1679 char* copy = NULL;
1680
14d26b44
TC
1681 /* strtold() accepts 0x-prefixed hex and in POSIX implementations,
1682 0b-prefixed binary numbers, which is backward incompatible
1683 */
e56dfd96 1684 if ((len == 0 || len - (s-orig) >= 2) && *s == '0' &&
14d26b44
TC
1685 (isALPHA_FOLD_EQ(s[1], 'x') || isALPHA_FOLD_EQ(s[1], 'b'))) {
1686 *value = 0;
1687 return (char *)s+1;
1688 }
1689
e79f3c06
TK
1690 /* We do not want strtod to parse whitespace after the sign, since
1691 * that would give backward-incompatible results. So we rewind and
1692 * let strtod handle the whitespace and sign character itself. */
7035863f
TK
1693 s = orig;
1694
d94e901a
KW
1695 /* If the length is passed in, the input string isn't NUL-terminated,
1696 * and in it turns out the function below assumes it is; therefore we
1697 * create a copy and NUL-terminate that */
1698 if (len) {
1699 Newx(copy, len + 1, char);
1700 Copy(orig, copy, len, char);
1701 copy[len] = '\0';
7035863f 1702 s = copy;
d94e901a
KW
1703 }
1704
9ec8aea5 1705 result[2] = S_strtod(aTHX_ s, &endp);
d94e901a
KW
1706
1707 /* If we created a copy, 'endp' is in terms of that. Convert back to
1708 * the original */
1709 if (copy) {
aac39b03 1710 s = (s - copy) + (char *) orig;
d94e901a
KW
1711 endp = (endp - copy) + (char *) orig;
1712 Safefree(copy);
1713 }
1714
a4eca1d4 1715 if (s != endp) {
7035863f
TK
1716 /* Note that negation is handled by strtod. */
1717 *value = result[2];
a4eca1d4
JH
1718 return endp;
1719 }
1720 return NULL;
1721 }
1722#elif defined(USE_PERL_ATOF)
1723
8194bf88
DM
1724/* There is no point in processing more significant digits
1725 * than the NV can hold. Note that NV_DIG is a lower-bound value,
1726 * while we need an upper-bound value. We add 2 to account for this;
1727 * since it will have been conservative on both the first and last digit.
1728 * For example a 32-bit mantissa with an exponent of 4 would have
1729 * exact values in the set
1730 * 4
1731 * 8
1732 * ..
1733 * 17179869172
1734 * 17179869176
1735 * 17179869180
1736 *
1737 * where for the purposes of calculating NV_DIG we would have to discount
1738 * both the first and last digit, since neither can hold all values from
1739 * 0..9; but for calculating the value we must examine those two digits.
1740 */
58af9243 1741# ifdef MAX_SIG_DIG_PLUS
ffa277e5
AS
1742 /* It is not necessarily the case that adding 2 to NV_DIG gets all the
1743 possible digits in a NV, especially if NVs are not IEEE compliant
1744 (e.g., long doubles on IRIX) - Allen <allens@cpan.org> */
58af9243
KW
1745# define MAX_SIG_DIGITS (NV_DIG+MAX_SIG_DIG_PLUS)
1746# else
1747# define MAX_SIG_DIGITS (NV_DIG+2)
1748# endif
8194bf88
DM
1749
1750/* the max number we can accumulate in a UV, and still safely do 10*N+9 */
58af9243 1751# define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
98994639 1752
8194bf88
DM
1753 /* we accumulate digits into an integer; when this becomes too
1754 * large, we add the total to NV and start again */
98994639 1755
6928bedc 1756 while (s < send) {
1604cfb0
MS
1757 if (isDIGIT(*s)) {
1758 seen_digit = 1;
1759 old_digit = digit;
1760 digit = *s++ - '0';
1761 if (seen_dp)
1762 exp_adjust[1]++;
1763
1764 /* don't start counting until we see the first significant
1765 * digit, eg the 5 in 0.00005... */
1766 if (!sig_digits && digit == 0)
1767 continue;
1768
1769 if (++sig_digits > MAX_SIG_DIGITS) {
1770 /* limits of precision reached */
1771 if (digit > 5) {
1772 ++accumulator[seen_dp];
1773 } else if (digit == 5) {
1774 if (old_digit % 2) { /* round to even - Allen */
1775 ++accumulator[seen_dp];
1776 }
1777 }
1778 if (seen_dp) {
1779 exp_adjust[1]--;
1780 } else {
1781 exp_adjust[0]++;
1782 }
1783 /* skip remaining digits */
1784 while (s < send && isDIGIT(*s)) {
1785 ++s;
1786 if (! seen_dp) {
1787 exp_adjust[0]++;
1788 }
1789 }
1790 /* warn of loss of precision? */
1791 }
1792 else {
1793 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
1794 /* add accumulator to result and start again */
1795 result[seen_dp] = S_mulexp10(result[seen_dp],
1796 exp_acc[seen_dp])
1797 + (NV)accumulator[seen_dp];
1798 accumulator[seen_dp] = 0;
1799 exp_acc[seen_dp] = 0;
1800 }
1801 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
1802 ++exp_acc[seen_dp];
1803 }
1804 }
1805 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
1806 seen_dp = 1;
1807 if (sig_digits > MAX_SIG_DIGITS) {
1808 while (s < send && isDIGIT(*s)) {
1809 ++s;
1810 }
1811 break;
1812 }
1813 }
1814 else {
1815 break;
1816 }
98994639
HS
1817 }
1818
20f6aaab
AS
1819 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
1820 if (seen_dp) {
1604cfb0 1821 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
20f6aaab 1822 }
98994639 1823
6928bedc 1824 if (s < send && seen_digit && (isALPHA_FOLD_EQ(*s, 'e'))) {
1604cfb0
MS
1825 bool expnegative = 0;
1826
1827 ++s;
1828 switch (*s) {
1829 case '-':
1830 expnegative = 1;
1831 /* FALLTHROUGH */
1832 case '+':
1833 ++s;
1834 }
1835 while (s < send && isDIGIT(*s))
1836 exponent = exponent * 10 + (*s++ - '0');
1837 if (expnegative)
1838 exponent = -exponent;
98994639
HS
1839 }
1840
1841 /* now apply the exponent */
20f6aaab
AS
1842
1843 if (seen_dp) {
1604cfb0
MS
1844 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
1845 + S_mulexp10(result[1],exponent-exp_adjust[1]);
20f6aaab 1846 } else {
1604cfb0 1847 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
20f6aaab 1848 }
98994639
HS
1849
1850 /* now apply the sign */
1851 if (negative)
1604cfb0 1852 result[2] = -result[2];
20f6aaab 1853 *value = result[2];
73d840c0 1854 return (char *)s;
dfee9352
TC
1855#else /* USE_PERL_ATOF */
1856 /* If you see this error you both don't have strtod (or configured -Ud_strtod or
1857 or it's long double/quadmath equivalent) and disabled USE_PERL_ATOF, thus
1858 removing any way for perl to convert strings to floating point numbers.
1859 */
58af9243 1860# error No mechanism to convert strings to numbers available
dfee9352 1861#endif
98994639
HS
1862}
1863
5d34af89 1864/*
3d9d9213 1865=for apidoc isinfnan
5d34af89 1866
5f4d68d1 1867C<Perl_isinfnan()> is a utility function that returns true if the NV
796b6530
KW
1868argument is either an infinity or a C<NaN>, false otherwise. To test
1869in more detail, use C<Perl_isinf()> and C<Perl_isnan()>.
5d34af89 1870
68611e6f
JH
1871This is also the logical inverse of Perl_isfinite().
1872
5d34af89
JH
1873=cut
1874*/
1cd88304
JH
1875bool
1876Perl_isinfnan(NV nv)
1877{
a5dc2484 1878 PERL_UNUSED_ARG(nv);
1cd88304
JH
1879#ifdef Perl_isinf
1880 if (Perl_isinf(nv))
1881 return TRUE;
1882#endif
1883#ifdef Perl_isnan
1884 if (Perl_isnan(nv))
1885 return TRUE;
1886#endif
1887 return FALSE;
1888}
1889
354b74ae 1890/*
af147c81 1891=for apidoc isinfnansv
354b74ae 1892
796b6530 1893Checks whether the argument would be either an infinity or C<NaN> when used
354b74ae 1894as a number, but is careful not to trigger non-numeric or uninitialized
796b6530 1895warnings. it assumes the caller has done C<SvGETMAGIC(sv)> already.
354b74ae 1896
7294e9f9
HS
1897Note that this always accepts trailing garbage (similar to C<grok_number_flags>
1898with C<PERL_SCAN_TRAILING>), so C<"inferior"> and C<"NAND gates"> will
1899return true.
1900
354b74ae
FC
1901=cut
1902*/
1903
1904bool
1905Perl_isinfnansv(pTHX_ SV *sv)
1906{
1907 PERL_ARGS_ASSERT_ISINFNANSV;
1908 if (!SvOK(sv))
1909 return FALSE;
1910 if (SvNOKp(sv))
1911 return Perl_isinfnan(SvNVX(sv));
1912 if (SvIOKp(sv))
1913 return FALSE;
1914 {
1915 STRLEN len;
1916 const char *s = SvPV_nomg_const(sv, len);
3823048b 1917 return cBOOL(grok_infnan(&s, s+len));
354b74ae
FC
1918 }
1919}
1920
d67dac15 1921#ifndef HAS_MODFL
68611e6f
JH
1922/* C99 has truncl, pre-C99 Solaris had aintl. We can use either with
1923 * copysignl to emulate modfl, which is in some platforms missing or
1924 * broken. */
d67dac15
JH
1925# if defined(HAS_TRUNCL) && defined(HAS_COPYSIGNL)
1926long double
1927Perl_my_modfl(long double x, long double *ip)
1928{
68611e6f
JH
1929 *ip = truncl(x);
1930 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
d67dac15
JH
1931}
1932# elif defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
55954f19
JH
1933long double
1934Perl_my_modfl(long double x, long double *ip)
1935{
68611e6f
JH
1936 *ip = aintl(x);
1937 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
55954f19 1938}
d67dac15 1939# endif
55954f19
JH
1940#endif
1941
7b9b7dff 1942/* Similarly, with ilogbl and scalbnl we can emulate frexpl. */
55954f19
JH
1943#if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1944long double
1945Perl_my_frexpl(long double x, int *e) {
68611e6f
JH
1946 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1947 return (scalbnl(x, -*e));
55954f19
JH
1948}
1949#endif
66610fdd
RGS
1950
1951/*
ed140128
AD
1952=for apidoc Perl_signbit
1953
1954Return a non-zero integer if the sign bit on an NV is set, and 0 if
19c1206d 1955it is not.
ed140128 1956
796b6530
KW
1957If F<Configure> detects this system has a C<signbit()> that will work with
1958our NVs, then we just use it via the C<#define> in F<perl.h>. Otherwise,
8b7fad81 1959fall back on this implementation. The main use of this function
796b6530 1960is catching C<-0.0>.
ed140128 1961
796b6530
KW
1962C<Configure> notes: This function is called C<'Perl_signbit'> instead of a
1963plain C<'signbit'> because it is easy to imagine a system having a C<signbit()>
ed140128 1964function or macro that doesn't happen to work with our particular choice
796b6530 1965of NVs. We shouldn't just re-C<#define> C<signbit> as C<Perl_signbit> and expect
ed140128 1966the standard system headers to be happy. Also, this is a no-context
796b6530
KW
1967function (no C<pTHX_>) because C<Perl_signbit()> is usually re-C<#defined> in
1968F<perl.h> as a simple macro call to the system's C<signbit()>.
1969Users should just always call C<Perl_signbit()>.
ed140128
AD
1970
1971=cut
1972*/
1973#if !defined(HAS_SIGNBIT)
1974int
1975Perl_signbit(NV x) {
8b7fad81 1976# ifdef Perl_fp_class_nzero
406d5545
JH
1977 return Perl_fp_class_nzero(x);
1978 /* Try finding the high byte, and assume it's highest bit
1979 * is the sign. This assumption is probably wrong somewhere. */
572cd850
JH
1980# elif defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
1981 return (((unsigned char *)&x)[9] & 0x80);
1982# elif defined(NV_LITTLE_ENDIAN)
1983 /* Note that NVSIZE is sizeof(NV), which would make the below be
1984 * wrong if the end bytes are unused, which happens with the x86
1985 * 80-bit long doubles, which is why take care of that above. */
1986 return (((unsigned char *)&x)[NVSIZE - 1] & 0x80);
1987# elif defined(NV_BIG_ENDIAN)
1988 return (((unsigned char *)&x)[0] & 0x80);
1989# else
406d5545 1990 /* This last resort fallback is wrong for the negative zero. */
3585840c 1991 return (x < 0.0) ? 1 : 0;
572cd850 1992# endif
ed140128
AD
1993}
1994#endif
1995
1996/*
14d04a33 1997 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1998 */