Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / op / 64bitint.t
CommitLineData
e312add1 1#./perl
2
0f4b6630 3BEGIN {
ea2b5ef6 4 eval { my $q = pack "q", 0 };
0f4b6630 5 if ($@) {
195d559b 6 print "1..0 # Skip: no 64-bit types\n";
0f4b6630 7 exit(0);
8 }
ea2b5ef6 9 chdir 't' if -d 't';
20822f61 10 @INC = '../lib';
0f4b6630 11}
12
686fa4bb 13# This could use many more tests.
0f4b6630 14
d0ba1bd2 15# so that using > 0xfffffff constants and
972b05a9 16# 32+ bit integers don't cause noise
59d8ce62 17use warnings;
4438c4b7 18no warnings qw(overflow portable);
ea2b5ef6 19
53305cf1 20print "1..67\n";
0f4b6630 21
59d8ce62 22# as 6 * 6 = 36, the last digit of 6**n will always be six. Hence the last
23# digit of 16**n will always be six. Hence 16**n - 1 will always end in 5.
24# Assumption is that UVs will always be a multiple of 4 bits long.
25
26my $UV_max = ~0;
27die "UV_max eq '$UV_max', doesn't end in 5; your UV isn't 4n bits long :-(."
28 unless $UV_max =~ /5$/;
29my $UV_max_less3 = $UV_max - 3;
30my $maths_preserves_UVs = $UV_max_less3 =~ /^\d+2$/; # 5 - 3 is 2.
31if ($maths_preserves_UVs) {
32 print "# This perl's maths preserves all bits of a UV.\n";
33} else {
34 print "# This perl's maths does not preserve all bits of a UV.\n";
35}
36
0f4b6630 37my $q = 12345678901;
38my $r = 23456789012;
20fe1ea2 39my $f = 0xffffffff;
0f4b6630 40my $x;
2d4389e4 41my $y;
0f4b6630 42
43$x = unpack "q", pack "q", $q;
20fe1ea2 44print "not " unless $x == $q && $x > $f;
0f4b6630 45print "ok 1\n";
46
47
22f3ae8c 48$x = sprintf("%lld", 12345678901);
20fe1ea2 49print "not " unless $x eq $q && $x > $f;
0f4b6630 50print "ok 2\n";
51
52
0f4b6630 53$x = sprintf("%lld", $q);
20fe1ea2 54print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 55print "ok 3\n";
0f4b6630 56
57$x = sprintf("%Ld", $q);
20fe1ea2 58print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 59print "ok 4\n";
0f4b6630 60
61$x = sprintf("%qd", $q);
20fe1ea2 62print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 63print "ok 5\n";
0f4b6630 64
0f4b6630 65
66$x = sprintf("%llx", $q);
20fe1ea2 67print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
22f3ae8c 68print "ok 6\n";
0f4b6630 69
70$x = sprintf("%Lx", $q);
20fe1ea2 71print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
22f3ae8c 72print "ok 7\n";
0f4b6630 73
74$x = sprintf("%qx", $q);
20fe1ea2 75print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
22f3ae8c 76print "ok 8\n";
0f4b6630 77
0f4b6630 78
79$x = sprintf("%llo", $q);
20fe1ea2 80print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
22f3ae8c 81print "ok 9\n";
0f4b6630 82
83$x = sprintf("%Lo", $q);
20fe1ea2 84print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
22f3ae8c 85print "ok 10\n";
0f4b6630 86
87$x = sprintf("%qo", $q);
20fe1ea2 88print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
22f3ae8c 89print "ok 11\n";
0f4b6630 90
0f4b6630 91
92$x = sprintf("%llb", $q);
20fe1ea2 93print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
94 oct("0b$x") > $f;
22f3ae8c 95print "ok 12\n";
0f4b6630 96
97$x = sprintf("%Lb", $q);
20fe1ea2 98print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
99 oct("0b$x") > $f;
22f3ae8c 100print "ok 13\n";
0f4b6630 101
102$x = sprintf("%qb", $q);
20fe1ea2 103print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
104 oct("0b$x") > $f;
22f3ae8c 105print "ok 14\n";
0f4b6630 106
107
22f3ae8c 108$x = sprintf("%llu", $q);
20fe1ea2 109print "not " unless $x eq $q && $x > $f;
22f3ae8c 110print "ok 15\n";
0f4b6630 111
22f3ae8c 112$x = sprintf("%Lu", $q);
20fe1ea2 113print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 114print "ok 16\n";
0f4b6630 115
22f3ae8c 116$x = sprintf("%qu", $q);
20fe1ea2 117print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 118print "ok 17\n";
0f4b6630 119
120
29fe7a80 121$x = sprintf("%D", $q);
20fe1ea2 122print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 123print "ok 18\n";
29fe7a80 124
125$x = sprintf("%U", $q);
20fe1ea2 126print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 127print "ok 19\n";
29fe7a80 128
129$x = sprintf("%O", $q);
20fe1ea2 130print "not " unless oct($x) == $q && oct($x) > $f;
22f3ae8c 131print "ok 20\n";
29fe7a80 132
133
0f4b6630 134$x = $q + $r;
20fe1ea2 135print "not " unless $x == 35802467913 && $x > $f;
22f3ae8c 136print "ok 21\n";
0f4b6630 137
138$x = $q - $r;
20fe1ea2 139print "not " unless $x == -11111110111 && -$x > $f;
22f3ae8c 140print "ok 22\n";
0f4b6630 141
f3ff050f 142if ($^O ne 'unicos') {
143 $x = $q * 1234567;
144 print "not " unless $x == 15241567763770867 && $x > $f;
145 print "ok 23\n";
0f4b6630 146
8d489514 147 $x /= 1234567;
148 print "not " unless $x == $q && $x > $f;
149 print "ok 24\n";
2d4389e4 150
8d489514 151 $x = 98765432109 % 12345678901;
152 print "not " unless $x == 901;
153 print "ok 25\n";
154
155 # The following 12 tests adapted from op/inc.
2d4389e4 156
f3ff050f 157 $a = 9223372036854775807;
158 $c = $a++;
159 print "not " unless $a == 9223372036854775808;
160 print "ok 26\n";
161
162 $a = 9223372036854775807;
163 $c = ++$a;
164 print "not "
165 unless $a == 9223372036854775808 && $c == $a;
166 print "ok 27\n";
167
168 $a = 9223372036854775807;
169 $c = $a + 1;
170 print "not "
171 unless $a == 9223372036854775807 && $c == 9223372036854775808;
172 print "ok 28\n";
173
174 $a = -9223372036854775808;
175 $c = $a--;
176 print "not "
177 unless $a == -9223372036854775809 && $c == -9223372036854775808;
178 print "ok 29\n";
179
180 $a = -9223372036854775808;
181 $c = --$a;
182 print "not "
183 unless $a == -9223372036854775809 && $c == $a;
184 print "ok 30\n";
185
186 $a = -9223372036854775808;
187 $c = $a - 1;
188 print "not "
189 unless $a == -9223372036854775808 && $c == -9223372036854775809;
190 print "ok 31\n";
191
192 $a = 9223372036854775808;
193 $a = -$a;
194 $c = $a--;
195 print "not "
196 unless $a == -9223372036854775809 && $c == -9223372036854775808;
197 print "ok 32\n";
198
199 $a = 9223372036854775808;
200 $a = -$a;
201 $c = --$a;
202 print "not "
203 unless $a == -9223372036854775809 && $c == $a;
204 print "ok 33\n";
205
206 $a = 9223372036854775808;
207 $a = -$a;
208 $c = $a - 1;
209 print "not "
210 unless $a == -9223372036854775808 && $c == -9223372036854775809;
211 print "ok 34\n";
212
213 $a = 9223372036854775808;
214 $b = -$a;
215 $c = $b--;
216 print "not "
217 unless $b == -$a-1 && $c == -$a;
218 print "ok 35\n";
219
220 $a = 9223372036854775808;
221 $b = -$a;
222 $c = --$b;
223 print "not "
224 unless $b == -$a-1 && $c == $b;
225 print "ok 36\n";
226
227 $a = 9223372036854775808;
228 $b = -$a;
229 $b = $b - 1;
230 print "not "
231 unless $b == -(++$a);
232 print "ok 37\n";
233
234} else {
235 # Unicos has imprecise doubles (14 decimal digits or so),
8d489514 236 # especially if operating near the UV/IV limits the low-order bits
237 # become mangled even by simple arithmetic operations.
238 for (23..37) {
2f7c487e 239 print "ok $_ # skipped: too imprecise numbers\n";
f3ff050f 240 }
241}
e312add1 242
2d4389e4 243
c5a0f51a 244$x = '';
245print "not " unless (vec($x, 1, 64) = $q) == $q;
e312add1 246print "ok 38\n";
c5a0f51a 247
248print "not " unless vec($x, 1, 64) == $q && vec($x, 1, 64) > $f;
e312add1 249print "ok 39\n";
c5a0f51a 250
251print "not " unless vec($x, 0, 64) == 0 && vec($x, 2, 64) == 0;
e312add1 252print "ok 40\n";
c5a0f51a 253
972b05a9 254
255print "not " unless ~0 == 0xffffffffffffffff;
e312add1 256print "ok 41\n";
972b05a9 257
258print "not " unless (0xffffffff<<32) == 0xffffffff00000000;
e312add1 259print "ok 42\n";
972b05a9 260
261print "not " unless ((0xffffffff)<<32)>>32 == 0xffffffff;
e312add1 262print "ok 43\n";
972b05a9 263
264print "not " unless 1<<63 == 0x8000000000000000;
e312add1 265print "ok 44\n";
972b05a9 266
267print "not " unless (sprintf "%#Vx", 1<<63) eq '0x8000000000000000';
e312add1 268print "ok 45\n";
972b05a9 269
270print "not " unless (0x8000000000000000 | 1) == 0x8000000000000001;
e312add1 271print "ok 46\n";
972b05a9 272
f3ff050f 273print "not "
274 unless (0xf000000000000000 & 0x8000000000000000) == 0x8000000000000000;
e312add1 275print "ok 47\n";
972b05a9 276
f3ff050f 277print "not "
278 unless (0xf000000000000000 ^ 0xfffffffffffffff0) == 0x0ffffffffffffff0;
e312add1 279print "ok 48\n";
972b05a9 280
686fa4bb 281
f3ff050f 282print "not "
283 unless (sprintf "%b", ~0) eq
284 '1111111111111111111111111111111111111111111111111111111111111111';
686fa4bb 285print "ok 49\n";
286
f3ff050f 287print "not "
288 unless (sprintf "%64b", ~0) eq
289 '1111111111111111111111111111111111111111111111111111111111111111';
686fa4bb 290print "ok 50\n";
291
292print "not " unless (sprintf "%d", ~0>>1) eq '9223372036854775807';
293print "ok 51\n";
294
295print "not " unless (sprintf "%u", ~0) eq '18446744073709551615';
296print "ok 52\n";
297
868d6b85 298# If the 53..55 fail you have problems in the parser's string->int conversion,
299# see toke.c:scan_num().
300
301$q = -9223372036854775808;
00450673 302print "# $q ne\n# -9223372036854775808\nnot " unless "$q" eq "-9223372036854775808";
868d6b85 303print "ok 53\n";
304
305$q = 9223372036854775807;
00450673 306print "# $q ne\n# 9223372036854775807\nnot " unless "$q" eq "9223372036854775807";
868d6b85 307print "ok 54\n";
308
309$q = 18446744073709551615;
00450673 310print "# $q ne\n# 18446744073709551615\nnot " unless "$q" eq "18446744073709551615";
868d6b85 311print "ok 55\n";
312
85b81d93 313# Test that sv_2nv then sv_2iv is the same as sv_2iv direct
314# fails if whatever Atol is defined as can't actually cope with >32 bits.
315my $num = 4294967297;
316my $string = "4294967297";
317{
318 use integer;
319 $num += 0;
320 $string += 0;
321}
322if ($num eq $string) {
323 print "ok 56\n";
324} else {
325 print "not ok 56 # \"$num\" ne \"$string\"\n";
326}
327
328# Test that sv_2nv then sv_2uv is the same as sv_2uv direct
329$num = 4294967297;
330$string = "4294967297";
331$num &= 0;
332$string &= 0;
333if ($num eq $string) {
334 print "ok 57\n";
335} else {
336 print "not ok 57 # \"$num\" ne \"$string\"\n";
337}
338
09bb3e27 339$q = "18446744073709551616e0";
340$q += 0;
341print "# \"18446744073709551616e0\" += 0 gives $q\nnot " if "$q" eq "18446744073709551615";
342print "ok 58\n";
343
5479d192 344# 0xFFFFFFFFFFFFFFFF == 1 * 3 * 5 * 17 * 257 * 641 * 65537 * 6700417'
345$q = 0xFFFFFFFFFFFFFFFF / 3;
59d8ce62 346if ($q == 0x5555555555555555 and ($q != 0x5555555555555556
347 or !$maths_preserves_UVs)) {
5479d192 348 print "ok 59\n";
349} else {
350 print "not ok 59 # 0xFFFFFFFFFFFFFFFF / 3 = $q\n";
351 print "# Should not be floating point\n" if $q =~ tr/e.//;
352}
09bb3e27 353
e2c88acc 354$q = 0xFFFFFFFFFFFFFFFF % 0x5555555555555555;
355if ($q == 0) {
356 print "ok 60\n";
357} else {
358 print "not ok 60 # 0xFFFFFFFFFFFFFFFF % 0x5555555555555555 => $q\n";
359}
360
361$q = 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0;
362if ($q == 0xF) {
363 print "ok 61\n";
364} else {
365 print "not ok 61 # 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0 => $q\n";
366}
367
368$q = 0x8000000000000000 % 9223372036854775807;
369if ($q == 1) {
370 print "ok 62\n";
371} else {
372 print "not ok 62 # 0x8000000000000000 % 9223372036854775807 => $q\n";
373}
374
375$q = 0x8000000000000000 % -9223372036854775807;
376if ($q == -9223372036854775806) {
377 print "ok 63\n";
378} else {
379 print "not ok 63 # 0x8000000000000000 % -9223372036854775807 => $q\n";
380}
381
53305cf1 382{
383 use integer;
384 $q = hex "0x123456789abcdef0";
385 if ($q == 0x123456789abcdef0 and $q != 0x123456789abcdef1) {
386 print "ok 64\n";
387 } else {
388 printf "not ok 64 # hex \"0x123456789abcdef0\" = $q (%X)\n", $q;
389 print "# Should not be floating point\n" if $q =~ tr/e.//;
390 }
391
392 $q = oct "0x123456789abcdef0";
393 if ($q == 0x123456789abcdef0 and $q != 0x123456789abcdef1) {
394 print "ok 65\n";
395 } else {
396 printf "not ok 65 # oct \"0x123456789abcdef0\" = $q (%X)\n", $q;
397 print "# Should not be floating point\n" if $q =~ tr/e.//;
398 }
399
400 $q = oct "765432176543217654321";
401 if ($q == 0765432176543217654321 and $q != 0765432176543217654322) {
402 print "ok 66\n";
403 } else {
404 printf "not ok 66 # oct \"765432176543217654321\" = $q (%o)\n", $q;
405 print "# Should not be floating point\n" if $q =~ tr/e.//;
406 }
407
408 $q = oct "0b0101010101010101010101010101010101010101010101010101010101010101";
409 if ($q == 0x5555555555555555 and $q != 0x5555555555555556) {
410 print "ok 67\n";
411 } else {
412 printf "not ok 67 # oct \"0b0101010101010101010101010101010101010101010101010101010101010101\" = $q (%b)\n", $q;
413 print "# Should not be floating point\n" if $q =~ tr/e.//;
414 }
415}
416
c5a0f51a 417# eof