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

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa.cpp

Issue 1436153002: Apply clang-format with Chromium-style without column limit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa.h ('k') | third_party/WebKit/Source/wtf/dtoa/bignum.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /**************************************************************** 1 /****************************************************************
2 * 2 *
3 * The author of this software is David M. Gay. 3 * The author of this software is David M. Gay.
4 * 4 *
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. 5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights reserved. 6 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights reserved.
7 * 7 *
8 * Permission to use, copy, modify, and distribute this software for any 8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose without fee is hereby granted, provided that this entire notice 9 * purpose without fee is hereby granted, provided that this entire notice
10 * is included in all copies of any software which is or includes a copy 10 * is included in all copies of any software which is or includes a copy
(...skipping 25 matching lines...) Expand all
36 #include "dtoa.h" 36 #include "dtoa.h"
37 37
38 #include "wtf/CPU.h" 38 #include "wtf/CPU.h"
39 #include "wtf/MathExtras.h" 39 #include "wtf/MathExtras.h"
40 #include "wtf/ThreadingPrimitives.h" 40 #include "wtf/ThreadingPrimitives.h"
41 #include "wtf/Vector.h" 41 #include "wtf/Vector.h"
42 42
43 #include <string.h> 43 #include <string.h>
44 44
45 #if COMPILER(MSVC) 45 #if COMPILER(MSVC)
46 #pragma warning(disable: 4244) 46 #pragma warning(disable : 4244)
47 #pragma warning(disable: 4245) 47 #pragma warning(disable : 4245)
48 #pragma warning(disable: 4554) 48 #pragma warning(disable : 4554)
49 #endif 49 #endif
50 50
51 namespace WTF { 51 namespace WTF {
52 52
53 Mutex* s_dtoaP5Mutex; 53 Mutex* s_dtoaP5Mutex;
54 54
55 typedef union { 55 typedef union {
56 double d; 56 double d;
57 uint32_t L[2]; 57 uint32_t L[2];
58 } U; 58 } U;
59 59
60 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN) 60 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
61 #define word0(x) (x)->L[0] 61 #define word0(x) (x)->L[0]
62 #define word1(x) (x)->L[1] 62 #define word1(x) (x)->L[1]
63 #else 63 #else
64 #define word0(x) (x)->L[1] 64 #define word0(x) (x)->L[1]
65 #define word1(x) (x)->L[0] 65 #define word1(x) (x)->L[0]
66 #endif 66 #endif
67 #define dval(x) (x)->d 67 #define dval(x) (x)->d
68 68
69 #define Exp_shift 20 69 #define Exp_shift 20
70 #define Exp_shift1 20 70 #define Exp_shift1 20
71 #define Exp_msk1 0x100000 71 #define Exp_msk1 0x100000
72 #define Exp_msk11 0x100000 72 #define Exp_msk11 0x100000
73 #define Exp_mask 0x7ff00000 73 #define Exp_mask 0x7ff00000
74 #define P 53 74 #define P 53
75 #define Bias 1023 75 #define Bias 1023
76 #define Emin (-1022) 76 #define Emin (-1022)
77 #define Exp_1 0x3ff00000 77 #define Exp_1 0x3ff00000
78 #define Exp_11 0x3ff00000 78 #define Exp_11 0x3ff00000
79 #define Ebits 11 79 #define Ebits 11
80 #define Frac_mask 0xfffff 80 #define Frac_mask 0xfffff
81 #define Frac_mask1 0xfffff 81 #define Frac_mask1 0xfffff
82 #define Ten_pmax 22 82 #define Ten_pmax 22
83 #define Bletch 0x10 83 #define Bletch 0x10
84 #define Bndry_mask 0xfffff 84 #define Bndry_mask 0xfffff
85 #define Bndry_mask1 0xfffff 85 #define Bndry_mask1 0xfffff
86 #define LSB 1 86 #define LSB 1
87 #define Sign_bit 0x80000000 87 #define Sign_bit 0x80000000
88 #define Log2P 1 88 #define Log2P 1
89 #define Tiny0 0 89 #define Tiny0 0
90 #define Tiny1 1 90 #define Tiny1 1
91 #define Quick_max 14 91 #define Quick_max 14
92 #define Int_max 14 92 #define Int_max 14
93 93
94 #define rounded_product(a, b) a *= b 94 #define rounded_product(a, b) a *= b
95 #define rounded_quotient(a, b) a /= b 95 #define rounded_quotient(a, b) a /= b
96 96
97 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) 97 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
98 #define Big1 0xffffffff 98 #define Big1 0xffffffff
99 99
100 #if CPU(X86_64) 100 #if CPU(X86_64)
101 // FIXME: should we enable this on all 64-bit CPUs? 101 // FIXME: should we enable this on all 64-bit CPUs?
102 // 64-bit emulation provided by the compiler is likely to be slower than dtoa ow n code on 32-bit hardware. 102 // 64-bit emulation provided by the compiler is likely to be slower than dtoa ow n code on 32-bit hardware.
103 #define USE_LONG_LONG 103 #define USE_LONG_LONG
104 #endif 104 #endif
105 105
106 #ifndef USE_LONG_LONG 106 #ifndef USE_LONG_LONG
107 /* The following definition of Storeinc is appropriate for MIPS processors. 107 /* The following definition of Storeinc is appropriate for MIPS processors.
108 * An alternative that might be better on some machines is 108 * An alternative that might be better on some machines is
109 * *p++ = high << 16 | low & 0xffff; 109 * *p++ = high << 16 | low & 0xffff;
110 */ 110 */
111 static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low ) 111 static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low ) {
112 { 112 uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
113 uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
114 #if CPU(BIG_ENDIAN) 113 #if CPU(BIG_ENDIAN)
115 p16[0] = high; 114 p16[0] = high;
116 p16[1] = low; 115 p16[1] = low;
117 #else 116 #else
118 p16[1] = high; 117 p16[1] = high;
119 p16[0] = low; 118 p16[0] = low;
120 #endif 119 #endif
121 return p + 1; 120 return p + 1;
122 } 121 }
123 #endif 122 #endif
124 123
125 struct BigInt { 124 struct BigInt {
126 BigInt() : sign(0) { } 125 BigInt()
127 int sign; 126 : sign(0) {}
128 127 int sign;
129 void clear() 128
130 { 129 void clear() {
131 sign = 0; 130 sign = 0;
132 m_words.clear(); 131 m_words.clear();
133 } 132 }
134 133
135 size_t size() const 134 size_t size() const {
136 { 135 return m_words.size();
137 return m_words.size(); 136 }
138 } 137
139 138 void resize(size_t s) {
140 void resize(size_t s) 139 m_words.resize(s);
141 { 140 }
142 m_words.resize(s); 141
143 } 142 uint32_t* words() {
144 143 return m_words.data();
145 uint32_t* words() 144 }
146 { 145
147 return m_words.data(); 146 const uint32_t* words() const {
148 } 147 return m_words.data();
149 148 }
150 const uint32_t* words() const 149
151 { 150 void append(uint32_t w) {
152 return m_words.data(); 151 m_words.append(w);
153 } 152 }
154 153
155 void append(uint32_t w) 154 Vector<uint32_t, 16> m_words;
156 {
157 m_words.append(w);
158 }
159
160 Vector<uint32_t, 16> m_words;
161 }; 155 };
162 156
163 static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */ 157 static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */
164 { 158 {
165 #ifdef USE_LONG_LONG 159 #ifdef USE_LONG_LONG
166 unsigned long long carry; 160 unsigned long long carry;
167 #else 161 #else
168 uint32_t carry; 162 uint32_t carry;
169 #endif 163 #endif
170 164
171 int wds = b.size(); 165 int wds = b.size();
172 uint32_t* x = b.words(); 166 uint32_t* x = b.words();
173 int i = 0; 167 int i = 0;
174 carry = a; 168 carry = a;
175 do { 169 do {
176 #ifdef USE_LONG_LONG 170 #ifdef USE_LONG_LONG
177 unsigned long long y = *x * (unsigned long long)m + carry; 171 unsigned long long y = *x * (unsigned long long)m + carry;
178 carry = y >> 32; 172 carry = y >> 32;
179 *x++ = (uint32_t)y & 0xffffffffUL; 173 *x++ = (uint32_t)y & 0xffffffffUL;
180 #else 174 #else
181 uint32_t xi = *x; 175 uint32_t xi = *x;
182 uint32_t y = (xi & 0xffff) * m + carry; 176 uint32_t y = (xi & 0xffff) * m + carry;
183 uint32_t z = (xi >> 16) * m + (y >> 16); 177 uint32_t z = (xi >> 16) * m + (y >> 16);
178 carry = z >> 16;
179 *x++ = (z << 16) + (y & 0xffff);
180 #endif
181 } while (++i < wds);
182
183 if (carry)
184 b.append((uint32_t)carry);
185 }
186
187 static int hi0bits(uint32_t x) {
188 int k = 0;
189
190 if (!(x & 0xffff0000)) {
191 k = 16;
192 x <<= 16;
193 }
194 if (!(x & 0xff000000)) {
195 k += 8;
196 x <<= 8;
197 }
198 if (!(x & 0xf0000000)) {
199 k += 4;
200 x <<= 4;
201 }
202 if (!(x & 0xc0000000)) {
203 k += 2;
204 x <<= 2;
205 }
206 if (!(x & 0x80000000)) {
207 k++;
208 if (!(x & 0x40000000))
209 return 32;
210 }
211 return k;
212 }
213
214 static int lo0bits(uint32_t* y) {
215 int k;
216 uint32_t x = *y;
217
218 if (x & 7) {
219 if (x & 1)
220 return 0;
221 if (x & 2) {
222 *y = x >> 1;
223 return 1;
224 }
225 *y = x >> 2;
226 return 2;
227 }
228 k = 0;
229 if (!(x & 0xffff)) {
230 k = 16;
231 x >>= 16;
232 }
233 if (!(x & 0xff)) {
234 k += 8;
235 x >>= 8;
236 }
237 if (!(x & 0xf)) {
238 k += 4;
239 x >>= 4;
240 }
241 if (!(x & 0x3)) {
242 k += 2;
243 x >>= 2;
244 }
245 if (!(x & 1)) {
246 k++;
247 x >>= 1;
248 if (!x)
249 return 32;
250 }
251 *y = x;
252 return k;
253 }
254
255 static void i2b(BigInt& b, int i) {
256 b.sign = 0;
257 b.resize(1);
258 b.words()[0] = i;
259 }
260
261 static void mult(BigInt& aRef, const BigInt& bRef) {
262 const BigInt* a = &aRef;
263 const BigInt* b = &bRef;
264 BigInt c;
265 int wa, wb, wc;
266 const uint32_t* x = 0;
267 const uint32_t* xa;
268 const uint32_t* xb;
269 const uint32_t* xae;
270 const uint32_t* xbe;
271 uint32_t* xc;
272 uint32_t* xc0;
273 uint32_t y;
274 #ifdef USE_LONG_LONG
275 unsigned long long carry, z;
276 #else
277 uint32_t carry, z;
278 #endif
279
280 if (a->size() < b->size()) {
281 const BigInt* tmp = a;
282 a = b;
283 b = tmp;
284 }
285
286 wa = a->size();
287 wb = b->size();
288 wc = wa + wb;
289 c.resize(wc);
290
291 for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
292 *xc = 0;
293 xa = a->words();
294 xae = xa + wa;
295 xb = b->words();
296 xbe = xb + wb;
297 xc0 = c.words();
298 #ifdef USE_LONG_LONG
299 for (; xb < xbe; xc0++) {
300 if ((y = *xb++)) {
301 x = xa;
302 xc = xc0;
303 carry = 0;
304 do {
305 z = *x++ * (unsigned long long)y + *xc + carry;
306 carry = z >> 32;
307 *xc++ = (uint32_t)z & 0xffffffffUL;
308 } while (x < xae);
309 *xc = (uint32_t)carry;
310 }
311 }
312 #else
313 for (; xb < xbe; xb++, xc0++) {
314 if ((y = *xb & 0xffff)) {
315 x = xa;
316 xc = xc0;
317 carry = 0;
318 do {
319 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
184 carry = z >> 16; 320 carry = z >> 16;
185 *x++ = (z << 16) + (y & 0xffff); 321 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
186 #endif 322 carry = z2 >> 16;
187 } while (++i < wds); 323 xc = storeInc(xc, z2, z);
188 324 } while (x < xae);
189 if (carry) 325 *xc = carry;
190 b.append((uint32_t)carry); 326 }
191 } 327 if ((y = *xb >> 16)) {
192 328 x = xa;
193 static int hi0bits(uint32_t x) 329 xc = xc0;
194 { 330 carry = 0;
195 int k = 0; 331 uint32_t z2 = *xc;
196 332 do {
197 if (!(x & 0xffff0000)) { 333 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
198 k = 16; 334 carry = z >> 16;
199 x <<= 16; 335 xc = storeInc(xc, z, z2);
200 } 336 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
201 if (!(x & 0xff000000)) { 337 carry = z2 >> 16;
202 k += 8; 338 } while (x < xae);
203 x <<= 8; 339 *xc = z2;
204 } 340 }
205 if (!(x & 0xf0000000)) { 341 }
206 k += 4; 342 #endif
207 x <<= 4; 343 for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) {
208 } 344 }
209 if (!(x & 0xc0000000)) { 345 c.resize(wc);
210 k += 2; 346 aRef = c;
211 x <<= 2;
212 }
213 if (!(x & 0x80000000)) {
214 k++;
215 if (!(x & 0x40000000))
216 return 32;
217 }
218 return k;
219 }
220
221 static int lo0bits(uint32_t* y)
222 {
223 int k;
224 uint32_t x = *y;
225
226 if (x & 7) {
227 if (x & 1)
228 return 0;
229 if (x & 2) {
230 *y = x >> 1;
231 return 1;
232 }
233 *y = x >> 2;
234 return 2;
235 }
236 k = 0;
237 if (!(x & 0xffff)) {
238 k = 16;
239 x >>= 16;
240 }
241 if (!(x & 0xff)) {
242 k += 8;
243 x >>= 8;
244 }
245 if (!(x & 0xf)) {
246 k += 4;
247 x >>= 4;
248 }
249 if (!(x & 0x3)) {
250 k += 2;
251 x >>= 2;
252 }
253 if (!(x & 1)) {
254 k++;
255 x >>= 1;
256 if (!x)
257 return 32;
258 }
259 *y = x;
260 return k;
261 }
262
263 static void i2b(BigInt& b, int i)
264 {
265 b.sign = 0;
266 b.resize(1);
267 b.words()[0] = i;
268 }
269
270 static void mult(BigInt& aRef, const BigInt& bRef)
271 {
272 const BigInt* a = &aRef;
273 const BigInt* b = &bRef;
274 BigInt c;
275 int wa, wb, wc;
276 const uint32_t* x = 0;
277 const uint32_t* xa;
278 const uint32_t* xb;
279 const uint32_t* xae;
280 const uint32_t* xbe;
281 uint32_t* xc;
282 uint32_t* xc0;
283 uint32_t y;
284 #ifdef USE_LONG_LONG
285 unsigned long long carry, z;
286 #else
287 uint32_t carry, z;
288 #endif
289
290 if (a->size() < b->size()) {
291 const BigInt* tmp = a;
292 a = b;
293 b = tmp;
294 }
295
296 wa = a->size();
297 wb = b->size();
298 wc = wa + wb;
299 c.resize(wc);
300
301 for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
302 *xc = 0;
303 xa = a->words();
304 xae = xa + wa;
305 xb = b->words();
306 xbe = xb + wb;
307 xc0 = c.words();
308 #ifdef USE_LONG_LONG
309 for (; xb < xbe; xc0++) {
310 if ((y = *xb++)) {
311 x = xa;
312 xc = xc0;
313 carry = 0;
314 do {
315 z = *x++ * (unsigned long long)y + *xc + carry;
316 carry = z >> 32;
317 *xc++ = (uint32_t)z & 0xffffffffUL;
318 } while (x < xae);
319 *xc = (uint32_t)carry;
320 }
321 }
322 #else
323 for (; xb < xbe; xb++, xc0++) {
324 if ((y = *xb & 0xffff)) {
325 x = xa;
326 xc = xc0;
327 carry = 0;
328 do {
329 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
330 carry = z >> 16;
331 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
332 carry = z2 >> 16;
333 xc = storeInc(xc, z2, z);
334 } while (x < xae);
335 *xc = carry;
336 }
337 if ((y = *xb >> 16)) {
338 x = xa;
339 xc = xc0;
340 carry = 0;
341 uint32_t z2 = *xc;
342 do {
343 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
344 carry = z >> 16;
345 xc = storeInc(xc, z, z2);
346 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
347 carry = z2 >> 16;
348 } while (x < xae);
349 *xc = z2;
350 }
351 }
352 #endif
353 for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
354 c.resize(wc);
355 aRef = c;
356 } 347 }
357 348
358 struct P5Node { 349 struct P5Node {
359 WTF_MAKE_NONCOPYABLE(P5Node); USING_FAST_MALLOC(P5Node); 350 WTF_MAKE_NONCOPYABLE(P5Node);
360 public: 351 USING_FAST_MALLOC(P5Node);
361 P5Node() { } 352
362 BigInt val; 353 public:
363 P5Node* next; 354 P5Node() {}
355 BigInt val;
356 P5Node* next;
364 }; 357 };
365 358
366 static P5Node* p5s; 359 static P5Node* p5s;
367 static int p5sCount; 360 static int p5sCount;
368 361
369 static ALWAYS_INLINE void pow5mult(BigInt& b, int k) 362 static ALWAYS_INLINE void pow5mult(BigInt& b, int k) {
370 { 363 static int p05[3] = {5, 25, 125};
371 static int p05[3] = { 5, 25, 125 }; 364
372 365 if (int i = k & 3)
373 if (int i = k & 3) 366 multadd(b, p05[i - 1], 0);
374 multadd(b, p05[i - 1], 0); 367
375 368 if (!(k >>= 2))
376 if (!(k >>= 2)) 369 return;
377 return; 370
378 371 s_dtoaP5Mutex->lock();
379 s_dtoaP5Mutex->lock(); 372 P5Node* p5 = p5s;
380 P5Node* p5 = p5s; 373
381 374 if (!p5) {
382 if (!p5) { 375 /* first time */
383 /* first time */ 376 p5 = new P5Node;
384 p5 = new P5Node; 377 i2b(p5->val, 625);
385 i2b(p5->val, 625); 378 p5->next = 0;
386 p5->next = 0; 379 p5s = p5;
387 p5s = p5; 380 p5sCount = 1;
388 p5sCount = 1; 381 }
389 } 382
390 383 int p5sCountLocal = p5sCount;
391 int p5sCountLocal = p5sCount; 384 s_dtoaP5Mutex->unlock();
392 s_dtoaP5Mutex->unlock(); 385 int p5sUsed = 0;
393 int p5sUsed = 0; 386
394 387 for (;;) {
395 for (;;) { 388 if (k & 1)
396 if (k & 1) 389 mult(b, p5->val);
397 mult(b, p5->val); 390
398 391 if (!(k >>= 1))
399 if (!(k >>= 1)) 392 break;
400 break; 393
401 394 if (++p5sUsed == p5sCountLocal) {
402 if (++p5sUsed == p5sCountLocal) { 395 s_dtoaP5Mutex->lock();
403 s_dtoaP5Mutex->lock(); 396 if (p5sUsed == p5sCount) {
404 if (p5sUsed == p5sCount) { 397 ASSERT(!p5->next);
405 ASSERT(!p5->next); 398 p5->next = new P5Node;
406 p5->next = new P5Node; 399 p5->next->next = 0;
407 p5->next->next = 0; 400 p5->next->val = p5->val;
408 p5->next->val = p5->val; 401 mult(p5->next->val, p5->next->val);
409 mult(p5->next->val, p5->next->val); 402 ++p5sCount;
410 ++p5sCount; 403 }
411 } 404
412 405 p5sCountLocal = p5sCount;
413 p5sCountLocal = p5sCount; 406 s_dtoaP5Mutex->unlock();
414 s_dtoaP5Mutex->unlock(); 407 }
415 } 408 p5 = p5->next;
416 p5 = p5->next; 409 }
417 } 410 }
418 } 411
419 412 static ALWAYS_INLINE void lshift(BigInt& b, int k) {
420 static ALWAYS_INLINE void lshift(BigInt& b, int k) 413 int n = k >> 5;
421 { 414
422 int n = k >> 5; 415 int origSize = b.size();
423 416 int n1 = n + origSize + 1;
424 int origSize = b.size(); 417
425 int n1 = n + origSize + 1; 418 if (k &= 0x1f)
426 419 b.resize(b.size() + n + 1);
427 if (k &= 0x1f) 420 else
428 b.resize(b.size() + n + 1); 421 b.resize(b.size() + n);
429 else 422
430 b.resize(b.size() + n); 423 const uint32_t* srcStart = b.words();
431 424 uint32_t* dstStart = b.words();
432 const uint32_t* srcStart = b.words(); 425 const uint32_t* src = srcStart + origSize - 1;
433 uint32_t* dstStart = b.words(); 426 uint32_t* dst = dstStart + n1 - 1;
434 const uint32_t* src = srcStart + origSize - 1; 427 if (k) {
435 uint32_t* dst = dstStart + n1 - 1; 428 uint32_t hiSubword = 0;
436 if (k) { 429 int s = 32 - k;
437 uint32_t hiSubword = 0; 430 for (; src >= srcStart; --src) {
438 int s = 32 - k; 431 *dst-- = hiSubword | *src >> s;
439 for (; src >= srcStart; --src) { 432 hiSubword = *src << k;
440 *dst-- = hiSubword | *src >> s; 433 }
441 hiSubword = *src << k; 434 *dst = hiSubword;
442 } 435 ASSERT(dst == dstStart + n);
443 *dst = hiSubword; 436
444 ASSERT(dst == dstStart + n); 437 b.resize(origSize + n + !!b.words()[n1 - 1]);
445 438 } else {
446 b.resize(origSize + n + !!b.words()[n1 - 1]); 439 do {
447 } 440 *--dst = *src--;
448 else { 441 } while (src >= srcStart);
449 do { 442 }
450 *--dst = *src--; 443 for (dst = dstStart + n; dst != dstStart;)
451 } while (src >= srcStart); 444 *--dst = 0;
452 } 445
453 for (dst = dstStart + n; dst != dstStart; ) 446 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
454 *--dst = 0; 447 }
455 448
456 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]); 449 static int cmp(const BigInt& a, const BigInt& b) {
457 } 450 const uint32_t *xa, *xa0, *xb, *xb0;
458 451 int i, j;
459 static int cmp(const BigInt& a, const BigInt& b) 452
460 { 453 i = a.size();
461 const uint32_t *xa, *xa0, *xb, *xb0; 454 j = b.size();
462 int i, j; 455 ASSERT(i <= 1 || a.words()[i - 1]);
463 456 ASSERT(j <= 1 || b.words()[j - 1]);
464 i = a.size(); 457 if (i -= j)
465 j = b.size(); 458 return i;
466 ASSERT(i <= 1 || a.words()[i - 1]); 459 xa0 = a.words();
467 ASSERT(j <= 1 || b.words()[j - 1]); 460 xa = xa0 + j;
468 if (i -= j) 461 xb0 = b.words();
469 return i; 462 xb = xb0 + j;
470 xa0 = a.words(); 463 for (;;) {
471 xa = xa0 + j; 464 if (*--xa != *--xb)
472 xb0 = b.words(); 465 return *xa < *xb ? -1 : 1;
473 xb = xb0 + j; 466 if (xa <= xa0)
474 for (;;) { 467 break;
475 if (*--xa != *--xb) 468 }
476 return *xa < *xb ? -1 : 1; 469 return 0;
477 if (xa <= xa0) 470 }
478 break; 471
479 } 472 static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef ) {
480 return 0; 473 const BigInt* a = &aRef;
481 } 474 const BigInt* b = &bRef;
482 475 int i, wa, wb;
483 static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef ) 476 uint32_t* xc;
484 { 477
485 const BigInt* a = &aRef; 478 i = cmp(*a, *b);
486 const BigInt* b = &bRef; 479 if (!i) {
487 int i, wa, wb; 480 c.sign = 0;
488 uint32_t* xc; 481 c.resize(1);
489 482 c.words()[0] = 0;
490 i = cmp(*a, *b); 483 return;
491 if (!i) { 484 }
492 c.sign = 0; 485 if (i < 0) {
493 c.resize(1); 486 const BigInt* tmp = a;
494 c.words()[0] = 0; 487 a = b;
495 return; 488 b = tmp;
496 } 489 i = 1;
497 if (i < 0) { 490 } else
498 const BigInt* tmp = a; 491 i = 0;
499 a = b; 492
500 b = tmp; 493 wa = a->size();
501 i = 1; 494 const uint32_t* xa = a->words();
502 } else 495 const uint32_t* xae = xa + wa;
503 i = 0; 496 wb = b->size();
504 497 const uint32_t* xb = b->words();
505 wa = a->size(); 498 const uint32_t* xbe = xb + wb;
506 const uint32_t* xa = a->words(); 499
507 const uint32_t* xae = xa + wa; 500 c.resize(wa);
508 wb = b->size(); 501 c.sign = i;
509 const uint32_t* xb = b->words(); 502 xc = c.words();
510 const uint32_t* xbe = xb + wb;
511
512 c.resize(wa);
513 c.sign = i;
514 xc = c.words();
515 #ifdef USE_LONG_LONG 503 #ifdef USE_LONG_LONG
516 unsigned long long borrow = 0; 504 unsigned long long borrow = 0;
517 do { 505 do {
518 unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow; 506 unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
519 borrow = y >> 32 & (uint32_t)1; 507 borrow = y >> 32 & (uint32_t)1;
520 *xc++ = (uint32_t)y & 0xffffffffUL; 508 *xc++ = (uint32_t)y & 0xffffffffUL;
521 } while (xb < xbe); 509 } while (xb < xbe);
522 while (xa < xae) { 510 while (xa < xae) {
523 unsigned long long y = *xa++ - borrow; 511 unsigned long long y = *xa++ - borrow;
524 borrow = y >> 32 & (uint32_t)1; 512 borrow = y >> 32 & (uint32_t)1;
525 *xc++ = (uint32_t)y & 0xffffffffUL; 513 *xc++ = (uint32_t)y & 0xffffffffUL;
526 } 514 }
527 #else 515 #else
528 uint32_t borrow = 0; 516 uint32_t borrow = 0;
529 do { 517 do {
530 uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; 518 uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
531 borrow = (y & 0x10000) >> 16; 519 borrow = (y & 0x10000) >> 16;
532 uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; 520 uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
533 borrow = (z & 0x10000) >> 16; 521 borrow = (z & 0x10000) >> 16;
534 xc = storeInc(xc, z, y); 522 xc = storeInc(xc, z, y);
535 } while (xb < xbe); 523 } while (xb < xbe);
536 while (xa < xae) { 524 while (xa < xae) {
537 uint32_t y = (*xa & 0xffff) - borrow; 525 uint32_t y = (*xa & 0xffff) - borrow;
538 borrow = (y & 0x10000) >> 16; 526 borrow = (y & 0x10000) >> 16;
539 uint32_t z = (*xa++ >> 16) - borrow; 527 uint32_t z = (*xa++ >> 16) - borrow;
540 borrow = (z & 0x10000) >> 16; 528 borrow = (z & 0x10000) >> 16;
541 xc = storeInc(xc, z, y); 529 xc = storeInc(xc, z, y);
542 } 530 }
543 #endif 531 #endif
544 while (!*--xc) 532 while (!*--xc)
545 wa--; 533 wa--;
546 c.resize(wa); 534 c.resize(wa);
547 } 535 }
548 536
549 static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits) 537 static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits) {
550 { 538 int de, k;
551 int de, k; 539 uint32_t* x;
552 uint32_t* x; 540 uint32_t y, z;
553 uint32_t y, z; 541 int i;
554 int i;
555 #define d0 word0(d) 542 #define d0 word0(d)
556 #define d1 word1(d) 543 #define d1 word1(d)
557 544
558 b.sign = 0; 545 b.sign = 0;
546 b.resize(1);
547 x = b.words();
548
549 z = d0 & Frac_mask;
550 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
551 if ((de = (int)(d0 >> Exp_shift)))
552 z |= Exp_msk1;
553 if ((y = d1)) {
554 if ((k = lo0bits(&y))) {
555 x[0] = y | (z << (32 - k));
556 z >>= k;
557 } else
558 x[0] = y;
559 if (z) {
560 b.resize(2);
561 x[1] = z;
562 }
563
564 i = b.size();
565 } else {
566 k = lo0bits(&z);
567 x[0] = z;
568 i = 1;
559 b.resize(1); 569 b.resize(1);
560 x = b.words(); 570 k += 32;
561 571 }
562 z = d0 & Frac_mask; 572 if (de) {
563 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ 573 *e = de - Bias - (P - 1) + k;
564 if ((de = (int)(d0 >> Exp_shift))) 574 *bits = P - k;
565 z |= Exp_msk1; 575 } else {
566 if ((y = d1)) { 576 *e = 0 - Bias - (P - 1) + 1 + k;
567 if ((k = lo0bits(&y))) { 577 *bits = (32 * i) - hi0bits(x[i - 1]);
568 x[0] = y | (z << (32 - k)); 578 }
569 z >>= k;
570 } else
571 x[0] = y;
572 if (z) {
573 b.resize(2);
574 x[1] = z;
575 }
576
577 i = b.size();
578 } else {
579 k = lo0bits(&z);
580 x[0] = z;
581 i = 1;
582 b.resize(1);
583 k += 32;
584 }
585 if (de) {
586 *e = de - Bias - (P - 1) + k;
587 *bits = P - k;
588 } else {
589 *e = 0 - Bias - (P - 1) + 1 + k;
590 *bits = (32 * i) - hi0bits(x[i - 1]);
591 }
592 } 579 }
593 #undef d0 580 #undef d0
594 #undef d1 581 #undef d1
595 582
596 static const double tens[] = { 583 static const double tens[] = {
597 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 584 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
598 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 585 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
599 1e20, 1e21, 1e22 586 1e20, 1e21, 1e22};
600 };
601 587
602 static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; 588 static const double bigtens[] = {1e16, 1e32, 1e64, 1e128, 1e256};
603 589
604 #define Scale_Bit 0x10 590 #define Scale_Bit 0x10
605 #define n_bigtens 5 591 #define n_bigtens 5
606 592
607 static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S) 593 static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S) {
608 { 594 size_t n;
609 size_t n; 595 uint32_t* bx;
610 uint32_t* bx; 596 uint32_t* bxe;
611 uint32_t* bxe; 597 uint32_t q;
612 uint32_t q; 598 uint32_t* sx;
613 uint32_t* sx; 599 uint32_t* sxe;
614 uint32_t* sxe;
615 #ifdef USE_LONG_LONG 600 #ifdef USE_LONG_LONG
616 unsigned long long borrow, carry, y, ys; 601 unsigned long long borrow, carry, y, ys;
617 #else 602 #else
618 uint32_t borrow, carry, y, ys; 603 uint32_t borrow, carry, y, ys;
619 uint32_t si, z, zs; 604 uint32_t si, z, zs;
620 #endif 605 #endif
621 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]); 606 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
622 ASSERT(S.size() <= 1 || S.words()[S.size() - 1]); 607 ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
623 608
624 n = S.size(); 609 n = S.size();
625 ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem"); 610 ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
626 if (b.size() < n) 611 if (b.size() < n)
627 return 0; 612 return 0;
613 sx = S.words();
614 sxe = sx + --n;
615 bx = b.words();
616 bxe = bx + n;
617 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
618 ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
619 if (q) {
620 borrow = 0;
621 carry = 0;
622 do {
623 #ifdef USE_LONG_LONG
624 ys = *sx++ * (unsigned long long)q + carry;
625 carry = ys >> 32;
626 y = *bx - (ys & 0xffffffffUL) - borrow;
627 borrow = y >> 32 & (uint32_t)1;
628 *bx++ = (uint32_t)y & 0xffffffffUL;
629 #else
630 si = *sx++;
631 ys = (si & 0xffff) * q + carry;
632 zs = (si >> 16) * q + (ys >> 16);
633 carry = zs >> 16;
634 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
635 borrow = (y & 0x10000) >> 16;
636 z = (*bx >> 16) - (zs & 0xffff) - borrow;
637 borrow = (z & 0x10000) >> 16;
638 bx = storeInc(bx, z, y);
639 #endif
640 } while (sx <= sxe);
641 if (!*bxe) {
642 bx = b.words();
643 while (--bxe > bx && !*bxe)
644 --n;
645 b.resize(n);
646 }
647 }
648 if (cmp(b, S) >= 0) {
649 q++;
650 borrow = 0;
651 carry = 0;
652 bx = b.words();
628 sx = S.words(); 653 sx = S.words();
629 sxe = sx + --n; 654 do {
655 #ifdef USE_LONG_LONG
656 ys = *sx++ + carry;
657 carry = ys >> 32;
658 y = *bx - (ys & 0xffffffffUL) - borrow;
659 borrow = y >> 32 & (uint32_t)1;
660 *bx++ = (uint32_t)y & 0xffffffffUL;
661 #else
662 si = *sx++;
663 ys = (si & 0xffff) + carry;
664 zs = (si >> 16) + (ys >> 16);
665 carry = zs >> 16;
666 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
667 borrow = (y & 0x10000) >> 16;
668 z = (*bx >> 16) - (zs & 0xffff) - borrow;
669 borrow = (z & 0x10000) >> 16;
670 bx = storeInc(bx, z, y);
671 #endif
672 } while (sx <= sxe);
630 bx = b.words(); 673 bx = b.words();
631 bxe = bx + n; 674 bxe = bx + n;
632 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ 675 if (!*bxe) {
633 ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem"); 676 while (--bxe > bx && !*bxe)
634 if (q) { 677 --n;
635 borrow = 0; 678 b.resize(n);
636 carry = 0;
637 do {
638 #ifdef USE_LONG_LONG
639 ys = *sx++ * (unsigned long long)q + carry;
640 carry = ys >> 32;
641 y = *bx - (ys & 0xffffffffUL) - borrow;
642 borrow = y >> 32 & (uint32_t)1;
643 *bx++ = (uint32_t)y & 0xffffffffUL;
644 #else
645 si = *sx++;
646 ys = (si & 0xffff) * q + carry;
647 zs = (si >> 16) * q + (ys >> 16);
648 carry = zs >> 16;
649 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
650 borrow = (y & 0x10000) >> 16;
651 z = (*bx >> 16) - (zs & 0xffff) - borrow;
652 borrow = (z & 0x10000) >> 16;
653 bx = storeInc(bx, z, y);
654 #endif
655 } while (sx <= sxe);
656 if (!*bxe) {
657 bx = b.words();
658 while (--bxe > bx && !*bxe)
659 --n;
660 b.resize(n);
661 }
662 } 679 }
663 if (cmp(b, S) >= 0) { 680 }
664 q++; 681 return q;
665 borrow = 0;
666 carry = 0;
667 bx = b.words();
668 sx = S.words();
669 do {
670 #ifdef USE_LONG_LONG
671 ys = *sx++ + carry;
672 carry = ys >> 32;
673 y = *bx - (ys & 0xffffffffUL) - borrow;
674 borrow = y >> 32 & (uint32_t)1;
675 *bx++ = (uint32_t)y & 0xffffffffUL;
676 #else
677 si = *sx++;
678 ys = (si & 0xffff) + carry;
679 zs = (si >> 16) + (ys >> 16);
680 carry = zs >> 16;
681 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
682 borrow = (y & 0x10000) >> 16;
683 z = (*bx >> 16) - (zs & 0xffff) - borrow;
684 borrow = (z & 0x10000) >> 16;
685 bx = storeInc(bx, z, y);
686 #endif
687 } while (sx <= sxe);
688 bx = b.words();
689 bxe = bx + n;
690 if (!*bxe) {
691 while (--bxe > bx && !*bxe)
692 --n;
693 b.resize(n);
694 }
695 }
696 return q;
697 } 682 }
698 683
699 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. 684 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
700 * 685 *
701 * Inspired by "How to Print Floating-Point Numbers Accurately" by 686 * Inspired by "How to Print Floating-Point Numbers Accurately" by
702 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. 687 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
703 * 688 *
704 * Modifications: 689 * Modifications:
705 * 1. Rather than iterating, we use a simple numeric overestimate 690 * 1. Rather than iterating, we use a simple numeric overestimate
706 * to determine k = floor(log10(d)). We scale relevant 691 * to determine k = floor(log10(d)). We scale relevant
(...skipping 17 matching lines...) Expand all
724 * to get by with floating-point arithmetic; we resort to 709 * to get by with floating-point arithmetic; we resort to
725 * multiple-precision integer arithmetic only if we cannot 710 * multiple-precision integer arithmetic only if we cannot
726 * guarantee that the floating-point calculation has given 711 * guarantee that the floating-point calculation has given
727 * the correctly rounded result. For k requested digits and 712 * the correctly rounded result. For k requested digits and
728 * "uniformly" distributed input, the probability is 713 * "uniformly" distributed input, the probability is
729 * something like 10^(k-15) that we must resort to the int32_t 714 * something like 10^(k-15) that we must resort to the int32_t
730 * calculation. 715 * calculation.
731 * 716 *
732 * Note: 'leftright' translates to 'generate shortest possible string'. 717 * Note: 'leftright' translates to 'generate shortest possible string'.
733 */ 718 */
734 template<bool roundingNone, bool roundingSignificantFigures, bool roundingDecima lPlaces, bool leftright> 719 template <bool roundingNone, bool roundingSignificantFigures, bool roundingDecim alPlaces, bool leftright>
735 void dtoa(DtoaBuffer result, double dd, int ndigits, bool& signOut, int& exponen tOut, unsigned& precisionOut) 720 void dtoa(DtoaBuffer result, double dd, int ndigits, bool& signOut, int& exponen tOut, unsigned& precisionOut) {
736 { 721 // Exactly one rounding mode must be specified.
737 // Exactly one rounding mode must be specified. 722 ASSERT(roundingNone + roundingSignificantFigures + roundingDecimalPlaces == 1) ;
738 ASSERT(roundingNone + roundingSignificantFigures + roundingDecimalPlaces == 1); 723 // roundingNone only allowed (only sensible?) with leftright set.
739 // roundingNone only allowed (only sensible?) with leftright set. 724 ASSERT(!roundingNone || leftright);
740 ASSERT(!roundingNone || leftright);
741 725
742 ASSERT(std::isfinite(dd)); 726 ASSERT(std::isfinite(dd));
743 727
744 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0, 728 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
745 j, j1, k, k0, k_check, m2, m5, s2, s5, 729 j, j1, k, k0, k_check, m2, m5, s2, s5,
746 spec_case; 730 spec_case;
747 int32_t L; 731 int32_t L;
748 int denorm; 732 int denorm;
749 uint32_t x; 733 uint32_t x;
750 BigInt b, delta, mlo, mhi, S; 734 BigInt b, delta, mlo, mhi, S;
751 U d2, eps, u; 735 U d2, eps, u;
752 double ds; 736 double ds;
753 char* s; 737 char* s;
754 char* s0; 738 char* s0;
755 739
756 u.d = dd; 740 u.d = dd;
757 741
758 /* Infinity or NaN */ 742 /* Infinity or NaN */
759 ASSERT((word0(&u) & Exp_mask) != Exp_mask); 743 ASSERT((word0(&u) & Exp_mask) != Exp_mask);
760 744
761 // JavaScript toString conversion treats -0 as 0. 745 // JavaScript toString conversion treats -0 as 0.
762 if (!dval(&u)) { 746 if (!dval(&u)) {
763 signOut = false; 747 signOut = false;
764 exponentOut = 0; 748 exponentOut = 0;
765 precisionOut = 1; 749 precisionOut = 1;
766 result[0] = '0'; 750 result[0] = '0';
767 result[1] = '\0'; 751 result[1] = '\0';
768 return; 752 return;
769 } 753 }
770 754
771 if (word0(&u) & Sign_bit) { 755 if (word0(&u) & Sign_bit) {
772 signOut = true; 756 signOut = true;
773 word0(&u) &= ~Sign_bit; // clear sign bit 757 word0(&u) &= ~Sign_bit; // clear sign bit
774 } else 758 } else
775 signOut = false; 759 signOut = false;
776 760
777 d2b(b, &u, &be, &bbits); 761 d2b(b, &u, &be, &bbits);
778 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) { 762 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
779 dval(&d2) = dval(&u); 763 dval(&d2) = dval(&u);
780 word0(&d2) &= Frac_mask1; 764 word0(&d2) &= Frac_mask1;
781 word0(&d2) |= Exp_11; 765 word0(&d2) |= Exp_11;
782 766
783 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 767 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
784 * log10(x) = log(x) / log(10) 768 * log10(x) = log(x) / log(10)
785 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) 769 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
786 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) 770 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
787 * 771 *
788 * This suggests computing an approximation k to log10(d) by 772 * This suggests computing an approximation k to log10(d) by
789 * 773 *
790 * k = (i - Bias)*0.301029995663981 774 * k = (i - Bias)*0.301029995663981
791 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); 775 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
792 * 776 *
793 * We want k to be too large rather than too small. 777 * We want k to be too large rather than too small.
794 * The error in the first-order Taylor series approximation 778 * The error in the first-order Taylor series approximation
795 * is in our favor, so we just round up the constant enough 779 * is in our favor, so we just round up the constant enough
796 * to compensate for any error in the multiplication of 780 * to compensate for any error in the multiplication of
797 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, 781 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
798 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, 782 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
799 * adding 1e-13 to the constant term more than suffices. 783 * adding 1e-13 to the constant term more than suffices.
800 * Hence we adjust the constant term to 0.1760912590558. 784 * Hence we adjust the constant term to 0.1760912590558.
801 * (We could get a more accurate k by invoking log10, 785 * (We could get a more accurate k by invoking log10,
802 * but this is probably not worthwhile.) 786 * but this is probably not worthwhile.)
803 */ 787 */
804 788
805 i -= Bias; 789 i -= Bias;
806 denorm = 0; 790 denorm = 0;
807 } else { 791 } else {
808 /* d is denormalized */ 792 /* d is denormalized */
809 793
810 i = bbits + be + (Bias + (P - 1) - 1); 794 i = bbits + be + (Bias + (P - 1) - 1);
811 x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32)) 795 x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
812 : word1(&u) << (32 - i); 796 : word1(&u) << (32 - i);
813 dval(&d2) = x; 797 dval(&d2) = x;
814 word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */ 798 word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
815 i -= (Bias + (P - 1) - 1) + 1; 799 i -= (Bias + (P - 1) - 1) + 1;
816 denorm = 1; 800 denorm = 1;
817 } 801 }
818 ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029 995663981); 802 ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.30102999 5663981);
819 k = (int)ds; 803 k = (int)ds;
820 if (ds < 0. && ds != k) 804 if (ds < 0. && ds != k)
821 k--; /* want k = floor(ds) */ 805 k--; /* want k = floor(ds) */
822 k_check = 1; 806 k_check = 1;
823 if (k >= 0 && k <= Ten_pmax) { 807 if (k >= 0 && k <= Ten_pmax) {
824 if (dval(&u) < tens[k]) 808 if (dval(&u) < tens[k])
825 k--; 809 k--;
826 k_check = 0; 810 k_check = 0;
827 } 811 }
828 j = bbits - i - 1; 812 j = bbits - i - 1;
829 if (j >= 0) { 813 if (j >= 0) {
830 b2 = 0; 814 b2 = 0;
831 s2 = j; 815 s2 = j;
832 } else { 816 } else {
833 b2 = -j; 817 b2 = -j;
834 s2 = 0; 818 s2 = 0;
835 } 819 }
836 if (k >= 0) { 820 if (k >= 0) {
837 b5 = 0; 821 b5 = 0;
838 s5 = k; 822 s5 = k;
839 s2 += k; 823 s2 += k;
840 } else { 824 } else {
841 b2 -= k; 825 b2 -= k;
842 b5 = -k; 826 b5 = -k;
843 s5 = 0; 827 s5 = 0;
844 } 828 }
845 829
846 if (roundingNone) { 830 if (roundingNone) {
847 ilim = ilim1 = -1; 831 ilim = ilim1 = -1;
848 i = 18; 832 i = 18;
849 ndigits = 0; 833 ndigits = 0;
850 } 834 }
851 if (roundingSignificantFigures) { 835 if (roundingSignificantFigures) {
852 if (ndigits <= 0) 836 if (ndigits <= 0)
853 ndigits = 1; 837 ndigits = 1;
854 ilim = ilim1 = i = ndigits; 838 ilim = ilim1 = i = ndigits;
855 } 839 }
856 if (roundingDecimalPlaces) { 840 if (roundingDecimalPlaces) {
857 i = ndigits + k + 1; 841 i = ndigits + k + 1;
858 ilim = i; 842 ilim = i;
859 ilim1 = i - 1; 843 ilim1 = i - 1;
860 if (i <= 0) 844 if (i <= 0)
861 i = 1; 845 i = 1;
862 } 846 }
863 847
864 s = s0 = result; 848 s = s0 = result;
865 849
866 if (ilim >= 0 && ilim <= Quick_max) { 850 if (ilim >= 0 && ilim <= Quick_max) {
867 /* Try to get by with floating-point arithmetic. */ 851 /* Try to get by with floating-point arithmetic. */
868 852
869 i = 0; 853 i = 0;
870 dval(&d2) = dval(&u); 854 dval(&d2) = dval(&u);
871 k0 = k; 855 k0 = k;
872 ilim0 = ilim; 856 ilim0 = ilim;
873 ieps = 2; /* conservative */ 857 ieps = 2; /* conservative */
874 if (k > 0) { 858 if (k > 0) {
875 ds = tens[k & 0xf]; 859 ds = tens[k & 0xf];
876 j = k >> 4; 860 j = k >> 4;
877 if (j & Bletch) { 861 if (j & Bletch) {
878 /* prevent overflows */ 862 /* prevent overflows */
879 j &= Bletch - 1; 863 j &= Bletch - 1;
880 dval(&u) /= bigtens[n_bigtens - 1]; 864 dval(&u) /= bigtens[n_bigtens - 1];
881 ieps++; 865 ieps++;
882 } 866 }
883 for (; j; j >>= 1, i++) { 867 for (; j; j >>= 1, i++) {
884 if (j & 1) { 868 if (j & 1) {
885 ieps++; 869 ieps++;
886 ds *= bigtens[i]; 870 ds *= bigtens[i];
887 }
888 }
889 dval(&u) /= ds;
890 } else if ((j1 = -k)) {
891 dval(&u) *= tens[j1 & 0xf];
892 for (j = j1 >> 4; j; j >>= 1, i++) {
893 if (j & 1) {
894 ieps++;
895 dval(&u) *= bigtens[i];
896 }
897 }
898 } 871 }
899 if (k_check && dval(&u) < 1. && ilim > 0) { 872 }
900 if (ilim1 <= 0) 873 dval(&u) /= ds;
901 goto fastFailed; 874 } else if ((j1 = -k)) {
902 ilim = ilim1; 875 dval(&u) *= tens[j1 & 0xf];
903 k--; 876 for (j = j1 >> 4; j; j >>= 1, i++) {
904 dval(&u) *= 10.; 877 if (j & 1) {
905 ieps++; 878 ieps++;
879 dval(&u) *= bigtens[i];
906 } 880 }
907 dval(&eps) = (ieps * dval(&u)) + 7.; 881 }
908 word0(&eps) -= (P - 1) * Exp_msk1; 882 }
909 if (!ilim) { 883 if (k_check && dval(&u) < 1. && ilim > 0) {
910 S.clear(); 884 if (ilim1 <= 0)
911 mhi.clear(); 885 goto fastFailed;
912 dval(&u) -= 5.; 886 ilim = ilim1;
913 if (dval(&u) > dval(&eps)) 887 k--;
914 goto oneDigit; 888 dval(&u) *= 10.;
915 if (dval(&u) < -dval(&eps)) 889 ieps++;
916 goto noDigits; 890 }
917 goto fastFailed; 891 dval(&eps) = (ieps * dval(&u)) + 7.;
918 } 892 word0(&eps) -= (P - 1) * Exp_msk1;
919 if (leftright) { 893 if (!ilim) {
920 /* Use Steele & White method of only 894 S.clear();
895 mhi.clear();
896 dval(&u) -= 5.;
897 if (dval(&u) > dval(&eps))
898 goto oneDigit;
899 if (dval(&u) < -dval(&eps))
900 goto noDigits;
901 goto fastFailed;
902 }
903 if (leftright) {
904 /* Use Steele & White method of only
921 * generating digits needed. 905 * generating digits needed.
922 */ 906 */
923 dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps); 907 dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
924 for (i = 0;;) { 908 for (i = 0;;) {
925 L = (long int)dval(&u); 909 L = (long int)dval(&u);
926 dval(&u) -= L; 910 dval(&u) -= L;
927 *s++ = '0' + (int)L; 911 *s++ = '0' + (int)L;
928 if (dval(&u) < dval(&eps)) 912 if (dval(&u) < dval(&eps))
929 goto ret; 913 goto ret;
930 if (1. - dval(&u) < dval(&eps)) 914 if (1. - dval(&u) < dval(&eps))
931 goto bumpUp; 915 goto bumpUp;
932 if (++i >= ilim) 916 if (++i >= ilim)
933 break; 917 break;
934 dval(&eps) *= 10.; 918 dval(&eps) *= 10.;
935 dval(&u) *= 10.; 919 dval(&u) *= 10.;
920 }
921 } else {
922 /* Generate ilim digits, then fix them up. */
923 dval(&eps) *= tens[ilim - 1];
924 for (i = 1;; i++, dval(&u) *= 10.) {
925 L = (int32_t)(dval(&u));
926 if (!(dval(&u) -= L))
927 ilim = i;
928 *s++ = '0' + (int)L;
929 if (i == ilim) {
930 if (dval(&u) > 0.5 + dval(&eps))
931 goto bumpUp;
932 if (dval(&u) < 0.5 - dval(&eps)) {
933 while (*--s == '0') {
936 } 934 }
937 } else { 935 s++;
938 /* Generate ilim digits, then fix them up. */ 936 goto ret;
939 dval(&eps) *= tens[ilim - 1]; 937 }
940 for (i = 1;; i++, dval(&u) *= 10.) { 938 break;
941 L = (int32_t)(dval(&u)); 939 }
942 if (!(dval(&u) -= L)) 940 }
943 ilim = i; 941 }
944 *s++ = '0' + (int)L; 942 fastFailed:
945 if (i == ilim) { 943 s = s0;
946 if (dval(&u) > 0.5 + dval(&eps)) 944 dval(&u) = dval(&d2);
947 goto bumpUp; 945 k = k0;
948 if (dval(&u) < 0.5 - dval(&eps)) { 946 ilim = ilim0;
949 while (*--s == '0') { } 947 }
950 s++; 948
951 goto ret; 949 /* Do we have a "small" integer? */
952 } 950
953 break; 951 if (be >= 0 && k <= Int_max) {
954 } 952 /* Yes. */
953 ds = tens[k];
954 if (ndigits < 0 && ilim <= 0) {
955 S.clear();
956 mhi.clear();
957 if (ilim < 0 || dval(&u) <= 5 * ds)
958 goto noDigits;
959 goto oneDigit;
960 }
961 for (i = 1;; i++, dval(&u) *= 10.) {
962 L = (int32_t)(dval(&u) / ds);
963 dval(&u) -= L * ds;
964 *s++ = '0' + (int)L;
965 if (!dval(&u)) {
966 break;
967 }
968 if (i == ilim) {
969 dval(&u) += dval(&u);
970 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
971 bumpUp:
972 while (*--s == '9')
973 if (s == s0) {
974 k++;
975 *s = '0';
976 break;
955 } 977 }
978 ++*s++;
956 } 979 }
957 fastFailed: 980 break;
958 s = s0; 981 }
959 dval(&u) = dval(&d2); 982 }
960 k = k0; 983 goto ret;
961 ilim = ilim0; 984 }
962 } 985
963 986 m2 = b2;
964 /* Do we have a "small" integer? */ 987 m5 = b5;
965 988 mhi.clear();
966 if (be >= 0 && k <= Int_max) { 989 mlo.clear();
967 /* Yes. */ 990 if (leftright) {
968 ds = tens[k]; 991 i = denorm ? be + (Bias + (P - 1) - 1 + 1) : 1 + P - bbits;
969 if (ndigits < 0 && ilim <= 0) { 992 b2 += i;
970 S.clear(); 993 s2 += i;
971 mhi.clear(); 994 i2b(mhi, 1);
972 if (ilim < 0 || dval(&u) <= 5 * ds) 995 }
973 goto noDigits; 996 if (m2 > 0 && s2 > 0) {
974 goto oneDigit; 997 i = m2 < s2 ? m2 : s2;
975 } 998 b2 -= i;
976 for (i = 1;; i++, dval(&u) *= 10.) { 999 m2 -= i;
977 L = (int32_t)(dval(&u) / ds); 1000 s2 -= i;
978 dval(&u) -= L * ds; 1001 }
979 *s++ = '0' + (int)L; 1002 if (b5 > 0) {
980 if (!dval(&u)) {
981 break;
982 }
983 if (i == ilim) {
984 dval(&u) += dval(&u);
985 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
986 bumpUp:
987 while (*--s == '9')
988 if (s == s0) {
989 k++;
990 *s = '0';
991 break;
992 }
993 ++*s++;
994 }
995 break;
996 }
997 }
998 goto ret;
999 }
1000
1001 m2 = b2;
1002 m5 = b5;
1003 mhi.clear();
1004 mlo.clear();
1005 if (leftright) { 1003 if (leftright) {
1006 i = denorm ? be + (Bias + (P - 1) - 1 + 1) : 1 + P - bbits; 1004 if (m5 > 0) {
1007 b2 += i; 1005 pow5mult(mhi, m5);
1008 s2 += i; 1006 mult(b, mhi);
1009 i2b(mhi, 1); 1007 }
1010 } 1008 if ((j = b5 - m5))
1011 if (m2 > 0 && s2 > 0) { 1009 pow5mult(b, j);
1012 i = m2 < s2 ? m2 : s2; 1010 } else
1013 b2 -= i; 1011 pow5mult(b, b5);
1014 m2 -= i; 1012 }
1015 s2 -= i; 1013 i2b(S, 1);
1016 } 1014 if (s5 > 0)
1017 if (b5 > 0) { 1015 pow5mult(S, s5);
1018 if (leftright) { 1016
1019 if (m5 > 0) { 1017 /* Check for special case that d is a normalized power of 2. */
1020 pow5mult(mhi, m5); 1018
1021 mult(b, mhi); 1019 spec_case = 0;
1022 } 1020 if ((roundingNone || leftright) && (!word1(&u) && !(word0(&u) & Bndry_mask) && word0(&u) & (Exp_mask & ~Exp_msk1))) {
1023 if ((j = b5 - m5)) 1021 /* The special case */
1024 pow5mult(b, j); 1022 b2 += Log2P;
1025 } else 1023 s2 += Log2P;
1026 pow5mult(b, b5); 1024 spec_case = 1;
1027 } 1025 }
1028 i2b(S, 1); 1026
1029 if (s5 > 0) 1027 /* Arrange for convenient computation of quotients:
1030 pow5mult(S, s5);
1031
1032 /* Check for special case that d is a normalized power of 2. */
1033
1034 spec_case = 0;
1035 if ((roundingNone || leftright) && (!word1(&u) && !(word0(&u) & Bndry_mask) && word0(&u) & (Exp_mask & ~Exp_msk1))) {
1036 /* The special case */
1037 b2 += Log2P;
1038 s2 += Log2P;
1039 spec_case = 1;
1040 }
1041
1042 /* Arrange for convenient computation of quotients:
1043 * shift left if necessary so divisor has 4 leading 0 bits. 1028 * shift left if necessary so divisor has 4 leading 0 bits.
1044 * 1029 *
1045 * Perhaps we should just compute leading 28 bits of S once 1030 * Perhaps we should just compute leading 28 bits of S once
1046 * and for all and pass them and a shift to quorem, so it 1031 * and for all and pass them and a shift to quorem, so it
1047 * can do shifts and ors to compute the numerator for q. 1032 * can do shifts and ors to compute the numerator for q.
1048 */ 1033 */
1049 if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f)) 1034 if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
1050 i = 32 - i; 1035 i = 32 - i;
1051 if (i > 4) { 1036 if (i > 4) {
1052 i -= 4; 1037 i -= 4;
1053 b2 += i; 1038 b2 += i;
1054 m2 += i; 1039 m2 += i;
1055 s2 += i; 1040 s2 += i;
1056 } else if (i < 4) { 1041 } else if (i < 4) {
1057 i += 28; 1042 i += 28;
1058 b2 += i; 1043 b2 += i;
1059 m2 += i; 1044 m2 += i;
1060 s2 += i; 1045 s2 += i;
1061 } 1046 }
1062 if (b2 > 0) 1047 if (b2 > 0)
1063 lshift(b, b2); 1048 lshift(b, b2);
1064 if (s2 > 0) 1049 if (s2 > 0)
1065 lshift(S, s2); 1050 lshift(S, s2);
1066 if (k_check) { 1051 if (k_check) {
1067 if (cmp(b, S) < 0) { 1052 if (cmp(b, S) < 0) {
1068 k--; 1053 k--;
1069 multadd(b, 10, 0); /* we botched the k estimate */ 1054 multadd(b, 10, 0); /* we botched the k estimate */
1070 if (leftright) 1055 if (leftright)
1071 multadd(mhi, 10, 0); 1056 multadd(mhi, 10, 0);
1072 ilim = ilim1; 1057 ilim = ilim1;
1073 } 1058 }
1074 } 1059 }
1075 if (ilim <= 0 && roundingDecimalPlaces) { 1060 if (ilim <= 0 && roundingDecimalPlaces) {
1076 if (ilim < 0) 1061 if (ilim < 0)
1077 goto noDigits; 1062 goto noDigits;
1078 multadd(S, 5, 0); 1063 multadd(S, 5, 0);
1079 // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 would flush to zero. 1064 // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 wou ld flush to zero.
1080 if (cmp(b, S) < 0) 1065 if (cmp(b, S) < 0)
1081 goto noDigits; 1066 goto noDigits;
1082 goto oneDigit; 1067 goto oneDigit;
1083 } 1068 }
1084 if (leftright) { 1069 if (leftright) {
1085 if (m2 > 0) 1070 if (m2 > 0)
1086 lshift(mhi, m2); 1071 lshift(mhi, m2);
1087 1072
1088 /* Compute mlo -- check for special case 1073 /* Compute mlo -- check for special case
1089 * that d is a normalized power of 2. 1074 * that d is a normalized power of 2.
1090 */ 1075 */
1091 1076
1092 mlo = mhi; 1077 mlo = mhi;
1093 if (spec_case) 1078 if (spec_case)
1094 lshift(mhi, Log2P); 1079 lshift(mhi, Log2P);
1095 1080
1096 for (i = 1;;i++) { 1081 for (i = 1;; i++) {
1097 dig = quorem(b, S) + '0'; 1082 dig = quorem(b, S) + '0';
1098 /* Do we yet have the shortest decimal string 1083 /* Do we yet have the shortest decimal string
1099 * that will round to d? 1084 * that will round to d?
1100 */ 1085 */
1101 j = cmp(b, mlo); 1086 j = cmp(b, mlo);
1102 diff(delta, S, mhi); 1087 diff(delta, S, mhi);
1103 j1 = delta.sign ? 1 : cmp(b, delta); 1088 j1 = delta.sign ? 1 : cmp(b, delta);
1104 #ifdef DTOA_ROUND_BIASED 1089 #ifdef DTOA_ROUND_BIASED
1105 if (j < 0 || !j) { 1090 if (j < 0 || !j) {
1106 #else 1091 #else
1107 // FIXME: ECMA-262 specifies that equidistant results round away fro m 1092 // FIXME: ECMA-262 specifies that equidistant results round away from
1108 // zero, which probably means we shouldn't be on the unbiased code p ath 1093 // zero, which probably means we shouldn't be on the unbiased code path
1109 // (the (word1(&u) & 1) clause is looking highly suspicious). I have n't 1094 // (the (word1(&u) & 1) clause is looking highly suspicious). I haven't
1110 // yet understood this code well enough to make the call, but we sho uld 1095 // yet understood this code well enough to make the call, but we should
1111 // probably be enabling DTOA_ROUND_BIASED. I think the interesting c orner 1096 // probably be enabling DTOA_ROUND_BIASED. I think the interesting corner
1112 // case to understand is probably "Math.pow(0.5, 24).toString()". 1097 // case to understand is probably "Math.pow(0.5, 24).toString()".
1113 // I believe this value is interesting because I think it is precise ly 1098 // I believe this value is interesting because I think it is precisely
1114 // representable in binary floating point, and its decimal represent ation 1099 // representable in binary floating point, and its decimal representation
1115 // has a single digit that Steele & White reduction can remove, with the 1100 // has a single digit that Steele & White reduction can remove, with the
1116 // value 5 (thus equidistant from the next numbers above and below). 1101 // value 5 (thus equidistant from the next numbers above and below).
1117 // We produce the correct answer using either codepath, and I don't as 1102 // We produce the correct answer using either codepath, and I don't as
1118 // yet understand why. :-) 1103 // yet understand why. :-)
1119 if (!j1 && !(word1(&u) & 1)) { 1104 if (!j1 && !(word1(&u) & 1)) {
1120 if (dig == '9') 1105 if (dig == '9')
1121 goto round9up; 1106 goto round9up;
1122 if (j > 0) 1107 if (j > 0)
1123 dig++; 1108 dig++;
1124 *s++ = dig; 1109 *s++ = dig;
1125 goto ret; 1110 goto ret;
1126 } 1111 }
1127 if (j < 0 || (!j && !(word1(&u) & 1))) { 1112 if (j < 0 || (!j && !(word1(&u) & 1))) {
1128 #endif 1113 #endif
1129 if ((b.words()[0] || b.size() > 1) && (j1 > 0)) { 1114 if ((b.words()[0] || b.size() > 1) && (j1 > 0)) {
1130 lshift(b, 1); 1115 lshift(b, 1);
1131 j1 = cmp(b, S); 1116 j1 = cmp(b, S);
1132 // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))), 1117 // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))),
1133 // but ECMA-262 specifies that equidistant values (e.g. (.5) .toFixed()) should 1118 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed() ) should
1134 // be rounded away from zero. 1119 // be rounded away from zero.
1135 if (j1 >= 0) { 1120 if (j1 >= 0) {
1136 if (dig == '9') 1121 if (dig == '9')
1137 goto round9up; 1122 goto round9up;
1138 dig++; 1123 dig++;
1139 } 1124 }
1140 }
1141 *s++ = dig;
1142 goto ret;
1143 }
1144 if (j1 > 0) {
1145 if (dig == '9') { /* possible if i == 1 */
1146 round9up:
1147 *s++ = '9';
1148 goto roundoff;
1149 }
1150 *s++ = dig + 1;
1151 goto ret;
1152 }
1153 *s++ = dig;
1154 if (i == ilim)
1155 break;
1156 multadd(b, 10, 0);
1157 multadd(mlo, 10, 0);
1158 multadd(mhi, 10, 0);
1159 } 1125 }
1160 } else { 1126 *s++ = dig;
1161 for (i = 1;; i++) { 1127 goto ret;
1162 *s++ = dig = quorem(b, S) + '0'; 1128 }
1163 if (!b.words()[0] && b.size() <= 1) 1129 if (j1 > 0) {
1164 goto ret; 1130 if (dig == '9') { /* possible if i == 1 */
1165 if (i >= ilim) 1131 round9up:
1166 break; 1132 *s++ = '9';
1167 multadd(b, 10, 0); 1133 goto roundoff;
1168 } 1134 }
1169 } 1135 *s++ = dig + 1;
1170 1136 goto ret;
1171 /* Round off last digit */ 1137 }
1172 1138 *s++ = dig;
1173 lshift(b, 1); 1139 if (i == ilim)
1174 j = cmp(b, S); 1140 break;
1175 // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1))), 1141 multadd(b, 10, 0);
1176 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) shou ld 1142 multadd(mlo, 10, 0);
1177 // be rounded away from zero. 1143 multadd(mhi, 10, 0);
1178 if (j >= 0) { 1144 }
1179 roundoff: 1145 } else {
1180 while (*--s == '9') 1146 for (i = 1;; i++) {
1181 if (s == s0) { 1147 *s++ = dig = quorem(b, S) + '0';
1182 k++; 1148 if (!b.words()[0] && b.size() <= 1)
1183 *s++ = '1'; 1149 goto ret;
1184 goto ret; 1150 if (i >= ilim)
1185 } 1151 break;
1186 ++*s++; 1152 multadd(b, 10, 0);
1187 } else { 1153 }
1188 while (*--s == '0') { } 1154 }
1189 s++; 1155
1190 } 1156 /* Round off last digit */
1191 goto ret; 1157
1158 lshift(b, 1);
1159 j = cmp(b, S);
1160 // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1) )),
1161 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1162 // be rounded away from zero.
1163 if (j >= 0) {
1164 roundoff:
1165 while (*--s == '9')
1166 if (s == s0) {
1167 k++;
1168 *s++ = '1';
1169 goto ret;
1170 }
1171 ++*s++;
1172 } else {
1173 while (*--s == '0') {
1174 }
1175 s++;
1176 }
1177 goto ret;
1192 noDigits: 1178 noDigits:
1193 exponentOut = 0; 1179 exponentOut = 0;
1194 precisionOut = 1; 1180 precisionOut = 1;
1195 result[0] = '0'; 1181 result[0] = '0';
1196 result[1] = '\0'; 1182 result[1] = '\0';
1197 return; 1183 return;
1198 oneDigit: 1184 oneDigit:
1199 *s++ = '1'; 1185 *s++ = '1';
1200 k++; 1186 k++;
1201 goto ret; 1187 goto ret;
1202 ret: 1188 ret:
1203 ASSERT(s > result); 1189 ASSERT(s > result);
1204 *s = 0; 1190 *s = 0;
1205 exponentOut = k; 1191 exponentOut = k;
1206 precisionOut = s - result; 1192 precisionOut = s - result;
1207 } 1193 }
1208 1194
1209 void dtoa(DtoaBuffer result, double dd, bool& sign, int& exponent, unsigned& pre cision) 1195 void dtoa(DtoaBuffer result, double dd, bool& sign, int& exponent, unsigned& pre cision) {
1210 { 1196 // flags are roundingNone, leftright.
1211 // flags are roundingNone, leftright. 1197 dtoa<true, false, false, true>(result, dd, 0, sign, exponent, precision);
1212 dtoa<true, false, false, true>(result, dd, 0, sign, exponent, precision); 1198 }
1213 } 1199
1214 1200 void dtoaRoundSF(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exp onent, unsigned& precision) {
1215 void dtoaRoundSF(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exp onent, unsigned& precision) 1201 // flag is roundingSignificantFigures.
1216 { 1202 dtoa<false, true, false, false>(result, dd, ndigits, sign, exponent, precision );
1217 // flag is roundingSignificantFigures. 1203 }
1218 dtoa<false, true, false, false>(result, dd, ndigits, sign, exponent, precisi on); 1204
1219 } 1205 void dtoaRoundDP(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exp onent, unsigned& precision) {
1220 1206 // flag is roundingDecimalPlaces.
1221 void dtoaRoundDP(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exp onent, unsigned& precision) 1207 dtoa<false, false, true, false>(result, dd, ndigits, sign, exponent, precision );
1222 { 1208 }
1223 // flag is roundingDecimalPlaces. 1209
1224 dtoa<false, false, true, false>(result, dd, ndigits, sign, exponent, precisi on); 1210 const char* numberToString(double d, NumberToStringBuffer buffer) {
1225 } 1211 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1226 1212 const double_conversion::DoubleToStringConverter& converter = double_conversio n::DoubleToStringConverter::EcmaScriptConverter();
1227 const char* numberToString(double d, NumberToStringBuffer buffer) 1213 converter.ToShortest(d, &builder);
1228 { 1214 return builder.Finalize();
1229 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength) ; 1215 }
1230 const double_conversion::DoubleToStringConverter& converter = double_convers ion::DoubleToStringConverter::EcmaScriptConverter(); 1216
1231 converter.ToShortest(d, &builder); 1217 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToSt ringBuffer buffer, double_conversion::StringBuilder& builder) {
1218 size_t length = builder.position();
1219
1220 // If there is an exponent, stripping trailing zeros would be incorrect.
1221 // FIXME: Zeros should be stripped before the 'e'.
1222 if (memchr(buffer, 'e', length))
1232 return builder.Finalize(); 1223 return builder.Finalize();
1233 } 1224
1234 1225 size_t decimalPointPosition = 0;
1235 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToSt ringBuffer buffer, double_conversion::StringBuilder& builder) 1226 for (; decimalPointPosition < length; ++decimalPointPosition) {
1236 { 1227 if (buffer[decimalPointPosition] == '.')
1237 size_t length = builder.position(); 1228 break;
1238 1229 }
1239 // If there is an exponent, stripping trailing zeros would be incorrect. 1230
1240 // FIXME: Zeros should be stripped before the 'e'. 1231 // No decimal seperator found, early exit.
1241 if (memchr(buffer, 'e', length)) 1232 if (decimalPointPosition == length)
1242 return builder.Finalize();
1243
1244 size_t decimalPointPosition = 0;
1245 for (; decimalPointPosition < length; ++decimalPointPosition) {
1246 if (buffer[decimalPointPosition] == '.')
1247 break;
1248 }
1249
1250 // No decimal seperator found, early exit.
1251 if (decimalPointPosition == length)
1252 return builder.Finalize();
1253
1254 size_t truncatedLength = length - 1;
1255 for (; truncatedLength > decimalPointPosition; --truncatedLength) {
1256 if (buffer[truncatedLength] != '0')
1257 break;
1258 }
1259
1260 // No trailing zeros found to strip.
1261 if (truncatedLength == length - 1)
1262 return builder.Finalize();
1263
1264 // If we removed all trailing zeros, remove the decimal point as well.
1265 if (truncatedLength == decimalPointPosition) {
1266 ASSERT(truncatedLength > 0);
1267 --truncatedLength;
1268 }
1269
1270 // Truncate the StringBuilder, and return the final result.
1271 builder.SetPosition(truncatedLength + 1);
1272 return builder.Finalize(); 1233 return builder.Finalize();
1273 } 1234
1274 1235 size_t truncatedLength = length - 1;
1275 const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer buffer, bool truncateTrailingZeros) 1236 for (; truncatedLength > decimalPointPosition; --truncatedLength) {
1276 { 1237 if (buffer[truncatedLength] != '0')
1277 // Mimic String::format("%.[precision]g", ...), but use dtoas rounding facil ities. 1238 break;
1278 // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision. 1239 }
1279 // The e format is used only when the exponent of the value is less than -4 or greater than or equal to the 1240
1280 // precision argument. Trailing zeros are truncated, and the decimal point a ppears only if one or more digits follow it. 1241 // No trailing zeros found to strip.
1281 // "precision": The precision specifies the maximum number of significant di gits printed. 1242 if (truncatedLength == length - 1)
1282 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength) ;
1283 const double_conversion::DoubleToStringConverter& converter = double_convers ion::DoubleToStringConverter::EcmaScriptConverter();
1284 converter.ToPrecision(d, significantFigures, &builder);
1285 if (!truncateTrailingZeros)
1286 return builder.Finalize();
1287 // FIXME: Trailing zeros should never be added in the first place. The
1288 // current implementation does not strip when there is an exponent, eg.
1289 // 1.50000e+10.
1290 return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder);
1291 }
1292
1293 const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToS tringBuffer buffer)
1294 {
1295 // Mimic String::format("%.[precision]f", ...), but use dtoas rounding facil ities.
1296 // "f": Signed value having the form [ - ]dddd.dddd, where dddd is one or mo re decimal digits.
1297 // The number of digits before the decimal point depends on the magnitude of the number, and
1298 // the number of digits after the decimal point depends on the requested pre cision.
1299 // "precision": The precision value specifies the number of digits after the decimal point.
1300 // If a decimal point appears, at least one digit appears before it.
1301 // The value is rounded to the appropriate number of digits.
1302 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength) ;
1303 const double_conversion::DoubleToStringConverter& converter = double_convers ion::DoubleToStringConverter::EcmaScriptConverter();
1304 converter.ToFixed(d, decimalPlaces, &builder);
1305 return builder.Finalize(); 1243 return builder.Finalize();
1244
1245 // If we removed all trailing zeros, remove the decimal point as well.
1246 if (truncatedLength == decimalPointPosition) {
1247 ASSERT(truncatedLength > 0);
1248 --truncatedLength;
1249 }
1250
1251 // Truncate the StringBuilder, and return the final result.
1252 builder.SetPosition(truncatedLength + 1);
1253 return builder.Finalize();
1254 }
1255
1256 const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer buffer, bool truncateTrailingZeros) {
1257 // Mimic String::format("%.[precision]g", ...), but use dtoas rounding facilit ies.
1258 // "g": Signed value printed in f or e format, whichever is more compact for t he given value and precision.
1259 // The e format is used only when the exponent of the value is less than -4 or greater than or equal to the
1260 // precision argument. Trailing zeros are truncated, and the decimal point app ears only if one or more digits follow it.
1261 // "precision": The precision specifies the maximum number of significant digi ts printed.
1262 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1263 const double_conversion::DoubleToStringConverter& converter = double_conversio n::DoubleToStringConverter::EcmaScriptConverter();
1264 converter.ToPrecision(d, significantFigures, &builder);
1265 if (!truncateTrailingZeros)
1266 return builder.Finalize();
1267 // FIXME: Trailing zeros should never be added in the first place. The
1268 // current implementation does not strip when there is an exponent, eg.
1269 // 1.50000e+10.
1270 return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder);
1271 }
1272
1273 const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToS tringBuffer buffer) {
1274 // Mimic String::format("%.[precision]f", ...), but use dtoas rounding facilit ies.
1275 // "f": Signed value having the form [ - ]dddd.dddd, where dddd is one or more decimal digits.
1276 // The number of digits before the decimal point depends on the magnitude of t he number, and
1277 // the number of digits after the decimal point depends on the requested preci sion.
1278 // "precision": The precision value specifies the number of digits after the d ecimal point.
1279 // If a decimal point appears, at least one digit appears before it.
1280 // The value is rounded to the appropriate number of digits.
1281 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1282 const double_conversion::DoubleToStringConverter& converter = double_conversio n::DoubleToStringConverter::EcmaScriptConverter();
1283 converter.ToFixed(d, decimalPlaces, &builder);
1284 return builder.Finalize();
1306 } 1285 }
1307 1286
1308 namespace Internal { 1287 namespace Internal {
1309 1288
1310 double parseDoubleFromLongString(const UChar* string, size_t length, size_t& par sedLength) 1289 double parseDoubleFromLongString(const UChar* string, size_t length, size_t& par sedLength) {
1311 { 1290 Vector<LChar> conversionBuffer(length);
1312 Vector<LChar> conversionBuffer(length); 1291 for (size_t i = 0; i < length; ++i)
1313 for (size_t i = 0; i < length; ++i) 1292 conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0;
1314 conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0; 1293 return parseDouble(conversionBuffer.data(), length, parsedLength);
1315 return parseDouble(conversionBuffer.data(), length, parsedLength); 1294 }
1316 } 1295
1317 1296 } // namespace Internal
1318 } // namespace Internal 1297
1319 1298 } // namespace WTF
1320 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa.h ('k') | third_party/WebKit/Source/wtf/dtoa/bignum.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698