Add editor boilerplates to all C files
[p5sagit/p5-mst-13.2.git] / numeric.c
CommitLineData
98994639 1/* numeric.c
2 *
4bb101f2 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
1d325971 4 * 2000, 2001, 2002, 2003, 2005 by Larry Wall and others
98994639 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/*
12 * "That only makes eleven (plus one mislaid) and not fourteen, unless
13 * wizards count differently to other people."
14 */
15
ccfc67b7 16/*
17=head1 Numeric functions
166f8a29 18
19This file contains all the stuff needed by perl for manipulating numeric
20values, including such things as replacements for the OS's atof() function
21
22=cut
23
ccfc67b7 24*/
25
98994639 26#include "EXTERN.h"
27#define PERL_IN_NUMERIC_C
28#include "perl.h"
29
30U32
31Perl_cast_ulong(pTHX_ NV f)
32{
33 if (f < 0.0)
34 return f < I32_MIN ? (U32) I32_MIN : (U32)(I32) f;
35 if (f < U32_MAX_P1) {
36#if CASTFLAGS & 2
37 if (f < U32_MAX_P1_HALF)
38 return (U32) f;
39 f -= U32_MAX_P1_HALF;
40 return ((U32) f) | (1 + U32_MAX >> 1);
41#else
42 return (U32) f;
43#endif
44 }
45 return f > 0 ? U32_MAX : 0 /* NaN */;
46}
47
48I32
49Perl_cast_i32(pTHX_ NV f)
50{
51 if (f < I32_MAX_P1)
52 return f < I32_MIN ? I32_MIN : (I32) f;
53 if (f < U32_MAX_P1) {
54#if CASTFLAGS & 2
55 if (f < U32_MAX_P1_HALF)
56 return (I32)(U32) f;
57 f -= U32_MAX_P1_HALF;
58 return (I32)(((U32) f) | (1 + U32_MAX >> 1));
59#else
60 return (I32)(U32) f;
61#endif
62 }
63 return f > 0 ? (I32)U32_MAX : 0 /* NaN */;
64}
65
66IV
67Perl_cast_iv(pTHX_ NV f)
68{
69 if (f < IV_MAX_P1)
70 return f < IV_MIN ? IV_MIN : (IV) f;
71 if (f < UV_MAX_P1) {
72#if CASTFLAGS & 2
73 /* For future flexibility allowing for sizeof(UV) >= sizeof(IV) */
74 if (f < UV_MAX_P1_HALF)
75 return (IV)(UV) f;
76 f -= UV_MAX_P1_HALF;
77 return (IV)(((UV) f) | (1 + UV_MAX >> 1));
78#else
79 return (IV)(UV) f;
80#endif
81 }
82 return f > 0 ? (IV)UV_MAX : 0 /* NaN */;
83}
84
85UV
86Perl_cast_uv(pTHX_ NV f)
87{
88 if (f < 0.0)
89 return f < IV_MIN ? (UV) IV_MIN : (UV)(IV) f;
90 if (f < UV_MAX_P1) {
91#if CASTFLAGS & 2
92 if (f < UV_MAX_P1_HALF)
93 return (UV) f;
94 f -= UV_MAX_P1_HALF;
95 return ((UV) f) | (1 + UV_MAX >> 1);
96#else
97 return (UV) f;
98#endif
99 }
100 return f > 0 ? UV_MAX : 0 /* NaN */;
101}
102
103#if defined(HUGE_VAL) || (defined(USE_LONG_DOUBLE) && defined(HUGE_VALL))
104/*
105 * This hack is to force load of "huge" support from libm.a
106 * So it is in perl for (say) POSIX to use.
107 * Needed for SunOS with Sun's 'acc' for example.
108 */
109NV
110Perl_huge(void)
111{
112# if defined(USE_LONG_DOUBLE) && defined(HUGE_VALL)
113 return HUGE_VALL;
114# endif
115 return HUGE_VAL;
116}
117#endif
118
53305cf1 119/*
120=for apidoc grok_bin
98994639 121
53305cf1 122converts a string representing a binary number to numeric form.
123
124On entry I<start> and I<*len> give the string to scan, I<*flags> gives
125conversion flags, and I<result> should be NULL or a pointer to an NV.
126The scan stops at the end of the string, or the first invalid character.
7b667b5f 127Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
128invalid character will also trigger a warning.
129On return I<*len> is set to the length of the scanned string,
130and I<*flags> gives output flags.
53305cf1 131
7fc63493 132If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
53305cf1 133and nothing is written to I<*result>. If the value is > UV_MAX C<grok_bin>
134returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
135and writes the value to I<*result> (or the value is discarded if I<result>
136is NULL).
137
7b667b5f 138The binary number may optionally be prefixed with "0b" or "b" unless
a4c04bdc 139C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
140C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the binary
53305cf1 141number may use '_' characters to separate digits.
142
143=cut
144 */
145
146UV
7fc63493 147Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) {
53305cf1 148 const char *s = start;
149 STRLEN len = *len_p;
150 UV value = 0;
151 NV value_nv = 0;
152
153 const UV max_div_2 = UV_MAX / 2;
7fc63493 154 const bool allow_underscores = *flags & PERL_SCAN_ALLOW_UNDERSCORES;
53305cf1 155 bool overflowed = FALSE;
7fc63493 156 char bit;
53305cf1 157
a4c04bdc 158 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
159 /* strip off leading b or 0b.
160 for compatibility silently suffer "b" and "0b" as valid binary
161 numbers. */
162 if (len >= 1) {
163 if (s[0] == 'b') {
164 s++;
165 len--;
166 }
167 else if (len >= 2 && s[0] == '0' && s[1] == 'b') {
168 s+=2;
169 len-=2;
170 }
171 }
53305cf1 172 }
173
7fc63493 174 for (; len-- && (bit = *s); s++) {
53305cf1 175 if (bit == '0' || bit == '1') {
176 /* Write it in this wonky order with a goto to attempt to get the
177 compiler to make the common case integer-only loop pretty tight.
178 With gcc seems to be much straighter code than old scan_bin. */
179 redo:
180 if (!overflowed) {
181 if (value <= max_div_2) {
182 value = (value << 1) | (bit - '0');
183 continue;
184 }
185 /* Bah. We're just overflowed. */
186 if (ckWARN_d(WARN_OVERFLOW))
9014280d 187 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
53305cf1 188 "Integer overflow in binary number");
189 overflowed = TRUE;
190 value_nv = (NV) value;
191 }
192 value_nv *= 2.0;
98994639 193 /* If an NV has not enough bits in its mantissa to
d1be9408 194 * represent a UV this summing of small low-order numbers
98994639 195 * is a waste of time (because the NV cannot preserve
196 * the low-order bits anyway): we could just remember when
53305cf1 197 * did we overflow and in the end just multiply value_nv by the
98994639 198 * right amount. */
53305cf1 199 value_nv += (NV)(bit - '0');
200 continue;
201 }
202 if (bit == '_' && len && allow_underscores && (bit = s[1])
203 && (bit == '0' || bit == '1'))
98994639 204 {
205 --len;
206 ++s;
53305cf1 207 goto redo;
98994639 208 }
94dd8549 209 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
9014280d 210 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
53305cf1 211 "Illegal binary digit '%c' ignored", *s);
212 break;
98994639 213 }
53305cf1 214
215 if ( ( overflowed && value_nv > 4294967295.0)
98994639 216#if UVSIZE > 4
53305cf1 217 || (!overflowed && value > 0xffffffff )
98994639 218#endif
219 ) {
220 if (ckWARN(WARN_PORTABLE))
9014280d 221 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
53305cf1 222 "Binary number > 0b11111111111111111111111111111111 non-portable");
223 }
224 *len_p = s - start;
225 if (!overflowed) {
226 *flags = 0;
227 return value;
98994639 228 }
53305cf1 229 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
230 if (result)
231 *result = value_nv;
232 return UV_MAX;
98994639 233}
234
53305cf1 235/*
236=for apidoc grok_hex
237
238converts a string representing a hex number to numeric form.
239
240On entry I<start> and I<*len> give the string to scan, I<*flags> gives
241conversion flags, and I<result> should be NULL or a pointer to an NV.
7b667b5f 242The scan stops at the end of the string, or the first invalid character.
243Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
244invalid character will also trigger a warning.
245On return I<*len> is set to the length of the scanned string,
246and I<*flags> gives output flags.
53305cf1 247
248If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
249and nothing is written to I<*result>. If the value is > UV_MAX C<grok_hex>
250returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
251and writes the value to I<*result> (or the value is discarded if I<result>
252is NULL).
253
d1be9408 254The hex number may optionally be prefixed with "0x" or "x" unless
a4c04bdc 255C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
256C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the hex
53305cf1 257number may use '_' characters to separate digits.
258
259=cut
260 */
261
262UV
7fc63493 263Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) {
27da23d5 264 dVAR;
53305cf1 265 const char *s = start;
266 STRLEN len = *len_p;
267 UV value = 0;
268 NV value_nv = 0;
269
270 const UV max_div_16 = UV_MAX / 16;
7fc63493 271 const bool allow_underscores = *flags & PERL_SCAN_ALLOW_UNDERSCORES;
53305cf1 272 bool overflowed = FALSE;
273 const char *hexdigit;
98994639 274
a4c04bdc 275 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
276 /* strip off leading x or 0x.
277 for compatibility silently suffer "x" and "0x" as valid hex numbers.
278 */
279 if (len >= 1) {
280 if (s[0] == 'x') {
281 s++;
282 len--;
283 }
284 else if (len >= 2 && s[0] == '0' && s[1] == 'x') {
285 s+=2;
286 len-=2;
287 }
288 }
98994639 289 }
290
291 for (; len-- && *s; s++) {
e1ec3a88 292 hexdigit = strchr(PL_hexdigit, *s);
53305cf1 293 if (hexdigit) {
294 /* Write it in this wonky order with a goto to attempt to get the
295 compiler to make the common case integer-only loop pretty tight.
296 With gcc seems to be much straighter code than old scan_hex. */
297 redo:
298 if (!overflowed) {
299 if (value <= max_div_16) {
300 value = (value << 4) | ((hexdigit - PL_hexdigit) & 15);
301 continue;
302 }
303 /* Bah. We're just overflowed. */
304 if (ckWARN_d(WARN_OVERFLOW))
9014280d 305 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
53305cf1 306 "Integer overflow in hexadecimal number");
307 overflowed = TRUE;
308 value_nv = (NV) value;
309 }
310 value_nv *= 16.0;
311 /* If an NV has not enough bits in its mantissa to
d1be9408 312 * represent a UV this summing of small low-order numbers
53305cf1 313 * is a waste of time (because the NV cannot preserve
314 * the low-order bits anyway): we could just remember when
315 * did we overflow and in the end just multiply value_nv by the
316 * right amount of 16-tuples. */
317 value_nv += (NV)((hexdigit - PL_hexdigit) & 15);
318 continue;
319 }
320 if (*s == '_' && len && allow_underscores && s[1]
e1ec3a88 321 && (hexdigit = strchr(PL_hexdigit, s[1])))
98994639 322 {
323 --len;
324 ++s;
53305cf1 325 goto redo;
98994639 326 }
94dd8549 327 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
9014280d 328 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
53305cf1 329 "Illegal hexadecimal digit '%c' ignored", *s);
330 break;
331 }
332
333 if ( ( overflowed && value_nv > 4294967295.0)
334#if UVSIZE > 4
335 || (!overflowed && value > 0xffffffff )
336#endif
337 ) {
338 if (ckWARN(WARN_PORTABLE))
9014280d 339 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
53305cf1 340 "Hexadecimal number > 0xffffffff non-portable");
341 }
342 *len_p = s - start;
343 if (!overflowed) {
344 *flags = 0;
345 return value;
346 }
347 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
348 if (result)
349 *result = value_nv;
350 return UV_MAX;
351}
352
353/*
354=for apidoc grok_oct
355
7b667b5f 356converts a string representing an octal number to numeric form.
357
358On entry I<start> and I<*len> give the string to scan, I<*flags> gives
359conversion flags, and I<result> should be NULL or a pointer to an NV.
360The scan stops at the end of the string, or the first invalid character.
361Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
362invalid character will also trigger a warning.
363On return I<*len> is set to the length of the scanned string,
364and I<*flags> gives output flags.
365
366If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
367and nothing is written to I<*result>. If the value is > UV_MAX C<grok_oct>
368returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
369and writes the value to I<*result> (or the value is discarded if I<result>
370is NULL).
371
372If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the octal
373number may use '_' characters to separate digits.
53305cf1 374
375=cut
376 */
377
378UV
7fc63493 379Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) {
53305cf1 380 const char *s = start;
381 STRLEN len = *len_p;
382 UV value = 0;
383 NV value_nv = 0;
384
385 const UV max_div_8 = UV_MAX / 8;
7fc63493 386 const bool allow_underscores = *flags & PERL_SCAN_ALLOW_UNDERSCORES;
53305cf1 387 bool overflowed = FALSE;
388
389 for (; len-- && *s; s++) {
390 /* gcc 2.95 optimiser not smart enough to figure that this subtraction
391 out front allows slicker code. */
392 int digit = *s - '0';
393 if (digit >= 0 && digit <= 7) {
394 /* Write it in this wonky order with a goto to attempt to get the
395 compiler to make the common case integer-only loop pretty tight.
396 */
397 redo:
398 if (!overflowed) {
399 if (value <= max_div_8) {
400 value = (value << 3) | digit;
401 continue;
402 }
403 /* Bah. We're just overflowed. */
404 if (ckWARN_d(WARN_OVERFLOW))
9014280d 405 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
53305cf1 406 "Integer overflow in octal number");
407 overflowed = TRUE;
408 value_nv = (NV) value;
409 }
410 value_nv *= 8.0;
98994639 411 /* If an NV has not enough bits in its mantissa to
d1be9408 412 * represent a UV this summing of small low-order numbers
98994639 413 * is a waste of time (because the NV cannot preserve
414 * the low-order bits anyway): we could just remember when
53305cf1 415 * did we overflow and in the end just multiply value_nv by the
416 * right amount of 8-tuples. */
417 value_nv += (NV)digit;
418 continue;
419 }
420 if (digit == ('_' - '0') && len && allow_underscores
421 && (digit = s[1] - '0') && (digit >= 0 && digit <= 7))
422 {
423 --len;
424 ++s;
425 goto redo;
426 }
427 /* Allow \octal to work the DWIM way (that is, stop scanning
7b667b5f 428 * as soon as non-octal characters are seen, complain only if
53305cf1 429 * someone seems to want to use the digits eight and nine). */
430 if (digit == 8 || digit == 9) {
94dd8549 431 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
9014280d 432 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
53305cf1 433 "Illegal octal digit '%c' ignored", *s);
434 }
435 break;
98994639 436 }
53305cf1 437
438 if ( ( overflowed && value_nv > 4294967295.0)
98994639 439#if UVSIZE > 4
53305cf1 440 || (!overflowed && value > 0xffffffff )
98994639 441#endif
442 ) {
443 if (ckWARN(WARN_PORTABLE))
9014280d 444 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
53305cf1 445 "Octal number > 037777777777 non-portable");
446 }
447 *len_p = s - start;
448 if (!overflowed) {
449 *flags = 0;
450 return value;
98994639 451 }
53305cf1 452 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
453 if (result)
454 *result = value_nv;
455 return UV_MAX;
456}
457
458/*
459=for apidoc scan_bin
460
461For backwards compatibility. Use C<grok_bin> instead.
462
463=for apidoc scan_hex
464
465For backwards compatibility. Use C<grok_hex> instead.
466
467=for apidoc scan_oct
468
469For backwards compatibility. Use C<grok_oct> instead.
470
471=cut
472 */
473
474NV
73d840c0 475Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
53305cf1 476{
477 NV rnv;
478 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
73d840c0 479 const UV ruv = grok_bin (start, &len, &flags, &rnv);
53305cf1 480
481 *retlen = len;
482 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
483}
484
485NV
73d840c0 486Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
53305cf1 487{
488 NV rnv;
489 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
73d840c0 490 const UV ruv = grok_oct (start, &len, &flags, &rnv);
53305cf1 491
492 *retlen = len;
493 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
494}
495
496NV
73d840c0 497Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
53305cf1 498{
499 NV rnv;
500 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
73d840c0 501 const UV ruv = grok_hex (start, &len, &flags, &rnv);
53305cf1 502
503 *retlen = len;
504 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
98994639 505}
506
507/*
508=for apidoc grok_numeric_radix
509
510Scan and skip for a numeric decimal separator (radix).
511
512=cut
513 */
514bool
515Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
516{
517#ifdef USE_LOCALE_NUMERIC
518 if (PL_numeric_radix_sv && IN_LOCALE) {
519 STRLEN len;
73d840c0 520 const char* radix = SvPV(PL_numeric_radix_sv, len);
98994639 521 if (*sp + len <= send && memEQ(*sp, radix, len)) {
522 *sp += len;
523 return TRUE;
524 }
525 }
526 /* always try "." if numeric radix didn't match because
527 * we may have data from different locales mixed */
528#endif
529 if (*sp < send && **sp == '.') {
530 ++*sp;
531 return TRUE;
532 }
533 return FALSE;
534}
535
536/*
537=for apidoc grok_number
538
539Recognise (or not) a number. The type of the number is returned
540(0 if unrecognised), otherwise it is a bit-ORed combination of
541IS_NUMBER_IN_UV, IS_NUMBER_GREATER_THAN_UV_MAX, IS_NUMBER_NOT_INT,
aa8b85de 542IS_NUMBER_NEG, IS_NUMBER_INFINITY, IS_NUMBER_NAN (defined in perl.h).
60939fb8 543
544If the value of the number can fit an in UV, it is returned in the *valuep
545IS_NUMBER_IN_UV will be set to indicate that *valuep is valid, IS_NUMBER_IN_UV
546will never be set unless *valuep is valid, but *valuep may have been assigned
547to during processing even though IS_NUMBER_IN_UV is not set on return.
548If valuep is NULL, IS_NUMBER_IN_UV will be set for the same cases as when
549valuep is non-NULL, but no actual assignment (or SEGV) will occur.
550
551IS_NUMBER_NOT_INT will be set with IS_NUMBER_IN_UV if trailing decimals were
552seen (in which case *valuep gives the true value truncated to an integer), and
553IS_NUMBER_NEG if the number is negative (in which case *valuep holds the
554absolute value). IS_NUMBER_IN_UV is not set if e notation was used or the
555number is larger than a UV.
98994639 556
557=cut
558 */
559int
560Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
561{
60939fb8 562 const char *s = pv;
563 const char *send = pv + len;
564 const UV max_div_10 = UV_MAX / 10;
565 const char max_mod_10 = UV_MAX % 10;
566 int numtype = 0;
567 int sawinf = 0;
aa8b85de 568 int sawnan = 0;
60939fb8 569
570 while (s < send && isSPACE(*s))
571 s++;
572 if (s == send) {
573 return 0;
574 } else if (*s == '-') {
575 s++;
576 numtype = IS_NUMBER_NEG;
577 }
578 else if (*s == '+')
579 s++;
580
581 if (s == send)
582 return 0;
583
584 /* next must be digit or the radix separator or beginning of infinity */
585 if (isDIGIT(*s)) {
586 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
587 overflow. */
588 UV value = *s - '0';
589 /* This construction seems to be more optimiser friendly.
590 (without it gcc does the isDIGIT test and the *s - '0' separately)
591 With it gcc on arm is managing 6 instructions (6 cycles) per digit.
592 In theory the optimiser could deduce how far to unroll the loop
593 before checking for overflow. */
58bb9ec3 594 if (++s < send) {
595 int digit = *s - '0';
60939fb8 596 if (digit >= 0 && digit <= 9) {
597 value = value * 10 + digit;
58bb9ec3 598 if (++s < send) {
599 digit = *s - '0';
60939fb8 600 if (digit >= 0 && digit <= 9) {
601 value = value * 10 + digit;
58bb9ec3 602 if (++s < send) {
603 digit = *s - '0';
60939fb8 604 if (digit >= 0 && digit <= 9) {
605 value = value * 10 + digit;
58bb9ec3 606 if (++s < send) {
607 digit = *s - '0';
60939fb8 608 if (digit >= 0 && digit <= 9) {
609 value = value * 10 + digit;
58bb9ec3 610 if (++s < send) {
611 digit = *s - '0';
60939fb8 612 if (digit >= 0 && digit <= 9) {
613 value = value * 10 + digit;
58bb9ec3 614 if (++s < send) {
615 digit = *s - '0';
60939fb8 616 if (digit >= 0 && digit <= 9) {
617 value = value * 10 + digit;
58bb9ec3 618 if (++s < send) {
619 digit = *s - '0';
60939fb8 620 if (digit >= 0 && digit <= 9) {
621 value = value * 10 + digit;
58bb9ec3 622 if (++s < send) {
623 digit = *s - '0';
60939fb8 624 if (digit >= 0 && digit <= 9) {
625 value = value * 10 + digit;
58bb9ec3 626 if (++s < send) {
60939fb8 627 /* Now got 9 digits, so need to check
628 each time for overflow. */
58bb9ec3 629 digit = *s - '0';
60939fb8 630 while (digit >= 0 && digit <= 9
631 && (value < max_div_10
632 || (value == max_div_10
633 && digit <= max_mod_10))) {
634 value = value * 10 + digit;
58bb9ec3 635 if (++s < send)
636 digit = *s - '0';
60939fb8 637 else
638 break;
639 }
640 if (digit >= 0 && digit <= 9
51bd16da 641 && (s < send)) {
60939fb8 642 /* value overflowed.
643 skip the remaining digits, don't
644 worry about setting *valuep. */
645 do {
646 s++;
647 } while (s < send && isDIGIT(*s));
648 numtype |=
649 IS_NUMBER_GREATER_THAN_UV_MAX;
650 goto skip_value;
651 }
652 }
653 }
98994639 654 }
60939fb8 655 }
656 }
657 }
658 }
659 }
660 }
661 }
662 }
663 }
664 }
665 }
98994639 666 }
60939fb8 667 }
98994639 668 }
60939fb8 669 numtype |= IS_NUMBER_IN_UV;
670 if (valuep)
671 *valuep = value;
672
673 skip_value:
674 if (GROK_NUMERIC_RADIX(&s, send)) {
675 numtype |= IS_NUMBER_NOT_INT;
676 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
677 s++;
98994639 678 }
60939fb8 679 }
680 else if (GROK_NUMERIC_RADIX(&s, send)) {
681 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
682 /* no digits before the radix means we need digits after it */
683 if (s < send && isDIGIT(*s)) {
684 do {
685 s++;
686 } while (s < send && isDIGIT(*s));
687 if (valuep) {
688 /* integer approximation is valid - it's 0. */
689 *valuep = 0;
690 }
98994639 691 }
60939fb8 692 else
693 return 0;
694 } else if (*s == 'I' || *s == 'i') {
695 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
696 s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
697 s++; if (s < send && (*s == 'I' || *s == 'i')) {
698 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
699 s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
700 s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
701 s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
702 s++;
98994639 703 }
60939fb8 704 sawinf = 1;
aa8b85de 705 } else if (*s == 'N' || *s == 'n') {
706 /* XXX TODO: There are signaling NaNs and quiet NaNs. */
707 s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
708 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
709 s++;
710 sawnan = 1;
711 } else
98994639 712 return 0;
60939fb8 713
714 if (sawinf) {
715 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
716 numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
aa8b85de 717 } else if (sawnan) {
718 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
719 numtype |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
60939fb8 720 } else if (s < send) {
721 /* we can have an optional exponent part */
722 if (*s == 'e' || *s == 'E') {
723 /* The only flag we keep is sign. Blow away any "it's UV" */
724 numtype &= IS_NUMBER_NEG;
725 numtype |= IS_NUMBER_NOT_INT;
726 s++;
727 if (s < send && (*s == '-' || *s == '+'))
728 s++;
729 if (s < send && isDIGIT(*s)) {
730 do {
731 s++;
732 } while (s < send && isDIGIT(*s));
733 }
734 else
735 return 0;
736 }
737 }
738 while (s < send && isSPACE(*s))
739 s++;
740 if (s >= send)
aa8b85de 741 return numtype;
60939fb8 742 if (len == 10 && memEQ(pv, "0 but true", 10)) {
743 if (valuep)
744 *valuep = 0;
745 return IS_NUMBER_IN_UV;
746 }
747 return 0;
98994639 748}
749
4801ca72 750STATIC NV
98994639 751S_mulexp10(NV value, I32 exponent)
752{
753 NV result = 1.0;
754 NV power = 10.0;
755 bool negative = 0;
756 I32 bit;
757
758 if (exponent == 0)
759 return value;
20f6aaab 760 if (value == 0)
761 return 0;
87032ba1 762
24866caa 763 /* On OpenVMS VAX we by default use the D_FLOAT double format,
67597c89 764 * and that format does not have *easy* capabilities [1] for
24866caa 765 * overflowing doubles 'silently' as IEEE fp does. We also need
766 * to support G_FLOAT on both VAX and Alpha, and though the exponent
767 * range is much larger than D_FLOAT it still doesn't do silent
768 * overflow. Therefore we need to detect early whether we would
769 * overflow (this is the behaviour of the native string-to-float
770 * conversion routines, and therefore of native applications, too).
67597c89 771 *
24866caa 772 * [1] Trying to establish a condition handler to trap floating point
773 * exceptions is not a good idea. */
87032ba1 774
775 /* In UNICOS and in certain Cray models (such as T90) there is no
776 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
777 * There is something you can do if you are willing to use some
778 * inline assembler: the instruction is called DFI-- but that will
779 * disable *all* floating point interrupts, a little bit too large
780 * a hammer. Therefore we need to catch potential overflows before
781 * it's too late. */
353813d9 782
783#if ((defined(VMS) && !defined(__IEEE_FP)) || defined(_UNICOS)) && defined(NV_MAX_10_EXP)
784 STMT_START {
785 NV exp_v = log10(value);
786 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
787 return NV_MAX;
788 if (exponent < 0) {
789 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
790 return 0.0;
791 while (-exponent >= NV_MAX_10_EXP) {
792 /* combination does not overflow, but 10^(-exponent) does */
793 value /= 10;
794 ++exponent;
795 }
796 }
797 } STMT_END;
87032ba1 798#endif
799
353813d9 800 if (exponent < 0) {
801 negative = 1;
802 exponent = -exponent;
803 }
98994639 804 for (bit = 1; exponent; bit <<= 1) {
805 if (exponent & bit) {
806 exponent ^= bit;
807 result *= power;
236f0012 808 /* Floating point exceptions are supposed to be turned off,
809 * but if we're obviously done, don't risk another iteration.
810 */
811 if (exponent == 0) break;
98994639 812 }
813 power *= power;
814 }
815 return negative ? value / result : value * result;
816}
817
818NV
819Perl_my_atof(pTHX_ const char* s)
820{
821 NV x = 0.0;
822#ifdef USE_LOCALE_NUMERIC
823 if (PL_numeric_local && IN_LOCALE) {
824 NV y;
825
826 /* Scan the number twice; once using locale and once without;
827 * choose the larger result (in absolute value). */
a36244b7 828 Perl_atof2(s, x);
98994639 829 SET_NUMERIC_STANDARD();
a36244b7 830 Perl_atof2(s, y);
98994639 831 SET_NUMERIC_LOCAL();
832 if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
833 return y;
834 }
835 else
a36244b7 836 Perl_atof2(s, x);
98994639 837#else
a36244b7 838 Perl_atof2(s, x);
98994639 839#endif
840 return x;
841}
842
843char*
844Perl_my_atof2(pTHX_ const char* orig, NV* value)
845{
20f6aaab 846 NV result[3] = {0.0, 0.0, 0.0};
e1ec3a88 847 const char* s = orig;
a36244b7 848#ifdef USE_PERL_ATOF
20f6aaab 849 UV accumulator[2] = {0,0}; /* before/after dp */
a36244b7 850 bool negative = 0;
e1ec3a88 851 const char* send = s + strlen(orig) - 1;
8194bf88 852 bool seen_digit = 0;
20f6aaab 853 I32 exp_adjust[2] = {0,0};
854 I32 exp_acc[2] = {-1, -1};
855 /* the current exponent adjust for the accumulators */
98994639 856 I32 exponent = 0;
8194bf88 857 I32 seen_dp = 0;
20f6aaab 858 I32 digit = 0;
859 I32 old_digit = 0;
8194bf88 860 I32 sig_digits = 0; /* noof significant digits seen so far */
861
862/* There is no point in processing more significant digits
863 * than the NV can hold. Note that NV_DIG is a lower-bound value,
864 * while we need an upper-bound value. We add 2 to account for this;
865 * since it will have been conservative on both the first and last digit.
866 * For example a 32-bit mantissa with an exponent of 4 would have
867 * exact values in the set
868 * 4
869 * 8
870 * ..
871 * 17179869172
872 * 17179869176
873 * 17179869180
874 *
875 * where for the purposes of calculating NV_DIG we would have to discount
876 * both the first and last digit, since neither can hold all values from
877 * 0..9; but for calculating the value we must examine those two digits.
878 */
879#define MAX_SIG_DIGITS (NV_DIG+2)
880
881/* the max number we can accumulate in a UV, and still safely do 10*N+9 */
882#define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
98994639 883
96a05aee 884 /* leading whitespace */
885 while (isSPACE(*s))
886 ++s;
887
98994639 888 /* sign */
889 switch (*s) {
890 case '-':
891 negative = 1;
892 /* fall through */
893 case '+':
894 ++s;
895 }
896
8194bf88 897 /* we accumulate digits into an integer; when this becomes too
898 * large, we add the total to NV and start again */
98994639 899
8194bf88 900 while (1) {
901 if (isDIGIT(*s)) {
902 seen_digit = 1;
20f6aaab 903 old_digit = digit;
8194bf88 904 digit = *s++ - '0';
20f6aaab 905 if (seen_dp)
906 exp_adjust[1]++;
98994639 907
8194bf88 908 /* don't start counting until we see the first significant
909 * digit, eg the 5 in 0.00005... */
910 if (!sig_digits && digit == 0)
911 continue;
912
913 if (++sig_digits > MAX_SIG_DIGITS) {
98994639 914 /* limits of precision reached */
20f6aaab 915 if (digit > 5) {
916 ++accumulator[seen_dp];
917 } else if (digit == 5) {
918 if (old_digit % 2) { /* round to even - Allen */
919 ++accumulator[seen_dp];
920 }
921 }
922 if (seen_dp) {
923 exp_adjust[1]--;
924 } else {
925 exp_adjust[0]++;
926 }
8194bf88 927 /* skip remaining digits */
98994639 928 while (isDIGIT(*s)) {
98994639 929 ++s;
20f6aaab 930 if (! seen_dp) {
931 exp_adjust[0]++;
932 }
98994639 933 }
934 /* warn of loss of precision? */
98994639 935 }
8194bf88 936 else {
20f6aaab 937 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
8194bf88 938 /* add accumulator to result and start again */
20f6aaab 939 result[seen_dp] = S_mulexp10(result[seen_dp],
940 exp_acc[seen_dp])
941 + (NV)accumulator[seen_dp];
942 accumulator[seen_dp] = 0;
943 exp_acc[seen_dp] = 0;
98994639 944 }
20f6aaab 945 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
946 ++exp_acc[seen_dp];
98994639 947 }
8194bf88 948 }
e1ec3a88 949 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
8194bf88 950 seen_dp = 1;
20f6aaab 951 if (sig_digits > MAX_SIG_DIGITS) {
952 ++s;
953 while (isDIGIT(*s)) {
954 ++s;
955 }
956 break;
957 }
8194bf88 958 }
959 else {
960 break;
98994639 961 }
962 }
963
20f6aaab 964 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
965 if (seen_dp) {
966 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
967 }
98994639 968
8194bf88 969 if (seen_digit && (*s == 'e' || *s == 'E')) {
98994639 970 bool expnegative = 0;
971
972 ++s;
973 switch (*s) {
974 case '-':
975 expnegative = 1;
976 /* fall through */
977 case '+':
978 ++s;
979 }
980 while (isDIGIT(*s))
981 exponent = exponent * 10 + (*s++ - '0');
982 if (expnegative)
983 exponent = -exponent;
984 }
985
20f6aaab 986
987
98994639 988 /* now apply the exponent */
20f6aaab 989
990 if (seen_dp) {
991 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
992 + S_mulexp10(result[1],exponent-exp_adjust[1]);
993 } else {
994 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
995 }
98994639 996
997 /* now apply the sign */
998 if (negative)
20f6aaab 999 result[2] = -result[2];
a36244b7 1000#endif /* USE_PERL_ATOF */
20f6aaab 1001 *value = result[2];
73d840c0 1002 return (char *)s;
98994639 1003}
1004
55954f19 1005#if ! defined(HAS_MODFL) && defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
1006long double
1007Perl_my_modfl(long double x, long double *ip)
1008{
1009 *ip = aintl(x);
1010 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1011}
1012#endif
1013
1014#if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1015long double
1016Perl_my_frexpl(long double x, int *e) {
1017 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1018 return (scalbnl(x, -*e));
1019}
1020#endif
66610fdd 1021
1022/*
1023 * Local variables:
1024 * c-indentation-style: bsd
1025 * c-basic-offset: 4
1026 * indent-tabs-mode: t
1027 * End:
1028 *
1029 * vim: ts=8 sts=4 sw=4 noet:
1030*/