1// Copyright 2009 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 29 30// Test fast div and mod. 31 32function divmod(div_func, mod_func, x, y) { 33 var div_answer = (div_func)(x); 34 assertEquals(x / y, div_answer, x + "/" + y); 35 var mod_answer = (mod_func)(x); 36 assertEquals(x % y, mod_answer, x + "%" + y); 37 var minus_div_answer = (div_func)(-x); 38 assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y); 39 var minus_mod_answer = (mod_func)(-x); 40 assertEquals(-x % y, minus_mod_answer, "-" + x + "%" + y); 41} 42 43 44function run_tests_for(divisor) { 45 print("(function(left) { return left / " + divisor + "; })"); 46 var div_func = this.eval("(function(left) { return left / " + divisor + "; })"); 47 var mod_func = this.eval("(function(left) { return left % " + divisor + "; })"); 48 var exp; 49 // Strange number test. 50 divmod(div_func, mod_func, 0, divisor); 51 divmod(div_func, mod_func, 1 / 0, divisor); 52 // Floating point number test. 53 for (exp = -1024; exp <= 1024; exp += 8) { 54 divmod(div_func, mod_func, Math.pow(2, exp), divisor); 55 divmod(div_func, mod_func, 0.9999999 * Math.pow(2, exp), divisor); 56 divmod(div_func, mod_func, 1.0000001 * Math.pow(2, exp), divisor); 57 } 58 // Integer number test. 59 for (exp = 0; exp <= 32; exp++) { 60 divmod(div_func, mod_func, 1 << exp, divisor); 61 divmod(div_func, mod_func, (1 << exp) + 1, divisor); 62 divmod(div_func, mod_func, (1 << exp) - 1, divisor); 63 } 64 divmod(div_func, mod_func, Math.floor(0x1fffffff / 3), divisor); 65 divmod(div_func, mod_func, Math.floor(-0x20000000 / 3), divisor); 66} 67 68 69var divisors = [ 70 0, 71 1, 72 2, 73 3, 74 4, 75 5, 76 6, 77 7, 78 8, 79 9, 80 10, 81 0x1000000, 82 0x40000000, 83 12, 84 60, 85 100, 86 1000 * 60 * 60 * 24]; 87 88for (var i = 0; i < divisors.length; i++) { 89 run_tests_for(divisors[i]); 90} 91 92// Test extreme corner cases of modulo. 93 94// Computes the modulo by slow but lossless operations. 95function compute_mod(dividend, divisor) { 96 // Return NaN if either operand is NaN, if divisor is 0 or 97 // dividend is an infinity. Return dividend if divisor is an infinity. 98 if (isNaN(dividend) || isNaN(divisor) || divisor == 0) { return NaN; } 99 var sign = 1; 100 if (dividend < 0) { dividend = -dividend; sign = -1; } 101 if (dividend == Infinity) { return NaN; } 102 if (divisor < 0) { divisor = -divisor; } 103 if (divisor == Infinity) { return sign * dividend; } 104 function rec_mod(a, b) { 105 // Subtracts maximal possible multiplum of b from a. 106 if (a >= b) { 107 a = rec_mod(a, 2 * b); 108 if (a >= b) { a -= b; } 109 } 110 return a; 111 } 112 return sign * rec_mod(dividend, divisor); 113} 114 115(function () { 116 var large_non_smi = 1234567891234.12245; 117 var small_non_smi = 43.2367243; 118 var repeating_decimal = 0.3; 119 var finite_decimal = 0.5; 120 var smi = 43; 121 var power_of_two = 64; 122 var min_normal = Number.MIN_VALUE * Math.pow(2, 52); 123 var max_denormal = Number.MIN_VALUE * (Math.pow(2, 52) - 1); 124 125 // All combinations of NaN, Infinity, normal, denormal and zero. 126 var example_numbers = [ 127 NaN, 128 0, 129 Number.MIN_VALUE, 130 3 * Number.MIN_VALUE, 131 max_denormal, 132 min_normal, 133 repeating_decimal, 134 finite_decimal, 135 smi, 136 power_of_two, 137 small_non_smi, 138 large_non_smi, 139 Number.MAX_VALUE, 140 Infinity 141 ]; 142 143 function doTest(a, b) { 144 var exp = compute_mod(a, b); 145 var act = a % b; 146 assertEquals(exp, act, a + " % " + b); 147 } 148 149 for (var i = 0; i < example_numbers.length; i++) { 150 for (var j = 0; j < example_numbers.length; j++) { 151 var a = example_numbers[i]; 152 var b = example_numbers[j]; 153 doTest(a,b); 154 doTest(-a,b); 155 doTest(a,-b); 156 doTest(-a,-b); 157 } 158 } 159})(); 160 161 162(function () { 163 // Edge cases 164 var zero = 0; 165 var minsmi32 = -0x40000000; 166 var minsmi64 = -0x80000000; 167 var somenum = 3532; 168 assertEquals(-0, zero / -1, "0 / -1"); 169 assertEquals(1, minsmi32 / -0x40000000, "minsmi/minsmi-32"); 170 assertEquals(1, minsmi64 / -0x80000000, "minsmi/minsmi-64"); 171 assertEquals(somenum, somenum % -0x40000000, "%minsmi-32"); 172 assertEquals(somenum, somenum % -0x80000000, "%minsmi-64"); 173})(); 174 175 176// Side-effect-free expressions containing bit operations use 177// an optimized compiler with int32 values. Ensure that modulus 178// produces negative zeros correctly. 179function negative_zero_modulus_test() { 180 var x = 4; 181 var y = -4; 182 x = x + x - x; 183 y = y + y - y; 184 var z = (y | y | y | y) % x; 185 assertEquals(-1 / 0, 1 / z); 186 z = (x | x | x | x) % x; 187 assertEquals(1 / 0, 1 / z); 188 z = (y | y | y | y) % y; 189 assertEquals(-1 / 0, 1 / z); 190 z = (x | x | x | x) % y; 191 assertEquals(1 / 0, 1 / z); 192} 193 194negative_zero_modulus_test(); 195 196 197function lithium_integer_mod() { 198 var left_operands = [ 199 0, 200 305419896, // 0x12345678 201 ]; 202 203 // Test the standard lithium code for modulo opeartions. 204 var mod_func; 205 for (var i = 0; i < left_operands.length; i++) { 206 for (var j = 0; j < divisors.length; j++) { 207 mod_func = this.eval("(function(left) { return left % " + divisors[j]+ "; })"); 208 assertEquals((mod_func)(left_operands[i]), left_operands[i] % divisors[j]); 209 assertEquals((mod_func)(-left_operands[i]), -left_operands[i] % divisors[j]); 210 } 211 } 212 213 var results_powers_of_two = [ 214 // 0 215 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 216 // 305419896 == 0x12345678 217 [0, 0, 0, 8, 24, 56, 120, 120, 120, 632, 1656, 1656, 5752, 5752, 22136, 22136, 22136, 22136, 284280, 284280, 1332856, 3430008, 3430008, 3430008, 3430008, 36984440, 36984440, 36984440, 305419896, 305419896, 305419896], 218 ]; 219 220 // Test the lithium code for modulo operations with a variable power of two 221 // right hand side operand. 222 for (var i = 0; i < left_operands.length; i++) { 223 for (var j = 0; j < 31; j++) { 224 assertEquals(results_powers_of_two[i][j], left_operands[i] % (2 << j)); 225 assertEquals(results_powers_of_two[i][j], left_operands[i] % -(2 << j)); 226 assertEquals(-results_powers_of_two[i][j], -left_operands[i] % (2 << j)); 227 assertEquals(-results_powers_of_two[i][j], -left_operands[i] % -(2 << j)); 228 } 229 } 230 231 // Test the lithium code for modulo operations with a constant power of two 232 // right hand side operand. 233 for (var i = 0; i < left_operands.length; i++) { 234 // With positive left hand side operand. 235 assertEquals(results_powers_of_two[i][0], left_operands[i] % -(2 << 0)); 236 assertEquals(results_powers_of_two[i][1], left_operands[i] % (2 << 1)); 237 assertEquals(results_powers_of_two[i][2], left_operands[i] % -(2 << 2)); 238 assertEquals(results_powers_of_two[i][3], left_operands[i] % (2 << 3)); 239 assertEquals(results_powers_of_two[i][4], left_operands[i] % -(2 << 4)); 240 assertEquals(results_powers_of_two[i][5], left_operands[i] % (2 << 5)); 241 assertEquals(results_powers_of_two[i][6], left_operands[i] % -(2 << 6)); 242 assertEquals(results_powers_of_two[i][7], left_operands[i] % (2 << 7)); 243 assertEquals(results_powers_of_two[i][8], left_operands[i] % -(2 << 8)); 244 assertEquals(results_powers_of_two[i][9], left_operands[i] % (2 << 9)); 245 assertEquals(results_powers_of_two[i][10], left_operands[i] % -(2 << 10)); 246 assertEquals(results_powers_of_two[i][11], left_operands[i] % (2 << 11)); 247 assertEquals(results_powers_of_two[i][12], left_operands[i] % -(2 << 12)); 248 assertEquals(results_powers_of_two[i][13], left_operands[i] % (2 << 13)); 249 assertEquals(results_powers_of_two[i][14], left_operands[i] % -(2 << 14)); 250 assertEquals(results_powers_of_two[i][15], left_operands[i] % (2 << 15)); 251 assertEquals(results_powers_of_two[i][16], left_operands[i] % -(2 << 16)); 252 assertEquals(results_powers_of_two[i][17], left_operands[i] % (2 << 17)); 253 assertEquals(results_powers_of_two[i][18], left_operands[i] % -(2 << 18)); 254 assertEquals(results_powers_of_two[i][19], left_operands[i] % (2 << 19)); 255 assertEquals(results_powers_of_two[i][20], left_operands[i] % -(2 << 20)); 256 assertEquals(results_powers_of_two[i][21], left_operands[i] % (2 << 21)); 257 assertEquals(results_powers_of_two[i][22], left_operands[i] % -(2 << 22)); 258 assertEquals(results_powers_of_two[i][23], left_operands[i] % (2 << 23)); 259 assertEquals(results_powers_of_two[i][24], left_operands[i] % -(2 << 24)); 260 assertEquals(results_powers_of_two[i][25], left_operands[i] % (2 << 25)); 261 assertEquals(results_powers_of_two[i][26], left_operands[i] % -(2 << 26)); 262 assertEquals(results_powers_of_two[i][27], left_operands[i] % (2 << 27)); 263 assertEquals(results_powers_of_two[i][28], left_operands[i] % -(2 << 28)); 264 assertEquals(results_powers_of_two[i][29], left_operands[i] % (2 << 29)); 265 assertEquals(results_powers_of_two[i][30], left_operands[i] % -(2 << 30)); 266 // With negative left hand side operand. 267 assertEquals(-results_powers_of_two[i][0], -left_operands[i] % -(2 << 0)); 268 assertEquals(-results_powers_of_two[i][1], -left_operands[i] % (2 << 1)); 269 assertEquals(-results_powers_of_two[i][2], -left_operands[i] % -(2 << 2)); 270 assertEquals(-results_powers_of_two[i][3], -left_operands[i] % (2 << 3)); 271 assertEquals(-results_powers_of_two[i][4], -left_operands[i] % -(2 << 4)); 272 assertEquals(-results_powers_of_two[i][5], -left_operands[i] % (2 << 5)); 273 assertEquals(-results_powers_of_two[i][6], -left_operands[i] % -(2 << 6)); 274 assertEquals(-results_powers_of_two[i][7], -left_operands[i] % (2 << 7)); 275 assertEquals(-results_powers_of_two[i][8], -left_operands[i] % -(2 << 8)); 276 assertEquals(-results_powers_of_two[i][9], -left_operands[i] % (2 << 9)); 277 assertEquals(-results_powers_of_two[i][10], -left_operands[i] % -(2 << 10)); 278 assertEquals(-results_powers_of_two[i][11], -left_operands[i] % (2 << 11)); 279 assertEquals(-results_powers_of_two[i][12], -left_operands[i] % -(2 << 12)); 280 assertEquals(-results_powers_of_two[i][13], -left_operands[i] % (2 << 13)); 281 assertEquals(-results_powers_of_two[i][14], -left_operands[i] % -(2 << 14)); 282 assertEquals(-results_powers_of_two[i][15], -left_operands[i] % (2 << 15)); 283 assertEquals(-results_powers_of_two[i][16], -left_operands[i] % -(2 << 16)); 284 assertEquals(-results_powers_of_two[i][17], -left_operands[i] % (2 << 17)); 285 assertEquals(-results_powers_of_two[i][18], -left_operands[i] % -(2 << 18)); 286 assertEquals(-results_powers_of_two[i][19], -left_operands[i] % (2 << 19)); 287 assertEquals(-results_powers_of_two[i][20], -left_operands[i] % -(2 << 20)); 288 assertEquals(-results_powers_of_two[i][21], -left_operands[i] % (2 << 21)); 289 assertEquals(-results_powers_of_two[i][22], -left_operands[i] % -(2 << 22)); 290 assertEquals(-results_powers_of_two[i][23], -left_operands[i] % (2 << 23)); 291 assertEquals(-results_powers_of_two[i][24], -left_operands[i] % -(2 << 24)); 292 assertEquals(-results_powers_of_two[i][25], -left_operands[i] % (2 << 25)); 293 assertEquals(-results_powers_of_two[i][26], -left_operands[i] % -(2 << 26)); 294 assertEquals(-results_powers_of_two[i][27], -left_operands[i] % (2 << 27)); 295 assertEquals(-results_powers_of_two[i][28], -left_operands[i] % -(2 << 28)); 296 assertEquals(-results_powers_of_two[i][29], -left_operands[i] % (2 << 29)); 297 assertEquals(-results_powers_of_two[i][30], -left_operands[i] % -(2 << 30)); 298 } 299 300} 301 302lithium_integer_mod(); 303%OptimizeFunctionOnNextCall(lithium_integer_mod) 304lithium_integer_mod(); 305