3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
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.
12 * "That only makes eleven (plus one mislaid) and not fourteen,
13 * unless wizards count differently to other people." --Beorn
15 * [p.115 of _The Hobbit_: "Queer Lodgings"]
19 =head1 Numeric functions
23 This file contains all the stuff needed by perl for manipulating numeric
24 values, including such things as replacements for the OS's atof() function
29 #define PERL_IN_NUMERIC_C
35 S_strtod(pTHX_ const char * const s, char ** e)
37 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
40 STORE_LC_NUMERIC_SET_TO_NEEDED();
44 result = strtoflt128(s, e);
46 # elif defined(HAS_STRTOLD) && defined(HAS_LONG_DOUBLE) \
47 && defined(USE_LONG_DOUBLE)
48 # if defined(__MINGW64_VERSION_MAJOR)
49 /***********************************************
50 We are unable to use strtold because of
51 https://sourceforge.net/p/mingw-w64/bugs/711/
53 https://sourceforge.net/p/mingw-w64/bugs/725/
55 but __mingw_strtold is fine.
56 ***********************************************/
58 result = __mingw_strtold(s, e);
62 result = strtold(s, e);
65 # elif defined(HAS_STRTOD)
67 result = strtod(s, e);
76 #endif /* #ifdef Perl_strtod */
82 This function is equivalent to the libc strtod() function, and is available
83 even on platforms that lack plain strtod(). Its return value is the best
84 available precision depending on platform capabilities and F<Configure>
87 It properly handles the locale radix character, meaning it expects a dot except
88 when called from within the scope of S<C<use locale>>, in which case the radix
89 character should be that specified by the current locale.
91 The synonym Strod() may be used instead.
98 my_strtod(const char * const s, char **e)
102 PERL_ARGS_ASSERT_MY_STRTOD;
106 return S_strtod(aTHX_ s, e);
112 char ** end_ptr = NULL;
114 *end_ptr = my_atof2(s, &result);
132 Perl_cast_ulong(NV f)
135 return f < I32_MIN ? (U32) I32_MIN : (U32)(I32) f;
136 if (f < U32_MAX_P1) {
138 if (f < U32_MAX_P1_HALF)
140 f -= U32_MAX_P1_HALF;
141 return ((U32) f) | (1 + (U32_MAX >> 1));
146 return f > 0 ? U32_MAX : 0 /* NaN */;
153 return f < I32_MIN ? I32_MIN : (I32) f;
154 if (f < U32_MAX_P1) {
156 if (f < U32_MAX_P1_HALF)
158 f -= U32_MAX_P1_HALF;
159 return (I32)(((U32) f) | (1 + (U32_MAX >> 1)));
164 return f > 0 ? (I32)U32_MAX : 0 /* NaN */;
171 return f < IV_MIN ? IV_MIN : (IV) f;
174 /* For future flexibility allowing for sizeof(UV) >= sizeof(IV) */
175 if (f < UV_MAX_P1_HALF)
178 return (IV)(((UV) f) | (1 + (UV_MAX >> 1)));
183 return f > 0 ? (IV)UV_MAX : 0 /* NaN */;
190 return f < IV_MIN ? (UV) IV_MIN : (UV)(IV) f;
193 if (f < UV_MAX_P1_HALF)
196 return ((UV) f) | (1 + (UV_MAX >> 1));
201 return f > 0 ? UV_MAX : 0 /* NaN */;
207 converts a string representing a binary number to numeric form.
209 On entry C<start> and C<*len> give the string to scan, C<*flags> gives
210 conversion flags, and C<result> should be C<NULL> or a pointer to an NV.
211 The scan stops at the end of the string, or the first invalid character.
212 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>, encountering an
213 invalid character will also trigger a warning.
214 On return C<*len> is set to the length of the scanned string,
215 and C<*flags> gives output flags.
217 If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
218 and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_bin>
219 returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
220 and writes the value to C<*result> (or the value is discarded if C<result>
223 The binary number may optionally be prefixed with C<"0b"> or C<"b"> unless
224 C<PERL_SCAN_DISALLOW_PREFIX> is set in C<*flags> on entry. If
225 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then the binary
226 number may use C<"_"> characters to separate digits.
230 Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE
231 which suppresses any message for non-portable numbers that are still valid
236 Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
238 const char *s = start;
243 const UV max_div_2 = UV_MAX / 2;
244 const bool allow_underscores = cBOOL(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
245 bool overflowed = FALSE;
248 PERL_ARGS_ASSERT_GROK_BIN;
250 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
251 /* strip off leading b or 0b.
252 for compatibility silently suffer "b" and "0b" as valid binary
255 if (isALPHA_FOLD_EQ(s[0], 'b')) {
259 else if (len >= 2 && s[0] == '0' && (isALPHA_FOLD_EQ(s[1], 'b'))) {
266 for (; len-- && (bit = *s); s++) {
267 if (bit == '0' || bit == '1') {
268 /* Write it in this wonky order with a goto to attempt to get the
269 compiler to make the common case integer-only loop pretty tight.
270 With gcc seems to be much straighter code than old scan_bin. */
273 if (value <= max_div_2) {
274 value = (value << 1) | (bit - '0');
277 /* Bah. We're just overflowed. */
278 /* diag_listed_as: Integer overflow in %s number */
279 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
280 "Integer overflow in binary number");
282 value_nv = (NV) value;
285 /* If an NV has not enough bits in its mantissa to
286 * represent a UV this summing of small low-order numbers
287 * is a waste of time (because the NV cannot preserve
288 * the low-order bits anyway): we could just remember when
289 * did we overflow and in the end just multiply value_nv by the
291 value_nv += (NV)(bit - '0');
294 if (bit == '_' && len && allow_underscores && (bit = s[1])
295 && (bit == '0' || bit == '1'))
301 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
302 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
303 "Illegal binary digit '%c' ignored", *s);
307 if ( ( overflowed && value_nv > 4294967295.0)
309 || (!overflowed && value > 0xffffffff
310 && ! (*flags & PERL_SCAN_SILENT_NON_PORTABLE))
313 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
314 "Binary number > 0b11111111111111111111111111111111 non-portable");
321 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
330 converts a string representing a hex number to numeric form.
332 On entry C<start> and C<*len_p> give the string to scan, C<*flags> gives
333 conversion flags, and C<result> should be C<NULL> or a pointer to an NV.
334 The scan stops at the end of the string, or the first invalid character.
335 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>, encountering an
336 invalid character will also trigger a warning.
337 On return C<*len> is set to the length of the scanned string,
338 and C<*flags> gives output flags.
340 If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
341 and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_hex>
342 returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
343 and writes the value to C<*result> (or the value is discarded if C<result>
346 The hex number may optionally be prefixed with C<"0x"> or C<"x"> unless
347 C<PERL_SCAN_DISALLOW_PREFIX> is set in C<*flags> on entry. If
348 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then the hex
349 number may use C<"_"> characters to separate digits.
353 Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE
354 which suppresses any message for non-portable numbers, but which are valid
359 Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
361 const char *s = start;
365 const UV max_div_16 = UV_MAX / 16;
366 const bool allow_underscores = cBOOL(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
367 bool overflowed = FALSE;
369 PERL_ARGS_ASSERT_GROK_HEX;
371 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
372 /* strip off leading x or 0x.
373 for compatibility silently suffer "x" and "0x" as valid hex numbers.
376 if (isALPHA_FOLD_EQ(s[0], 'x')) {
380 else if (len >= 2 && s[0] == '0' && (isALPHA_FOLD_EQ(s[1], 'x'))) {
387 for (; len-- && *s; s++) {
389 /* Write it in this wonky order with a goto to attempt to get the
390 compiler to make the common case integer-only loop pretty tight.
391 With gcc seems to be much straighter code than old scan_hex. */
394 if (value <= max_div_16) {
395 value = (value << 4) | XDIGIT_VALUE(*s);
398 /* Bah. We're just overflowed. */
399 /* diag_listed_as: Integer overflow in %s number */
400 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
401 "Integer overflow in hexadecimal number");
403 value_nv = (NV) value;
406 /* If an NV has not enough bits in its mantissa to
407 * represent a UV this summing of small low-order numbers
408 * is a waste of time (because the NV cannot preserve
409 * the low-order bits anyway): we could just remember when
410 * did we overflow and in the end just multiply value_nv by the
411 * right amount of 16-tuples. */
412 value_nv += (NV) XDIGIT_VALUE(*s);
415 if (*s == '_' && len && allow_underscores && s[1]
422 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
423 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
424 "Illegal hexadecimal digit '%c' ignored", *s);
428 if ( ( overflowed && value_nv > 4294967295.0)
430 || (!overflowed && value > 0xffffffff
431 && ! (*flags & PERL_SCAN_SILENT_NON_PORTABLE))
434 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
435 "Hexadecimal number > 0xffffffff non-portable");
442 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
451 converts a string representing an octal number to numeric form.
453 On entry C<start> and C<*len> give the string to scan, C<*flags> gives
454 conversion flags, and C<result> should be C<NULL> or a pointer to an NV.
455 The scan stops at the end of the string, or the first invalid character.
456 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>, encountering an
457 8 or 9 will also trigger a warning.
458 On return C<*len> is set to the length of the scanned string,
459 and C<*flags> gives output flags.
461 If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
462 and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_oct>
463 returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
464 and writes the value to C<*result> (or the value is discarded if C<result>
467 If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then the octal
468 number may use C<"_"> characters to separate digits.
472 Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE>
473 which suppresses any message for non-portable numbers, but which are valid
478 Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
480 const char *s = start;
484 const UV max_div_8 = UV_MAX / 8;
485 const bool allow_underscores = cBOOL(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
486 bool overflowed = FALSE;
488 PERL_ARGS_ASSERT_GROK_OCT;
490 for (; len-- && *s; s++) {
492 /* Write it in this wonky order with a goto to attempt to get the
493 compiler to make the common case integer-only loop pretty tight.
497 if (value <= max_div_8) {
498 value = (value << 3) | OCTAL_VALUE(*s);
501 /* Bah. We're just overflowed. */
502 /* diag_listed_as: Integer overflow in %s number */
503 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
504 "Integer overflow in octal number");
506 value_nv = (NV) value;
509 /* If an NV has not enough bits in its mantissa to
510 * represent a UV this summing of small low-order numbers
511 * is a waste of time (because the NV cannot preserve
512 * the low-order bits anyway): we could just remember when
513 * did we overflow and in the end just multiply value_nv by the
514 * right amount of 8-tuples. */
515 value_nv += (NV) OCTAL_VALUE(*s);
518 if (*s == '_' && len && allow_underscores && isOCTAL(s[1])) {
523 /* Allow \octal to work the DWIM way (that is, stop scanning
524 * as soon as non-octal characters are seen, complain only if
525 * someone seems to want to use the digits eight and nine. Since we
526 * know it is not octal, then if isDIGIT, must be an 8 or 9). */
528 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
529 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
530 "Illegal octal digit '%c' ignored", *s);
535 if ( ( overflowed && value_nv > 4294967295.0)
537 || (!overflowed && value > 0xffffffff
538 && ! (*flags & PERL_SCAN_SILENT_NON_PORTABLE))
541 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
542 "Octal number > 037777777777 non-portable");
549 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
558 For backwards compatibility. Use C<grok_bin> instead.
562 For backwards compatibility. Use C<grok_hex> instead.
566 For backwards compatibility. Use C<grok_oct> instead.
572 Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
575 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
576 const UV ruv = grok_bin (start, &len, &flags, &rnv);
578 PERL_ARGS_ASSERT_SCAN_BIN;
581 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
585 Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
588 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
589 const UV ruv = grok_oct (start, &len, &flags, &rnv);
591 PERL_ARGS_ASSERT_SCAN_OCT;
594 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
598 Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
601 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
602 const UV ruv = grok_hex (start, &len, &flags, &rnv);
604 PERL_ARGS_ASSERT_SCAN_HEX;
607 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
611 =for apidoc grok_numeric_radix
613 Scan and skip for a numeric decimal separator (radix).
618 Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
620 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
622 #ifdef USE_LOCALE_NUMERIC
624 if (IN_LC(LC_NUMERIC)) {
627 bool matches_radix = FALSE;
628 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
630 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
632 radix = SvPV(PL_numeric_radix_sv, len);
633 radix = savepvn(radix, len);
635 RESTORE_LC_NUMERIC();
637 if (*sp + len <= send) {
638 matches_radix = memEQ(*sp, radix, len);
651 /* always try "." if numeric radix didn't match because
652 * we may have data from different locales mixed */
653 if (*sp < send && **sp == '.') {
662 =for apidoc grok_infnan
664 Helper for C<grok_number()>, accepts various ways of spelling "infinity"
665 or "not a number", and returns one of the following flag combinations:
669 IS_NUMBER_INFINITY | IS_NUMBER_NEG
670 IS_NUMBER_NAN | IS_NUMBER_NEG
673 possibly |-ed with C<IS_NUMBER_TRAILING>.
675 If an infinity or a not-a-number is recognized, C<*sp> will point to
676 one byte past the end of the recognized string. If the recognition fails,
677 zero is returned, and C<*sp> will not move.
683 Perl_grok_infnan(pTHX_ const char** sp, const char* send)
687 #if defined(NV_INF) || defined(NV_NAN)
688 bool odh = FALSE; /* one-dot-hash: 1.#INF */
690 PERL_ARGS_ASSERT_GROK_INFNAN;
693 s++; if (s == send) return 0;
695 else if (*s == '-') {
696 flags |= IS_NUMBER_NEG; /* Yes, -NaN happens. Incorrect but happens. */
697 s++; if (s == send) return 0;
701 /* Visual C: 1.#SNAN, -1.#QNAN, 1#INF, 1.#IND (maybe also 1.#NAN)
702 * Let's keep the dot optional. */
703 s++; if (s == send) return 0;
705 s++; if (s == send) return 0;
708 s++; if (s == send) return 0;
714 if (isALPHA_FOLD_EQ(*s, 'I')) {
715 /* INF or IND (1.#IND is "indeterminate", a certain type of NAN) */
717 s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return 0;
718 s++; if (s == send) return 0;
719 if (isALPHA_FOLD_EQ(*s, 'F')) {
721 if (s < send && (isALPHA_FOLD_EQ(*s, 'I'))) {
723 flags | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT | IS_NUMBER_TRAILING;
724 s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return fail;
725 s++; if (s == send || isALPHA_FOLD_NE(*s, 'I')) return fail;
726 s++; if (s == send || isALPHA_FOLD_NE(*s, 'T')) return fail;
727 s++; if (s == send || isALPHA_FOLD_NE(*s, 'Y')) return fail;
730 while (*s == '0') { /* 1.#INF00 */
734 while (s < send && isSPACE(*s))
736 if (s < send && *s) {
737 flags |= IS_NUMBER_TRAILING;
739 flags |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
741 else if (isALPHA_FOLD_EQ(*s, 'D') && odh) { /* 1.#IND */
743 flags |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
744 while (*s == '0') { /* 1.#IND00 */
748 flags |= IS_NUMBER_TRAILING;
754 /* Maybe NAN of some sort */
756 if (isALPHA_FOLD_EQ(*s, 'S') || isALPHA_FOLD_EQ(*s, 'Q')) {
758 /* XXX do something with the snan/qnan difference */
759 s++; if (s == send) return 0;
762 if (isALPHA_FOLD_EQ(*s, 'N')) {
763 s++; if (s == send || isALPHA_FOLD_NE(*s, 'A')) return 0;
764 s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return 0;
767 flags |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
769 /* NaN can be followed by various stuff (NaNQ, NaNS), but
770 * there are also multiple different NaN values, and some
771 * implementations output the "payload" values,
772 * e.g. NaN123, NAN(abc), while some legacy implementations
773 * have weird stuff like NaN%. */
774 if (isALPHA_FOLD_EQ(*s, 'q') ||
775 isALPHA_FOLD_EQ(*s, 's')) {
776 /* "nanq" or "nans" are ok, though generating
777 * these portably is tricky. */
781 /* C99 style "nan(123)" or Perlish equivalent "nan($uv)". */
785 return flags | IS_NUMBER_TRAILING;
788 while (t < send && *t && *t != ')') {
792 return flags | IS_NUMBER_TRAILING;
797 if (s[0] == '0' && s + 2 < t &&
798 isALPHA_FOLD_EQ(s[1], 'x') &&
801 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
802 nanval = grok_hex(s, &len, &flags, NULL);
803 if ((flags & PERL_SCAN_GREATER_THAN_UV_MAX)) {
806 nantype = IS_NUMBER_IN_UV;
809 } else if (s[0] == '0' && s + 2 < t &&
810 isALPHA_FOLD_EQ(s[1], 'b') &&
811 (s[2] == '0' || s[2] == '1')) {
813 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
814 nanval = grok_bin(s, &len, &flags, NULL);
815 if ((flags & PERL_SCAN_GREATER_THAN_UV_MAX)) {
818 nantype = IS_NUMBER_IN_UV;
824 grok_number_flags(s, t - s, &nanval,
826 PERL_SCAN_ALLOW_UNDERSCORES);
827 /* Unfortunately grok_number_flags() doesn't
828 * tell how far we got and the ')' will always
829 * be "trailing", so we need to double-check
830 * whether we had something dubious. */
831 for (u = s; u < t; u++) {
833 flags |= IS_NUMBER_TRAILING;
840 /* XXX Doesn't do octal: nan("0123").
841 * Probably not a big loss. */
843 if ((nantype & IS_NUMBER_NOT_INT) ||
844 !(nantype && IS_NUMBER_IN_UV)) {
845 /* XXX the nanval is currently unused, that is,
846 * not inserted as the NaN payload of the NV.
847 * But the above code already parses the C99
848 * nan(...) format. See below, and see also
849 * the nan() in POSIX.xs.
851 * Certain configuration combinations where
852 * NVSIZE is greater than UVSIZE mean that
853 * a single UV cannot contain all the possible
854 * NaN payload bits. There would need to be
855 * some more generic syntax than "nan($uv)".
857 * Issues to keep in mind:
859 * (1) In most common cases there would
860 * not be an integral number of bytes that
861 * could be set, only a certain number of bits.
862 * For example for the common case of
863 * NVSIZE == UVSIZE == 8 there is room for 52
864 * bits in the payload, but the most significant
865 * bit is commonly reserved for the
866 * signaling/quiet bit, leaving 51 bits.
867 * Furthermore, the C99 nan() is supposed
868 * to generate quiet NaNs, so it is doubtful
869 * whether it should be able to generate
870 * signaling NaNs. For the x86 80-bit doubles
871 * (if building a long double Perl) there would
872 * be 62 bits (s/q bit being the 63rd).
874 * (2) Endianness of the payload bits. If the
875 * payload is specified as an UV, the low-order
876 * bits of the UV are naturally little-endianed
877 * (rightmost) bits of the payload. The endianness
878 * of UVs and NVs can be different. */
882 flags |= IS_NUMBER_TRAILING;
885 /* Looked like nan(...), but no close paren. */
886 flags |= IS_NUMBER_TRAILING;
889 while (s < send && isSPACE(*s))
891 if (s < send && *s) {
892 /* Note that we here implicitly accept (parse as
893 * "nan", but with warnings) also any other weird
894 * trailing stuff for "nan". In the above we just
895 * check that if we got the C99-style "nan(...)",
896 * the "..." looks sane.
897 * If in future we accept more ways of specifying
898 * the nan payload, the accepting would happen around
900 flags |= IS_NUMBER_TRAILING;
909 while (s < send && isSPACE(*s))
913 PERL_UNUSED_ARG(send);
914 #endif /* #if defined(NV_INF) || defined(NV_NAN) */
920 =for apidoc grok_number_flags
922 Recognise (or not) a number. The type of the number is returned
923 (0 if unrecognised), otherwise it is a bit-ORed combination of
924 C<IS_NUMBER_IN_UV>, C<IS_NUMBER_GREATER_THAN_UV_MAX>, C<IS_NUMBER_NOT_INT>,
925 C<IS_NUMBER_NEG>, C<IS_NUMBER_INFINITY>, C<IS_NUMBER_NAN> (defined in perl.h).
927 If the value of the number can fit in a UV, it is returned in C<*valuep>.
928 C<IS_NUMBER_IN_UV> will be set to indicate that C<*valuep> is valid, C<IS_NUMBER_IN_UV>
929 will never be set unless C<*valuep> is valid, but C<*valuep> may have been assigned
930 to during processing even though C<IS_NUMBER_IN_UV> is not set on return.
931 If C<valuep> is C<NULL>, C<IS_NUMBER_IN_UV> will be set for the same cases as when
932 C<valuep> is non-C<NULL>, but no actual assignment (or SEGV) will occur.
934 C<IS_NUMBER_NOT_INT> will be set with C<IS_NUMBER_IN_UV> if trailing decimals were
935 seen (in which case C<*valuep> gives the true value truncated to an integer), and
936 C<IS_NUMBER_NEG> if the number is negative (in which case C<*valuep> holds the
937 absolute value). C<IS_NUMBER_IN_UV> is not set if e notation was used or the
938 number is larger than a UV.
940 C<flags> allows only C<PERL_SCAN_TRAILING>, which allows for trailing
941 non-numeric text on an otherwise successful I<grok>, setting
942 C<IS_NUMBER_TRAILING> on the result.
944 =for apidoc grok_number
946 Identical to C<grok_number_flags()> with C<flags> set to zero.
951 Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
953 PERL_ARGS_ASSERT_GROK_NUMBER;
955 return grok_number_flags(pv, len, valuep, 0);
958 static const UV uv_max_div_10 = UV_MAX / 10;
959 static const U8 uv_max_mod_10 = UV_MAX % 10;
962 Perl_grok_number_flags(pTHX_ const char *pv, STRLEN len, UV *valuep, U32 flags)
965 const char * const send = pv + len;
969 PERL_ARGS_ASSERT_GROK_NUMBER_FLAGS;
971 while (s < send && isSPACE(*s))
975 } else if (*s == '-') {
977 numtype = IS_NUMBER_NEG;
985 /* The first digit (after optional sign): note that might
986 * also point to "infinity" or "nan", or "1.#INF". */
989 /* next must be digit or the radix separator or beginning of infinity/nan */
991 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
994 /* This construction seems to be more optimiser friendly.
995 (without it gcc does the isDIGIT test and the *s - '0' separately)
996 With it gcc on arm is managing 6 instructions (6 cycles) per digit.
997 In theory the optimiser could deduce how far to unroll the loop
998 before checking for overflow. */
1000 int digit = *s - '0';
1001 if (inRANGE(digit, 0, 9)) {
1002 value = value * 10 + digit;
1005 if (inRANGE(digit, 0, 9)) {
1006 value = value * 10 + digit;
1009 if (inRANGE(digit, 0, 9)) {
1010 value = value * 10 + digit;
1013 if (inRANGE(digit, 0, 9)) {
1014 value = value * 10 + digit;
1017 if (inRANGE(digit, 0, 9)) {
1018 value = value * 10 + digit;
1021 if (inRANGE(digit, 0, 9)) {
1022 value = value * 10 + digit;
1025 if (inRANGE(digit, 0, 9)) {
1026 value = value * 10 + digit;
1029 if (inRANGE(digit, 0, 9)) {
1030 value = value * 10 + digit;
1032 /* Now got 9 digits, so need to check
1033 each time for overflow. */
1035 while ( inRANGE(digit, 0, 9)
1036 && (value < uv_max_div_10
1037 || (value == uv_max_div_10
1038 && digit <= uv_max_mod_10))) {
1039 value = value * 10 + digit;
1045 if (inRANGE(digit, 0, 9)
1047 /* value overflowed.
1048 skip the remaining digits, don't
1049 worry about setting *valuep. */
1052 } while (s < send && isDIGIT(*s));
1054 IS_NUMBER_GREATER_THAN_UV_MAX;
1074 numtype |= IS_NUMBER_IN_UV;
1079 if (GROK_NUMERIC_RADIX(&s, send)) {
1080 numtype |= IS_NUMBER_NOT_INT;
1081 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
1085 else if (GROK_NUMERIC_RADIX(&s, send)) {
1086 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
1087 /* no digits before the radix means we need digits after it */
1088 if (s < send && isDIGIT(*s)) {
1091 } while (s < send && isDIGIT(*s));
1093 /* integer approximation is valid - it's 0. */
1101 if (s > d && s < send) {
1102 /* we can have an optional exponent part */
1103 if (isALPHA_FOLD_EQ(*s, 'e')) {
1105 if (s < send && (*s == '-' || *s == '+'))
1107 if (s < send && isDIGIT(*s)) {
1110 } while (s < send && isDIGIT(*s));
1112 else if (flags & PERL_SCAN_TRAILING)
1113 return numtype | IS_NUMBER_TRAILING;
1117 /* The only flag we keep is sign. Blow away any "it's UV" */
1118 numtype &= IS_NUMBER_NEG;
1119 numtype |= IS_NUMBER_NOT_INT;
1122 while (s < send && isSPACE(*s))
1126 if (memEQs(pv, len, "0 but true")) {
1129 return IS_NUMBER_IN_UV;
1131 /* We could be e.g. at "Inf" or "NaN", or at the "#" of "1.#INF". */
1132 if ((s + 2 < send) && strchr("inqs#", toFOLD(*s))) {
1133 /* Really detect inf/nan. Start at d, not s, since the above
1134 * code might have already consumed the "1." or "1". */
1135 const int infnan = Perl_grok_infnan(aTHX_ &d, send);
1136 if ((infnan & IS_NUMBER_INFINITY)) {
1137 return (numtype | infnan); /* Keep sign for infinity. */
1139 else if ((infnan & IS_NUMBER_NAN)) {
1140 return (numtype | infnan) & ~IS_NUMBER_NEG; /* Clear sign for nan. */
1143 else if (flags & PERL_SCAN_TRAILING) {
1144 return numtype | IS_NUMBER_TRAILING;
1151 =for apidoc grok_atoUV
1153 parse a string, looking for a decimal unsigned integer.
1155 On entry, C<pv> points to the beginning of the string;
1156 C<valptr> points to a UV that will receive the converted value, if found;
1157 C<endptr> is either NULL or points to a variable that points to one byte
1158 beyond the point in C<pv> that this routine should examine.
1159 If C<endptr> is NULL, C<pv> is assumed to be NUL-terminated.
1161 Returns FALSE if C<pv> doesn't represent a valid unsigned integer value (with
1162 no leading zeros). Otherwise it returns TRUE, and sets C<*valptr> to that
1165 If you constrain the portion of C<pv> that is looked at by this function (by
1166 passing a non-NULL C<endptr>), and if the intial bytes of that portion form a
1167 valid value, it will return TRUE, setting C<*endptr> to the byte following the
1168 final digit of the value. But if there is no constraint at what's looked at,
1169 all of C<pv> must be valid in order for TRUE to be returned.
1171 The only characters this accepts are the decimal digits '0'..'9'.
1173 As opposed to L<atoi(3)> or L<strtol(3)>, C<grok_atoUV> does NOT allow optional
1174 leading whitespace, nor negative inputs. If such features are required, the
1175 calling code needs to explicitly implement those.
1177 Note that this function returns FALSE for inputs that would overflow a UV,
1178 or have leading zeros. Thus a single C<0> is accepted, but not C<00> nor
1179 C<01>, C<002>, I<etc>.
1181 Background: C<atoi> has severe problems with illegal inputs, it cannot be
1182 used for incremental parsing, and therefore should be avoided
1183 C<atoi> and C<strtol> are also affected by locale settings, which can also be
1184 seen as a bug (global state controlled by user environment).
1191 Perl_grok_atoUV(const char *pv, UV *valptr, const char** endptr)
1195 const char* end2; /* Used in case endptr is NULL. */
1196 UV val = 0; /* The parsed value. */
1198 PERL_ARGS_ASSERT_GROK_ATOUV;
1204 end2 = s + strlen(s);
1214 /* Single-digit inputs are quite common. */
1216 if (s < *eptr && isDIGIT(*s)) {
1217 /* Fail on extra leading zeros. */
1220 while (s < *eptr && isDIGIT(*s)) {
1221 /* This could be unrolled like in grok_number(), but
1222 * the expected uses of this are not speed-needy, and
1223 * unlikely to need full 64-bitness. */
1224 const U8 digit = *s++ - '0';
1225 if (val < uv_max_div_10 ||
1226 (val == uv_max_div_10 && digit <= uv_max_mod_10)) {
1227 val = val * 10 + digit;
1234 if (endptr == NULL) {
1236 return FALSE; /* If endptr is NULL, no trailing non-digits allowed. */
1249 S_mulexp10(NV value, I32 exponent)
1261 /* On OpenVMS VAX we by default use the D_FLOAT double format,
1262 * and that format does not have *easy* capabilities [1] for
1263 * overflowing doubles 'silently' as IEEE fp does. We also need
1264 * to support G_FLOAT on both VAX and Alpha, and though the exponent
1265 * range is much larger than D_FLOAT it still doesn't do silent
1266 * overflow. Therefore we need to detect early whether we would
1267 * overflow (this is the behaviour of the native string-to-float
1268 * conversion routines, and therefore of native applications, too).
1270 * [1] Trying to establish a condition handler to trap floating point
1271 * exceptions is not a good idea. */
1273 /* In UNICOS and in certain Cray models (such as T90) there is no
1274 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
1275 * There is something you can do if you are willing to use some
1276 * inline assembler: the instruction is called DFI-- but that will
1277 * disable *all* floating point interrupts, a little bit too large
1278 * a hammer. Therefore we need to catch potential overflows before
1281 #if ((defined(VMS) && !defined(_IEEE_FP)) || defined(_UNICOS) || defined(DOUBLE_IS_VAX_FLOAT)) && defined(NV_MAX_10_EXP)
1283 const NV exp_v = log10(value);
1284 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
1287 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
1289 while (-exponent >= NV_MAX_10_EXP) {
1290 /* combination does not overflow, but 10^(-exponent) does */
1300 exponent = -exponent;
1301 #ifdef NV_MAX_10_EXP
1302 /* for something like 1234 x 10^-309, the action of calculating
1303 * the intermediate value 10^309 then returning 1234 / (10^309)
1304 * will fail, since 10^309 becomes infinity. In this case try to
1305 * refactor it as 123 / (10^308) etc.
1307 while (value && exponent > NV_MAX_10_EXP) {
1315 #if defined(__osf__)
1316 /* Even with cc -ieee + ieee_set_fp_control(IEEE_TRAP_ENABLE_INV)
1317 * Tru64 fp behavior on inf/nan is somewhat broken. Another way
1318 * to do this would be ieee_set_fp_control(IEEE_TRAP_ENABLE_OVF)
1319 * but that breaks another set of infnan.t tests. */
1320 # define FP_OVERFLOWS_TO_ZERO
1322 for (bit = 1; exponent; bit <<= 1) {
1323 if (exponent & bit) {
1326 #ifdef FP_OVERFLOWS_TO_ZERO
1329 return value < 0 ? -NV_INF : NV_INF;
1331 return value < 0 ? -FLT_MAX : FLT_MAX;
1334 /* Floating point exceptions are supposed to be turned off,
1335 * but if we're obviously done, don't risk another iteration.
1337 if (exponent == 0) break;
1341 return negative ? value / result : value * result;
1343 #endif /* #ifndef Perl_strtod */
1346 # define ATOF(s, x) my_atof2(s, &x)
1348 # define ATOF(s, x) Perl_atof2(s, x)
1352 Perl_my_atof(pTHX_ const char* s)
1354 /* 's' must be NUL terminated */
1358 PERL_ARGS_ASSERT_MY_ATOF;
1360 #if ! defined(USE_LOCALE_NUMERIC)
1367 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
1368 STORE_LC_NUMERIC_SET_TO_NEEDED();
1369 if (! (PL_numeric_radix_sv && IN_LC(LC_NUMERIC))) {
1374 /* Look through the string for the first thing that looks like a
1375 * decimal point: either the value in the current locale or the
1376 * standard fallback of '.'. The one which appears earliest in the
1377 * input string is the one that we should have atof look for. Note
1378 * that we have to determine this beforehand because on some
1379 * systems, Perl_atof2 is just a wrapper around the system's atof.
1381 const char * const standard_pos = strchr(s, '.');
1382 const char * const local_pos
1383 = strstr(s, SvPV_nolen(PL_numeric_radix_sv));
1384 const bool use_standard_radix
1385 = standard_pos && (!local_pos || standard_pos < local_pos);
1387 if (use_standard_radix) {
1388 SET_NUMERIC_STANDARD();
1389 LOCK_LC_NUMERIC_STANDARD();
1394 if (use_standard_radix) {
1395 UNLOCK_LC_NUMERIC_STANDARD();
1396 SET_NUMERIC_UNDERLYING();
1399 RESTORE_LC_NUMERIC();
1407 #if defined(NV_INF) || defined(NV_NAN)
1410 # pragma warning(push)
1411 # pragma warning(disable:4756;disable:4056)
1414 S_my_atof_infnan(pTHX_ const char* s, bool negative, const char* send, NV* value)
1416 const char *p0 = negative ? s - 1 : s;
1418 const int infnan = grok_infnan(&p, send);
1419 if (infnan && p != p0) {
1420 /* If we can generate inf/nan directly, let's do so. */
1422 if ((infnan & IS_NUMBER_INFINITY)) {
1423 *value = (infnan & IS_NUMBER_NEG) ? -NV_INF: NV_INF;
1428 if ((infnan & IS_NUMBER_NAN)) {
1434 /* If still here, we didn't have either NV_INF or NV_NAN,
1435 * and can try falling back to native strtod/strtold.
1437 * The native interface might not recognize all the possible
1438 * inf/nan strings Perl recognizes. What we can try
1439 * is to try faking the input. We will try inf/-inf/nan
1440 * as the most promising/portable input. */
1442 const char* fake = "silence compiler warning";
1446 if ((infnan & IS_NUMBER_INFINITY)) {
1447 fake = ((infnan & IS_NUMBER_NEG)) ? "-inf" : "inf";
1451 if ((infnan & IS_NUMBER_NAN)) {
1455 assert(strNE(fake, "silence compiler warning"));
1456 nv = S_strtod(aTHX_ fake, &endp);
1459 if ((infnan & IS_NUMBER_INFINITY)) {
1464 /* last resort, may generate SIGFPE */
1465 *value = Perl_exp((NV)1e9);
1466 if ((infnan & IS_NUMBER_NEG))
1469 return (char*)p; /* p, not endp */
1473 if ((infnan & IS_NUMBER_NAN)) {
1478 /* last resort, may generate SIGFPE */
1479 *value = Perl_log((NV)-1.0);
1481 return (char*)p; /* p, not endp */
1486 #endif /* #ifdef Perl_strtod */
1491 # pragma warning(pop)
1494 #endif /* if defined(NV_INF) || defined(NV_NAN) */
1497 Perl_my_atof2(pTHX_ const char* orig, NV* value)
1499 PERL_ARGS_ASSERT_MY_ATOF2;
1500 return my_atof3(orig, value, 0);
1504 Perl_my_atof3(pTHX_ const char* orig, NV* value, const STRLEN len)
1506 const char* s = orig;
1507 NV result[3] = {0.0, 0.0, 0.0};
1508 #if defined(USE_PERL_ATOF) || defined(Perl_strtod)
1509 const char* send = s + ((len != 0)
1511 : strlen(orig)); /* one past the last */
1514 #if defined(USE_PERL_ATOF) && !defined(Perl_strtod)
1515 UV accumulator[2] = {0,0}; /* before/after dp */
1516 bool seen_digit = 0;
1517 I32 exp_adjust[2] = {0,0};
1518 I32 exp_acc[2] = {-1, -1};
1519 /* the current exponent adjust for the accumulators */
1524 I32 sig_digits = 0; /* noof significant digits seen so far */
1527 #if defined(USE_PERL_ATOF) || defined(Perl_strtod)
1528 PERL_ARGS_ASSERT_MY_ATOF3;
1530 /* leading whitespace */
1531 while (s < send && isSPACE(*s))
1549 if ((endp = S_my_atof_infnan(aTHX_ s, negative, send, value)))
1552 /* If the length is passed in, the input string isn't NUL-terminated,
1553 * and in it turns out the function below assumes it is; therefore we
1554 * create a copy and NUL-terminate that */
1556 Newx(copy, len + 1, char);
1557 Copy(orig, copy, len, char);
1559 s = copy + (s - orig);
1562 result[2] = S_strtod(aTHX_ s, &endp);
1564 /* If we created a copy, 'endp' is in terms of that. Convert back to
1567 s = (s - copy) + (char *) orig;
1568 endp = (endp - copy) + (char *) orig;
1573 *value = negative ? -result[2] : result[2];
1578 #elif defined(USE_PERL_ATOF)
1580 /* There is no point in processing more significant digits
1581 * than the NV can hold. Note that NV_DIG is a lower-bound value,
1582 * while we need an upper-bound value. We add 2 to account for this;
1583 * since it will have been conservative on both the first and last digit.
1584 * For example a 32-bit mantissa with an exponent of 4 would have
1585 * exact values in the set
1593 * where for the purposes of calculating NV_DIG we would have to discount
1594 * both the first and last digit, since neither can hold all values from
1595 * 0..9; but for calculating the value we must examine those two digits.
1597 #ifdef MAX_SIG_DIG_PLUS
1598 /* It is not necessarily the case that adding 2 to NV_DIG gets all the
1599 possible digits in a NV, especially if NVs are not IEEE compliant
1600 (e.g., long doubles on IRIX) - Allen <allens@cpan.org> */
1601 # define MAX_SIG_DIGITS (NV_DIG+MAX_SIG_DIG_PLUS)
1603 # define MAX_SIG_DIGITS (NV_DIG+2)
1606 /* the max number we can accumulate in a UV, and still safely do 10*N+9 */
1607 #define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
1609 #if defined(NV_INF) || defined(NV_NAN)
1612 if ((endp = S_my_atof_infnan(aTHX_ s, negative, send, value)))
1617 /* we accumulate digits into an integer; when this becomes too
1618 * large, we add the total to NV and start again */
1628 /* don't start counting until we see the first significant
1629 * digit, eg the 5 in 0.00005... */
1630 if (!sig_digits && digit == 0)
1633 if (++sig_digits > MAX_SIG_DIGITS) {
1634 /* limits of precision reached */
1636 ++accumulator[seen_dp];
1637 } else if (digit == 5) {
1638 if (old_digit % 2) { /* round to even - Allen */
1639 ++accumulator[seen_dp];
1647 /* skip remaining digits */
1648 while (s < send && isDIGIT(*s)) {
1654 /* warn of loss of precision? */
1657 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
1658 /* add accumulator to result and start again */
1659 result[seen_dp] = S_mulexp10(result[seen_dp],
1661 + (NV)accumulator[seen_dp];
1662 accumulator[seen_dp] = 0;
1663 exp_acc[seen_dp] = 0;
1665 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
1669 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
1671 if (sig_digits > MAX_SIG_DIGITS) {
1672 while (s < send && isDIGIT(*s)) {
1683 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
1685 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
1688 if (s < send && seen_digit && (isALPHA_FOLD_EQ(*s, 'e'))) {
1689 bool expnegative = 0;
1699 while (s < send && isDIGIT(*s))
1700 exponent = exponent * 10 + (*s++ - '0');
1702 exponent = -exponent;
1705 /* now apply the exponent */
1708 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
1709 + S_mulexp10(result[1],exponent-exp_adjust[1]);
1711 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
1714 /* now apply the sign */
1716 result[2] = -result[2];
1717 #endif /* USE_PERL_ATOF */
1723 =for apidoc isinfnan
1725 C<Perl_isinfnan()> is utility function that returns true if the NV
1726 argument is either an infinity or a C<NaN>, false otherwise. To test
1727 in more detail, use C<Perl_isinf()> and C<Perl_isnan()>.
1729 This is also the logical inverse of Perl_isfinite().
1734 Perl_isinfnan(NV nv)
1736 PERL_UNUSED_ARG(nv);
1751 Checks whether the argument would be either an infinity or C<NaN> when used
1752 as a number, but is careful not to trigger non-numeric or uninitialized
1753 warnings. it assumes the caller has done C<SvGETMAGIC(sv)> already.
1759 Perl_isinfnansv(pTHX_ SV *sv)
1761 PERL_ARGS_ASSERT_ISINFNANSV;
1765 return Perl_isinfnan(SvNVX(sv));
1770 const char *s = SvPV_nomg_const(sv, len);
1771 return cBOOL(grok_infnan(&s, s+len));
1776 /* C99 has truncl, pre-C99 Solaris had aintl. We can use either with
1777 * copysignl to emulate modfl, which is in some platforms missing or
1779 # if defined(HAS_TRUNCL) && defined(HAS_COPYSIGNL)
1781 Perl_my_modfl(long double x, long double *ip)
1784 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1786 # elif defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
1788 Perl_my_modfl(long double x, long double *ip)
1791 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1796 /* Similarly, with ilogbl and scalbnl we can emulate frexpl. */
1797 #if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1799 Perl_my_frexpl(long double x, int *e) {
1800 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1801 return (scalbnl(x, -*e));
1806 =for apidoc Perl_signbit
1808 Return a non-zero integer if the sign bit on an NV is set, and 0 if
1811 If F<Configure> detects this system has a C<signbit()> that will work with
1812 our NVs, then we just use it via the C<#define> in F<perl.h>. Otherwise,
1813 fall back on this implementation. The main use of this function
1814 is catching C<-0.0>.
1816 C<Configure> notes: This function is called C<'Perl_signbit'> instead of a
1817 plain C<'signbit'> because it is easy to imagine a system having a C<signbit()>
1818 function or macro that doesn't happen to work with our particular choice
1819 of NVs. We shouldn't just re-C<#define> C<signbit> as C<Perl_signbit> and expect
1820 the standard system headers to be happy. Also, this is a no-context
1821 function (no C<pTHX_>) because C<Perl_signbit()> is usually re-C<#defined> in
1822 F<perl.h> as a simple macro call to the system's C<signbit()>.
1823 Users should just always call C<Perl_signbit()>.
1827 #if !defined(HAS_SIGNBIT)
1829 Perl_signbit(NV x) {
1830 # ifdef Perl_fp_class_nzero
1831 return Perl_fp_class_nzero(x);
1832 /* Try finding the high byte, and assume it's highest bit
1833 * is the sign. This assumption is probably wrong somewhere. */
1834 # elif defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
1835 return (((unsigned char *)&x)[9] & 0x80);
1836 # elif defined(NV_LITTLE_ENDIAN)
1837 /* Note that NVSIZE is sizeof(NV), which would make the below be
1838 * wrong if the end bytes are unused, which happens with the x86
1839 * 80-bit long doubles, which is why take care of that above. */
1840 return (((unsigned char *)&x)[NVSIZE - 1] & 0x80);
1841 # elif defined(NV_BIG_ENDIAN)
1842 return (((unsigned char *)&x)[0] & 0x80);
1844 /* This last resort fallback is wrong for the negative zero. */
1845 return (x < 0.0) ? 1 : 0;
1851 * ex: set ts=8 sts=4 sw=4 et: