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