OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 |
| 3 #include <stdlib.h> |
| 4 |
| 5 #include "v8.h" |
| 6 |
| 7 #include "platform.h" |
| 8 #include "cctest.h" |
| 9 #include "diy_fp.h" |
| 10 #include "double.h" |
| 11 |
| 12 using namespace v8::internal; |
| 13 |
| 14 |
| 15 TEST(Uint64Conversions) { |
| 16 // Start by checking the byte-order. |
| 17 uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF); |
| 18 CHECK_EQ(3512700564088504e-318, Double(ordered).value()); |
| 19 |
| 20 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001); |
| 21 CHECK_EQ(5e-324, Double(min_double64).value()); |
| 22 |
| 23 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff); |
| 24 CHECK_EQ(1.7976931348623157e308, Double(max_double64).value()); |
| 25 } |
| 26 |
| 27 TEST(AsDiyFp) { |
| 28 uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF); |
| 29 DiyFp diy_fp = Double(ordered).AsDiyFp(); |
| 30 CHECK_EQ(0x12 - 0x3FF - 52, diy_fp.e()); |
| 31 // The last 52 bits + 1 hidden bit a position 52. |
| 32 CHECK_EQ(V8_2PART_UINT64_C(0x00134567, 89ABCDEF), diy_fp.f()); |
| 33 |
| 34 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001); |
| 35 diy_fp = Double(min_double64).AsDiyFp(); |
| 36 CHECK_EQ(-0x3FF - 52 + 1, diy_fp.e()); |
| 37 // This is a denormal; so no hidden bit. |
| 38 uint64_t correct_f = 1; |
| 39 CHECK_EQ(correct_f, diy_fp.f()); |
| 40 |
| 41 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff); |
| 42 diy_fp = Double(max_double64).AsDiyFp(); |
| 43 CHECK_EQ(0x7FE - 0x3FF - 52, diy_fp.e()); |
| 44 correct_f = V8_2PART_UINT64_C(0x001fffff, ffffffff); |
| 45 CHECK_EQ(correct_f, diy_fp.f()); |
| 46 } |
| 47 |
| 48 |
| 49 TEST(AsNormalizedDiyFp) { |
| 50 uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF); |
| 51 DiyFp diy_fp = Double(ordered).AsNormalizedDiyFp(); |
| 52 CHECK_EQ(0x12 - 0x3FF - 52 - 11, diy_fp.e()); |
| 53 uint64_t correct_f = V8_2PART_UINT64_C(0x00134567, 89ABCDEF) << 11; |
| 54 CHECK_EQ(correct_f, diy_fp.f()); |
| 55 |
| 56 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001); |
| 57 diy_fp = Double(min_double64).AsNormalizedDiyFp(); |
| 58 CHECK_EQ(-0x3FF - 52 + 1 - 63, diy_fp.e()); |
| 59 // This is a denormal; so no hidden bit. |
| 60 correct_f = V8_2PART_UINT64_C(0x80000000, 00000000); |
| 61 CHECK_EQ(correct_f, diy_fp.f()); |
| 62 |
| 63 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff); |
| 64 diy_fp = Double(max_double64).AsNormalizedDiyFp(); |
| 65 CHECK_EQ(0x7FE - 0x3FF - 52 - 11, diy_fp.e()); |
| 66 correct_f = V8_2PART_UINT64_C(0x001fffff, ffffffff) << 11; |
| 67 CHECK_EQ(correct_f, diy_fp.f()); |
| 68 } |
| 69 |
| 70 TEST(IsDenormal) { |
| 71 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001); |
| 72 CHECK(Double(min_double64).IsDenormal()); |
| 73 uint64_t bits = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); |
| 74 CHECK(Double(bits).IsDenormal()); |
| 75 bits = V8_2PART_UINT64_C(0x00100000, 00000000); |
| 76 CHECK(!Double(bits).IsDenormal()); |
| 77 } |
| 78 |
| 79 TEST(IsSpecial) { |
| 80 CHECK(Double(V8_INFINITY).IsSpecial()); |
| 81 CHECK(Double(-V8_INFINITY).IsSpecial()); |
| 82 CHECK(Double(OS::nan_value()).IsSpecial()); |
| 83 uint64_t bits = V8_2PART_UINT64_C(0xFFF12345, 00000000); |
| 84 CHECK(Double(bits).IsSpecial()); |
| 85 // Denormals are not special: |
| 86 CHECK(!Double(5e-324).IsSpecial()); |
| 87 // And some random numbers: |
| 88 CHECK(!Double(0.0).IsSpecial()); |
| 89 CHECK(!Double(1.0).IsSpecial()); |
| 90 CHECK(!Double(1000000.0).IsSpecial()); |
| 91 CHECK(!Double(1e23).IsSpecial()); |
| 92 CHECK(!Double(1.7976931348623157e308).IsSpecial()); |
| 93 } |
| 94 |
| 95 TEST(NormalizedBoundaries) { |
| 96 DiyFp boundary_plus; |
| 97 DiyFp boundary_minus; |
| 98 DiyFp diy_fp = Double(1.5).AsNormalizedDiyFp(); |
| 99 Double(1.5).NormalizedBoundaries(&boundary_minus, &boundary_plus); |
| 100 CHECK_EQ(diy_fp.e(), boundary_minus.e()); |
| 101 CHECK_EQ(diy_fp.e(), boundary_plus.e()); |
| 102 // 1.5 does not have a significand of the form 2^p (for some p). |
| 103 // Therefore its boundaries are at the same distance. |
| 104 CHECK_EQ(diy_fp.f() - boundary_minus.f(), boundary_plus.f() - diy_fp.f()); |
| 105 CHECK_EQ(static_cast<uint64_t>(1) << 10, diy_fp.f() - boundary_minus.f()); |
| 106 |
| 107 diy_fp = Double(1.0).AsNormalizedDiyFp(); |
| 108 Double(1.0).NormalizedBoundaries(&boundary_minus, &boundary_plus); |
| 109 CHECK_EQ(diy_fp.e(), boundary_minus.e()); |
| 110 CHECK_EQ(diy_fp.e(), boundary_plus.e()); |
| 111 // 1.0 does have a significand of the form 2^p (for some p). |
| 112 // Therefore its lower boundary is twice as close as the upper boundary. |
| 113 CHECK_GT(boundary_plus.f() - diy_fp.f(), diy_fp.f() - boundary_minus.f()); |
| 114 CHECK_EQ(static_cast<uint64_t>(1) << 9, diy_fp.f() - boundary_minus.f()); |
| 115 CHECK_EQ(static_cast<uint64_t>(1) << 10, boundary_plus.f() - diy_fp.f()); |
| 116 |
| 117 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001); |
| 118 diy_fp = Double(min_double64).AsNormalizedDiyFp(); |
| 119 Double(min_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus); |
| 120 CHECK_EQ(diy_fp.e(), boundary_minus.e()); |
| 121 CHECK_EQ(diy_fp.e(), boundary_plus.e()); |
| 122 // min-value does not have a significand of the form 2^p (for some p). |
| 123 // Therefore its boundaries are at the same distance. |
| 124 CHECK_EQ(diy_fp.f() - boundary_minus.f(), boundary_plus.f() - diy_fp.f()); |
| 125 // Denormals have their boundaries much closer. |
| 126 CHECK_EQ(static_cast<uint64_t>(1) << 62, diy_fp.f() - boundary_minus.f()); |
| 127 |
| 128 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); |
| 129 diy_fp = Double(smallest_normal64).AsNormalizedDiyFp(); |
| 130 Double(smallest_normal64).NormalizedBoundaries(&boundary_minus, |
| 131 &boundary_plus); |
| 132 CHECK_EQ(diy_fp.e(), boundary_minus.e()); |
| 133 CHECK_EQ(diy_fp.e(), boundary_plus.e()); |
| 134 // Even though the significand is of the form 2^p (for some p), its boundaries |
| 135 // are at the same distance. (This is the only exception). |
| 136 CHECK_EQ(diy_fp.f() - boundary_minus.f(), boundary_plus.f() - diy_fp.f()); |
| 137 CHECK_EQ(static_cast<uint64_t>(1) << 10, diy_fp.f() - boundary_minus.f()); |
| 138 |
| 139 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); |
| 140 diy_fp = Double(largest_denormal64).AsNormalizedDiyFp(); |
| 141 Double(largest_denormal64).NormalizedBoundaries(&boundary_minus, |
| 142 &boundary_plus); |
| 143 CHECK_EQ(diy_fp.e(), boundary_minus.e()); |
| 144 CHECK_EQ(diy_fp.e(), boundary_plus.e()); |
| 145 CHECK_EQ(diy_fp.f() - boundary_minus.f(), boundary_plus.f() - diy_fp.f()); |
| 146 CHECK_EQ(static_cast<uint64_t>(1) << 11, diy_fp.f() - boundary_minus.f()); |
| 147 |
| 148 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff); |
| 149 diy_fp = Double(max_double64).AsNormalizedDiyFp(); |
| 150 Double(max_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus); |
| 151 CHECK_EQ(diy_fp.e(), boundary_minus.e()); |
| 152 CHECK_EQ(diy_fp.e(), boundary_plus.e()); |
| 153 // max-value does not have a significand of the form 2^p (for some p). |
| 154 // Therefore its boundaries are at the same distance. |
| 155 CHECK_EQ(diy_fp.f() - boundary_minus.f(), boundary_plus.f() - diy_fp.f()); |
| 156 CHECK_EQ(static_cast<uint64_t>(1) << 10, diy_fp.f() - boundary_minus.f()); |
| 157 } |
OLD | NEW |