Home | History | Annotate | Download | only in cctest
      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 #include <stdlib.h>
     29 
     30 #include <limits>
     31 
     32 #include "src/v8.h"
     33 
     34 #include "src/code-stubs.h"
     35 #include "src/factory.h"
     36 #include "src/macro-assembler.h"
     37 #include "src/platform.h"
     38 #include "test/cctest/cctest.h"
     39 #include "test/cctest/test-code-stubs.h"
     40 
     41 using namespace v8::internal;
     42 
     43 
     44 int STDCALL ConvertDToICVersion(double d) {
     45   union { double d; uint32_t u[2]; } dbl;
     46   dbl.d = d;
     47   uint32_t exponent_bits = dbl.u[1];
     48   int32_t shifted_mask = static_cast<int32_t>(Double::kExponentMask >> 32);
     49   int32_t exponent = (((exponent_bits & shifted_mask) >>
     50                        (Double::kPhysicalSignificandSize - 32)) -
     51                       HeapNumber::kExponentBias);
     52   if (exponent < 0) {
     53     return 0;
     54   }
     55   uint32_t unsigned_exponent = static_cast<uint32_t>(exponent);
     56   int result = 0;
     57   uint32_t max_exponent =
     58     static_cast<uint32_t>(Double::kPhysicalSignificandSize);
     59   if (unsigned_exponent >= max_exponent) {
     60     if ((exponent - Double::kPhysicalSignificandSize) < 32) {
     61       result = dbl.u[0] << (exponent - Double::kPhysicalSignificandSize);
     62     }
     63   } else {
     64     uint64_t big_result =
     65         (BitCast<uint64_t>(d) & Double::kSignificandMask) | Double::kHiddenBit;
     66     big_result = big_result >> (Double::kPhysicalSignificandSize - exponent);
     67     result = static_cast<uint32_t>(big_result);
     68   }
     69   if (static_cast<int32_t>(exponent_bits) < 0) {
     70     return (0 - result);
     71   } else {
     72     return result;
     73   }
     74 }
     75 
     76 
     77 void RunOneTruncationTestWithTest(ConvertDToICallWrapper callWrapper,
     78                                   ConvertDToIFunc func,
     79                                   double from,
     80                                   double raw) {
     81   uint64_t to = static_cast<int64_t>(raw);
     82   int result = (*callWrapper)(func, from);
     83   CHECK_EQ(static_cast<int>(to), result);
     84 }
     85 
     86 
     87 int32_t DefaultCallWrapper(ConvertDToIFunc func,
     88                            double from) {
     89   return (*func)(from);
     90 }
     91 
     92 
     93 // #define NaN and Infinity so that it's possible to cut-and-paste these tests
     94 // directly to a .js file and run them.
     95 #define NaN (OS::nan_value())
     96 #define Infinity (std::numeric_limits<double>::infinity())
     97 #define RunOneTruncationTest(p1, p2) \
     98     RunOneTruncationTestWithTest(callWrapper, func, p1, p2)
     99 
    100 
    101 void RunAllTruncationTests(ConvertDToIFunc func) {
    102   RunAllTruncationTests(DefaultCallWrapper, func);
    103 }
    104 
    105 
    106 void RunAllTruncationTests(ConvertDToICallWrapper callWrapper,
    107                            ConvertDToIFunc func) {
    108   RunOneTruncationTest(0, 0);
    109   RunOneTruncationTest(0.5, 0);
    110   RunOneTruncationTest(-0.5, 0);
    111   RunOneTruncationTest(1.5, 1);
    112   RunOneTruncationTest(-1.5, -1);
    113   RunOneTruncationTest(5.5, 5);
    114   RunOneTruncationTest(-5.0, -5);
    115   RunOneTruncationTest(NaN, 0);
    116   RunOneTruncationTest(Infinity, 0);
    117   RunOneTruncationTest(-NaN, 0);
    118   RunOneTruncationTest(-Infinity, 0);
    119   RunOneTruncationTest(4.94065645841e-324, 0);
    120   RunOneTruncationTest(-4.94065645841e-324, 0);
    121 
    122   RunOneTruncationTest(0.9999999999999999, 0);
    123   RunOneTruncationTest(-0.9999999999999999, 0);
    124   RunOneTruncationTest(4294967296.0, 0);
    125   RunOneTruncationTest(-4294967296.0, 0);
    126   RunOneTruncationTest(9223372036854775000.0, 4294966272.0);
    127   RunOneTruncationTest(-9223372036854775000.0, -4294966272.0);
    128   RunOneTruncationTest(4.5036e+15, 372629504);
    129   RunOneTruncationTest(-4.5036e+15, -372629504);
    130 
    131   RunOneTruncationTest(287524199.5377777, 0x11234567);
    132   RunOneTruncationTest(-287524199.5377777, -0x11234567);
    133   RunOneTruncationTest(2300193596.302222, 2300193596.0);
    134   RunOneTruncationTest(-2300193596.302222, -2300193596.0);
    135   RunOneTruncationTest(4600387192.604444, 305419896);
    136   RunOneTruncationTest(-4600387192.604444, -305419896);
    137   RunOneTruncationTest(4823855600872397.0, 1737075661);
    138   RunOneTruncationTest(-4823855600872397.0, -1737075661);
    139 
    140   RunOneTruncationTest(4503603922337791.0, -1);
    141   RunOneTruncationTest(-4503603922337791.0, 1);
    142   RunOneTruncationTest(4503601774854143.0, 2147483647);
    143   RunOneTruncationTest(-4503601774854143.0, -2147483647);
    144   RunOneTruncationTest(9007207844675582.0, -2);
    145   RunOneTruncationTest(-9007207844675582.0, 2);
    146 
    147   RunOneTruncationTest(2.4178527921507624e+24, -536870912);
    148   RunOneTruncationTest(-2.4178527921507624e+24, 536870912);
    149   RunOneTruncationTest(2.417853945072267e+24, -536870912);
    150   RunOneTruncationTest(-2.417853945072267e+24, 536870912);
    151 
    152   RunOneTruncationTest(4.8357055843015248e+24, -1073741824);
    153   RunOneTruncationTest(-4.8357055843015248e+24, 1073741824);
    154   RunOneTruncationTest(4.8357078901445341e+24, -1073741824);
    155   RunOneTruncationTest(-4.8357078901445341e+24, 1073741824);
    156 
    157   RunOneTruncationTest(2147483647.0, 2147483647.0);
    158   RunOneTruncationTest(-2147483648.0, -2147483648.0);
    159   RunOneTruncationTest(9.6714111686030497e+24, -2147483648.0);
    160   RunOneTruncationTest(-9.6714111686030497e+24, -2147483648.0);
    161   RunOneTruncationTest(9.6714157802890681e+24, -2147483648.0);
    162   RunOneTruncationTest(-9.6714157802890681e+24, -2147483648.0);
    163   RunOneTruncationTest(1.9342813113834065e+25, 2147483648.0);
    164   RunOneTruncationTest(-1.9342813113834065e+25, 2147483648.0);
    165 
    166   RunOneTruncationTest(3.868562622766813e+25, 0);
    167   RunOneTruncationTest(-3.868562622766813e+25, 0);
    168   RunOneTruncationTest(1.7976931348623157e+308, 0);
    169   RunOneTruncationTest(-1.7976931348623157e+308, 0);
    170 }
    171 
    172 #undef NaN
    173 #undef Infinity
    174 #undef RunOneTruncationTest
    175