Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / op / arith.t
CommitLineData
7dca457a 1#!./perl -w
54310121 2
7f45fac2 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
a4474c9e 8print "1..145\n";
54310121 9
10sub try ($$) {
11 print +($_[1] ? "ok" : "not ok"), " $_[0]\n";
12}
7dca457a 13sub tryeq ($$$) {
14 if ($_[1] == $_[2]) {
15 print "ok $_[0]\n";
16 } else {
17 print "not ok $_[0] # $_[1] != $_[2]\n";
18 }
19}
800e6488 20sub tryeq_sloppy ($$$) {
21 if ($_[1] == $_[2]) {
22 print "ok $_[0]\n";
23 } else {
24 my $error = abs ($_[1] - $_[2]) / $_[1];
fbe3e8bd 25 if ($error < 1e-9) {
800e6488 26 print "ok $_[0] # $_[1] is close to $_[2], \$^O eq $^O\n";
27 } else {
28 print "not ok $_[0] # $_[1] != $_[2]\n";
29 }
30 }
31}
54310121 32
a4474c9e 33my $T = 1;
34tryeq $T++, 13 % 4, 1;
35tryeq $T++, -13 % 4, 3;
36tryeq $T++, 13 % -4, -3;
37tryeq $T++, -13 % -4, -1;
38
39# Give abs() a good work-out before using it in anger
40tryeq $T++, abs(0), 0;
41tryeq $T++, abs(1), 1;
42tryeq $T++, abs(-1), 1;
43tryeq $T++, abs(2147483647), 2147483647;
44tryeq $T++, abs(-2147483647), 2147483647;
45tryeq $T++, abs(4294967295), 4294967295;
46tryeq $T++, abs(-4294967295), 4294967295;
47tryeq $T++, abs(9223372036854775807), 9223372036854775807;
48tryeq $T++, abs(-9223372036854775807), 9223372036854775807;
49tryeq $T++, abs(1e50), 1e50; # Assume no change whatever; no slop needed
50tryeq $T++, abs(-1e50), 1e50; # Assume only sign bit flipped
65843c0f 51
52my $limit = 1e6;
53
54# Division (and modulo) of floating point numbers
55# seem to be rather sloppy in Cray.
56$limit = 1e8 if $^O eq 'unicos';
57
a4474c9e 58try $T++, abs( 13e21 % 4e21 - 1e21) < $limit;
59try $T++, abs(-13e21 % 4e21 - 3e21) < $limit;
60try $T++, abs( 13e21 % -4e21 - -3e21) < $limit;
61try $T++, abs(-13e21 % -4e21 - -1e21) < $limit;
d658dc55 62
63# UVs should behave properly
64
a4474c9e 65tryeq $T++, 4063328477 % 65535, 27407;
66tryeq $T++, 4063328477 % 4063328476, 1;
67tryeq $T++, 4063328477 % 2031664238, 1;
68tryeq $T++, 2031664238 % 4063328477, 2031664238;
7dca457a 69
70# These should trigger wrapping on 32 bit IVs and UVs
71
a4474c9e 72tryeq $T++, 2147483647 + 0, 2147483647;
7dca457a 73
74# IV + IV promote to UV
a4474c9e 75tryeq $T++, 2147483647 + 1, 2147483648;
76tryeq $T++, 2147483640 + 10, 2147483650;
77tryeq $T++, 2147483647 + 2147483647, 4294967294;
7dca457a 78# IV + UV promote to NV
a4474c9e 79tryeq $T++, 2147483647 + 2147483649, 4294967296;
7dca457a 80# UV + IV promote to NV
a4474c9e 81tryeq $T++, 4294967294 + 2, 4294967296;
7dca457a 82# UV + UV promote to NV
a4474c9e 83tryeq $T++, 4294967295 + 4294967295, 8589934590;
7dca457a 84
85# UV + IV to IV
a4474c9e 86tryeq $T++, 2147483648 + -1, 2147483647;
87tryeq $T++, 2147483650 + -10, 2147483640;
7dca457a 88# IV + UV to IV
a4474c9e 89tryeq $T++, -1 + 2147483648, 2147483647;
90tryeq $T++, -10 + 4294967294, 4294967284;
7dca457a 91# IV + IV to NV
a4474c9e 92tryeq $T++, -2147483648 + -2147483648, -4294967296;
93tryeq $T++, -2147483640 + -10, -2147483650;
7dca457a 94
95# Hmm. Don't forget the simple stuff
a4474c9e 96tryeq $T++, 1 + 1, 2;
97tryeq $T++, 4 + -2, 2;
98tryeq $T++, -10 + 100, 90;
99tryeq $T++, -7 + -9, -16;
100tryeq $T++, -63 + +2, -61;
101tryeq $T++, 4 + -1, 3;
102tryeq $T++, -1 + 1, 0;
103tryeq $T++, +29 + -29, 0;
104tryeq $T++, -1 + 4, 3;
105tryeq $T++, +4 + -17, -13;
7dca457a 106
107# subtraction
a4474c9e 108tryeq $T++, 3 - 1, 2;
109tryeq $T++, 3 - 15, -12;
110tryeq $T++, 3 - -7, 10;
111tryeq $T++, -156 - 5, -161;
112tryeq $T++, -156 - -5, -151;
113tryeq $T++, -5 - -12, 7;
114tryeq $T++, -3 - -3, 0;
115tryeq $T++, 15 - 15, 0;
116
117tryeq $T++, 2147483647 - 0, 2147483647;
118tryeq $T++, 2147483648 - 0, 2147483648;
119tryeq $T++, -2147483648 - 0, -2147483648;
120
121tryeq $T++, 0 - -2147483647, 2147483647;
122tryeq $T++, -1 - -2147483648, 2147483647;
123tryeq $T++, 2 - -2147483648, 2147483650;
124
125tryeq $T++, 4294967294 - 3, 4294967291;
126tryeq $T++, -2147483648 - -1, -2147483647;
7dca457a 127
128# IV - IV promote to UV
a4474c9e 129tryeq $T++, 2147483647 - -1, 2147483648;
130tryeq $T++, 2147483647 - -2147483648, 4294967295;
7dca457a 131# UV - IV promote to NV
a4474c9e 132tryeq $T++, 4294967294 - -3, 4294967297;
7dca457a 133# IV - IV promote to NV
a4474c9e 134tryeq $T++, -2147483648 - +1, -2147483649;
7dca457a 135# UV - UV promote to IV
a4474c9e 136tryeq $T++, 2147483648 - 2147483650, -2;
7dca457a 137# IV - UV promote to IV
a4474c9e 138tryeq $T++, 2000000000 - 4000000000, -2000000000;
7dca457a 139
140# No warnings should appear;
141my $a;
142$a += 1;
a4474c9e 143tryeq $T++, $a, 1;
7dca457a 144undef $a;
145$a += -1;
a4474c9e 146tryeq $T++, $a, -1;
7dca457a 147undef $a;
148$a += 4294967290;
a4474c9e 149tryeq $T++, $a, 4294967290;
7dca457a 150undef $a;
151$a += -4294967290;
a4474c9e 152tryeq $T++, $a, -4294967290;
7dca457a 153undef $a;
154$a += 4294967297;
a4474c9e 155tryeq $T++, $a, 4294967297;
7dca457a 156undef $a;
157$a += -4294967297;
a4474c9e 158tryeq $T++, $a, -4294967297;
7dca457a 159
160my $s;
161$s -= 1;
a4474c9e 162tryeq $T++, $s, -1;
7dca457a 163undef $s;
164$s -= -1;
a4474c9e 165tryeq $T++, $s, +1;
7dca457a 166undef $s;
167$s -= -4294967290;
a4474c9e 168tryeq $T++, $s, +4294967290;
7dca457a 169undef $s;
170$s -= 4294967290;
a4474c9e 171tryeq $T++, $s, -4294967290;
7dca457a 172undef $s;
173$s -= 4294967297;
a4474c9e 174tryeq $T++, $s, -4294967297;
7dca457a 175undef $s;
176$s -= -4294967297;
a4474c9e 177tryeq $T++, $s, +4294967297;
7dca457a 178
179# Multiplication
180
a4474c9e 181tryeq $T++, 1 * 3, 3;
182tryeq $T++, -2 * 3, -6;
183tryeq $T++, 3 * -3, -9;
184tryeq $T++, -4 * -3, 12;
7dca457a 185
186# check with 0xFFFF and 0xFFFF
a4474c9e 187tryeq $T++, 65535 * 65535, 4294836225;
188tryeq $T++, 65535 * -65535, -4294836225;
189tryeq $T++, -65535 * 65535, -4294836225;
190tryeq $T++, -65535 * -65535, 4294836225;
7dca457a 191
192# check with 0xFFFF and 0x10001
a4474c9e 193tryeq $T++, 65535 * 65537, 4294967295;
194tryeq $T++, 65535 * -65537, -4294967295;
195tryeq $T++, -65535 * 65537, -4294967295;
196tryeq $T++, -65535 * -65537, 4294967295;
7dca457a 197
198# check with 0x10001 and 0xFFFF
a4474c9e 199tryeq $T++, 65537 * 65535, 4294967295;
200tryeq $T++, 65537 * -65535, -4294967295;
201tryeq $T++, -65537 * 65535, -4294967295;
202tryeq $T++, -65537 * -65535, 4294967295;
7dca457a 203
204# These should all be dones as NVs
a4474c9e 205tryeq $T++, 65537 * 65537, 4295098369;
206tryeq $T++, 65537 * -65537, -4295098369;
207tryeq $T++, -65537 * 65537, -4295098369;
208tryeq $T++, -65537 * -65537, 4295098369;
7dca457a 209
210# will overflow an IV (in 32-bit)
a4474c9e 211tryeq $T++, 46340 * 46342, 0x80001218;
212tryeq $T++, 46340 * -46342, -0x80001218;
213tryeq $T++, -46340 * 46342, -0x80001218;
214tryeq $T++, -46340 * -46342, 0x80001218;
7dca457a 215
a4474c9e 216tryeq $T++, 46342 * 46340, 0x80001218;
217tryeq $T++, 46342 * -46340, -0x80001218;
218tryeq $T++, -46342 * 46340, -0x80001218;
219tryeq $T++, -46342 * -46340, 0x80001218;
7dca457a 220
221# will overflow a positive IV (in 32-bit)
a4474c9e 222tryeq $T++, 65536 * 32768, 0x80000000;
223tryeq $T++, 65536 * -32768, -0x80000000;
224tryeq $T++, -65536 * 32768, -0x80000000;
225tryeq $T++, -65536 * -32768, 0x80000000;
7dca457a 226
a4474c9e 227tryeq $T++, 32768 * 65536, 0x80000000;
228tryeq $T++, 32768 * -65536, -0x80000000;
229tryeq $T++, -32768 * 65536, -0x80000000;
230tryeq $T++, -32768 * -65536, 0x80000000;
7dca457a 231
232# 2147483647 is prime. bah.
233
a4474c9e 234tryeq $T++, 46339 * 46341, 0x7ffea80f;
235tryeq $T++, 46339 * -46341, -0x7ffea80f;
236tryeq $T++, -46339 * 46341, -0x7ffea80f;
237tryeq $T++, -46339 * -46341, 0x7ffea80f;
96a05aee 238
239# leading space should be ignored
240
a4474c9e 241tryeq $T++, 1 + " 1", 2;
242tryeq $T++, 3 + " -1", 2;
243tryeq $T++, 1.2, " 1.2";
244tryeq $T++, -1.2, " -1.2";
5479d192 245
246# divide
247
a4474c9e 248tryeq $T++, 28/14, 2;
249tryeq $T++, 28/-7, -4;
250tryeq $T++, -28/4, -7;
251tryeq $T++, -28/-2, 14;
5479d192 252
a4474c9e 253tryeq $T++, 0x80000000/1, 0x80000000;
254tryeq $T++, 0x80000000/-1, -0x80000000;
255tryeq $T++, -0x80000000/1, -0x80000000;
256tryeq $T++, -0x80000000/-1, 0x80000000;
5479d192 257
258# The example for sloppy divide, rigged to avoid the peephole optimiser.
a4474c9e 259tryeq_sloppy $T++, "20." / "5.", 4;
5479d192 260
a4474c9e 261tryeq $T++, 2.5 / 2, 1.25;
262tryeq $T++, 3.5 / -2, -1.75;
263tryeq $T++, -4.5 / 2, -2.25;
264tryeq $T++, -5.5 / -2, 2.75;
5479d192 265
266# Bluuurg if your floating point can't accurately cope with powers of 2
e7311069 267# [I suspect this is parsing string->float problems, not actual arith]
a4474c9e 268tryeq_sloppy $T++, 18446744073709551616/1, 18446744073709551616; # Bluuurg
269tryeq_sloppy $T++, 18446744073709551616/2, 9223372036854775808;
270tryeq_sloppy $T++, 18446744073709551616/4294967296, 4294967296;
271tryeq_sloppy $T++, 18446744073709551616/9223372036854775808, 2;
e7311069 272
273{
274 # The peephole optimiser is wrong to think that it can substitute intops
275 # in place of regular ops, because i_multiply can overflow.
276 # Bug reported by "Sisyphus" <kalinabears@hdc.com.au>
277 my $n = 1127;
278
279 my $float = ($n % 1000) * 167772160.0;
a4474c9e 280 tryeq_sloppy $T++, $float, 21307064320;
e7311069 281
282 # On a 32 bit machine, if the i_multiply op is used, you will probably get
283 # -167772160. It's actually undefined behaviour, so anything may happen.
284 my $int = ($n % 1000) * 167772160;
a4474c9e 285 tryeq $T++, $int, 21307064320;
e228fed9 286
287 my $t = time;
288 my $t1000 = time() * 1000;
a4474c9e 289 try $T++, abs($t1000 -1000 * $t) <= 2000;
e7311069 290}
7f45fac2 291
6332ff5c 292my $vms_no_ieee;
293if ($^O eq 'VMS') {
a4474c9e 294 use vars '%Config';
295 eval {require Config; import Config};
6332ff5c 296 $vms_no_ieee = 1 unless defined($Config{useieee});
297}
298
299if ($^O eq 'vos') {
300 print "not ok ", $T++, " # TODO VOS raises SIGFPE instead of producing infinity.\n";
301}
302elsif ($vms_no_ieee) {
303 print $T++, " # SKIP -- the IEEE infinity model is unavailable in this configuration.\n"
304}
ad415dae 305elsif ($^O eq 'ultrix') {
a4474c9e 306 print "not ok ", $T++, " # TODO Ultrix enters deep nirvana instead of producing infinity.\n";
6332ff5c 307}
dfbc85ea 308else {
7f45fac2 309 # The computation of $v should overflow and produce "infinity"
310 # on any system whose max exponent is less than 10**1506.
311 # The exact string used to represent infinity varies by OS,
312 # so we don't test for it; all we care is that we don't die.
313 #
314 # Perl considers it to be an error if SIGFPE is raised.
315 # Chances are the interpreter will die, since it doesn't set
316 # up a handler for SIGFPE. That's why this test is last; to
317 # minimize the number of test failures. --PG
318
319 my $n = 5000;
320 my $v = 2;
321 while (--$n)
322 {
323 $v *= 2;
324 }
a4474c9e 325 print "ok ", $T++, "\n";
7f45fac2 326}