lib/Scalar/Number.xs

filter: Statementstmt 89.3% 118 / 132 Branchbran 88.4% 138 / 156 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.8% 256 / 288 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Scalar/Number.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #ifndef cBOOL
7 # define cBOOL(x) ((bool)!!(x))
8 #endif /* !cBOOL */
9  
10 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
11 #define PERL_DECIMAL_VERSION \
12 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
13 #define PERL_VERSION_GE(r,v,s) \
14 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
15  
16 #define Q_IOK_MAYBE_SPURIOUS (!PERL_VERSION_GE(5,7,1))
17 #define Q_STRING_ZERO_FLOATS \
18 (PERL_VERSION_GE(5,13,6) && !PERL_VERSION_GE(5,17,1))
19  
20 /*
21 * The way an SV is interpreted for its numerical value varies between Perl
22 * versions. The new way (perl 5.7.1+) is that the IOK and NOK flags
23 * strictly indicate that the numerical value is acceptably represented by
24 * the corresponding field. The old way (up to perl 5.7.0) is that the IOK
25 * and NOK flags indicate that the corresponding field is filled, but it
26 * might be a conversion from the other form. In the old form, most
27 * arithmetic is floating point, so to handle an integer that can't be
28 * represented in floating point it must be specially processed using
29 * integer-only operations, and so won't have NOK set. So the rules are:
30 *
31 * STYLE IOK NOK
32 * old no no use string_2num() to numerify, then try again
33 * old no yes use NV
34 * old yes no use IV/UV
35 * old yes yes use NV
36 * new no no use string_2num() to numerify, then try again
37 * new no yes use NV
38 * new yes no use IV/UV
39 * new yes yes use IV/UV
40 *
41 * Which set of rules applies is controlled by the Q_IOK_MAYBE_SPURIOUS flag.
42 */
43  
44 /*
45 * string_2num() resolves a string SV into one that has the same numeric
46 * value and has that numeric value expressed directly in the SV structure
47 * (as either an IV, UV, or NV). A mortal reference to the resulting SV
48 * is returned. The resulting SV is not necessarily a pure number; it may
49 * have an unrelated string value. Warns for non-numeric strings.
50 */
51  
52 #define string_2num(s) THX_string_2num(aTHX_ s)
53 13291 static SV *THX_string_2num(pTHX_ SV *s)
54 {
55 13291 ▶ if(SvIOK(s) || SvNOK(s)) return s;
Branch: 2 branches
BranchTrueFalseTotal
if(SvIOK(s) || SvNOK(s)) return s; 10089 3202 13291
if(SvIOK(s) || SvNOK(s)) return s; 4736 5353 10089
56 5353 s = sv_mortalcopy(s);
57 5353 ▶ if(!Q_IOK_MAYBE_SPURIOUS && (SvIV(s), SvIOK(s))) {
Branch: 1 branch
BranchTrueFalseTotal
if(!Q_IOK_MAYBE_SPURIOUS && (SvIV(s), SvIOK(s))) { 3675 1678 5353
58 3675 ▶ if(Q_HAVE_SIGNED_ZERO && SvIVX(s) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(Q_HAVE_SIGNED_ZERO && SvIVX(s) == 0) { 2811 864 3675
59 /* It's a zero, and asking for SvIV has squashed
60 * it to an integer zero, but it wouldn't
61 * necessarily be considered an integer zero
62 * by other operations. We seek to match the
63 * behaviour of the printf("%.f")-based test,
64 * thus regarding the behaviour of the negate
65 * operation canonical.
66 */
67 if(Q_STRING_ZERO_FLOATS) {
68 /* String zeroes now always turn into
69 * floating-point zeroes.
70 */
71 sv_setnv(s, SvNV(s));
72 } else {
73 /* Preserve sign iff the string value
74 * starts with a sign character.
75 */
76 2811 char c = *SvPV_nolen(s);
77 2811 ▶ if(c == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if(c == '-') { 230 2581 2811
78 230 sv_setnv(s, -0.0);
79 230 SvIOK_off(s);
80 2581 ▶ } else if(c == '+') {
Branch: 1 branch
BranchTrueFalseTotal
} else if(c == '+') { 230 2351 2581
81 230 sv_setnv(s, 0.0);
82 230 SvIOK_off(s);
83 } else {
84 2351 sv_setiv(s, 0);
85 2351 SvNOK_off(s);
86 }
87 }
88 }
89 } else {
90 1678 NV val = SvNV(s);
91 1678 ▶ if(!SvNOK(s)) sv_setnv(s, val);
Branch: 1 branch
BranchTrueFalseTotal
if(!SvNOK(s)) sv_setnv(s, val); 3 1675 1678
92 }
93 5353 return s;
94 }
95  
96 /*
97 * numscl_val_cmp() does a value comparison on two scalars that express
98 * their numeric values directly. It must not be called on general
99 * scalars.
100 */
101  
102 /* These variables store the values min_natint and max_natint+1,
103 respectively, in floating-point form. They are initialised by
104 the boot function. */
105 static NV neg_natint_limit, pos_natint_limit;
106  
107 #define numscl_val_cmp(a, b) THX_numscl_val_cmp(aTHX_ a, b)
108 4336 static SV *THX_numscl_val_cmp(pTHX_ SV *a, SV *b)
109 {
110 bool aiok, biok;
111 int result;
112 4336 aiok = Q_IOK_MAYBE_SPURIOUS ? !SvNOK(a) : cBOOL(SvIOK(a));
113 4336 biok = Q_IOK_MAYBE_SPURIOUS ? !SvNOK(b) : cBOOL(SvIOK(b));
114 4336 ▶ if(aiok && biok) {
Branch: 2 branches
BranchTrueFalseTotal
if(aiok && biok) { 2051 2285 4336
if(aiok && biok) { 971 1080 2051
115 971 ▶ if(SvIOK_UV(a)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvIOK_UV(a)) { 2 969 971
116 2 ▶ if(SvIOK_UV(b)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvIOK_UV(b)) { 2 0 2
117 2 UV au = SvUVX(a), bu = SvUVX(b);
118 2 ▶ result = au < bu ? -1 : au == bu ? 0 : +1;
Branch: 1 branch
BranchTrueFalseTotal
result = au < bu ? -1 : au == bu ? 0 : +1; 2 0 2
119 } else {
120 0 UV au = SvUVX(a);
121 0 IV bi = SvIVX(b);
122 0 ▶ result = bi < 0 ? +1 :
Branch: 1 branch
BranchTrueFalseTotal
result = bi < 0 ? +1 : 0 0 0
123 0 ▶ au < ((UV)bi) ? -1 :
Branch: 1 branch
BranchTrueFalseTotal
au < ((UV)bi) ? -1 : 0 0 0
124 0 au == ((UV)bi) ? 0 : +1;
125 }
126 } else {
127 969 ▶ if(SvIOK_UV(b)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvIOK_UV(b)) { 0 969 969
128 0 IV ai = SvIVX(a);
129 0 UV bu = SvUVX(b);
130 0 ▶ result = ai < 0 ? -1 :
Branch: 1 branch
BranchTrueFalseTotal
result = ai < 0 ? -1 : 0 0 0
131 0 ▶ ((UV)ai) < bu ? -1 :
Branch: 1 branch
BranchTrueFalseTotal
((UV)ai) < bu ? -1 : 0 0 0
132 0 ((UV)ai) == bu ? 0 : +1;
133 } else {
134 969 IV ai = SvIVX(a), bi = SvIVX(b);
135 969 ▶ result = ai < bi ? -1 : ai == bi ? 0 : +1;
Branch: 1 branch
BranchTrueFalseTotal
result = ai < bi ? -1 : ai == bi ? 0 : +1; 521 448 969
136 }
137 }
138 3365 ▶ } else if(SvNOK(a) && SvNOK(b)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if(SvNOK(a) && SvNOK(b)) { 2665 700 3365
} else if(SvNOK(a) && SvNOK(b)) { 1964 701 2665
139 1964 NV an = SvNVX(a);
140 1964 NV bn = SvNVX(b);
141 1964 ▶ if(an != an || bn != bn)
Branch: 2 branches
BranchTrueFalseTotal
if(an != an || bn != bn) 1964 0 1964
if(an != an || bn != bn) 0 1964 1964
142 0 return &PL_sv_undef;
143 1964 ▶ result = an < bn ? -1 : an == bn ? 0 : +1;
Branch: 1 branch
BranchTrueFalseTotal
result = an < bn ? -1 : an == bn ? 0 : +1; 954 1010 1964
144 } else {
145 1401 bool reversed = biok;
146 1401 ▶ SV *x = reversed ? b : a, *y = reversed ? a : b;
Branch: 2 branches
BranchTrueFalseTotal
SV *x = reversed ? b : a, *y = reversed ? a : b; 701 700 1401
SV *x = reversed ? b : a, *y = reversed ? a : b; 701 700 1401
147 1401 NV yn = SvNVX(y);
148 UV xu;
149 1401 ▶ if(yn != yn)
Branch: 1 branch
BranchTrueFalseTotal
if(yn != yn) 0 1401 1401
150 0 return &PL_sv_undef;
151 1401 ▶ if(SvIOK_UV(x)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvIOK_UV(x)) { 0 1401 1401
152 0 xu = SvUVX(x);
153 } else {
154 1401 IV xi = SvIVX(x);
155 1401 xu = (UV)xi;
156 1401 ▶ if(xi < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(xi < 0) { 320 1081 1401
157 320 xu = -xu;
158 320 yn = -yn;
159 320 reversed = !reversed;
160 }
161 }
162 1401 ▶ if(yn < 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if(yn < 0.0) { 540 861 1401
163 540 result = +1;
164 861 ▶ } else if(yn >= pos_natint_limit) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(yn >= pos_natint_limit) { 216 645 861
165 216 result = -1;
166 } else {
167 645 UV yu = yn;
168 738 ▶ result = xu < yu ? -1 : xu > yu ? +1 :
Branch: 2 branches
BranchTrueFalseTotal
result = xu < yu ? -1 : xu > yu ? +1 : 469 176 645
result = xu < yu ? -1 : xu > yu ? +1 : 93 376 469
169 93 ▶ yn - ((NV)yu) == 0.0 ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
yn - ((NV)yu) == 0.0 ? 0 : -1; 17 76 93
170 }
171 1401 ▶ if(reversed)
Branch: 1 branch
BranchTrueFalseTotal
if(reversed) 701 700 1401
172 701 result = -result;
173 }
174 4336 return newSViv(result);
175 }
176  
177 MODULE = Scalar::Number PACKAGE = Scalar::Number
178  
179 PROTOTYPES: DISABLE
180  
181 BOOT:
182 {
183 int i;
184 4 neg_natint_limit = -1.0;
185 4 pos_natint_limit = +2.0;
186 256 ▶ for(i = Q_NATINT_BITS; --i; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = Q_NATINT_BITS; --i; ) { 252 4 256
187 252 neg_natint_limit += neg_natint_limit;
188 252 pos_natint_limit += pos_natint_limit;
189 }
190 }
191  
192 SV *
193 _warnable_scalar_num_part(SV *scalar)
194 PROTOTYPE: $
195 CODE:
196 78 ▶ while(!SvIOK(scalar) && !SvNOK(scalar) && SvROK(scalar)) {
Branch: 3 branches
BranchTrueFalseTotal
while(!SvIOK(scalar) && !SvNOK(scalar) && SvROK(scalar)) { 61 17 78
while(!SvIOK(scalar) && !SvNOK(scalar) && SvROK(scalar)) { 52 9 61
while(!SvIOK(scalar) && !SvNOK(scalar) && SvROK(scalar)) { 13 39 52
197 13 ▶ if(SvAMAGIC(scalar)) {
Branch: 3 branches
BranchTrueFalseTotal
if(SvAMAGIC(scalar)) { 13 0 13
if(SvAMAGIC(scalar)) { 12 1 13
if(SvAMAGIC(scalar)) { 12 0 12
198 12 SV *t = AMG_CALLun(scalar, numer);
199 12 ▶ if(t && (!SvROK(t) || SvRV(t) != SvRV(scalar))) {
Branch: 3 branches
BranchTrueFalseTotal
if(t && (!SvROK(t) || SvRV(t) != SvRV(scalar))) { 12 0 12
if(t && (!SvROK(t) || SvRV(t) != SvRV(scalar))) { 4 8 12
if(t && (!SvROK(t) || SvRV(t) != SvRV(scalar))) { 2 2 4
200 10 scalar = t;
201 10 continue;
202 }
203 }
204 3 scalar = sv_2mortal(newSVuv(PTR2UV(SvRV(scalar))));
205 }
206 65 scalar = string_2num(scalar);
207 if(Q_IOK_MAYBE_SPURIOUS && SvNOK(scalar)) {
208 RETVAL = newSVnv(SvNVX(scalar));
209 65 ▶ } else if(SvIOK_notUV(scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(SvIOK_notUV(scalar)) { 34 31 65
210 34 RETVAL = newSViv(SvIVX(scalar));
211 31 ▶ } else if(SvIOK_UV(scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(SvIOK_UV(scalar)) { 2 29 31
212 2 RETVAL = newSVuv(SvUVX(scalar));
213 } else {
214 29 RETVAL = newSVnv(SvNVX(scalar));
215 }
216 OUTPUT:
217 RETVAL
218  
219 bool
220 sclnum_is_natint(SV *scalar)
221 PROTOTYPE: $
222 CODE:
223 748 scalar = string_2num(scalar);
224 748 ▶ if(Q_IOK_MAYBE_SPURIOUS ? !SvNOK(scalar) : SvIOK(scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
if(Q_IOK_MAYBE_SPURIOUS ? !SvNOK(scalar) : SvIOK(scalar)) { 414 334 748
225 414 RETVAL = 1;
226 } else {
227 334 NV val = SvNVX(scalar);
228 334 ▶ if(Q_HAVE_SIGNED_ZERO && val == 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if(Q_HAVE_SIGNED_ZERO && val == 0.0) { 20 314 334
229 20 RETVAL = 0;
230 314 ▶ } else if(val < 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(val < 0.0) { 156 158 314
231 286 ▶ RETVAL = val >= neg_natint_limit &&
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = val >= neg_natint_limit && 130 26 156
232 130 ▶ ((NV)(IV)val) == val;
Branch: 1 branch
BranchTrueFalseTotal
((NV)(IV)val) == val; 50 80 130
233 } else {
234 296 ▶ RETVAL = val < pos_natint_limit &&
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = val < pos_natint_limit && 138 20 158
235 138 ▶ ((NV)(UV)val) == val;
Branch: 1 branch
BranchTrueFalseTotal
((NV)(UV)val) == val; 58 80 138
236 }
237 }
238 OUTPUT:
239 RETVAL
240  
241 bool
242 sclnum_is_float(SV *scalar)
243 PROTOTYPE: $
244 CODE:
245 748 scalar = string_2num(scalar);
246 748 ▶ if(SvNOK(scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvNOK(scalar)) { 475 273 748
247 475 RETVAL = !(Q_HAVE_SIGNED_ZERO && !Q_IOK_MAYBE_SPURIOUS &&
248 475 ▶ SvIOK(scalar) && SvIVX(scalar) == 0);
Branch: 2 branches
BranchTrueFalseTotal
SvIOK(scalar) && SvIVX(scalar) == 0); 141 334 475
SvIOK(scalar) && SvIVX(scalar) == 0); 138 3 141
249 } else {
250 512 ▶ UV mag = SvIOK_UV(scalar) ? SvUVX(scalar) :
Branch: 1 branch
BranchTrueFalseTotal
UV mag = SvIOK_UV(scalar) ? SvUVX(scalar) : 34 239 273
251 239 ▶ SvIVX(scalar) < 0 ? -(UV)SvIVX(scalar) : SvIVX(scalar);
Branch: 1 branch
BranchTrueFalseTotal
SvIVX(scalar) < 0 ? -(UV)SvIVX(scalar) : SvIVX(scalar); 111 128 239
252 273 ▶ if(Q_HAVE_SIGNED_ZERO && mag == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(Q_HAVE_SIGNED_ZERO && mag == 0) { 19 254 273
253 19 RETVAL = 0;
254 } else {
255 #if Q_SIGNIFICAND_BITS+1 >= Q_NATINT_BITS
256 /* all native integers are representable as floats
257 * (except possibly zero, handled above)
258 */
259 RETVAL = 1;
260 #else /* Q_SIGNIFICAND_BITS+1 < Q_NATINT_BITS */
261 /* check length of integer */
262 254 RETVAL = 1;
263 762 ▶ while(mag >= (((UV)1) << (Q_SIGNIFICAND_BITS+1))) {
Branch: 1 branch
BranchTrueFalseTotal
while(mag >= (((UV)1) << (Q_SIGNIFICAND_BITS+1))) { 598 164 762
264 598 ▶ if(mag & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(mag & 1) { 90 508 598
265 90 RETVAL = 0;
266 90 break;
267 }
268 508 mag >>= 1;
269 }
270 #endif /* Q_SIGNIFICAND_BITS+1 < Q_NATINT_BITS */
271 }
272 }
273 OUTPUT:
274 RETVAL
275  
276 SV *
277 sclnum_val_cmp(SV *a, SV *b)
278 PROTOTYPE: $$
279 CODE:
280 0 RETVAL = numscl_val_cmp(string_2num(a), string_2num(b));
281 OUTPUT:
282 RETVAL
283  
284 SV *
285 sclnum_id_cmp(SV *a, SV *b)
286 PROTOTYPE: $$
287 PREINIT:
288 bool aiok, biok;
289 bool anan, bnan;
290 CODE:
291 5865 a = string_2num(a);
292 5865 b = string_2num(b);
293 5865 aiok = Q_IOK_MAYBE_SPURIOUS ? !SvNOK(a) : cBOOL(SvIOK(a));
294 5865 biok = Q_IOK_MAYBE_SPURIOUS ? !SvNOK(b) : cBOOL(SvIOK(b));
295 5865 ▶ anan = !aiok && SvNVX(a) != SvNVX(a);
Branch: 2 branches
BranchTrueFalseTotal
anan = !aiok && SvNVX(a) != SvNVX(a); 3089 2776 5865
anan = !aiok && SvNVX(a) != SvNVX(a); 160 2929 3089
296 5865 ▶ bnan = !biok && SvNVX(b) != SvNVX(b);
Branch: 2 branches
BranchTrueFalseTotal
bnan = !biok && SvNVX(b) != SvNVX(b); 3088 2777 5865
bnan = !biok && SvNVX(b) != SvNVX(b); 160 2928 3088
297 5865 ▶ if(anan || bnan) {
Branch: 2 branches
BranchTrueFalseTotal
if(anan || bnan) { 5705 160 5865
if(anan || bnan) { 156 5549 5705
298 316 RETVAL = newSViv(bnan - anan);
299 8274 ▶ } else if(Q_HAVE_SIGNED_ZERO &&
Branch: 2 branches
BranchTrueFalseTotal
} else if(Q_HAVE_SIGNED_ZERO && 2700 2849 5549
} else if(Q_HAVE_SIGNED_ZERO && 1321 1404 2725
300 5549 ▶ (aiok ? SvUVX(a) == 0 : SvNVX(a) == 0.0) &&
Branch: 2 branches
BranchTrueFalseTotal
(aiok ? SvUVX(a) == 0 : SvNVX(a) == 0.0) && 1441 1259 2700
(aiok ? SvUVX(a) == 0 : SvNVX(a) == 0.0) && 1284 1565 2849
301 3938 ▶ (biok ? SvUVX(b) == 0 : SvNVX(b) == 0.0)) {
Branch: 2 branches
BranchTrueFalseTotal
(biok ? SvUVX(b) == 0 : SvNVX(b) == 0.0)) { 649 672 1321
(biok ? SvUVX(b) == 0 : SvNVX(b) == 0.0)) { 564 840 1404
302 int atype, btype;
303 char tbuf[3];
304 1213 ▶ if(aiok) {
Branch: 1 branch
BranchTrueFalseTotal
if(aiok) { 649 564 1213
305 649 atype = 0;
306 } else {
307 564 sprintf(tbuf, "%+.f", (double)SvNVX(a));
308 564 ▶ atype = tbuf[0] == '-' ? -1 : +1;
Branch: 1 branch
BranchTrueFalseTotal
atype = tbuf[0] == '-' ? -1 : +1; 220 344 564
309 }
310 1213 ▶ if(biok) {
Branch: 1 branch
BranchTrueFalseTotal
if(biok) { 649 564 1213
311 649 btype = 0;
312 } else {
313 564 sprintf(tbuf, "%+.f", (double)SvNVX(b));
314 564 ▶ btype = tbuf[0] == '-' ? -1 : +1;
Branch: 1 branch
BranchTrueFalseTotal
btype = tbuf[0] == '-' ? -1 : +1; 220 344 564
315 }
316 1213 ▶ RETVAL = newSViv(atype < btype ? -1 : atype == btype ? 0 : +1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSViv(atype < btype ? -1 : atype == btype ? 0 : +1); 856 357 1213
317 } else {
318 4336 RETVAL = numscl_val_cmp(a, b);
319 }
320 OUTPUT:
321 RETVAL
« blib/lib/Scalar/Number.pm ↑ Summary