1// Copyright 2012 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// Flags: --allow-natives-syntax --nouse-inlining --enable-sudiv 29 30// Use this function as reference. Make sure it is not inlined. 31function div(a, b) { 32 return a / b; 33} 34 35var limit = 0x1000000; 36var exhaustive_limit = 100; 37var step = 10; 38var values = [0x10000001, 39 0x12345678, 40 -0x789abcdf, // 0x87654321 41 0x01234567, 42 0x76543210, 43 -0x80000000, // 0x80000000 44 0x7fffffff, 45 -0x0fffffff, // 0xf0000001 46 0x00000010, 47 -0x01000000 // 0xff000000 48 ]; 49 50function test_div() { 51 var c = 0; 52 for (var k = 0; k <= limit; k++) { 53 if (k > exhaustive_limit) { c += step; k += c; } 54 assertEquals(Math.floor(div(k, 1)), Math.floor(k / 1)); 55 assertEquals(Math.floor(div(k, -1)), Math.floor(k / -1)); 56 assertEquals(Math.floor(div(k, 2)), Math.floor(k / 2)); 57 assertEquals(Math.floor(div(k, -2)), Math.floor(k / -2)); 58 assertEquals(Math.floor(div(k, 3)), Math.floor(k / 3)); 59 assertEquals(Math.floor(div(k, -3)), Math.floor(k / -3)); 60 assertEquals(Math.floor(div(k, 4)), Math.floor(k / 4)); 61 assertEquals(Math.floor(div(k, -4)), Math.floor(k / -4)); 62 assertEquals(Math.floor(div(k, 5)), Math.floor(k / 5)); 63 assertEquals(Math.floor(div(k, -5)), Math.floor(k / -5)); 64 assertEquals(Math.floor(div(k, 6)), Math.floor(k / 6)); 65 assertEquals(Math.floor(div(k, -6)), Math.floor(k / -6)); 66 assertEquals(Math.floor(div(k, 7)), Math.floor(k / 7)); 67 assertEquals(Math.floor(div(k, -7)), Math.floor(k / -7)); 68 assertEquals(Math.floor(div(k, 8)), Math.floor(k / 8)); 69 assertEquals(Math.floor(div(k, -8)), Math.floor(k / -8)); 70 assertEquals(Math.floor(div(k, 9)), Math.floor(k / 9)); 71 assertEquals(Math.floor(div(k, -9)), Math.floor(k / -9)); 72 assertEquals(Math.floor(div(k, 10)), Math.floor(k / 10)); 73 assertEquals(Math.floor(div(k, -10)), Math.floor(k / -10)); 74 assertEquals(Math.floor(div(k, 11)), Math.floor(k / 11)); 75 assertEquals(Math.floor(div(k, -11)), Math.floor(k / -11)); 76 assertEquals(Math.floor(div(k, 12)), Math.floor(k / 12)); 77 assertEquals(Math.floor(div(k, -12)), Math.floor(k / -12)); 78 assertEquals(Math.floor(div(k, 13)), Math.floor(k / 13)); 79 assertEquals(Math.floor(div(k, -13)), Math.floor(k / -13)); 80 assertEquals(Math.floor(div(k, 14)), Math.floor(k / 14)); 81 assertEquals(Math.floor(div(k, -14)), Math.floor(k / -14)); 82 assertEquals(Math.floor(div(k, 15)), Math.floor(k / 15)); 83 assertEquals(Math.floor(div(k, -15)), Math.floor(k / -15)); 84 assertEquals(Math.floor(div(k, 16)), Math.floor(k / 16)); 85 assertEquals(Math.floor(div(k, -16)), Math.floor(k / -16)); 86 assertEquals(Math.floor(div(k, 17)), Math.floor(k / 17)); 87 assertEquals(Math.floor(div(k, -17)), Math.floor(k / -17)); 88 assertEquals(Math.floor(div(k, 18)), Math.floor(k / 18)); 89 assertEquals(Math.floor(div(k, -18)), Math.floor(k / -18)); 90 assertEquals(Math.floor(div(k, 19)), Math.floor(k / 19)); 91 assertEquals(Math.floor(div(k, -19)), Math.floor(k / -19)); 92 assertEquals(Math.floor(div(k, 20)), Math.floor(k / 20)); 93 assertEquals(Math.floor(div(k, -20)), Math.floor(k / -20)); 94 assertEquals(Math.floor(div(k, 21)), Math.floor(k / 21)); 95 assertEquals(Math.floor(div(k, -21)), Math.floor(k / -21)); 96 assertEquals(Math.floor(div(k, 22)), Math.floor(k / 22)); 97 assertEquals(Math.floor(div(k, -22)), Math.floor(k / -22)); 98 assertEquals(Math.floor(div(k, 23)), Math.floor(k / 23)); 99 assertEquals(Math.floor(div(k, -23)), Math.floor(k / -23)); 100 assertEquals(Math.floor(div(k, 24)), Math.floor(k / 24)); 101 assertEquals(Math.floor(div(k, -24)), Math.floor(k / -24)); 102 assertEquals(Math.floor(div(k, 25)), Math.floor(k / 25)); 103 assertEquals(Math.floor(div(k, -25)), Math.floor(k / -25)); 104 assertEquals(Math.floor(div(k, 125)), Math.floor(k / 125)); 105 assertEquals(Math.floor(div(k, -125)), Math.floor(k / -125)); 106 assertEquals(Math.floor(div(k, 625)), Math.floor(k / 625)); 107 assertEquals(Math.floor(div(k, -625)), Math.floor(k / -625)); 108 } 109 c = 0; 110 for (var k = 0; k <= limit; k++) { 111 if (k > exhaustive_limit) { c += step; k += c; } 112 assertEquals(Math.floor(div(-k, 1)), Math.floor(-k / 1)); 113 assertEquals(Math.floor(div(-k, -1)), Math.floor(-k / -1)); 114 assertEquals(Math.floor(div(-k, 2)), Math.floor(-k / 2)); 115 assertEquals(Math.floor(div(-k, -2)), Math.floor(-k / -2)); 116 assertEquals(Math.floor(div(-k, 3)), Math.floor(-k / 3)); 117 assertEquals(Math.floor(div(-k, -3)), Math.floor(-k / -3)); 118 assertEquals(Math.floor(div(-k, 4)), Math.floor(-k / 4)); 119 assertEquals(Math.floor(div(-k, -4)), Math.floor(-k / -4)); 120 assertEquals(Math.floor(div(-k, 5)), Math.floor(-k / 5)); 121 assertEquals(Math.floor(div(-k, -5)), Math.floor(-k / -5)); 122 assertEquals(Math.floor(div(-k, 6)), Math.floor(-k / 6)); 123 assertEquals(Math.floor(div(-k, -6)), Math.floor(-k / -6)); 124 assertEquals(Math.floor(div(-k, 7)), Math.floor(-k / 7)); 125 assertEquals(Math.floor(div(-k, -7)), Math.floor(-k / -7)); 126 assertEquals(Math.floor(div(-k, 8)), Math.floor(-k / 8)); 127 assertEquals(Math.floor(div(-k, -8)), Math.floor(-k / -8)); 128 assertEquals(Math.floor(div(-k, 9)), Math.floor(-k / 9)); 129 assertEquals(Math.floor(div(-k, -9)), Math.floor(-k / -9)); 130 assertEquals(Math.floor(div(-k, 10)), Math.floor(-k / 10)); 131 assertEquals(Math.floor(div(-k, -10)), Math.floor(-k / -10)); 132 assertEquals(Math.floor(div(-k, 11)), Math.floor(-k / 11)); 133 assertEquals(Math.floor(div(-k, -11)), Math.floor(-k / -11)); 134 assertEquals(Math.floor(div(-k, 12)), Math.floor(-k / 12)); 135 assertEquals(Math.floor(div(-k, -12)), Math.floor(-k / -12)); 136 assertEquals(Math.floor(div(-k, 13)), Math.floor(-k / 13)); 137 assertEquals(Math.floor(div(-k, -13)), Math.floor(-k / -13)); 138 assertEquals(Math.floor(div(-k, 14)), Math.floor(-k / 14)); 139 assertEquals(Math.floor(div(-k, -14)), Math.floor(-k / -14)); 140 assertEquals(Math.floor(div(-k, 15)), Math.floor(-k / 15)); 141 assertEquals(Math.floor(div(-k, -15)), Math.floor(-k / -15)); 142 assertEquals(Math.floor(div(-k, 16)), Math.floor(-k / 16)); 143 assertEquals(Math.floor(div(-k, -16)), Math.floor(-k / -16)); 144 assertEquals(Math.floor(div(-k, 17)), Math.floor(-k / 17)); 145 assertEquals(Math.floor(div(-k, -17)), Math.floor(-k / -17)); 146 assertEquals(Math.floor(div(-k, 18)), Math.floor(-k / 18)); 147 assertEquals(Math.floor(div(-k, -18)), Math.floor(-k / -18)); 148 assertEquals(Math.floor(div(-k, 19)), Math.floor(-k / 19)); 149 assertEquals(Math.floor(div(-k, -19)), Math.floor(-k / -19)); 150 assertEquals(Math.floor(div(-k, 20)), Math.floor(-k / 20)); 151 assertEquals(Math.floor(div(-k, -20)), Math.floor(-k / -20)); 152 assertEquals(Math.floor(div(-k, 21)), Math.floor(-k / 21)); 153 assertEquals(Math.floor(div(-k, -21)), Math.floor(-k / -21)); 154 assertEquals(Math.floor(div(-k, 22)), Math.floor(-k / 22)); 155 assertEquals(Math.floor(div(-k, -22)), Math.floor(-k / -22)); 156 assertEquals(Math.floor(div(-k, 23)), Math.floor(-k / 23)); 157 assertEquals(Math.floor(div(-k, -23)), Math.floor(-k / -23)); 158 assertEquals(Math.floor(div(-k, 24)), Math.floor(-k / 24)); 159 assertEquals(Math.floor(div(-k, -24)), Math.floor(-k / -24)); 160 assertEquals(Math.floor(div(-k, 25)), Math.floor(-k / 25)); 161 assertEquals(Math.floor(div(-k, -25)), Math.floor(-k / -25)); 162 assertEquals(Math.floor(div(-k, 125)), Math.floor(-k / 125)); 163 assertEquals(Math.floor(div(-k, -125)), Math.floor(-k / -125)); 164 assertEquals(Math.floor(div(-k, 625)), Math.floor(-k / 625)); 165 assertEquals(Math.floor(div(-k, -625)), Math.floor(-k / -625)); 166 } 167 // Test for edge cases. 168 // Use (values[key] | 0) to force the integer type. 169 for (var i = 0; i < values.length; i++) { 170 for (var j = 0; j < values.length; j++) { 171 assertEquals(Math.floor(div((values[i] | 0), (values[j] | 0))), 172 Math.floor((values[i] | 0) / (values[j] | 0))); 173 assertEquals(Math.floor(div(-(values[i] | 0), (values[j] | 0))), 174 Math.floor(-(values[i] | 0) / (values[j] | 0))); 175 assertEquals(Math.floor(div((values[i] | 0), -(values[j] | 0))), 176 Math.floor((values[i] | 0) / -(values[j] | 0))); 177 assertEquals(Math.floor(div(-(values[i] | 0), -(values[j] | 0))), 178 Math.floor(-(values[i] | 0) / -(values[j] | 0))); 179 } 180 } 181} 182 183test_div(); 184%OptimizeFunctionOnNextCall(test_div); 185test_div(); 186 187// Test for flooring correctness. 188var values2 = [1, 3, 10, 99, 100, 101, 0x7fffffff]; 189function test_div2() { 190 for (var i = 0; i < values2.length; i++) { 191 for (var j = 0; j < values2.length; j++) { 192 assertEquals(Math.floor(div((values2[i] | 0), (values2[j] | 0))), 193 Math.floor((values2[i] | 0) / (values2[j] | 0))); 194 assertEquals(Math.floor(div(-(values2[i] | 0), (values2[j] | 0))), 195 Math.floor(-(values2[i] | 0) / (values2[j] | 0))); 196 assertEquals(Math.floor(div((values2[i] | 0), -(values2[j] | 0))), 197 Math.floor((values2[i] | 0) / -(values2[j] | 0))); 198 assertEquals(Math.floor(div(-(values2[i] | 0), -(values2[j] | 0))), 199 Math.floor(-(values2[i] | 0) / -(values2[j] | 0))); 200 } 201 } 202} 203 204test_div2(); 205%OptimizeFunctionOnNextCall(test_div2); 206test_div2(); 207 208 209// Test for negative zero, overflow and division by 0. 210// Separate the tests to prevent deoptimizations from making the other optimized 211// test unreachable. 212 213// We box the value in an array to avoid constant propagation. 214var neg_one_in_array = [-1]; 215var zero_in_array = [0]; 216var min_int_in_array = [-2147483648]; 217 218// Test for dividing by constant. 219function IsNegativeZero(x) { 220 assertTrue(x == 0); // Is 0 or -0. 221 var y = 1 / x; 222 assertFalse(isFinite(y)); 223 return y < 0; 224} 225 226function test_div_deopt_minus_zero() { 227 for (var i = 0; i < 2; ++i) { 228 assertTrue(IsNegativeZero(Math.floor((zero_in_array[0] | 0) / -1))); 229 } 230} 231 232function test_div_deopt_overflow() { 233 for (var i = 0; i < 2; ++i) { 234 // We use '| 0' to force the representation to int32. 235 assertEquals(-min_int_in_array[0], 236 Math.floor((min_int_in_array[0] | 0) / -1)); 237 } 238} 239 240function test_div_deopt_div_by_zero() { 241 for (var i = 0; i < 2; ++i) { 242 assertEquals(div(i, 0), 243 Math.floor(i / 0)); 244 } 245} 246 247test_div_deopt_minus_zero(); 248test_div_deopt_overflow(); 249test_div_deopt_div_by_zero(); 250%OptimizeFunctionOnNextCall(test_div_deopt_minus_zero); 251%OptimizeFunctionOnNextCall(test_div_deopt_overflow); 252%OptimizeFunctionOnNextCall(test_div_deopt_div_by_zero); 253test_div_deopt_minus_zero(); 254test_div_deopt_overflow(); 255test_div_deopt_div_by_zero(); 256 257// Test for dividing by variable. 258function test_div_deopt_minus_zero_v() { 259 for (var i = 0; i < 2; ++i) { 260 assertTrue(IsNegativeZero(Math.floor((zero_in_array[0] | 0) / 261 neg_one_in_array[0]))); 262 } 263} 264 265function test_div_deopt_overflow_v() { 266 for (var i = 0; i < 2; ++i) { 267 // We use '| 0' to force the representation to int32. 268 assertEquals(-min_int_in_array[0], 269 Math.floor((min_int_in_array[0] | 0) / neg_one_in_array[0])); 270 } 271} 272 273function test_div_deopt_div_by_zero_v() { 274 for (var i = 0; i < 2; ++i) { 275 assertEquals(div(i, 0), 276 Math.floor(i / zero_in_array[0])); 277 } 278} 279 280test_div_deopt_minus_zero_v(); 281test_div_deopt_overflow_v(); 282test_div_deopt_div_by_zero_v(); 283%OptimizeFunctionOnNextCall(test_div_deopt_minus_zero_v); 284%OptimizeFunctionOnNextCall(test_div_deopt_overflow_v); 285%OptimizeFunctionOnNextCall(test_div_deopt_div_by_zero_v); 286test_div_deopt_minus_zero_v(); 287test_div_deopt_overflow_v(); 288test_div_deopt_div_by_zero_v(); 289 290 291// Test for flooring division with negative dividend. 292function flooring_div_by_3(y) { 293 return Math.floor(y / 3); 294} 295 296assertEquals(-1, flooring_div_by_3(-2)); 297assertEquals(-1, flooring_div_by_3(-2)); 298%OptimizeFunctionOnNextCall(flooring_div_by_3); 299assertEquals(-1, flooring_div_by_3(-2)); 300