Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 36 assertEquals(x % y, mod_answer, x + "%" + y); | 36 assertEquals(x % y, mod_answer, x + "%" + y); |
| 37 var minus_div_answer = (div_func)(-x); | 37 var minus_div_answer = (div_func)(-x); |
| 38 assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y); | 38 assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y); |
| 39 var minus_mod_answer = (mod_func)(-x); | 39 var minus_mod_answer = (mod_func)(-x); |
| 40 assertEquals(-x % y, minus_mod_answer, "-" + x + "%" + y); | 40 assertEquals(-x % y, minus_mod_answer, "-" + x + "%" + y); |
| 41 } | 41 } |
| 42 | 42 |
| 43 | 43 |
| 44 function run_tests_for(divisor) { | 44 function run_tests_for(divisor) { |
| 45 print("(function(left) { return left / " + divisor + "; })"); | 45 print("(function(left) { return left / " + divisor + "; })"); |
| 46 var div_func = this.eval("(function(left) { return left / " + divisor + "; })" ); | 46 var div_func = this.eval("(function(left) { return left / " + divisor + "; })" ); |
|
Rico
2011/12/08 10:24:37
long line
| |
| 47 var mod_func = this.eval("(function(left) { return left % " + divisor + "; })" ); | 47 var mod_func = this.eval("(function(left) { return left % " + divisor + "; })" ); |
|
Rico
2011/12/08 10:24:37
long line
| |
| 48 var exp; | 48 var exp; |
| 49 // Strange number test. | 49 // Strange number test. |
| 50 divmod(div_func, mod_func, 0, divisor); | 50 divmod(div_func, mod_func, 0, divisor); |
| 51 divmod(div_func, mod_func, 1 / 0, divisor); | 51 divmod(div_func, mod_func, 1 / 0, divisor); |
| 52 // Floating point number test. | 52 // Floating point number test. |
| 53 for (exp = -1024; exp <= 1024; exp += 8) { | 53 for (exp = -1024; exp <= 1024; exp += 8) { |
| 54 divmod(div_func, mod_func, Math.pow(2, exp), divisor); | 54 divmod(div_func, mod_func, Math.pow(2, exp), divisor); |
| 55 divmod(div_func, mod_func, 0.9999999 * 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); | 56 divmod(div_func, mod_func, 1.0000001 * Math.pow(2, exp), divisor); |
| 57 } | 57 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Test extreme corner cases of modulo. | 92 // Test extreme corner cases of modulo. |
| 93 | 93 |
| 94 // Computes the modulo by slow but lossless operations. | 94 // Computes the modulo by slow but lossless operations. |
| 95 function compute_mod(dividend, divisor) { | 95 function compute_mod(dividend, divisor) { |
| 96 // Return NaN if either operand is NaN, if divisor is 0 or | 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. | 97 // dividend is an infinity. Return dividend if divisor is an infinity. |
| 98 if (isNaN(dividend) || isNaN(divisor) || divisor == 0) { return NaN; } | 98 if (isNaN(dividend) || isNaN(divisor) || divisor == 0) { return NaN; } |
| 99 var sign = 1; | 99 var sign = 1; |
| 100 if (dividend < 0) { dividend = -dividend; sign = -1; } | 100 if (dividend < 0) { dividend = -dividend; sign = -1; } |
|
Rico
2011/12/08 10:24:37
body on individual lines
| |
| 101 if (dividend == Infinity) { return NaN; } | 101 if (dividend == Infinity) { return NaN; } |
| 102 if (divisor < 0) { divisor = -divisor; } | 102 if (divisor < 0) { divisor = -divisor; } |
| 103 if (divisor == Infinity) { return sign * dividend; } | 103 if (divisor == Infinity) { return sign * dividend; } |
| 104 function rec_mod(a, b) { | 104 function rec_mod(a, b) { |
| 105 // Subtracts maximal possible multiplum of b from a. | 105 // Subtracts maximal possible multiplum of b from a. |
| 106 if (a >= b) { | 106 if (a >= b) { |
| 107 a = rec_mod(a, 2 * b); | 107 a = rec_mod(a, 2 * b); |
| 108 if (a >= b) { a -= b; } | 108 if (a >= b) { a -= b; } |
| 109 } | 109 } |
| 110 return a; | 110 return a; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 function lithium_integer_mod() { | 197 function lithium_integer_mod() { |
| 198 var left_operands = [ | 198 var left_operands = [ |
| 199 0, | 199 0, |
| 200 305419896, // 0x12345678 | 200 305419896, // 0x12345678 |
| 201 ]; | 201 ]; |
| 202 | 202 |
| 203 // Test the standard lithium code for modulo opeartions. | 203 // Test the standard lithium code for modulo opeartions. |
| 204 var mod_func; | 204 var mod_func; |
| 205 for (var i = 0; i < left_operands.length; i++) { | 205 for (var i = 0; i < left_operands.length; i++) { |
| 206 for (var j = 0; j < divisors.length; j++) { | 206 for (var j = 0; j < divisors.length; j++) { |
| 207 mod_func = this.eval("(function(left) { return left % " + divisors[j]+ "; })"); | 207 mod_func = this.eval("(function(left) { return left % " + divisors[j]+ "; })"); |
|
Rico
2011/12/08 10:24:37
long line
| |
| 208 assertEquals((mod_func)(left_operands[i]), left_operands[i] % divisors[j]) ; | 208 assertEquals((mod_func)(left_operands[i]), left_operands[i] % divisors[j]) ; |
|
Rico
2011/12/08 10:24:37
long line
| |
| 209 assertEquals((mod_func)(-left_operands[i]), -left_operands[i] % divisors[j ]); | 209 assertEquals((mod_func)(-left_operands[i]), -left_operands[i] % divisors[j ]); |
|
Rico
2011/12/08 10:24:37
long line
| |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 | 212 |
| 213 var results_powers_of_two = [ | 213 var results_powers_of_two = [ |
| 214 // 0 | 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], | 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], |
|
Rico
2011/12/08 10:24:37
long line
| |
| 216 // 305419896 == 0x12345678 | 216 // 305419896 == 0x12345678 |
| 217 [0, 0, 0, 8, 24, 56, 120, 120, 120, 632, 1656, 1656, 5752, 5752, 22136, 2213 6, 22136, 22136, 284280, 284280, 1332856, 3430008, 3430008, 3430008, 3430008, 36 984440, 36984440, 36984440, 305419896, 305419896, 305419896], | 217 [0, 0, 0, 8, 24, 56, 120, 120, 120, 632, 1656, 1656, 5752, 5752, 22136, 2213 6, 22136, 22136, 284280, 284280, 1332856, 3430008, 3430008, 3430008, 3430008, 36 984440, 36984440, 36984440, 305419896, 305419896, 305419896], |
|
Rico
2011/12/08 10:24:37
long line
| |
| 218 ]; | 218 ]; |
| 219 | 219 |
| 220 // Test the lithium code for modulo operations with a variable power of two | 220 // Test the lithium code for modulo operations with a variable power of two |
| 221 // right hand side operand. | 221 // right hand side operand. |
| 222 for (var i = 0; i < left_operands.length; i++) { | 222 for (var i = 0; i < left_operands.length; i++) { |
| 223 for (var j = 0; j < 31; j++) { | 223 for (var j = 0; j < 31; j++) { |
| 224 assertEquals(results_powers_of_two[i][j], left_operands[i] % (2 << 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)); | 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)); | 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)); | 227 assertEquals(-results_powers_of_two[i][j], -left_operands[i] % -(2 << j)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 assertEquals(-results_powers_of_two[i][26], -left_operands[i] % -(2 << 26)); | 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)); | 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)); | 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)); | 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)); | 297 assertEquals(-results_powers_of_two[i][30], -left_operands[i] % -(2 << 30)); |
| 298 } | 298 } |
| 299 | 299 |
| 300 } | 300 } |
| 301 | 301 |
| 302 lithium_integer_mod(); | 302 lithium_integer_mod(); |
| 303 %OptimizeFunctionOnNextCall(lithium_integer_mod) | 303 %OptimizeFunctionOnNextCall(lithium_integer_mod); |
| 304 lithium_integer_mod(); | 304 lithium_integer_mod(); |
| OLD | NEW |