blob: 46f975799fc5b58ebf3c27db0a51ad0f729fc4a6 [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#include "fast-dtoa.h"
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000037#include "gay-precision.h"
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000038#include "gay-shortest.h"
39
40using namespace v8::internal;
41
42static const int kBufferSize = 100;
43
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000044
45// Removes trailing '0' digits.
46static void TrimRepresentation(Vector<char> representation) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +000047 int len = StrLength(representation.start());
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000048 int i;
49 for (i = len - 1; i >= 0; --i) {
50 if (representation[i] != '0') break;
51 }
52 representation[i + 1] = '\0';
53}
54
55
56TEST(FastDtoaShortestVariousDoubles) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000057 char buffer_container[kBufferSize];
58 Vector<char> buffer(buffer_container, kBufferSize);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000059 int length;
60 int point;
61 int status;
62
63 double min_double = 5e-324;
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000064 status = FastDtoa(min_double, FAST_DTOA_SHORTEST, 0,
65 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000066 CHECK(status);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000067 CHECK_EQ("5", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000068 CHECK_EQ(-323, point);
69
70 double max_double = 1.7976931348623157e308;
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000071 status = FastDtoa(max_double, FAST_DTOA_SHORTEST, 0,
72 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000073 CHECK(status);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000074 CHECK_EQ("17976931348623157", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000075 CHECK_EQ(309, point);
76
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000077 status = FastDtoa(4294967272.0, FAST_DTOA_SHORTEST, 0,
78 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000079 CHECK(status);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000080 CHECK_EQ("4294967272", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000081 CHECK_EQ(10, point);
82
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000083 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_SHORTEST, 0,
84 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000085 CHECK(status);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000086 CHECK_EQ("4185580496821357", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000087 CHECK_EQ(299, point);
88
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000089 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_SHORTEST, 0,
90 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000091 CHECK(status);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000092 CHECK_EQ("5562684646268003", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000093 CHECK_EQ(-308, point);
94
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000095 status = FastDtoa(2147483648.0, FAST_DTOA_SHORTEST, 0,
96 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000097 CHECK(status);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000098 CHECK_EQ("2147483648", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000099 CHECK_EQ(10, point);
100
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000101 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_SHORTEST, 0,
102 buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000103 if (status) { // Not all FastDtoa variants manage to compute this number.
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000104 CHECK_EQ("35844466002796428", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000105 CHECK_EQ(299, point);
106 }
107
108 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
109 double v = Double(smallest_normal64).value();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000110 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000111 if (status) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000112 CHECK_EQ("22250738585072014", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000113 CHECK_EQ(-307, point);
114 }
115
116 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
117 v = Double(largest_denormal64).value();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000118 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000119 if (status) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000120 CHECK_EQ("2225073858507201", buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000121 CHECK_EQ(-307, point);
122 }
123}
124
125
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000126TEST(FastDtoaPrecisionVariousDoubles) {
127 char buffer_container[kBufferSize];
128 Vector<char> buffer(buffer_container, kBufferSize);
129 int length;
130 int point;
131 int status;
132
133 status = FastDtoa(1.0, FAST_DTOA_PRECISION, 3, buffer, &length, &point);
134 CHECK(status);
135 CHECK_GE(3, length);
136 TrimRepresentation(buffer);
137 CHECK_EQ("1", buffer.start());
138 CHECK_EQ(1, point);
139
140 status = FastDtoa(1.5, FAST_DTOA_PRECISION, 10, buffer, &length, &point);
141 if (status) {
142 CHECK_GE(10, length);
143 TrimRepresentation(buffer);
144 CHECK_EQ("15", buffer.start());
145 CHECK_EQ(1, point);
146 }
147
148 double min_double = 5e-324;
149 status = FastDtoa(min_double, FAST_DTOA_PRECISION, 5,
150 buffer, &length, &point);
151 CHECK(status);
152 CHECK_EQ("49407", buffer.start());
153 CHECK_EQ(-323, point);
154
155 double max_double = 1.7976931348623157e308;
156 status = FastDtoa(max_double, FAST_DTOA_PRECISION, 7,
157 buffer, &length, &point);
158 CHECK(status);
159 CHECK_EQ("1797693", buffer.start());
160 CHECK_EQ(309, point);
161
162 status = FastDtoa(4294967272.0, FAST_DTOA_PRECISION, 14,
163 buffer, &length, &point);
164 if (status) {
165 CHECK_GE(14, length);
166 TrimRepresentation(buffer);
167 CHECK_EQ("4294967272", buffer.start());
168 CHECK_EQ(10, point);
169 }
170
171 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_PRECISION, 17,
172 buffer, &length, &point);
173 CHECK(status);
174 CHECK_EQ("41855804968213567", buffer.start());
175 CHECK_EQ(299, point);
176
177 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_PRECISION, 1,
178 buffer, &length, &point);
179 CHECK(status);
180 CHECK_EQ("6", buffer.start());
181 CHECK_EQ(-308, point);
182
183 status = FastDtoa(2147483648.0, FAST_DTOA_PRECISION, 5,
184 buffer, &length, &point);
185 CHECK(status);
186 CHECK_EQ("21475", buffer.start());
187 CHECK_EQ(10, point);
188
189 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_PRECISION, 10,
190 buffer, &length, &point);
191 CHECK(status);
192 CHECK_GE(10, length);
193 TrimRepresentation(buffer);
194 CHECK_EQ("35844466", buffer.start());
195 CHECK_EQ(299, point);
196
197 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
198 double v = Double(smallest_normal64).value();
199 status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
200 CHECK(status);
201 CHECK_EQ("22250738585072014", buffer.start());
202 CHECK_EQ(-307, point);
203
204 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
205 v = Double(largest_denormal64).value();
206 status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
207 CHECK(status);
208 CHECK_GE(20, length);
209 TrimRepresentation(buffer);
210 CHECK_EQ("22250738585072009", buffer.start());
211 CHECK_EQ(-307, point);
212
213 v = 3.3161339052167390562200598e-237;
214 status = FastDtoa(v, FAST_DTOA_PRECISION, 18, buffer, &length, &point);
215 CHECK(status);
216 CHECK_EQ("331613390521673906", buffer.start());
217 CHECK_EQ(-236, point);
218
219 v = 7.9885183916008099497815232e+191;
220 status = FastDtoa(v, FAST_DTOA_PRECISION, 4, buffer, &length, &point);
221 CHECK(status);
222 CHECK_EQ("7989", buffer.start());
223 CHECK_EQ(192, point);
224}
225
226
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000227TEST(FastDtoaGayShortest) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000228 char buffer_container[kBufferSize];
229 Vector<char> buffer(buffer_container, kBufferSize);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000230 bool status;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000231 int length;
232 int point;
233 int succeeded = 0;
234 int total = 0;
235 bool needed_max_length = false;
236
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000237 Vector<const PrecomputedShortest> precomputed =
238 PrecomputedShortestRepresentations();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000239 for (int i = 0; i < precomputed.length(); ++i) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000240 const PrecomputedShortest current_test = precomputed[i];
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000241 total++;
242 double v = current_test.v;
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000243 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000244 CHECK_GE(kFastDtoaMaximalLength, length);
245 if (!status) continue;
246 if (length == kFastDtoaMaximalLength) needed_max_length = true;
247 succeeded++;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000248 CHECK_EQ(current_test.decimal_point, point);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000249 CHECK_EQ(current_test.representation, buffer.start());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000250 }
251 CHECK_GT(succeeded*1.0/total, 0.99);
252 CHECK(needed_max_length);
253}
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000254
255
256TEST(FastDtoaGayPrecision) {
257 char buffer_container[kBufferSize];
258 Vector<char> buffer(buffer_container, kBufferSize);
259 bool status;
260 int length;
261 int point;
262 int succeeded = 0;
263 int total = 0;
264 // Count separately for entries with less than 15 requested digits.
265 int succeeded_15 = 0;
266 int total_15 = 0;
267
268 Vector<const PrecomputedPrecision> precomputed =
269 PrecomputedPrecisionRepresentations();
270 for (int i = 0; i < precomputed.length(); ++i) {
271 const PrecomputedPrecision current_test = precomputed[i];
272 double v = current_test.v;
273 int number_digits = current_test.number_digits;
274 total++;
275 if (number_digits <= 15) total_15++;
276 status = FastDtoa(v, FAST_DTOA_PRECISION, number_digits,
277 buffer, &length, &point);
278 CHECK_GE(number_digits, length);
279 if (!status) continue;
280 succeeded++;
281 if (number_digits <= 15) succeeded_15++;
282 TrimRepresentation(buffer);
283 CHECK_EQ(current_test.decimal_point, point);
284 CHECK_EQ(current_test.representation, buffer.start());
285 }
286 // The precomputed numbers contain many entries with many requested
287 // digits. These have a high failure rate and we therefore expect a lower
288 // success rate than for the shortest representation.
289 CHECK_GT(succeeded*1.0/total, 0.85);
290 // However with less than 15 digits almost the algorithm should almost always
291 // succeed.
292 CHECK_GT(succeeded_15*1.0/total_15, 0.9999);
293}