OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 | 2 |
3 #include <stdlib.h> | 3 #include <stdlib.h> |
4 | 4 |
5 #include "v8.h" | 5 #include "v8.h" |
6 | 6 |
7 #include "platform.h" | 7 #include "platform.h" |
8 #include "cctest.h" | 8 #include "cctest.h" |
9 #include "diy-fp.h" | 9 #include "diy-fp.h" |
10 #include "double.h" | 10 #include "double.h" |
11 #include "fast-dtoa.h" | 11 #include "fast-dtoa.h" |
12 #include "gay-shortest.h" | 12 #include "gay-shortest.h" |
13 | 13 |
14 using namespace v8::internal; | 14 using namespace v8::internal; |
15 | 15 |
16 static const int kBufferSize = 100; | 16 static const int kBufferSize = 100; |
17 | 17 |
18 TEST(FastDtoaVariousDoubles) { | 18 TEST(FastDtoaVariousDoubles) { |
19 char buffer_container[kBufferSize]; | 19 char buffer_container[kBufferSize]; |
20 Vector<char> buffer(buffer_container, kBufferSize); | 20 Vector<char> buffer(buffer_container, kBufferSize); |
21 int sign; | |
22 int length; | 21 int length; |
23 int point; | 22 int point; |
24 int status; | 23 int status; |
25 | 24 |
26 double min_double = 5e-324; | 25 double min_double = 5e-324; |
27 status = FastDtoa(min_double, buffer, &sign, &length, &point); | 26 status = FastDtoa(min_double, buffer, &length, &point); |
28 CHECK(status); | 27 CHECK(status); |
29 CHECK_EQ(0, sign); | |
30 CHECK_EQ("5", buffer.start()); | 28 CHECK_EQ("5", buffer.start()); |
31 CHECK_EQ(-323, point); | 29 CHECK_EQ(-323, point); |
32 | 30 |
33 double max_double = 1.7976931348623157e308; | 31 double max_double = 1.7976931348623157e308; |
34 status = FastDtoa(max_double, buffer, &sign, &length, &point); | 32 status = FastDtoa(max_double, buffer, &length, &point); |
35 CHECK(status); | 33 CHECK(status); |
36 CHECK_EQ(0, sign); | |
37 CHECK_EQ("17976931348623157", buffer.start()); | 34 CHECK_EQ("17976931348623157", buffer.start()); |
38 CHECK_EQ(309, point); | 35 CHECK_EQ(309, point); |
39 | 36 |
40 status = FastDtoa(4294967272.0, buffer, &sign, &length, &point); | 37 status = FastDtoa(4294967272.0, buffer, &length, &point); |
41 CHECK(status); | 38 CHECK(status); |
42 CHECK_EQ(0, sign); | |
43 CHECK_EQ("4294967272", buffer.start()); | 39 CHECK_EQ("4294967272", buffer.start()); |
44 CHECK_EQ(10, point); | 40 CHECK_EQ(10, point); |
45 | 41 |
46 status = FastDtoa(4.1855804968213567e298, buffer, &sign, &length, &point); | 42 status = FastDtoa(4.1855804968213567e298, buffer, &length, &point); |
47 CHECK(status); | 43 CHECK(status); |
48 CHECK_EQ(0, sign); | |
49 CHECK_EQ("4185580496821357", buffer.start()); | 44 CHECK_EQ("4185580496821357", buffer.start()); |
50 CHECK_EQ(299, point); | 45 CHECK_EQ(299, point); |
51 | 46 |
52 status = FastDtoa(5.5626846462680035e-309, buffer, &sign, &length, &point); | 47 status = FastDtoa(5.5626846462680035e-309, buffer, &length, &point); |
53 CHECK(status); | 48 CHECK(status); |
54 CHECK_EQ(0, sign); | |
55 CHECK_EQ("5562684646268003", buffer.start()); | 49 CHECK_EQ("5562684646268003", buffer.start()); |
56 CHECK_EQ(-308, point); | 50 CHECK_EQ(-308, point); |
57 | 51 |
58 status = FastDtoa(2147483648.0, buffer, &sign, &length, &point); | 52 status = FastDtoa(2147483648.0, buffer, &length, &point); |
59 CHECK(status); | 53 CHECK(status); |
60 CHECK_EQ(0, sign); | |
61 CHECK_EQ("2147483648", buffer.start()); | 54 CHECK_EQ("2147483648", buffer.start()); |
62 CHECK_EQ(10, point); | 55 CHECK_EQ(10, point); |
63 | 56 |
64 status = FastDtoa(3.5844466002796428e+298, buffer, &sign, &length, &point); | 57 status = FastDtoa(3.5844466002796428e+298, buffer, &length, &point); |
65 if (status) { // Not all FastDtoa variants manage to compute this number. | 58 if (status) { // Not all FastDtoa variants manage to compute this number. |
66 CHECK_EQ("35844466002796428", buffer.start()); | 59 CHECK_EQ("35844466002796428", buffer.start()); |
67 CHECK_EQ(0, sign); | |
68 CHECK_EQ(299, point); | 60 CHECK_EQ(299, point); |
69 } | 61 } |
70 | 62 |
71 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); | 63 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); |
72 double v = Double(smallest_normal64).value(); | 64 double v = Double(smallest_normal64).value(); |
73 status = FastDtoa(v, buffer, &sign, &length, &point); | 65 status = FastDtoa(v, buffer, &length, &point); |
74 if (status) { | 66 if (status) { |
75 CHECK_EQ(0, sign); | |
76 CHECK_EQ("22250738585072014", buffer.start()); | 67 CHECK_EQ("22250738585072014", buffer.start()); |
77 CHECK_EQ(-307, point); | 68 CHECK_EQ(-307, point); |
78 } | 69 } |
79 | 70 |
80 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); | 71 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); |
81 v = Double(largest_denormal64).value(); | 72 v = Double(largest_denormal64).value(); |
82 status = FastDtoa(v, buffer, &sign, &length, &point); | 73 status = FastDtoa(v, buffer, &length, &point); |
83 if (status) { | 74 if (status) { |
84 CHECK_EQ(0, sign); | |
85 CHECK_EQ("2225073858507201", buffer.start()); | 75 CHECK_EQ("2225073858507201", buffer.start()); |
86 CHECK_EQ(-307, point); | 76 CHECK_EQ(-307, point); |
87 } | 77 } |
88 } | 78 } |
89 | 79 |
90 | 80 |
91 TEST(FastDtoaGayShortest) { | 81 TEST(FastDtoaGayShortest) { |
92 char buffer_container[kBufferSize]; | 82 char buffer_container[kBufferSize]; |
93 Vector<char> buffer(buffer_container, kBufferSize); | 83 Vector<char> buffer(buffer_container, kBufferSize); |
94 bool status; | 84 bool status; |
95 int sign; | |
96 int length; | 85 int length; |
97 int point; | 86 int point; |
98 int succeeded = 0; | 87 int succeeded = 0; |
99 int total = 0; | 88 int total = 0; |
100 bool needed_max_length = false; | 89 bool needed_max_length = false; |
101 | 90 |
102 Vector<const GayShortest> precomputed = PrecomputedShortestRepresentations(); | 91 Vector<const PrecomputedShortest> precomputed = |
| 92 PrecomputedShortestRepresentations(); |
103 for (int i = 0; i < precomputed.length(); ++i) { | 93 for (int i = 0; i < precomputed.length(); ++i) { |
104 const GayShortest current_test = precomputed[i]; | 94 const PrecomputedShortest current_test = precomputed[i]; |
105 total++; | 95 total++; |
106 double v = current_test.v; | 96 double v = current_test.v; |
107 status = FastDtoa(v, buffer, &sign, &length, &point); | 97 status = FastDtoa(v, buffer, &length, &point); |
108 CHECK_GE(kFastDtoaMaximalLength, length); | 98 CHECK_GE(kFastDtoaMaximalLength, length); |
109 if (!status) continue; | 99 if (!status) continue; |
110 if (length == kFastDtoaMaximalLength) needed_max_length = true; | 100 if (length == kFastDtoaMaximalLength) needed_max_length = true; |
111 succeeded++; | 101 succeeded++; |
112 CHECK_EQ(0, sign); // All precomputed numbers are positive. | |
113 CHECK_EQ(current_test.decimal_point, point); | 102 CHECK_EQ(current_test.decimal_point, point); |
114 CHECK_EQ(current_test.representation, buffer.start()); | 103 CHECK_EQ(current_test.representation, buffer.start()); |
115 } | 104 } |
116 CHECK_GT(succeeded*1.0/total, 0.99); | 105 CHECK_GT(succeeded*1.0/total, 0.99); |
117 CHECK(needed_max_length); | 106 CHECK(needed_max_length); |
118 } | 107 } |
OLD | NEW |