1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // 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.
27
28 #include <stdlib.h>
29
30 #include "cctest.h"
31 #include "double-conversion/diy-fp.h"
32 #include "double-conversion/fast-dtoa.h"
33 #include "gay-precision.h"
34 #include "gay-shortest.h"
35 #include "gay-shortest-single.h"
36 #include "double-conversion/ieee.h"
37 #include "double-conversion/utils.h"
38
39 using namespace double_conversion;
40
41 static const int kBufferSize = 100;
42
43
44 // Removes trailing '0' digits.
TrimRepresentation(Vector<char> representation)45 static void TrimRepresentation(Vector<char> representation) {
46 int len = static_cast<int>(strlen(representation.start()));
47 int i;
48 for (i = len - 1; i >= 0; --i) {
49 if (representation[i] != '0') break;
50 }
51 representation[i + 1] = '\0';
52 }
53
54
TEST(FastDtoaShortestVariousDoubles)55 TEST(FastDtoaShortestVariousDoubles) {
56 char buffer_container[kBufferSize];
57 Vector<char> buffer(buffer_container, kBufferSize);
58 int length;
59 int point;
60 bool status;
61
62 double min_double = 5e-324;
63 status = FastDtoa(min_double, FAST_DTOA_SHORTEST, 0,
64 buffer, &length, &point);
65 CHECK(status);
66 CHECK_EQ("5", buffer.start());
67 CHECK_EQ(-323, point);
68
69 double max_double = 1.7976931348623157e308;
70 status = FastDtoa(max_double, FAST_DTOA_SHORTEST, 0,
71 buffer, &length, &point);
72 CHECK(status);
73 CHECK_EQ("17976931348623157", buffer.start());
74 CHECK_EQ(309, point);
75
76 status = FastDtoa(4294967272.0, FAST_DTOA_SHORTEST, 0,
77 buffer, &length, &point);
78 CHECK(status);
79 CHECK_EQ("4294967272", buffer.start());
80 CHECK_EQ(10, point);
81
82 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_SHORTEST, 0,
83 buffer, &length, &point);
84 CHECK(status);
85 CHECK_EQ("4185580496821357", buffer.start());
86 CHECK_EQ(299, point);
87
88 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_SHORTEST, 0,
89 buffer, &length, &point);
90 CHECK(status);
91 CHECK_EQ("5562684646268003", buffer.start());
92 CHECK_EQ(-308, point);
93
94 status = FastDtoa(2147483648.0, FAST_DTOA_SHORTEST, 0,
95 buffer, &length, &point);
96 CHECK(status);
97 CHECK_EQ("2147483648", buffer.start());
98 CHECK_EQ(10, point);
99
100 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_SHORTEST, 0,
101 buffer, &length, &point);
102 if (status) { // Not all FastDtoa variants manage to compute this number.
103 CHECK_EQ("35844466002796428", buffer.start());
104 CHECK_EQ(299, point);
105 }
106
107 uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
108 double v = Double(smallest_normal64).value();
109 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
110 if (status) {
111 CHECK_EQ("22250738585072014", buffer.start());
112 CHECK_EQ(-307, point);
113 }
114
115 uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
116 v = Double(largest_denormal64).value();
117 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
118 if (status) {
119 CHECK_EQ("2225073858507201", buffer.start());
120 CHECK_EQ(-307, point);
121 }
122 }
123
124
TEST(FastDtoaShortestVariousFloats)125 TEST(FastDtoaShortestVariousFloats) {
126 char buffer_container[kBufferSize];
127 Vector<char> buffer(buffer_container, kBufferSize);
128 int length;
129 int point;
130 bool status;
131
132 float min_float = 1e-45f;
133 status = FastDtoa(min_float, FAST_DTOA_SHORTEST_SINGLE, 0,
134 buffer, &length, &point);
135 CHECK(status);
136 CHECK_EQ("1", buffer.start());
137 CHECK_EQ(-44, point);
138
139
140 float max_float = 3.4028234e38f;
141 status = FastDtoa(max_float, FAST_DTOA_SHORTEST_SINGLE, 0,
142 buffer, &length, &point);
143 CHECK(status);
144 CHECK_EQ("34028235", buffer.start());
145 CHECK_EQ(39, point);
146
147 status = FastDtoa(4294967272.0f, FAST_DTOA_SHORTEST_SINGLE, 0,
148 buffer, &length, &point);
149 CHECK(status);
150 CHECK_EQ("42949673", buffer.start());
151 CHECK_EQ(10, point);
152
153 status = FastDtoa(3.32306998946228968226e+35f, FAST_DTOA_SHORTEST_SINGLE, 0,
154 buffer, &length, &point);
155 CHECK(status);
156 CHECK_EQ("332307", buffer.start());
157 CHECK_EQ(36, point);
158
159 status = FastDtoa(1.2341e-41f, FAST_DTOA_SHORTEST_SINGLE, 0,
160 buffer, &length, &point);
161 CHECK(status);
162 CHECK_EQ("12341", buffer.start());
163 CHECK_EQ(-40, point);
164
165 status = FastDtoa(3.3554432e7, FAST_DTOA_SHORTEST_SINGLE, 0,
166 buffer, &length, &point);
167 CHECK(status);
168 CHECK_EQ("33554432", buffer.start());
169 CHECK_EQ(8, point);
170
171 status = FastDtoa(3.26494756798464e14f, FAST_DTOA_SHORTEST_SINGLE, 0,
172 buffer, &length, &point);
173 CHECK(status);
174 CHECK_EQ("32649476", buffer.start());
175 CHECK_EQ(15, point);
176
177 status = FastDtoa(3.91132223637771935344e37f, FAST_DTOA_SHORTEST_SINGLE, 0,
178 buffer, &length, &point);
179 if (status) { // Not all FastDtoa variants manage to compute this number.
180 CHECK_EQ("39113222", buffer.start());
181 CHECK_EQ(38, point);
182 }
183
184 uint32_t smallest_normal32 = 0x00800000;
185 float v = Single(smallest_normal32).value();
186 status = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point);
187 if (status) {
188 CHECK_EQ("11754944", buffer.start());
189 CHECK_EQ(-37, point);
190 }
191
192 uint32_t largest_denormal32 = 0x007FFFFF;
193 v = Single(largest_denormal32).value();
194 status = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point);
195 CHECK(status);
196 CHECK_EQ("11754942", buffer.start());
197 CHECK_EQ(-37, point);
198 }
199
200
TEST(FastDtoaPrecisionVariousDoubles)201 TEST(FastDtoaPrecisionVariousDoubles) {
202 char buffer_container[kBufferSize];
203 Vector<char> buffer(buffer_container, kBufferSize);
204 int length;
205 int point;
206 bool status;
207
208 status = FastDtoa(1.0, FAST_DTOA_PRECISION, 3, buffer, &length, &point);
209 CHECK(status);
210 CHECK(3 >= length);
211 TrimRepresentation(buffer);
212 CHECK_EQ("1", buffer.start());
213 CHECK_EQ(1, point);
214
215 status = FastDtoa(1.5, FAST_DTOA_PRECISION, 10, buffer, &length, &point);
216 if (status) {
217 CHECK(10 >= length);
218 TrimRepresentation(buffer);
219 CHECK_EQ("15", buffer.start());
220 CHECK_EQ(1, point);
221 }
222
223 double min_double = 5e-324;
224 status = FastDtoa(min_double, FAST_DTOA_PRECISION, 5,
225 buffer, &length, &point);
226 CHECK(status);
227 CHECK_EQ("49407", buffer.start());
228 CHECK_EQ(-323, point);
229
230 double max_double = 1.7976931348623157e308;
231 status = FastDtoa(max_double, FAST_DTOA_PRECISION, 7,
232 buffer, &length, &point);
233 CHECK(status);
234 CHECK_EQ("1797693", buffer.start());
235 CHECK_EQ(309, point);
236
237 status = FastDtoa(4294967272.0, FAST_DTOA_PRECISION, 14,
238 buffer, &length, &point);
239 if (status) {
240 CHECK(14 >= length);
241 TrimRepresentation(buffer);
242 CHECK_EQ("4294967272", buffer.start());
243 CHECK_EQ(10, point);
244 }
245
246 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_PRECISION, 17,
247 buffer, &length, &point);
248 CHECK(status);
249 CHECK_EQ("41855804968213567", buffer.start());
250 CHECK_EQ(299, point);
251
252 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_PRECISION, 1,
253 buffer, &length, &point);
254 CHECK(status);
255 CHECK_EQ("6", buffer.start());
256 CHECK_EQ(-308, point);
257
258 status = FastDtoa(2147483648.0, FAST_DTOA_PRECISION, 5,
259 buffer, &length, &point);
260 CHECK(status);
261 CHECK_EQ("21475", buffer.start());
262 CHECK_EQ(10, point);
263
264 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_PRECISION, 10,
265 buffer, &length, &point);
266 CHECK(status);
267 CHECK(10 >= length);
268 TrimRepresentation(buffer);
269 CHECK_EQ("35844466", buffer.start());
270 CHECK_EQ(299, point);
271
272 uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
273 double v = Double(smallest_normal64).value();
274 status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
275 CHECK(status);
276 CHECK_EQ("22250738585072014", buffer.start());
277 CHECK_EQ(-307, point);
278
279 uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
280 v = Double(largest_denormal64).value();
281 status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point);
282 CHECK(status);
283 CHECK(20 >= length);
284 TrimRepresentation(buffer);
285 CHECK_EQ("22250738585072009", buffer.start());
286 CHECK_EQ(-307, point);
287
288 v = 3.3161339052167390562200598e-237;
289 status = FastDtoa(v, FAST_DTOA_PRECISION, 18, buffer, &length, &point);
290 CHECK(status);
291 CHECK_EQ("331613390521673906", buffer.start());
292 CHECK_EQ(-236, point);
293
294 v = 7.9885183916008099497815232e+191;
295 status = FastDtoa(v, FAST_DTOA_PRECISION, 4, buffer, &length, &point);
296 CHECK(status);
297 CHECK_EQ("7989", buffer.start());
298 CHECK_EQ(192, point);
299 }
300
301
TEST(FastDtoaGayShortest)302 TEST(FastDtoaGayShortest) {
303 char buffer_container[kBufferSize];
304 Vector<char> buffer(buffer_container, kBufferSize);
305 bool status;
306 int length;
307 int point;
308 int succeeded = 0;
309 int total = 0;
310 bool needed_max_length = false;
311
312 Vector<const PrecomputedShortest> precomputed =
313 PrecomputedShortestRepresentations();
314 for (int i = 0; i < precomputed.length(); ++i) {
315 const PrecomputedShortest current_test = precomputed[i];
316 total++;
317 double v = current_test.v;
318 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point);
319 CHECK(kFastDtoaMaximalLength >= length);
320 if (!status) continue;
321 if (length == kFastDtoaMaximalLength) needed_max_length = true;
322 succeeded++;
323 CHECK_EQ(current_test.decimal_point, point);
324 CHECK_EQ(current_test.representation, buffer.start());
325 }
326 CHECK(succeeded*1.0/total > 0.99);
327 CHECK(needed_max_length);
328 }
329
330
TEST(FastDtoaGayShortestSingle)331 TEST(FastDtoaGayShortestSingle) {
332 char buffer_container[kBufferSize];
333 Vector<char> buffer(buffer_container, kBufferSize);
334 bool status;
335 int length;
336 int point;
337 int succeeded = 0;
338 int total = 0;
339 bool needed_max_length = false;
340
341 Vector<const PrecomputedShortestSingle> precomputed =
342 PrecomputedShortestSingleRepresentations();
343 for (int i = 0; i < precomputed.length(); ++i) {
344 const PrecomputedShortestSingle current_test = precomputed[i];
345 total++;
346 float v = current_test.v;
347 status = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point);
348 CHECK(kFastDtoaMaximalSingleLength >= length);
349 if (!status) continue;
350 if (length == kFastDtoaMaximalSingleLength) needed_max_length = true;
351 succeeded++;
352 CHECK_EQ(current_test.decimal_point, point);
353 CHECK_EQ(current_test.representation, buffer.start());
354 }
355 CHECK(succeeded*1.0/total > 0.98);
356 CHECK(needed_max_length);
357 }
358
359
TEST(FastDtoaGayPrecision)360 TEST(FastDtoaGayPrecision) {
361 char buffer_container[kBufferSize];
362 Vector<char> buffer(buffer_container, kBufferSize);
363 bool status;
364 int length;
365 int point;
366 int succeeded = 0;
367 int total = 0;
368 // Count separately for entries with less than 15 requested digits.
369 int succeeded_15 = 0;
370 int total_15 = 0;
371
372 Vector<const PrecomputedPrecision> precomputed =
373 PrecomputedPrecisionRepresentations();
374 for (int i = 0; i < precomputed.length(); ++i) {
375 const PrecomputedPrecision current_test = precomputed[i];
376 double v = current_test.v;
377 int number_digits = current_test.number_digits;
378 total++;
379 if (number_digits <= 15) total_15++;
380 status = FastDtoa(v, FAST_DTOA_PRECISION, number_digits,
381 buffer, &length, &point);
382 CHECK(number_digits >= length);
383 if (!status) continue;
384 succeeded++;
385 if (number_digits <= 15) succeeded_15++;
386 TrimRepresentation(buffer);
387 CHECK_EQ(current_test.decimal_point, point);
388 CHECK_EQ(current_test.representation, buffer.start());
389 }
390 // The precomputed numbers contain many entries with many requested
391 // digits. These have a high failure rate and we therefore expect a lower
392 // success rate than for the shortest representation.
393 CHECK(succeeded*1.0/total > 0.85);
394 // However with less than 15 digits almost the algorithm should almost always
395 // succeed.
396 CHECK(succeeded_15*1.0/total_15 > 0.9999);
397 }
398