Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: test/cctest/test-grisu3.cc

Issue 866002: Fast double-to-ascii conversion. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/test-double.cc ('k') | tools/generate-ten-powers.scm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "gay_shortest.h"
11 #include "grisu3.h" 12 #include "grisu3.h"
12 13
13 #include "test-dtoa.h"
14
15 using namespace v8::internal; 14 using namespace v8::internal;
16 15
17 TEST(DoubleExtremes) { 16 static const int kBufferSize = 100;
17
18 TEST(GrisuVariousDoubles) {
18 char buffer[kBufferSize]; 19 char buffer[kBufferSize];
20 int sign;
19 int length; 21 int length;
20 int sign;
21 int point; 22 int point;
22 bool status; 23 int status;
24
23 double min_double = 5e-324; 25 double min_double = 5e-324;
24 status = grisu3(min_double, buffer, &sign, &length, &point); 26 status = grisu3(min_double, buffer, &sign, &length, &point);
25 CHECK(status); 27 CHECK(status);
26 CHECK_EQ(0, sign); 28 CHECK_EQ(0, sign);
27 CHECK_EQ("5", buffer); 29 CHECK_EQ("5", buffer);
28 CHECK_EQ(-323, point); 30 CHECK_EQ(-323, point);
29 31
30 double max_double = 1.7976931348623157e308; 32 double max_double = 1.7976931348623157e308;
31 status = grisu3(max_double, buffer, &sign, &length, &point); 33 status = grisu3(max_double, buffer, &sign, &length, &point);
32 CHECK(status); 34 CHECK(status);
33 CHECK_EQ(0, sign); 35 CHECK_EQ(0, sign);
34 CHECK_EQ("17976931348623157", buffer); 36 CHECK_EQ("17976931348623157", buffer);
35 CHECK_EQ(309, point); 37 CHECK_EQ(309, point);
36 }
37 38
38
39 TEST(DoubleTestFunctions) {
40 char buffer[kBufferSize];
41
42 strncpy(buffer, "12345", kBufferSize);
43 CHECK(IsCorrect(123.45, buffer, 0, 5, 3));
44 strncpy(buffer, "12345", kBufferSize);
45 CHECK(IsCorrect(1.2345, buffer, 0, 5, 1));
46 strncpy(buffer, "12345", kBufferSize);
47 CHECK(!IsCorrect(1.2344, buffer, 0, 5, 1));
48 strncpy(buffer, "12345", kBufferSize);
49 CHECK(!IsCorrect(1.2345, buffer, 0, 5, 2));
50 strncpy(buffer, "12345", kBufferSize);
51 CHECK(!IsCorrect(1.2345, buffer, 0, 4, 1));
52
53 strncpy(buffer, "1234", kBufferSize);
54 CHECK(IsRounded(123.44, buffer, 0, 4, 3));
55 strncpy(buffer, "1234", kBufferSize);
56 CHECK(!IsRounded(123.4500000000001, buffer, 0, 4, 3));
57 strncpy(buffer, "1234", kBufferSize);
58 CHECK(IsRounded(123.44999999, buffer, 0, 4, 3));
59 strncpy(buffer, "1234", kBufferSize);
60 CHECK(IsRounded(123.44999999, buffer, 0, 3, 3));
61
62 strncpy(buffer, "1234567000000000000000000001", kBufferSize);
63 CHECK(IsShortest(123.45, buffer, 0, 5, 3));
64 strncpy(buffer, "1234567000000000000000000001", kBufferSize);
65 CHECK(IsShortest(123.4567, buffer, 0, 7, 3));
66 strncpy(buffer, "1234567000000000000000000001", kBufferSize);
67 CHECK(!IsShortest(123.4567, buffer, 0, strlen(buffer), 3));
68
69 strncpy(buffer, "123456699999999999999999999999999999", kBufferSize);
70 CHECK(!IsShortest(123.4567, buffer, 0, strlen(buffer), 3));
71 strncpy(buffer, "123456699999999999999999999999999999", kBufferSize);
72 CHECK(IsShortest(123.456, buffer, 0, 6, 3));
73 }
74
75
76 TEST(VariousDoubles) {
77 char buffer[kBufferSize];
78 int sign;
79 int length;
80 int point;
81 int status;
82 status = grisu3(4294967272.0, buffer, &sign, &length, &point); 39 status = grisu3(4294967272.0, buffer, &sign, &length, &point);
83 CHECK(status); 40 CHECK(status);
84 CHECK_EQ(0, sign); 41 CHECK_EQ(0, sign);
85 CHECK_EQ("4294967272", buffer); 42 CHECK_EQ("4294967272", buffer);
86 CHECK_EQ(10, point); 43 CHECK_EQ(10, point);
87 44
88 status = grisu3(4.1855804968213567e298, buffer, &sign, &length, &point); 45 status = grisu3(4.1855804968213567e298, buffer, &sign, &length, &point);
89 CHECK(status); 46 CHECK(status);
90 CHECK_EQ(0, sign); 47 CHECK_EQ(0, sign);
91 CHECK_EQ("4185580496821357", buffer); 48 CHECK_EQ("4185580496821357", buffer);
(...skipping 16 matching lines...) Expand all
108 CHECK_EQ("35844466002796428", buffer); 65 CHECK_EQ("35844466002796428", buffer);
109 CHECK_EQ(0, sign); 66 CHECK_EQ(0, sign);
110 CHECK_EQ(299, point); 67 CHECK_EQ(299, point);
111 } 68 }
112 69
113 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); 70 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
114 double v = Double(smallest_normal64).value(); 71 double v = Double(smallest_normal64).value();
115 status = grisu3(v, buffer, &sign, &length, &point); 72 status = grisu3(v, buffer, &sign, &length, &point);
116 if (status) { 73 if (status) {
117 CHECK_EQ(0, sign); 74 CHECK_EQ(0, sign);
118 CHECK(IsCorrect(v, buffer, 0, length, point)); 75 CHECK_EQ("22250738585072014", buffer);
119 CHECK(IsRounded(v, buffer, 0, length, point)); 76 CHECK_EQ(-307, point);
120 CHECK(IsShortest(v, buffer, 0, length, point));
121 } 77 }
122 78
123 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); 79 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
124 v = Double(largest_denormal64).value(); 80 v = Double(largest_denormal64).value();
125 status = grisu3(v, buffer, &sign, &length, &point); 81 status = grisu3(v, buffer, &sign, &length, &point);
126 if (status) { 82 if (status) {
127 CHECK_EQ(0, sign); 83 CHECK_EQ(0, sign);
128 CHECK(IsCorrect(v, buffer, 0, length, point)); 84 CHECK_EQ("2225073858507201", buffer);
129 CHECK(IsRounded(v, buffer, 0, length, point)); 85 CHECK_EQ(-307, point);
130 CHECK(IsShortest(v, buffer, 0, length, point));
131 } 86 }
132 } 87 }
133 88
134 89
135 static double random_double() { 90 TEST(GrisuGayShortest) {
136 uint64_t double64 = 0; 91 char buffer[kBufferSize];
137 for (int i = 0; i < 8; i++) { 92 bool status;
138 double64 <<= 8; 93 int sign;
139 double64 += rand() % 256; // NOLINT 94 int length;
140 } 95 int point;
141 return Double(double64).value();
142 }
143
144
145 TEST(RandomDoubles) {
146 // For a more thorough testing increase the iteration count.
147 // We also check kGrisu3MaximalLength in here.
148 const int kIterationCount = 100000;
149 int succeeded = 0; 96 int succeeded = 0;
150 int total = 0; 97 int total = 0;
151 char buffer[kBufferSize];
152 int length;
153 int sign;
154 int point;
155 bool needed_max_length = false; 98 bool needed_max_length = false;
156 99
157 for (int i = 0; i < kIterationCount; ++i) { 100 Vector<const GayShortest> precomputed = PrecomputedShortestRepresentations();
158 double v = random_double(); 101 for (int i = 0; i < precomputed.length(); ++i) {
159 if (v != v) continue; // NaN 102 const GayShortest current_test = precomputed[i];
160 if (v == 0.0) continue;
161 if (v < 0) v = -v;
162 total++; 103 total++;
163 int status = grisu3(v, buffer, &sign, &length, &point); 104 double v = current_test.v;
105 status = grisu3(v, buffer, &sign, &length, &point);
164 CHECK_GE(kGrisu3MaximalLength, length); 106 CHECK_GE(kGrisu3MaximalLength, length);
107 if (!status) continue;
165 if (length == kGrisu3MaximalLength) needed_max_length = true; 108 if (length == kGrisu3MaximalLength) needed_max_length = true;
166 if (!status) continue;
167 succeeded++; 109 succeeded++;
168 CHECK(IsCorrect(v, buffer, 0, length, point)); 110 CHECK_EQ(0, sign); // All precomputed numbers are positive.
169 CHECK(IsRounded(v, buffer, 0, length, point)); 111 CHECK_EQ(current_test.decimal_point, point);
170 CHECK(IsShortest(v, buffer, 0, length, point)); 112 CHECK_EQ(current_test.representation, buffer);
171 } 113 }
172 CHECK_GT(succeeded*1.0/total, 0.99); 114 CHECK_GT(succeeded*1.0/total, 0.99);
173 CHECK(needed_max_length); 115 CHECK(needed_max_length);
174 } 116 }
OLDNEW
« no previous file with comments | « test/cctest/test-double.cc ('k') | tools/generate-ten-powers.scm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698