| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 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 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 | 29 |
| 30 #include <limits> | 30 #include <limits> |
| 31 | 31 |
| 32 #include "v8.h" | 32 #include "v8.h" |
| 33 | 33 |
| 34 #include "cctest.h" | 34 #include "cctest.h" |
| 35 #include "code-stubs.h" | 35 #include "code-stubs.h" |
| 36 #include "test-code-stubs.h" |
| 36 #include "factory.h" | 37 #include "factory.h" |
| 37 #include "macro-assembler.h" | 38 #include "macro-assembler.h" |
| 38 #include "platform.h" | 39 #include "platform.h" |
| 39 | 40 |
| 40 #if __GNUC__ | |
| 41 #define STDCALL __attribute__((stdcall)) | |
| 42 #else | |
| 43 #define STDCALL __stdcall | |
| 44 #endif | |
| 45 | |
| 46 using namespace v8::internal; | 41 using namespace v8::internal; |
| 47 | 42 |
| 48 | |
| 49 typedef int32_t STDCALL ConvertDToIFuncType(double input); | |
| 50 typedef ConvertDToIFuncType* ConvertDToIFunc; | |
| 51 | |
| 52 | |
| 53 int STDCALL ConvertDToICVersion(double d) { | |
| 54 Address double_ptr = reinterpret_cast<Address>(&d); | |
| 55 uint32_t exponent_bits = Memory::uint32_at(double_ptr + kDoubleSize / 2); | |
| 56 int32_t shifted_mask = static_cast<int32_t>(Double::kExponentMask >> 32); | |
| 57 int32_t exponent = (((exponent_bits & shifted_mask) >> | |
| 58 (Double::kPhysicalSignificandSize - 32)) - | |
| 59 HeapNumber::kExponentBias); | |
| 60 uint32_t unsigned_exponent = static_cast<uint32_t>(exponent); | |
| 61 int result = 0; | |
| 62 uint32_t max_exponent = | |
| 63 static_cast<uint32_t>(Double::kPhysicalSignificandSize); | |
| 64 if (unsigned_exponent >= max_exponent) { | |
| 65 if ((exponent - Double::kPhysicalSignificandSize) < 32) { | |
| 66 result = Memory::uint32_at(double_ptr) << | |
| 67 (exponent - Double::kPhysicalSignificandSize); | |
| 68 } | |
| 69 } else { | |
| 70 uint64_t big_result = | |
| 71 (BitCast<uint64_t>(d) & Double::kSignificandMask) | Double::kHiddenBit; | |
| 72 big_result = big_result >> (Double::kPhysicalSignificandSize - exponent); | |
| 73 result = static_cast<uint32_t>(big_result); | |
| 74 } | |
| 75 if (static_cast<int32_t>(exponent_bits) < 0) { | |
| 76 return (0 - result); | |
| 77 } else { | |
| 78 return result; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 | |
| 83 void RunOneTruncationTestWithTest(ConvertDToIFunc func, | |
| 84 double from, | |
| 85 double raw) { | |
| 86 uint64_t to = static_cast<int64_t>(raw); | |
| 87 int result = (*func)(from); | |
| 88 CHECK_EQ(static_cast<int>(to), result); | |
| 89 } | |
| 90 | |
| 91 | |
| 92 // #define NaN and Infinity so that it's possible to cut-and-paste these tests | |
| 93 // directly to a .js file and run them. | |
| 94 #define NaN (OS::nan_value()) | |
| 95 #define Infinity (std::numeric_limits<double>::infinity()) | |
| 96 #define RunOneTruncationTest(p1, p2) RunOneTruncationTestWithTest(func, p1, p2) | |
| 97 | |
| 98 void RunAllTruncationTests(ConvertDToIFunc func) { | |
| 99 RunOneTruncationTest(0, 0); | |
| 100 RunOneTruncationTest(0.5, 0); | |
| 101 RunOneTruncationTest(-0.5, 0); | |
| 102 RunOneTruncationTest(1.5, 1); | |
| 103 RunOneTruncationTest(-1.5, -1); | |
| 104 RunOneTruncationTest(5.5, 5); | |
| 105 RunOneTruncationTest(-5.0, -5); | |
| 106 RunOneTruncationTest(NaN, 0); | |
| 107 RunOneTruncationTest(Infinity, 0); | |
| 108 RunOneTruncationTest(-NaN, 0); | |
| 109 RunOneTruncationTest(-Infinity, 0); | |
| 110 | |
| 111 RunOneTruncationTest(4.5036e+15, 0x1635E000); | |
| 112 RunOneTruncationTest(-4.5036e+15, -372629504); | |
| 113 | |
| 114 RunOneTruncationTest(4503603922337791.0, -1); | |
| 115 RunOneTruncationTest(-4503603922337791.0, 1); | |
| 116 RunOneTruncationTest(4503601774854143.0, 2147483647); | |
| 117 RunOneTruncationTest(-4503601774854143.0, -2147483647); | |
| 118 RunOneTruncationTest(9007207844675582.0, -2); | |
| 119 RunOneTruncationTest(-9007207844675582.0, 2); | |
| 120 RunOneTruncationTest(2.4178527921507624e+24, -536870912); | |
| 121 RunOneTruncationTest(-2.4178527921507624e+24, 536870912); | |
| 122 RunOneTruncationTest(2.417853945072267e+24, -536870912); | |
| 123 RunOneTruncationTest(-2.417853945072267e+24, 536870912); | |
| 124 | |
| 125 RunOneTruncationTest(4.8357055843015248e+24, -1073741824); | |
| 126 RunOneTruncationTest(-4.8357055843015248e+24, 1073741824); | |
| 127 RunOneTruncationTest(4.8357078901445341e+24, -1073741824); | |
| 128 RunOneTruncationTest(-4.8357078901445341e+24, 1073741824); | |
| 129 | |
| 130 RunOneTruncationTest(9.6714111686030497e+24, -2147483648.0); | |
| 131 RunOneTruncationTest(-9.6714111686030497e+24, -2147483648.0); | |
| 132 RunOneTruncationTest(9.6714157802890681e+24, -2147483648.0); | |
| 133 RunOneTruncationTest(-9.6714157802890681e+24, -2147483648.0); | |
| 134 } | |
| 135 | |
| 136 #undef NaN | |
| 137 #undef Infinity | |
| 138 #undef RunOneTruncationTest | |
| 139 | |
| 140 #define __ assm. | 43 #define __ assm. |
| 141 | 44 |
| 142 ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate, | 45 ConvertDToIFunc MakeConvertDToIFuncTrampoline(Isolate* isolate, |
| 143 Register source_reg, | 46 Register source_reg, |
| 144 Register destination_reg) { | 47 Register destination_reg) { |
| 145 // Allocate an executable page of memory. | 48 // Allocate an executable page of memory. |
| 146 size_t actual_size; | 49 size_t actual_size; |
| 147 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, | 50 byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize, |
| 148 &actual_size, | 51 &actual_size, |
| 149 true)); | 52 true)); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, edx)); | 172 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, edx)); |
| 270 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, edi)); | 173 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, edi)); |
| 271 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, esi)); | 174 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, esi, esi)); |
| 272 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, eax)); | 175 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, eax)); |
| 273 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, ebx)); | 176 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, ebx)); |
| 274 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, ecx)); | 177 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, ecx)); |
| 275 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, edx)); | 178 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, edx)); |
| 276 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, edi)); | 179 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, edi)); |
| 277 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, esi)); | 180 RunAllTruncationTests(MakeConvertDToIFuncTrampoline(isolate, edi, esi)); |
| 278 } | 181 } |
| OLD | NEW |