Home | History | Annotate | Download | only in regress
      1 // Copyright 2008 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 // Ensure that we can correctly change the sign of the most negative smi.
     29 
     30 // Possible Smi ranges.
     31 var ranges = [{min: -1073741824, max: 1073741823, bits: 31},
     32               {min: -2147483648, max: 2147483647, bits: 32}];
     33 
     34 for (var i = 0; i < ranges.length; i++) {
     35   var range = ranges[i];
     36   var min_smi = range.min;
     37   var max_smi = range.max;
     38   var bits = range.bits;
     39   var name = bits + "-bit";
     40 
     41   var result = max_smi + 1;
     42 
     43   // Min smi as literal
     44   assertEquals(result, eval(min_smi + " * -1"), name + "-litconmult");
     45   assertEquals(result, eval(min_smi + " / -1"), name + "-litcondiv");
     46   assertEquals(result, eval("-(" + min_smi + ")"), name + "-litneg");
     47   assertEquals(result, eval("0 - (" + min_smi + ")")), name + "-conlitsub";
     48 
     49   // As variable:
     50   assertEquals(result, min_smi * -1, name + "-varconmult");
     51   assertEquals(result, min_smi / -1, name + "-varcondiv");
     52   assertEquals(result, -min_smi, name + "-varneg");
     53   assertEquals(result, 0 - min_smi, name + "-convarsub");
     54 
     55   // Only variables:
     56   var zero = 0;
     57   var minus_one = -1;
     58 
     59   assertEquals(result, min_smi * minus_one, name + "-varvarmult");
     60   assertEquals(result, min_smi / minus_one, name + "-varvardiv");
     61   assertEquals(result, zero - min_smi, name + "-varvarsub");
     62 
     63   // Constants as variables
     64   assertEquals(result, eval(min_smi + " * minus_one"), name + "-litvarmult");
     65   assertEquals(result, eval(min_smi + " / minus_one"), name + "-litvarmdiv");
     66   assertEquals(result, eval("0 - (" + min_smi + ")"), name + "-varlitsub");
     67 
     68   var half_min_smi = -(1 << (bits >> 1));
     69   var half_max_smi = 1 << ((bits - 1) >> 1);
     70 
     71   assertEquals(max_smi + 1, -half_min_smi * half_max_smi, name + "-half1");
     72   assertEquals(max_smi + 1, half_min_smi * -half_max_smi, name + "-half2");
     73   assertEquals(max_smi + 1, half_max_smi * -half_min_smi, name + "-half3");
     74   assertEquals(max_smi + 1, -half_max_smi * half_min_smi, name + "-half4");
     75 }
     76