| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --allow-natives-syntax |
| 29 |
| 28 // Test fast div and mod. | 30 // Test fast div and mod. |
| 29 | 31 |
| 30 function divmod(div_func, mod_func, x, y) { | 32 function divmod(div_func, mod_func, x, y) { |
| 31 var div_answer = (div_func)(x); | 33 var div_answer = (div_func)(x); |
| 32 assertEquals(x / y, div_answer, x + "/" + y); | 34 assertEquals(x / y, div_answer, x + "/" + y); |
| 33 var mod_answer = (mod_func)(x); | 35 var mod_answer = (mod_func)(x); |
| 34 assertEquals(x % y, mod_answer, x + "%" + y); | 36 assertEquals(x % y, mod_answer, x + "%" + y); |
| 35 var minus_div_answer = (div_func)(-x); | 37 var minus_div_answer = (div_func)(-x); |
| 36 assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y); | 38 assertEquals(-x / y, minus_div_answer, "-" + x + "/" + y); |
| 37 var minus_mod_answer = (mod_func)(-x); | 39 var minus_mod_answer = (mod_func)(-x); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 0x1000000, | 81 0x1000000, |
| 80 0x40000000, | 82 0x40000000, |
| 81 12, | 83 12, |
| 82 60, | 84 60, |
| 83 100, | 85 100, |
| 84 1000 * 60 * 60 * 24]; | 86 1000 * 60 * 60 * 24]; |
| 85 | 87 |
| 86 for (var i = 0; i < divisors.length; i++) { | 88 for (var i = 0; i < divisors.length; i++) { |
| 87 run_tests_for(divisors[i]); | 89 run_tests_for(divisors[i]); |
| 88 } | 90 } |
| 91 |
| 92 |
| 93 // Test extreme corner cases of modulo. |
| 94 |
| 95 // Computes the modulo by slow but lossless operations. |
| 96 function compute_mod(dividend, divisor) { |
| 97 // Return NaN if either operand is NaN, if divisor is 0 or |
| 98 // dividend is an infinity. Return dividend if divisor is an infinity. |
| 99 if (isNaN(dividend) || isNaN(divisor) || divisor == 0) { return NaN; } |
| 100 var sign = 1; |
| 101 if (dividend < 0) { dividend = -dividend; sign = -1; } |
| 102 if (dividend == Infinity) { return NaN; } |
| 103 if (divisor < 0) { divisor = -divisor; } |
| 104 if (divisor == Infinity) { return sign * dividend; } |
| 105 function rec_mod(a, b) { |
| 106 // Subtracts maximal possible multiplum of b from a. |
| 107 if (a >= b) { |
| 108 a = rec_mod(a, 2 * b); |
| 109 if (a >= b) { a -= b; } |
| 110 } |
| 111 return a; |
| 112 } |
| 113 return sign * rec_mod(dividend, divisor); |
| 114 } |
| 115 |
| 116 (function () { |
| 117 var large_non_smi = 1234567891234.12245; |
| 118 var small_non_smi = 43.2367243; |
| 119 var smi = 43; |
| 120 var min_normal = Number.MIN_VALUE * Math.pow(2, 52); |
| 121 var max_denormal = Number.MIN_VALUE * (Math.pow(2, 52) - 1); |
| 122 |
| 123 // All combinations of NaN, Infinity, normal, denormal and zero. |
| 124 var example_numbers = [ |
| 125 NaN, |
| 126 -Infinity, |
| 127 -Number.MAX_VALUE, |
| 128 -large_non_smi, |
| 129 -small_non_smi, |
| 130 -smi, |
| 131 -min_normal, |
| 132 -max_denormal, |
| 133 -3 * Number.MIN_VALUE, |
| 134 Number.MIN_VALUE, |
| 135 -0, |
| 136 0, |
| 137 Number.MIN_VALUE, |
| 138 3 * Number.MIN_VALUE, |
| 139 max_denormal, |
| 140 min_normal, |
| 141 smi, |
| 142 small_non_smi, |
| 143 large_non_smi, |
| 144 Number.MAX_VALUE, |
| 145 Infinity |
| 146 ]; |
| 147 |
| 148 for (var i = 0; i < example_numbers.length; i++) { |
| 149 for (var j = 0; j < example_numbers.length; j++) { |
| 150 var a = example_numbers[i]; |
| 151 var b = example_numbers[j]; |
| 152 var exp = compute_mod(a, b); |
| 153 var act1 = a % b; |
| 154 assertEquals(exp, act1, a + " % " + b); |
| 155 var act2 = %_NumberMod(a, b); // Don't special-cast smis. |
| 156 assertEquals(exp, act2, "%_NumberMod(" + a + ", " + b + ")"); |
| 157 } |
| 158 } |
| 159 })() |
| OLD | NEW |