OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Ensure that we can correctly change the sign of the most negative smi. | 28 // Ensure that we can correctly change the sign of the most negative smi. |
29 | 29 |
30 assertEquals(1073741824, -1073741824 * -1); | 30 // Possible Smi ranges. |
31 assertEquals(1073741824, -1073741824 / -1); | 31 var ranges = [{min: -1073741824, max: 1073741823, bits: 31}, |
32 assertEquals(1073741824, -(-1073741824)); | 32 {min: -2147483648, max: 2147483647, bits: 32}]; |
33 assertEquals(1073741824, 0 - (-1073741824)); | |
34 | 33 |
35 var min_smi = -1073741824; | 34 for (var i = 0; i < ranges.length; i++) { |
| 35 var range = ranges[i]; |
| 36 var min_smi = range.min; |
| 37 var max_smi = range.max; |
| 38 var bits = range.bits; |
| 39 var name = bits + "-bit"; |
36 | 40 |
37 assertEquals(1073741824, min_smi * -1); | 41 var result = max_smi + 1; |
38 assertEquals(1073741824, min_smi / -1); | |
39 assertEquals(1073741824, -min_smi); | |
40 assertEquals(1073741824, 0 - min_smi); | |
41 | 42 |
42 var zero = 0; | 43 // Min smi as literal |
43 var minus_one = -1; | 44 assertEquals(result, eval(min_smi + " * -1"), name + "-litconmult"); |
| 45 assertEquals(result, eval(min_smi + " / -1"), name + "-litcondiv"); |
| 46 assertEquals(result, eval("-(" + min_smi + ")"), name + "-litneg"); |
| 47 assertEquals(result, eval("0 - (" + min_smi + ")")), name + "-conlitsub"; |
44 | 48 |
45 assertEquals(1073741824, min_smi * minus_one); | 49 // As variable: |
46 assertEquals(1073741824, min_smi / minus_one); | 50 assertEquals(result, min_smi * -1, name + "-varconmult"); |
47 assertEquals(1073741824, -min_smi); | 51 assertEquals(result, min_smi / -1, name + "-varcondiv"); |
48 assertEquals(1073741824, zero - min_smi); | 52 assertEquals(result, -min_smi, name + "-varneg"); |
| 53 assertEquals(result, 0 - min_smi, name + "-convarsub"); |
49 | 54 |
50 assertEquals(1073741824, -1073741824 * minus_one); | 55 // Only variables: |
51 assertEquals(1073741824, -1073741824 / minus_one); | 56 var zero = 0; |
52 assertEquals(1073741824, -(-1073741824)); | 57 var minus_one = -1; |
53 assertEquals(1073741824, zero - (-1073741824)); | |
54 | 58 |
55 var half_min_smi = -(1<<15); | 59 assertEquals(result, min_smi * minus_one, name + "-varvarmult"); |
56 var half_max_smi = (1<<15); | 60 assertEquals(result, min_smi / minus_one, name + "-varvardiv"); |
| 61 assertEquals(result, zero - min_smi, name + "-varvarsub"); |
57 | 62 |
58 assertEquals(1073741824, -half_min_smi * half_max_smi); | 63 // Constants as variables |
59 assertEquals(1073741824, half_min_smi * -half_max_smi); | 64 assertEquals(result, eval(min_smi + " * minus_one"), name + "-litvarmult"); |
60 assertEquals(1073741824, half_max_smi * -half_min_smi); | 65 assertEquals(result, eval(min_smi + " / minus_one"), name + "-litvarmdiv"); |
61 assertEquals(1073741824, -half_max_smi * half_min_smi); | 66 assertEquals(result, eval("0 - (" + min_smi + ")"), name + "-varlitsub"); |
| 67 |
| 68 var half_min_smi = -(1 << (bits >> 1)); |
| 69 var half_max_smi = 1 << ((bits - 1) >> 1); |
| 70 |
| 71 assertEquals(max_smi + 1, -half_min_smi * half_max_smi, name + "-half1"); |
| 72 assertEquals(max_smi + 1, half_min_smi * -half_max_smi, name + "-half2"); |
| 73 assertEquals(max_smi + 1, half_max_smi * -half_min_smi, name + "-half3"); |
| 74 assertEquals(max_smi + 1, -half_max_smi * half_min_smi, name + "-half4"); |
| 75 } |
OLD | NEW |