blob: 0e50bdcc8af8d592a4dc5644fb23d79dcc179139 [file] [log] [blame]
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002// 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.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000027
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "platform.h"
33#include "cctest.h"
34#include "diy-fp.h"
35#include "double.h"
36
37
38using namespace v8::internal;
39
40
41TEST(Uint64Conversions) {
42 // Start by checking the byte-order.
43 uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF);
44 CHECK_EQ(3512700564088504e-318, Double(ordered).value());
45
46 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
47 CHECK_EQ(5e-324, Double(min_double64).value());
48
49 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
50 CHECK_EQ(1.7976931348623157e308, Double(max_double64).value());
51}
52
53TEST(AsDiyFp) {
54 uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF);
55 DiyFp diy_fp = Double(ordered).AsDiyFp();
56 CHECK_EQ(0x12 - 0x3FF - 52, diy_fp.e());
57 // The 52 mantissa bits, plus the implicit 1 in bit 52 as a UINT64.
58 CHECK(V8_2PART_UINT64_C(0x00134567, 89ABCDEF) == diy_fp.f()); // NOLINT
59
60 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
61 diy_fp = Double(min_double64).AsDiyFp();
62 CHECK_EQ(-0x3FF - 52 + 1, diy_fp.e());
63 // This is a denormal; so no hidden bit.
64 CHECK(1 == diy_fp.f()); // NOLINT
65
66 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
67 diy_fp = Double(max_double64).AsDiyFp();
68 CHECK_EQ(0x7FE - 0x3FF - 52, diy_fp.e());
69 CHECK(V8_2PART_UINT64_C(0x001fffff, ffffffff) == diy_fp.f()); // NOLINT
70}
71
72
73TEST(AsNormalizedDiyFp) {
74 uint64_t ordered = V8_2PART_UINT64_C(0x01234567, 89ABCDEF);
75 DiyFp diy_fp = Double(ordered).AsNormalizedDiyFp();
76 CHECK_EQ(0x12 - 0x3FF - 52 - 11, diy_fp.e());
77 CHECK((V8_2PART_UINT64_C(0x00134567, 89ABCDEF) << 11) ==
78 diy_fp.f()); // NOLINT
79
80 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
81 diy_fp = Double(min_double64).AsNormalizedDiyFp();
82 CHECK_EQ(-0x3FF - 52 + 1 - 63, diy_fp.e());
83 // This is a denormal; so no hidden bit.
84 CHECK(V8_2PART_UINT64_C(0x80000000, 00000000) == diy_fp.f()); // NOLINT
85
86 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
87 diy_fp = Double(max_double64).AsNormalizedDiyFp();
88 CHECK_EQ(0x7FE - 0x3FF - 52 - 11, diy_fp.e());
89 CHECK((V8_2PART_UINT64_C(0x001fffff, ffffffff) << 11) ==
90 diy_fp.f()); // NOLINT
91}
92
93
94TEST(IsDenormal) {
95 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
96 CHECK(Double(min_double64).IsDenormal());
97 uint64_t bits = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
98 CHECK(Double(bits).IsDenormal());
99 bits = V8_2PART_UINT64_C(0x00100000, 00000000);
100 CHECK(!Double(bits).IsDenormal());
101}
102
103
104TEST(IsSpecial) {
105 CHECK(Double(V8_INFINITY).IsSpecial());
106 CHECK(Double(-V8_INFINITY).IsSpecial());
107 CHECK(Double(OS::nan_value()).IsSpecial());
108 uint64_t bits = V8_2PART_UINT64_C(0xFFF12345, 00000000);
109 CHECK(Double(bits).IsSpecial());
110 // Denormals are not special:
111 CHECK(!Double(5e-324).IsSpecial());
112 CHECK(!Double(-5e-324).IsSpecial());
113 // And some random numbers:
114 CHECK(!Double(0.0).IsSpecial());
115 CHECK(!Double(-0.0).IsSpecial());
116 CHECK(!Double(1.0).IsSpecial());
117 CHECK(!Double(-1.0).IsSpecial());
118 CHECK(!Double(1000000.0).IsSpecial());
119 CHECK(!Double(-1000000.0).IsSpecial());
120 CHECK(!Double(1e23).IsSpecial());
121 CHECK(!Double(-1e23).IsSpecial());
122 CHECK(!Double(1.7976931348623157e308).IsSpecial());
123 CHECK(!Double(-1.7976931348623157e308).IsSpecial());
124}
125
126
127TEST(IsInfinite) {
128 CHECK(Double(V8_INFINITY).IsInfinite());
129 CHECK(Double(-V8_INFINITY).IsInfinite());
130 CHECK(!Double(OS::nan_value()).IsInfinite());
131 CHECK(!Double(0.0).IsInfinite());
132 CHECK(!Double(-0.0).IsInfinite());
133 CHECK(!Double(1.0).IsInfinite());
134 CHECK(!Double(-1.0).IsInfinite());
135 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
136 CHECK(!Double(min_double64).IsInfinite());
137}
138
139
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000140TEST(Sign) {
141 CHECK_EQ(1, Double(1.0).Sign());
142 CHECK_EQ(1, Double(V8_INFINITY).Sign());
143 CHECK_EQ(-1, Double(-V8_INFINITY).Sign());
144 CHECK_EQ(1, Double(0.0).Sign());
145 CHECK_EQ(-1, Double(-0.0).Sign());
146 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
147 CHECK_EQ(1, Double(min_double64).Sign());
148}
149
150
151TEST(NormalizedBoundaries) {
152 DiyFp boundary_plus;
153 DiyFp boundary_minus;
154 DiyFp diy_fp = Double(1.5).AsNormalizedDiyFp();
155 Double(1.5).NormalizedBoundaries(&boundary_minus, &boundary_plus);
156 CHECK_EQ(diy_fp.e(), boundary_minus.e());
157 CHECK_EQ(diy_fp.e(), boundary_plus.e());
158 // 1.5 does not have a significand of the form 2^p (for some p).
159 // Therefore its boundaries are at the same distance.
160 CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
161 CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
162
163 diy_fp = Double(1.0).AsNormalizedDiyFp();
164 Double(1.0).NormalizedBoundaries(&boundary_minus, &boundary_plus);
165 CHECK_EQ(diy_fp.e(), boundary_minus.e());
166 CHECK_EQ(diy_fp.e(), boundary_plus.e());
167 // 1.0 does have a significand of the form 2^p (for some p).
168 // Therefore its lower boundary is twice as close as the upper boundary.
169 CHECK_GT(boundary_plus.f() - diy_fp.f(), diy_fp.f() - boundary_minus.f());
170 CHECK((1 << 9) == diy_fp.f() - boundary_minus.f()); // NOLINT
171 CHECK((1 << 10) == boundary_plus.f() - diy_fp.f()); // NOLINT
172
173 uint64_t min_double64 = V8_2PART_UINT64_C(0x00000000, 00000001);
174 diy_fp = Double(min_double64).AsNormalizedDiyFp();
175 Double(min_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
176 CHECK_EQ(diy_fp.e(), boundary_minus.e());
177 CHECK_EQ(diy_fp.e(), boundary_plus.e());
178 // min-value does not have a significand of the form 2^p (for some p).
179 // Therefore its boundaries are at the same distance.
180 CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
181 // Denormals have their boundaries much closer.
182 CHECK((static_cast<uint64_t>(1) << 62) ==
183 diy_fp.f() - boundary_minus.f()); // NOLINT
184
185 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
186 diy_fp = Double(smallest_normal64).AsNormalizedDiyFp();
187 Double(smallest_normal64).NormalizedBoundaries(&boundary_minus,
188 &boundary_plus);
189 CHECK_EQ(diy_fp.e(), boundary_minus.e());
190 CHECK_EQ(diy_fp.e(), boundary_plus.e());
191 // Even though the significand is of the form 2^p (for some p), its boundaries
192 // are at the same distance. (This is the only exception).
193 CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
194 CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
195
196 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
197 diy_fp = Double(largest_denormal64).AsNormalizedDiyFp();
198 Double(largest_denormal64).NormalizedBoundaries(&boundary_minus,
199 &boundary_plus);
200 CHECK_EQ(diy_fp.e(), boundary_minus.e());
201 CHECK_EQ(diy_fp.e(), boundary_plus.e());
202 CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
203 CHECK((1 << 11) == diy_fp.f() - boundary_minus.f()); // NOLINT
204
205 uint64_t max_double64 = V8_2PART_UINT64_C(0x7fefffff, ffffffff);
206 diy_fp = Double(max_double64).AsNormalizedDiyFp();
207 Double(max_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
208 CHECK_EQ(diy_fp.e(), boundary_minus.e());
209 CHECK_EQ(diy_fp.e(), boundary_plus.e());
210 // max-value does not have a significand of the form 2^p (for some p).
211 // Therefore its boundaries are at the same distance.
212 CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
213 CHECK((1 << 10) == diy_fp.f() - boundary_minus.f()); // NOLINT
214}
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000215
216
217TEST(NextDouble) {
218 CHECK_EQ(4e-324, Double(0.0).NextDouble());
219 CHECK_EQ(0.0, Double(-0.0).NextDouble());
220 CHECK_EQ(-0.0, Double(-4e-324).NextDouble());
221 Double d0(-4e-324);
222 Double d1(d0.NextDouble());
223 Double d2(d1.NextDouble());
224 CHECK_EQ(-0.0, d1.value());
225 CHECK_EQ(0.0, d2.value());
226 CHECK_EQ(4e-324, d2.NextDouble());
227 CHECK_EQ(-1.7976931348623157e308, Double(-V8_INFINITY).NextDouble());
228 CHECK_EQ(V8_INFINITY,
229 Double(V8_2PART_UINT64_C(0x7fefffff, ffffffff)).NextDouble());
230}