v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-double.cc
Go to the documentation of this file.
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 
13 using namespace v8::internal;
14 
15 
16 TEST(Uint64Conversions) {
17  // Start by checking the byte-order.
18  uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF);
19  CHECK_EQ(3512700564088504e-318, Double(ordered).value());
20 
21  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
22  CHECK_EQ(5e-324, Double(min_double64).value());
23 
24  uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
25  CHECK_EQ(1.7976931348623157e308, Double(max_double64).value());
26 }
27 
28 TEST(AsDiyFp) {
29  uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF);
30  DiyFp diy_fp = Double(ordered).AsDiyFp();
31  CHECK_EQ(0x12 - 0x3FF - 52, diy_fp.e());
32  // The 52 mantissa bits, plus the implicit 1 in bit 52 as a UINT64.
33  CHECK(V8_2PART_UINT64_C(0x00134567, 89ABCDEF) == diy_fp.f()); // NOLINT
34 
35  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
36  diy_fp = Double(min_double64).AsDiyFp();
37  CHECK_EQ(-0x3FF - 52 + 1, diy_fp.e());
38  // This is a denormal; so no hidden bit.
39  CHECK(1 == diy_fp.f()); // NOLINT
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  CHECK(V8_2PART_UINT64_C(0x001fffff, ffffffff) == diy_fp.f()); // NOLINT
45 }
46 
47 
48 TEST(AsNormalizedDiyFp) {
49  uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF);
50  DiyFp diy_fp = Double(ordered).AsNormalizedDiyFp();
51  CHECK_EQ(0x12 - 0x3FF - 52 - 11, diy_fp.e());
52  CHECK((V8_2PART_UINT64_C(0x00134567, 89ABCDEF) << 11) ==
53  diy_fp.f()); // NOLINT
54 
55  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
56  diy_fp = Double(min_double64).AsNormalizedDiyFp();
57  CHECK_EQ(-0x3FF - 52 + 1 - 63, diy_fp.e());
58  // This is a denormal; so no hidden bit.
59  CHECK(V8_2PART_UINT64_C(0x80000000, 00000000) == diy_fp.f()); // NOLINT
60 
61  uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
62  diy_fp = Double(max_double64).AsNormalizedDiyFp();
63  CHECK_EQ(0x7FE - 0x3FF - 52 - 11, diy_fp.e());
64  CHECK((V8_2PART_UINT64_C(0x001fffff, ffffffff) << 11) ==
65  diy_fp.f()); // NOLINT
66 }
67 
68 
69 TEST(IsDenormal) {
70  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
71  CHECK(Double(min_double64).IsDenormal());
72  uint64_t bits = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
73  CHECK(Double(bits).IsDenormal());
74  bits = V8_2PART_UINT64_C(0x00100000, 00000000);
75  CHECK(!Double(bits).IsDenormal());
76 }
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  CHECK(!Double(-5e-324).IsSpecial());
88  // And some random numbers:
89  CHECK(!Double(0.0).IsSpecial());
90  CHECK(!Double(-0.0).IsSpecial());
91  CHECK(!Double(1.0).IsSpecial());
92  CHECK(!Double(-1.0).IsSpecial());
93  CHECK(!Double(1000000.0).IsSpecial());
94  CHECK(!Double(-1000000.0).IsSpecial());
95  CHECK(!Double(1e23).IsSpecial());
96  CHECK(!Double(-1e23).IsSpecial());
97  CHECK(!Double(1.7976931348623157e308).IsSpecial());
98  CHECK(!Double(-1.7976931348623157e308).IsSpecial());
99 }
100 
101 
102 TEST(IsInfinite) {
103  CHECK(Double(V8_INFINITY).IsInfinite());
104  CHECK(Double(-V8_INFINITY).IsInfinite());
105  CHECK(!Double(OS::nan_value()).IsInfinite());
106  CHECK(!Double(0.0).IsInfinite());
107  CHECK(!Double(-0.0).IsInfinite());
108  CHECK(!Double(1.0).IsInfinite());
109  CHECK(!Double(-1.0).IsInfinite());
110  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
111  CHECK(!Double(min_double64).IsInfinite());
112 }
113 
114 
115 TEST(Sign) {
116  CHECK_EQ(1, Double(1.0).Sign());
117  CHECK_EQ(1, Double(V8_INFINITY).Sign());
118  CHECK_EQ(-1, Double(-V8_INFINITY).Sign());
119  CHECK_EQ(1, Double(0.0).Sign());
120  CHECK_EQ(-1, Double(-0.0).Sign());
121  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
122  CHECK_EQ(1, Double(min_double64).Sign());
123 }
124 
125 
126 TEST(NormalizedBoundaries) {
127  DiyFp boundary_plus;
128  DiyFp boundary_minus;
129  DiyFp diy_fp = Double(1.5).AsNormalizedDiyFp();
130  Double(1.5).NormalizedBoundaries(&boundary_minus, &boundary_plus);
131  CHECK_EQ(diy_fp.e(), boundary_minus.e());
132  CHECK_EQ(diy_fp.e(), boundary_plus.e());
133  // 1.5 does not have a significand of the form 2^p (for some p).
134  // Therefore its boundaries are at the same distance.
135  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
136  CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
137 
138  diy_fp = Double(1.0).AsNormalizedDiyFp();
139  Double(1.0).NormalizedBoundaries(&boundary_minus, &boundary_plus);
140  CHECK_EQ(diy_fp.e(), boundary_minus.e());
141  CHECK_EQ(diy_fp.e(), boundary_plus.e());
142  // 1.0 does have a significand of the form 2^p (for some p).
143  // Therefore its lower boundary is twice as close as the upper boundary.
144  CHECK_GT(boundary_plus.f() - diy_fp.f(), diy_fp.f() - boundary_minus.f());
145  CHECK((1 << 9) == diy_fp.f() - boundary_minus.f()); // NOLINT
146  CHECK((1 << 10) == boundary_plus.f() - diy_fp.f()); // NOLINT
147 
148  uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
149  diy_fp = Double(min_double64).AsNormalizedDiyFp();
150  Double(min_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  // min-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(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
156  // Denormals have their boundaries much closer.
157  CHECK((static_cast<uint64_t>(1) << 62) ==
158  diy_fp.f() - boundary_minus.f()); // NOLINT
159 
160  uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
161  diy_fp = Double(smallest_normal64).AsNormalizedDiyFp();
162  Double(smallest_normal64).NormalizedBoundaries(&boundary_minus,
163  &boundary_plus);
164  CHECK_EQ(diy_fp.e(), boundary_minus.e());
165  CHECK_EQ(diy_fp.e(), boundary_plus.e());
166  // Even though the significand is of the form 2^p (for some p), its boundaries
167  // are at the same distance. (This is the only exception).
168  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
169  CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
170 
171  uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
172  diy_fp = Double(largest_denormal64).AsNormalizedDiyFp();
173  Double(largest_denormal64).NormalizedBoundaries(&boundary_minus,
174  &boundary_plus);
175  CHECK_EQ(diy_fp.e(), boundary_minus.e());
176  CHECK_EQ(diy_fp.e(), boundary_plus.e());
177  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
178  CHECK((1 << 11) == diy_fp.f() - boundary_minus.f()); // NOLINT
179 
180  uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
181  diy_fp = Double(max_double64).AsNormalizedDiyFp();
182  Double(max_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
183  CHECK_EQ(diy_fp.e(), boundary_minus.e());
184  CHECK_EQ(diy_fp.e(), boundary_plus.e());
185  // max-value does not have a significand of the form 2^p (for some p).
186  // Therefore its boundaries are at the same distance.
187  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
188  CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
189 }
190 
191 
192 TEST(NextDouble) {
193  CHECK_EQ(4e-324, Double(0.0).NextDouble());
194  CHECK_EQ(0.0, Double(-0.0).NextDouble());
195  CHECK_EQ(-0.0, Double(-4e-324).NextDouble());
196  Double d0(-4e-324);
197  Double d1(d0.NextDouble());
198  Double d2(d1.NextDouble());
199  CHECK_EQ(-0.0, d1.value());
200  CHECK_EQ(0.0, d2.value());
201  CHECK_EQ(4e-324, d2.NextDouble());
202  CHECK_EQ(-1.7976931348623157e308, Double(-V8_INFINITY).NextDouble());
204  Double(V8_2PART_UINT64_C(0x7fefffff, ffffffff)).NextDouble());
205 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
double NextDouble() const
Definition: double.h:88
#define CHECK_GT(a, b)
Definition: checks.h:227
const DwVfpRegister d0
uint64_t f() const
Definition: diy-fp.h:102
DiyFp AsDiyFp() const
Definition: double.h:59
#define CHECK(condition)
Definition: checks.h:56
#define V8_INFINITY
Definition: globals.h:32
int e() const
Definition: diy-fp.h:103
#define V8_2PART_UINT64_C(a, b)
Definition: globals.h:187
DiyFp AsNormalizedDiyFp() const
Definition: double.h:66
static double nan_value()
const DwVfpRegister d2
const DwVfpRegister d1
void NormalizedBoundaries(DiyFp *out_m_minus, DiyFp *out_m_plus) const
Definition: double.h:155