Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2013 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 --max-opt-count=1000
     29 
     30 var imul_func = Math.imul;
     31 function imul_polyfill(a, b) {
     32   var ah  = (a >>> 16) & 0xffff;
     33   var al = a & 0xffff;
     34   var bh  = (b >>> 16) & 0xffff;
     35   var bl = b & 0xffff;
     36   return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0);
     37 }
     38 
     39 function TestMathImul(expected, a, b) {
     40   function imul_meth_closure(a, b) { return Math.imul(a, b); }
     41   function imul_func_closure(a, b) { return imul_func(a, b); }
     42 
     43   // Test reference implementation.
     44   assertEquals(expected, imul_polyfill(a, b));
     45 
     46   // Test direct method call.
     47   assertEquals(expected, Math.imul(a, b));
     48 
     49   // Test direct function call.
     50   assertEquals(expected, imul_func(a, b));
     51 
     52   // Test optimized method call inside closure.
     53   assertEquals(expected, imul_meth_closure(a, b));
     54   assertEquals(expected, imul_meth_closure(a, b));
     55   %OptimizeFunctionOnNextCall(imul_meth_closure);
     56   assertEquals(expected, imul_meth_closure(a, b));
     57 
     58   // Test optimized function call inside closure.
     59   assertEquals(expected, imul_func_closure(a, b));
     60   assertEquals(expected, imul_func_closure(a, b));
     61   %OptimizeFunctionOnNextCall(imul_func_closure);
     62   assertEquals(expected, imul_func_closure(a, b));
     63 
     64   // Deoptimize closures and forget type feedback.
     65   %DeoptimizeFunction(imul_meth_closure);
     66   %DeoptimizeFunction(imul_func_closure);
     67   %ClearFunctionTypeFeedback(imul_meth_closure);
     68   %ClearFunctionTypeFeedback(imul_func_closure);
     69 }
     70 
     71 TestMathImul(8, 2, 4);
     72 TestMathImul(-8, -1, 8);
     73 TestMathImul(4, -2, -2);
     74 TestMathImul(-5, 0xffffffff, 5);
     75 TestMathImul(-10, 0xfffffffe, 5);
     76 
     77 TestMathImul(0, false, 7);
     78 TestMathImul(0, 7, false);
     79 TestMathImul(0, false, false);
     80 
     81 TestMathImul(7, true, 7);
     82 TestMathImul(7, 7, true);
     83 TestMathImul(1, true, true);
     84 
     85 TestMathImul(0, false, true);
     86 TestMathImul(0, true, false);
     87 
     88 TestMathImul(0, undefined, 7);
     89 TestMathImul(0, 7, undefined);
     90 TestMathImul(0, undefined, undefined);
     91 
     92 TestMathImul(0, -0, 7);
     93 TestMathImul(0, 7, -0);
     94 
     95 TestMathImul(0, 0.1, 7);
     96 TestMathImul(0, 7, 0.1);
     97 TestMathImul(0, 0.9, 7);
     98 TestMathImul(0, 7, 0.9);
     99 TestMathImul(7, 1.1, 7);
    100 TestMathImul(7, 7, 1.1);
    101 TestMathImul(7, 1.9, 7);
    102 TestMathImul(7, 7, 1.9);
    103 
    104 TestMathImul(0, "str", 7);
    105 TestMathImul(0, 7, "str");
    106 
    107 TestMathImul(0, {}, 7);
    108 TestMathImul(0, 7, {});
    109 
    110 TestMathImul(0, [], 7);
    111 TestMathImul(0, 7, []);
    112 
    113 // 2^30 is a smi boundary on arm and ia32.
    114 var two_30 = 1 << 30;
    115 
    116 TestMathImul(-two_30, two_30, 7);
    117 TestMathImul(-two_30, 7, two_30);
    118 TestMathImul(0, two_30, two_30);
    119 
    120 TestMathImul(two_30, -two_30, 7);
    121 TestMathImul(two_30, 7, -two_30);
    122 TestMathImul(0, -two_30, -two_30);
    123 
    124 // 2^31 is a smi boundary on x64.
    125 var two_31 = 2 * two_30;
    126 
    127 TestMathImul(-two_31, two_31, 7);
    128 TestMathImul(-two_31, 7, two_31);
    129 TestMathImul(0, two_31, two_31);
    130 
    131 TestMathImul(-two_31, -two_31, 7);
    132 TestMathImul(-two_31, 7, -two_31);
    133 TestMathImul(0, -two_31, -two_31);
    134 
    135 // 2^31 - 1 is the largest int32 value.
    136 var max_val = two_31 - 1;
    137 
    138 TestMathImul(two_31 - 7, max_val, 7);
    139 TestMathImul(two_31 - 7, 7, max_val);
    140 TestMathImul(1, max_val, max_val);
    141 
    142 // 2^16 is a boundary value that overflows when squared.
    143 var two_16 = 1 << 16;
    144 
    145 TestMathImul(0, two_16, two_16);
    146 TestMathImul(-two_16, two_16 - 1, two_16);
    147 TestMathImul(-two_16, two_16, two_16 - 1);
    148 TestMathImul(-2 * two_16 + 1, two_16 - 1, two_16 - 1);
    149