blob: 0ed26b1690ec1fea24ca6cf47051cbf0e84b4a6e [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2006-2008 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +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.
Steve Block6ded16b2010-05-10 14:33:55 +010027
28#include <stdlib.h>
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/v8.h"
Steve Block6ded16b2010-05-10 14:33:55 +010031
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "src/base/platform/platform.h"
33#include "src/diy-fp.h"
34#include "src/double.h"
35#include "src/fast-dtoa.h"
36#include "test/cctest/cctest.h"
37#include "test/cctest/gay-precision.h"
38#include "test/cctest/gay-shortest.h"
Steve Block6ded16b2010-05-10 14:33:55 +010039
40using namespace v8::internal;
41
42static const int kBufferSize = 100;
43
Kristian Monsen0d5e1162010-09-30 15:31:59 +010044
45// Removes trailing '0' digits.
46static void TrimRepresentation(Vector<char> representation) {
Steve Block1e0659c2011-05-24 12:43:12 +010047 int len = StrLength(representation.start());
Kristian Monsen0d5e1162010-09-30 15:31:59 +010048 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) {
Steve Block6ded16b2010-05-10 14:33:55 +010057 char buffer_container[kBufferSize];
58 Vector<char> buffer(buffer_container, kBufferSize);
Steve Block6ded16b2010-05-10 14:33:55 +010059 int length;
60 int point;
61 int status;
62
63 double min_double = 5e-324;
Kristian Monsen0d5e1162010-09-30 15:31:59 +010064 status = FastDtoa(min_double, FAST_DTOA_SHORTEST, 0,
65 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +010066 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 CHECK_EQ(0, strcmp("5", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +010068 CHECK_EQ(-323, point);
69
70 double max_double = 1.7976931348623157e308;
Kristian Monsen0d5e1162010-09-30 15:31:59 +010071 status = FastDtoa(max_double, FAST_DTOA_SHORTEST, 0,
72 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +010073 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 CHECK_EQ(0, strcmp("17976931348623157", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +010075 CHECK_EQ(309, point);
76
Kristian Monsen0d5e1162010-09-30 15:31:59 +010077 status = FastDtoa(4294967272.0, FAST_DTOA_SHORTEST, 0,
78 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +010079 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 CHECK_EQ(0, strcmp("4294967272", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +010081 CHECK_EQ(10, point);
82
Kristian Monsen0d5e1162010-09-30 15:31:59 +010083 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_SHORTEST, 0,
84 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +010085 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086 CHECK_EQ(0, strcmp("4185580496821357", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +010087 CHECK_EQ(299, point);
88
Kristian Monsen0d5e1162010-09-30 15:31:59 +010089 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_SHORTEST, 0,
90 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +010091 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 CHECK_EQ(0, strcmp("5562684646268003", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +010093 CHECK_EQ(-308, point);
94
Kristian Monsen0d5e1162010-09-30 15:31:59 +010095 status = FastDtoa(2147483648.0, FAST_DTOA_SHORTEST, 0,
96 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +010097 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098 CHECK_EQ(0, strcmp("2147483648", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +010099 CHECK_EQ(10, point);
100
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100101 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_SHORTEST, 0,
102 buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +0100103 if (status) { // Not all FastDtoa variants manage to compute this number.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 CHECK_EQ(0, strcmp("35844466002796428", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +0100105 CHECK_EQ(299, point);
106 }
107
108 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
109 double v = Double(smallest_normal64).value();
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100110 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +0100111 if (status) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 CHECK_EQ(0, strcmp("22250738585072014", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +0100113 CHECK_EQ(-307, point);
114 }
115
116 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
117 v = Double(largest_denormal64).value();
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100118 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +0100119 if (status) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 CHECK_EQ(0, strcmp("2225073858507201", buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +0100121 CHECK_EQ(-307, point);
122 }
123}
124
125
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100126TEST(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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 CHECK_EQ(0, strcmp("1", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100138 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144 CHECK_EQ(0, strcmp("15", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100145 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152 CHECK_EQ(0, strcmp("49407", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100153 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 CHECK_EQ(0, strcmp("1797693", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100160 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 CHECK_EQ(0, strcmp("4294967272", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100168 CHECK_EQ(10, point);
169 }
170
171 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_PRECISION, 17,
172 buffer, &length, &point);
173 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 CHECK_EQ(0, strcmp("41855804968213567", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100175 CHECK_EQ(299, point);
176
177 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_PRECISION, 1,
178 buffer, &length, &point);
179 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000180 CHECK_EQ(0, strcmp("6", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100181 CHECK_EQ(-308, point);
182
183 status = FastDtoa(2147483648.0, FAST_DTOA_PRECISION, 5,
184 buffer, &length, &point);
185 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 CHECK_EQ(0, strcmp("21475", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100187 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194 CHECK_EQ(0, strcmp("35844466", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100195 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201 CHECK_EQ(0, strcmp("22250738585072014", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100202 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000210 CHECK_EQ(0, strcmp("22250738585072009", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100211 CHECK_EQ(-307, point);
212
213 v = 3.3161339052167390562200598e-237;
214 status = FastDtoa(v, FAST_DTOA_PRECISION, 18, buffer, &length, &point);
215 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 CHECK_EQ(0, strcmp("331613390521673906", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100217 CHECK_EQ(-236, point);
218
219 v = 7.9885183916008099497815232e+191;
220 status = FastDtoa(v, FAST_DTOA_PRECISION, 4, buffer, &length, &point);
221 CHECK(status);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000222 CHECK_EQ(0, strcmp("7989", buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100223 CHECK_EQ(192, point);
224}
225
226
Steve Block6ded16b2010-05-10 14:33:55 +0100227TEST(FastDtoaGayShortest) {
228 char buffer_container[kBufferSize];
229 Vector<char> buffer(buffer_container, kBufferSize);
230 bool status;
Steve Block6ded16b2010-05-10 14:33:55 +0100231 int length;
232 int point;
233 int succeeded = 0;
234 int total = 0;
235 bool needed_max_length = false;
236
Kristian Monsen25f61362010-05-21 11:50:48 +0100237 Vector<const PrecomputedShortest> precomputed =
238 PrecomputedShortestRepresentations();
Steve Block6ded16b2010-05-10 14:33:55 +0100239 for (int i = 0; i < precomputed.length(); ++i) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100240 const PrecomputedShortest current_test = precomputed[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100241 total++;
242 double v = current_test.v;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100243 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
Steve Block6ded16b2010-05-10 14:33:55 +0100244 CHECK_GE(kFastDtoaMaximalLength, length);
245 if (!status) continue;
246 if (length == kFastDtoaMaximalLength) needed_max_length = true;
247 succeeded++;
Steve Block6ded16b2010-05-10 14:33:55 +0100248 CHECK_EQ(current_test.decimal_point, point);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 CHECK_EQ(0, strcmp(current_test.representation, buffer.start()));
Steve Block6ded16b2010-05-10 14:33:55 +0100250 }
251 CHECK_GT(succeeded*1.0/total, 0.99);
252 CHECK(needed_max_length);
253}
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100254
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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 CHECK_EQ(0, strcmp(current_test.representation, buffer.start()));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100285 }
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}