1// Copyright 2010 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// Repeat most the tests in smi-ops.js that use SMI_MIN and SMI_MAX, but
29// with SMI_MIN and SMI_MAX from the 64-bit platform, which represents all
30// signed 32-bit integer values as smis.
31
32const SMI_MAX = (1 << 30) - 1 + (1 << 30);  // Create without overflowing.
33const SMI_MIN = -SMI_MAX - 1;  // Create without overflowing.
34const ONE = 1;
35const ONE_HUNDRED = 100;
36
37const OBJ_42 = new (function() {
38  this.valueOf = function() { return 42; };
39})();
40
41assertEquals(42, OBJ_42.valueOf());
42
43
44function Add1(x) {
45  return x + 1;
46}
47
48function Add100(x) {
49  return x + 100;
50}
51
52function Add1Reversed(x) {
53  return 1 + x;
54}
55
56function Add100Reversed(x) {
57  return 100 + x;
58}
59
60
61assertEquals(1, Add1(0));  // fast case
62assertEquals(1, Add1Reversed(0));  // fast case
63assertEquals(SMI_MAX + ONE, Add1(SMI_MAX), "smimax + 1");
64assertEquals(SMI_MAX + ONE, Add1Reversed(SMI_MAX), "1 + smimax");
65assertEquals(42 + ONE, Add1(OBJ_42));  // non-smi
66assertEquals(42 + ONE, Add1Reversed(OBJ_42));  // non-smi
67
68assertEquals(100, Add100(0));  // fast case
69assertEquals(100, Add100Reversed(0));  // fast case
70assertEquals(SMI_MAX + ONE_HUNDRED, Add100(SMI_MAX), "smimax + 100");
71assertEquals(SMI_MAX + ONE_HUNDRED, Add100Reversed(SMI_MAX), " 100 + smimax");
72assertEquals(42 + ONE_HUNDRED, Add100(OBJ_42));  // non-smi
73assertEquals(42 + ONE_HUNDRED, Add100Reversed(OBJ_42));  // non-smi
74
75
76
77function Sub1(x) {
78  return x - 1;
79}
80
81function Sub100(x) {
82  return x - 100;
83}
84
85function Sub1Reversed(x) {
86  return 1 - x;
87}
88
89function Sub100Reversed(x) {
90  return 100 - x;
91}
92
93
94assertEquals(0, Sub1(1));  // fast case
95assertEquals(-1, Sub1Reversed(2));  // fast case
96assertEquals(SMI_MIN - ONE, Sub1(SMI_MIN));  // overflow
97assertEquals(ONE - SMI_MIN, Sub1Reversed(SMI_MIN));  // overflow
98assertEquals(42 - ONE, Sub1(OBJ_42));  // non-smi
99assertEquals(ONE - 42, Sub1Reversed(OBJ_42));  // non-smi
100
101assertEquals(0, Sub100(100));  // fast case
102assertEquals(1, Sub100Reversed(99));  // fast case
103assertEquals(SMI_MIN - ONE_HUNDRED, Sub100(SMI_MIN));  // overflow
104assertEquals(ONE_HUNDRED - SMI_MIN, Sub100Reversed(SMI_MIN));  // overflow
105assertEquals(42 - ONE_HUNDRED, Sub100(OBJ_42));  // non-smi
106assertEquals(ONE_HUNDRED - 42, Sub100Reversed(OBJ_42));  // non-smi
107
108
109function Shr1(x) {
110  return x >>> 1;
111}
112
113function Shr100(x) {
114  return x >>> 100;
115}
116
117function Shr1Reversed(x) {
118  return 1 >>> x;
119}
120
121function Shr100Reversed(x) {
122  return 100 >>> x;
123}
124
125function Sar1(x) {
126  return x >> 1;
127}
128
129function Sar100(x) {
130  return x >> 100;
131}
132
133function Sar1Reversed(x) {
134  return 1 >> x;
135}
136
137function Sar100Reversed(x) {
138  return 100 >> x;
139}
140
141
142assertEquals(0, Shr1(1));
143assertEquals(0, Sar1(1));
144assertEquals(0, Shr1Reversed(2));
145assertEquals(0, Sar1Reversed(2));
146assertEquals(1073741824, Shr1(SMI_MIN));
147assertEquals(-1073741824, Sar1(SMI_MIN));
148assertEquals(1, Shr1Reversed(SMI_MIN));
149assertEquals(1, Sar1Reversed(SMI_MIN));
150assertEquals(21, Shr1(OBJ_42));
151assertEquals(21, Sar1(OBJ_42));
152assertEquals(0, Shr1Reversed(OBJ_42));
153assertEquals(0, Sar1Reversed(OBJ_42));
154
155assertEquals(6, Shr100(100), "100 >>> 100");
156assertEquals(6, Sar100(100), "100 >> 100");
157assertEquals(12, Shr100Reversed(99));
158assertEquals(12, Sar100Reversed(99));
159assertEquals(134217728, Shr100(SMI_MIN));
160assertEquals(-134217728, Sar100(SMI_MIN));
161assertEquals(100, Shr100Reversed(SMI_MIN));
162assertEquals(100, Sar100Reversed(SMI_MIN));
163assertEquals(2, Shr100(OBJ_42));
164assertEquals(2, Sar100(OBJ_42));
165assertEquals(0, Shr100Reversed(OBJ_42));
166assertEquals(0, Sar100Reversed(OBJ_42));
167
168
169function Xor1(x) {
170  return x ^ 1;
171}
172
173function Xor100(x) {
174  return x ^ 100;
175}
176
177function Xor1Reversed(x) {
178  return 1 ^ x;
179}
180
181function Xor100Reversed(x) {
182  return 100 ^ x;
183}
184
185
186assertEquals(0, Xor1(1));
187assertEquals(3, Xor1Reversed(2));
188assertEquals(SMI_MIN + 1, Xor1(SMI_MIN));
189assertEquals(SMI_MIN + 1, Xor1Reversed(SMI_MIN));
190assertEquals(43, Xor1(OBJ_42));
191assertEquals(43, Xor1Reversed(OBJ_42));
192
193assertEquals(0, Xor100(100));
194assertEquals(7, Xor100Reversed(99));
195assertEquals(-2147483548, Xor100(SMI_MIN));
196assertEquals(-2147483548, Xor100Reversed(SMI_MIN));
197assertEquals(78, Xor100(OBJ_42));
198assertEquals(78, Xor100Reversed(OBJ_42));
199
200var x = 0x23; var y = 0x35;
201assertEquals(0x16, x ^ y);
202
203
204// Bitwise not.
205var v = 0;
206assertEquals(-1, ~v);
207v = SMI_MIN;
208assertEquals(0x7fffffff, ~v, "~smimin");
209v = SMI_MAX;
210assertEquals(-0x80000000, ~v, "~smimax");
211
212// Overflowing ++ and --.
213v = SMI_MAX;
214v++;
215assertEquals(0x80000000, v, "smimax++");
216v = SMI_MIN;
217v--;
218assertEquals(-0x80000001, v, "smimin--");
219
220// Check that comparisons of numbers separated by MIN_SMI work.
221assertFalse(SMI_MIN > 0);
222assertFalse(SMI_MIN + 1 > 1);
223assertFalse(SMI_MIN + 1 > 2);
224assertFalse(SMI_MIN + 2 > 1);
225assertFalse(0 < SMI_MIN);
226assertTrue(-1 < SMI_MAX);
227assertFalse(SMI_MAX < -1);
228