blob: cc7985d820e27d345cd833f31031a4431a974298 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28// Ensure that we can correctly change the sign of the most negative smi.
29
Steve Block3ce2e202009-11-05 08:53:23 +000030// Possible Smi ranges.
31var ranges = [{min: -1073741824, max: 1073741823, bits: 31},
32 {min: -2147483648, max: 2147483647, bits: 32}];
Steve Blocka7e24c12009-10-30 11:49:00 +000033
Steve Block3ce2e202009-11-05 08:53:23 +000034for (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";
Steve Blocka7e24c12009-10-30 11:49:00 +000040
Steve Block3ce2e202009-11-05 08:53:23 +000041 var result = max_smi + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000042
Steve Block3ce2e202009-11-05 08:53:23 +000043 // Min smi as literal
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";
Steve Blocka7e24c12009-10-30 11:49:00 +000048
Steve Block3ce2e202009-11-05 08:53:23 +000049 // As variable:
50 assertEquals(result, min_smi * -1, name + "-varconmult");
51 assertEquals(result, min_smi / -1, name + "-varcondiv");
52 assertEquals(result, -min_smi, name + "-varneg");
53 assertEquals(result, 0 - min_smi, name + "-convarsub");
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Steve Block3ce2e202009-11-05 08:53:23 +000055 // Only variables:
56 var zero = 0;
57 var minus_one = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +000058
Steve Block3ce2e202009-11-05 08:53:23 +000059 assertEquals(result, min_smi * minus_one, name + "-varvarmult");
60 assertEquals(result, min_smi / minus_one, name + "-varvardiv");
61 assertEquals(result, zero - min_smi, name + "-varvarsub");
Steve Blocka7e24c12009-10-30 11:49:00 +000062
Steve Block3ce2e202009-11-05 08:53:23 +000063 // Constants as variables
64 assertEquals(result, eval(min_smi + " * minus_one"), name + "-litvarmult");
65 assertEquals(result, eval(min_smi + " / minus_one"), name + "-litvarmdiv");
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}