Home | History | Annotate | Download | only in mjsunit
      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 
     32 function 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 
     44 function 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 
     69 var 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 
     88 for (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.
     95 function 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 
    130     // Due to a bug in fmod(), modulos involving denormals
    131     // return the wrong result for glibc <= 2.16.
    132     // Details: http://sourceware.org/bugzilla/show_bug.cgi?id=14048
    133 
    134     Number.MIN_VALUE,
    135     3 * Number.MIN_VALUE,
    136     max_denormal,
    137 
    138     min_normal,
    139     repeating_decimal,
    140     finite_decimal,
    141     smi,
    142     power_of_two,
    143     small_non_smi,
    144     large_non_smi,
    145     Number.MAX_VALUE,
    146     Infinity
    147   ];
    148 
    149   function doTest(a, b) {
    150     var exp = compute_mod(a, b);
    151     var act = a % b;
    152     assertEquals(exp, act, a + " % " + b);
    153   }
    154 
    155   for (var i = 0; i < example_numbers.length; i++) {
    156     for (var j = 0; j < example_numbers.length; j++) {
    157       var a = example_numbers[i];
    158       var b = example_numbers[j];
    159       doTest(a,b);
    160       doTest(-a,b);
    161       doTest(a,-b);
    162       doTest(-a,-b);
    163     }
    164   }
    165 })();
    166 
    167 
    168 (function () {
    169   // Edge cases
    170   var zero = 0;
    171   var minsmi32 = -0x40000000;
    172   var minsmi64 = -0x80000000;
    173   var somenum = 3532;
    174   assertEquals(-0, zero / -1, "0 / -1");
    175   assertEquals(1, minsmi32 / -0x40000000, "minsmi/minsmi-32");
    176   assertEquals(1, minsmi64 / -0x80000000, "minsmi/minsmi-64");
    177   assertEquals(somenum, somenum % -0x40000000, "%minsmi-32");
    178   assertEquals(somenum, somenum % -0x80000000, "%minsmi-64");
    179 })();
    180 
    181 
    182 // Side-effect-free expressions containing bit operations use
    183 // an optimized compiler with int32 values.   Ensure that modulus
    184 // produces negative zeros correctly.
    185 function negative_zero_modulus_test() {
    186   var x = 4;
    187   var y = -4;
    188   x = x + x - x;
    189   y = y + y - y;
    190   var z = (y | y | y | y) % x;
    191   assertEquals(-1 / 0, 1 / z);
    192   z = (x | x | x | x) % x;
    193   assertEquals(1 / 0, 1 / z);
    194   z = (y | y | y | y) % y;
    195   assertEquals(-1 / 0, 1 / z);
    196   z = (x | x | x | x) % y;
    197   assertEquals(1 / 0, 1 / z);
    198 }
    199 
    200 negative_zero_modulus_test();
    201 
    202 
    203 function lithium_integer_mod() {
    204   var left_operands = [
    205     0,
    206     305419896,  // 0x12345678
    207   ];
    208 
    209   // Test the standard lithium code for modulo opeartions.
    210   var mod_func;
    211   for (var i = 0; i < left_operands.length; i++) {
    212     for (var j = 0; j < divisors.length; j++) {
    213       mod_func = this.eval("(function(left) { return left % " + divisors[j]+ "; })");
    214       assertEquals((mod_func)(left_operands[i]), left_operands[i] % divisors[j]);
    215       assertEquals((mod_func)(-left_operands[i]), -left_operands[i] % divisors[j]);
    216     }
    217   }
    218 
    219   var results_powers_of_two = [
    220     // 0
    221     [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],
    222     // 305419896 == 0x12345678
    223     [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],
    224   ];
    225 
    226   // Test the lithium code for modulo operations with a variable power of two
    227   // right hand side operand.
    228   for (var i = 0; i < left_operands.length; i++) {
    229     for (var j = 0; j < 31; j++) {
    230       assertEquals(results_powers_of_two[i][j], left_operands[i] % (2 << j));
    231       assertEquals(results_powers_of_two[i][j], left_operands[i] % -(2 << j));
    232       assertEquals(-results_powers_of_two[i][j], -left_operands[i] % (2 << j));
    233       assertEquals(-results_powers_of_two[i][j], -left_operands[i] % -(2 << j));
    234     }
    235   }
    236 
    237   // Test the lithium code for modulo operations with a constant power of two
    238   // right hand side operand.
    239   for (var i = 0; i < left_operands.length; i++) {
    240     // With positive left hand side operand.
    241     assertEquals(results_powers_of_two[i][0], left_operands[i] % -(2 << 0));
    242     assertEquals(results_powers_of_two[i][1], left_operands[i] % (2 << 1));
    243     assertEquals(results_powers_of_two[i][2], left_operands[i] % -(2 << 2));
    244     assertEquals(results_powers_of_two[i][3], left_operands[i] % (2 << 3));
    245     assertEquals(results_powers_of_two[i][4], left_operands[i] % -(2 << 4));
    246     assertEquals(results_powers_of_two[i][5], left_operands[i] % (2 << 5));
    247     assertEquals(results_powers_of_two[i][6], left_operands[i] % -(2 << 6));
    248     assertEquals(results_powers_of_two[i][7], left_operands[i] % (2 << 7));
    249     assertEquals(results_powers_of_two[i][8], left_operands[i] % -(2 << 8));
    250     assertEquals(results_powers_of_two[i][9], left_operands[i] % (2 << 9));
    251     assertEquals(results_powers_of_two[i][10], left_operands[i] % -(2 << 10));
    252     assertEquals(results_powers_of_two[i][11], left_operands[i] % (2 << 11));
    253     assertEquals(results_powers_of_two[i][12], left_operands[i] % -(2 << 12));
    254     assertEquals(results_powers_of_two[i][13], left_operands[i] % (2 << 13));
    255     assertEquals(results_powers_of_two[i][14], left_operands[i] % -(2 << 14));
    256     assertEquals(results_powers_of_two[i][15], left_operands[i] % (2 << 15));
    257     assertEquals(results_powers_of_two[i][16], left_operands[i] % -(2 << 16));
    258     assertEquals(results_powers_of_two[i][17], left_operands[i] % (2 << 17));
    259     assertEquals(results_powers_of_two[i][18], left_operands[i] % -(2 << 18));
    260     assertEquals(results_powers_of_two[i][19], left_operands[i] % (2 << 19));
    261     assertEquals(results_powers_of_two[i][20], left_operands[i] % -(2 << 20));
    262     assertEquals(results_powers_of_two[i][21], left_operands[i] % (2 << 21));
    263     assertEquals(results_powers_of_two[i][22], left_operands[i] % -(2 << 22));
    264     assertEquals(results_powers_of_two[i][23], left_operands[i] % (2 << 23));
    265     assertEquals(results_powers_of_two[i][24], left_operands[i] % -(2 << 24));
    266     assertEquals(results_powers_of_two[i][25], left_operands[i] % (2 << 25));
    267     assertEquals(results_powers_of_two[i][26], left_operands[i] % -(2 << 26));
    268     assertEquals(results_powers_of_two[i][27], left_operands[i] % (2 << 27));
    269     assertEquals(results_powers_of_two[i][28], left_operands[i] % -(2 << 28));
    270     assertEquals(results_powers_of_two[i][29], left_operands[i] % (2 << 29));
    271     assertEquals(results_powers_of_two[i][30], left_operands[i] % -(2 << 30));
    272     // With negative left hand side operand.
    273     assertEquals(-results_powers_of_two[i][0], -left_operands[i] % -(2 << 0));
    274     assertEquals(-results_powers_of_two[i][1], -left_operands[i] % (2 << 1));
    275     assertEquals(-results_powers_of_two[i][2], -left_operands[i] % -(2 << 2));
    276     assertEquals(-results_powers_of_two[i][3], -left_operands[i] % (2 << 3));
    277     assertEquals(-results_powers_of_two[i][4], -left_operands[i] % -(2 << 4));
    278     assertEquals(-results_powers_of_two[i][5], -left_operands[i] % (2 << 5));
    279     assertEquals(-results_powers_of_two[i][6], -left_operands[i] % -(2 << 6));
    280     assertEquals(-results_powers_of_two[i][7], -left_operands[i] % (2 << 7));
    281     assertEquals(-results_powers_of_two[i][8], -left_operands[i] % -(2 << 8));
    282     assertEquals(-results_powers_of_two[i][9], -left_operands[i] % (2 << 9));
    283     assertEquals(-results_powers_of_two[i][10], -left_operands[i] % -(2 << 10));
    284     assertEquals(-results_powers_of_two[i][11], -left_operands[i] % (2 << 11));
    285     assertEquals(-results_powers_of_two[i][12], -left_operands[i] % -(2 << 12));
    286     assertEquals(-results_powers_of_two[i][13], -left_operands[i] % (2 << 13));
    287     assertEquals(-results_powers_of_two[i][14], -left_operands[i] % -(2 << 14));
    288     assertEquals(-results_powers_of_two[i][15], -left_operands[i] % (2 << 15));
    289     assertEquals(-results_powers_of_two[i][16], -left_operands[i] % -(2 << 16));
    290     assertEquals(-results_powers_of_two[i][17], -left_operands[i] % (2 << 17));
    291     assertEquals(-results_powers_of_two[i][18], -left_operands[i] % -(2 << 18));
    292     assertEquals(-results_powers_of_two[i][19], -left_operands[i] % (2 << 19));
    293     assertEquals(-results_powers_of_two[i][20], -left_operands[i] % -(2 << 20));
    294     assertEquals(-results_powers_of_two[i][21], -left_operands[i] % (2 << 21));
    295     assertEquals(-results_powers_of_two[i][22], -left_operands[i] % -(2 << 22));
    296     assertEquals(-results_powers_of_two[i][23], -left_operands[i] % (2 << 23));
    297     assertEquals(-results_powers_of_two[i][24], -left_operands[i] % -(2 << 24));
    298     assertEquals(-results_powers_of_two[i][25], -left_operands[i] % (2 << 25));
    299     assertEquals(-results_powers_of_two[i][26], -left_operands[i] % -(2 << 26));
    300     assertEquals(-results_powers_of_two[i][27], -left_operands[i] % (2 << 27));
    301     assertEquals(-results_powers_of_two[i][28], -left_operands[i] % -(2 << 28));
    302     assertEquals(-results_powers_of_two[i][29], -left_operands[i] % (2 << 29));
    303     assertEquals(-results_powers_of_two[i][30], -left_operands[i] % -(2 << 30));
    304   }
    305 
    306 }
    307 
    308 lithium_integer_mod();
    309 %OptimizeFunctionOnNextCall(lithium_integer_mod)
    310 lithium_integer_mod();
    311