OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart test optimization of modulo operator on Smi. | 4 // Dart test optimization of modulo operator on Smi. |
5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr | 5 // VMOptions=--optimization-counter-threshold=10 --no-use-osr |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 | |
10 main() { | 9 main() { |
11 // Prime IC cache. | 10 // Prime IC cache. |
12 noDom(1); | 11 noDom(1); |
13 noDom(-1); | 12 noDom(-1); |
14 for (int i = -30; i < 30; i++) { | 13 for (int i = -30; i < 30; i++) { |
15 Expect.equals(i % 256, foo(i)); | 14 Expect.equals(i % 256, foo(i)); |
16 Expect.equals(i % -256, boo(i)); | 15 Expect.equals(i % -256, boo(i)); |
17 Expect.throws(() => hoo(i), (e) => e is IntegerDivisionByZeroException); | 16 Expect.throws(() => hoo(i), (e) => e is IntegerDivisionByZeroException); |
18 | 17 |
19 Expect.equals(i ~/ 254 + i % 254, fooTwo(i)); | 18 Expect.equals(i ~/ 254 + i % 254, fooTwo(i)); |
20 Expect.equals(i ~/ -254 + i % -254, booTwo(i)); | 19 Expect.equals(i ~/ -254 + i % -254, booTwo(i)); |
21 Expect.throws(() => hooTwo(i), (e) => e is IntegerDivisionByZeroException); | 20 Expect.throws(() => hooTwo(i), (e) => e is IntegerDivisionByZeroException); |
22 if (i > 0) { | 21 if (i > 0) { |
23 Expect.equals(i % 10, noDom(i)); | 22 Expect.equals(i % 10, noDom(i)); |
24 } else { | 23 } else { |
25 Expect.equals(i ~/ 10, noDom(i)); | 24 Expect.equals(i ~/ 10, noDom(i)); |
26 } | 25 } |
27 Expect.equals((i ~/ 10) + (i % 10) + (i % 10), threeOp(i)); | 26 Expect.equals((i ~/ 10) + (i % 10) + (i % 10), threeOp(i)); |
28 Expect.equals((i ~/ 10) + (i ~/ 12) + (i % 10) + (i % 12), fourOp(i)); | 27 Expect.equals((i ~/ 10) + (i ~/ 12) + (i % 10) + (i % 12), fourOp(i)); |
29 | 28 |
30 // Zero test is done outside the loop. | 29 // Zero test is done outside the loop. |
31 if (i < 0) { | 30 if (i < 0) { |
32 Expect.equals(i % -i, foo2(i)); | 31 Expect.equals(i % -i, foo2(i)); |
33 Expect.equals(i ~/ -i + i % -i, fooTwo2(i)); | 32 Expect.equals(i ~/ -i + i % -i, fooTwo2(i)); |
34 } else if (i > 0) { | 33 } else if (i > 0) { |
35 Expect.equals(i % i, foo2(i)); | 34 Expect.equals(i % i, foo2(i)); |
36 Expect.equals(i ~/ i + i % i, fooTwo2(i)); | 35 Expect.equals(i ~/ i + i % i, fooTwo2(i)); |
37 } | 36 } |
38 } | 37 } |
39 Expect.throws(() => foo2(0), (e) => e is IntegerDivisionByZeroException); | 38 Expect.throws(() => foo2(0), (e) => e is IntegerDivisionByZeroException); |
40 Expect.throws(() => fooTwo2(0), (e) => e is IntegerDivisionByZeroException); | 39 Expect.throws(() => fooTwo2(0), (e) => e is IntegerDivisionByZeroException); |
41 } | 40 } |
42 | 41 |
43 foo(i) => i % 256; // This will get optimized to AND instruction. | 42 foo(i) => i % 256; // This will get optimized to AND instruction. |
44 boo(i) => i % -256; | 43 boo(i) => i % -256; |
45 hoo(i) => i % 0; | 44 hoo(i) => i % 0; |
46 | 45 |
47 fooTwo(i) => i ~/ 254 + i % 254; | 46 fooTwo(i) => i ~/ 254 + i % 254; |
48 booTwo(i) => i ~/ -254 + i % -254; | 47 booTwo(i) => i ~/ -254 + i % -254; |
49 hooTwo(i) => i ~/ 0 + i % 0; | 48 hooTwo(i) => i ~/ 0 + i % 0; |
50 | 49 |
51 noDom(a) { | 50 noDom(a) { |
52 var x; | 51 var x; |
53 if (a > 0) { | 52 if (a > 0) { |
54 x = a % 10; | 53 x = a % 10; |
55 } else { | 54 } else { |
56 x = a ~/ 10; | 55 x = a ~/ 10; |
57 } | 56 } |
58 return x; | 57 return x; |
59 } | 58 } |
60 | 59 |
61 threeOp(a) { | 60 threeOp(a) { |
62 var x = a ~/ 10; | 61 var x = a ~/ 10; |
63 var y = a % 10; | 62 var y = a % 10; |
64 var z = a % 10; | 63 var z = a % 10; |
65 return x + y + z; | 64 return x + y + z; |
66 } | 65 } |
67 | 66 |
68 | |
69 fourOp(a) { | 67 fourOp(a) { |
70 var x0 = a ~/ 10; | 68 var x0 = a ~/ 10; |
71 var x1 = a ~/ 12; | 69 var x1 = a ~/ 12; |
72 var y0 = a % 10; | 70 var y0 = a % 10; |
73 var y1 = a % 12; | 71 var y1 = a % 12; |
74 return x0 + x1 + y0 + y1; | 72 return x0 + x1 + y0 + y1; |
75 } | 73 } |
76 | 74 |
77 foo2(i) { | 75 foo2(i) { |
78 // Make sure x has a range computed. | 76 // Make sure x has a range computed. |
79 var x = 0; | 77 var x = 0; |
80 if (i < 0) { | 78 if (i < 0) { |
81 x = -i; | 79 x = -i; |
82 } else { | 80 } else { |
83 x = i; | 81 x = i; |
84 } | 82 } |
85 return i % x; | 83 return i % x; |
86 } | 84 } |
87 | 85 |
88 | |
89 fooTwo2(i) { | 86 fooTwo2(i) { |
90 // Make sure x has a range computed. | 87 // Make sure x has a range computed. |
91 var x = 0; | 88 var x = 0; |
92 if (i < 0) { | 89 if (i < 0) { |
93 x = -i; | 90 x = -i; |
94 } else { | 91 } else { |
95 x = i; | 92 x = i; |
96 } | 93 } |
97 return i ~/ x + i % x; | 94 return i ~/ x + i % x; |
98 } | 95 } |
OLD | NEW |