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