OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "fast-dtoa.h" | 28 #include "fast-dtoa.h" |
29 | 29 |
30 #include "cached-powers.h" | 30 #include "cached-powers.h" |
31 #include "diy-fp.h" | 31 #include "diy-fp.h" |
32 #include "double.h" | 32 #include "double.h" |
33 | 33 |
34 namespace WTF { | 34 namespace WTF { |
35 | 35 |
36 namespace double_conversion { | 36 namespace double_conversion { |
37 | 37 |
38 // The minimal and maximal target exponent define the range of w's binary | 38 // The minimal and maximal target exponent define the range of w's binary |
39 // exponent, where 'w' is the result of multiplying the input by a cached po
wer | 39 // exponent, where 'w' is the result of multiplying the input by a cached power |
40 // of ten. | 40 // of ten. |
41 // | 41 // |
42 // A different range might be chosen on a different platform, to optimize di
git | 42 // A different range might be chosen on a different platform, to optimize digit |
43 // generation, but a smaller range requires more powers of ten to be cached. | 43 // generation, but a smaller range requires more powers of ten to be cached. |
44 static const int kMinimalTargetExponent = -60; | 44 static const int kMinimalTargetExponent = -60; |
45 static const int kMaximalTargetExponent = -32; | 45 static const int kMaximalTargetExponent = -32; |
46 | 46 |
47 | 47 // Adjusts the last digit of the generated number, and screens out generated |
48 // Adjusts the last digit of the generated number, and screens out generated | 48 // solutions that may be inaccurate. A solution may be inaccurate if it is |
49 // solutions that may be inaccurate. A solution may be inaccurate if it is | 49 // outside the safe interval, or if we cannot prove that it is closer to the |
50 // outside the safe interval, or if we cannot prove that it is closer to the | 50 // input than a neighboring representation of the same length. |
51 // input than a neighboring representation of the same length. | 51 // |
52 // | 52 // Input: * buffer containing the digits of too_high / 10^kappa |
53 // Input: * buffer containing the digits of too_high / 10^kappa | 53 // * the buffer's length |
54 // * the buffer's length | 54 // * distance_too_high_w == (too_high - w).f() * unit |
55 // * distance_too_high_w == (too_high - w).f() * unit | 55 // * unsafe_interval == (too_high - too_low).f() * unit |
56 // * unsafe_interval == (too_high - too_low).f() * unit | 56 // * rest = (too_high - buffer * 10^kappa).f() * unit |
57 // * rest = (too_high - buffer * 10^kappa).f() * unit | 57 // * ten_kappa = 10^kappa * unit |
58 // * ten_kappa = 10^kappa * unit | 58 // * unit = the common multiplier |
59 // * unit = the common multiplier | 59 // Output: returns true if the buffer is guaranteed to contain the closest |
60 // Output: returns true if the buffer is guaranteed to contain the closest | 60 // representable number to the input. |
61 // representable number to the input. | 61 // Modifies the generated digits in the buffer to approach (round towards) w. |
62 // Modifies the generated digits in the buffer to approach (round towards)
w. | 62 static bool RoundWeed(Vector<char> buffer, |
63 static bool RoundWeed(Vector<char> buffer, | 63 int length, |
64 int length, | 64 uint64_t distance_too_high_w, |
65 uint64_t distance_too_high_w, | 65 uint64_t unsafe_interval, |
66 uint64_t unsafe_interval, | 66 uint64_t rest, |
67 uint64_t rest, | 67 uint64_t ten_kappa, |
68 uint64_t ten_kappa, | 68 uint64_t unit) { |
69 uint64_t unit) { | 69 uint64_t small_distance = distance_too_high_w - unit; |
70 uint64_t small_distance = distance_too_high_w - unit; | 70 uint64_t big_distance = distance_too_high_w + unit; |
71 uint64_t big_distance = distance_too_high_w + unit; | 71 // Let w_low = too_high - big_distance, and |
72 // Let w_low = too_high - big_distance, and | 72 // w_high = too_high - small_distance. |
73 // w_high = too_high - small_distance. | 73 // Note: w_low < w < w_high |
74 // Note: w_low < w < w_high | 74 // |
75 // | 75 // The real w (* unit) must lie somewhere inside the interval |
76 // The real w (* unit) must lie somewhere inside the interval | 76 // ]w_low; w_high[ (often written as "(w_low; w_high)") |
77 // ]w_low; w_high[ (often written as "(w_low; w_high)") | 77 |
78 | 78 // Basically the buffer currently contains a number in the unsafe interval |
79 // Basically the buffer currently contains a number in the unsafe interv
al | 79 // ]too_low; too_high[ with too_low < w < too_high |
80 // ]too_low; too_high[ with too_low < w < too_high | 80 // |
81 // | 81 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
82 // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - | 82 // ^v 1 unit ^ ^ ^ ^ |
83 // ^v 1 unit ^ ^ ^
^ | 83 // boundary_high --------------------- . . . . |
84 // boundary_high --------------------- . . .
. | 84 // ^v 1 unit . . . . |
85 // ^v 1 unit . . .
. | 85 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . . |
86 // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - .
. | 86 // . . ^ . . |
87 // . . ^ .
. | 87 // . big_distance . . . |
88 // . big_distance . .
. | 88 // . . . . rest |
89 // . . . .
rest | 89 // small_distance . . . . |
90 // small_distance . . .
. | 90 // v . . . . |
91 // v . . .
. | 91 // w_high - - - - - - - - - - - - - - - - - - . . . . |
92 // w_high - - - - - - - - - - - - - - - - - - . . .
. | 92 // ^v 1 unit . . . . |
93 // ^v 1 unit . . .
. | 93 // w ---------------------------------------- . . . . |
94 // w ---------------------------------------- . . .
. | 94 // ^v 1 unit v . . . |
95 // ^v 1 unit v . .
. | 95 // w_low - - - - - - - - - - - - - - - - - - - - - . . . |
96 // w_low - - - - - - - - - - - - - - - - - - - - - . .
. | 96 // . . v |
97 // . .
v | 97 // buffer --------------------------------------------------+-------+-------- |
98 // buffer --------------------------------------------------+-------+--
------ | 98 // . . |
99 // . . | 99 // safe_interval . |
100 // safe_interval . | 100 // v . |
101 // v . | 101 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - . |
102 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - . | 102 // ^v 1 unit . |
103 // ^v 1 unit . | 103 // boundary_low ------------------------- unsafe_interval |
104 // boundary_low ------------------------- unsafe_in
terval | 104 // ^v 1 unit v |
105 // ^v 1 unit v | 105 // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
106 // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - | 106 // |
107 // | 107 // |
108 // | 108 // Note that the value of buffer could lie anywhere inside the range too_low |
109 // Note that the value of buffer could lie anywhere inside the range too
_low | 109 // to too_high. |
110 // to too_high. | 110 // |
111 // | 111 // boundary_low, boundary_high and w are approximations of the real boundaries |
112 // boundary_low, boundary_high and w are approximations of the real boun
daries | 112 // and v (the input number). They are guaranteed to be precise up to one unit. |
113 // and v (the input number). They are guaranteed to be precise up to one
unit. | 113 // In fact the error is guaranteed to be strictly less than one unit. |
114 // In fact the error is guaranteed to be strictly less than one unit. | 114 // |
115 // | 115 // Anything that lies outside the unsafe interval is guaranteed not to round |
116 // Anything that lies outside the unsafe interval is guaranteed not to r
ound | 116 // to v when read again. |
117 // to v when read again. | 117 // Anything that lies inside the safe interval is guaranteed to round to v |
118 // Anything that lies inside the safe interval is guaranteed to round to
v | 118 // when read again. |
119 // when read again. | 119 // If the number inside the buffer lies inside the unsafe interval but not |
120 // If the number inside the buffer lies inside the unsafe interval but n
ot | 120 // inside the safe interval then we simply do not know and bail out (returning |
121 // inside the safe interval then we simply do not know and bail out (ret
urning | 121 // false). |
122 // false). | 122 // |
123 // | 123 // Similarly we have to take into account the imprecision of 'w' when finding |
124 // Similarly we have to take into account the imprecision of 'w' when fi
nding | 124 // the closest representation of 'w'. If we have two potential |
125 // the closest representation of 'w'. If we have two potential | 125 // representations, and one is closer to both w_low and w_high, then we know |
126 // representations, and one is closer to both w_low and w_high, then we
know | 126 // it is closer to the actual value v. |
127 // it is closer to the actual value v. | 127 // |
128 // | 128 // By generating the digits of too_high we got the largest (closest to |
129 // By generating the digits of too_high we got the largest (closest to | 129 // too_high) buffer that is still in the unsafe interval. In the case where |
130 // too_high) buffer that is still in the unsafe interval. In the case wh
ere | 130 // w_high < buffer < too_high we try to decrement the buffer. |
131 // w_high < buffer < too_high we try to decrement the buffer. | 131 // This way the buffer approaches (rounds towards) w. |
132 // This way the buffer approaches (rounds towards) w. | 132 // There are 3 conditions that stop the decrementation process: |
133 // There are 3 conditions that stop the decrementation process: | 133 // 1) the buffer is already below w_high |
134 // 1) the buffer is already below w_high | 134 // 2) decrementing the buffer would make it leave the unsafe interval |
135 // 2) decrementing the buffer would make it leave the unsafe interval | 135 // 3) decrementing the buffer would yield a number below w_high and farther |
136 // 3) decrementing the buffer would yield a number below w_high and fa
rther | 136 // away than the current number. In other words: |
137 // away than the current number. In other words: | 137 // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high |
138 // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer -
w_high | 138 // Instead of using the buffer directly we use its distance to too_high. |
139 // Instead of using the buffer directly we use its distance to too_high. | 139 // Conceptually rest ~= too_high - buffer |
140 // Conceptually rest ~= too_high - buffer | 140 // We need to do the following tests in this order to avoid over- and |
141 // We need to do the following tests in this order to avoid over- and | 141 // underflows. |
142 // underflows. | 142 ASSERT(rest <= unsafe_interval); |
143 ASSERT(rest <= unsafe_interval); | 143 while (rest < small_distance && // Negated condition 1 |
144 while (rest < small_distance && // Negated condition 1 | 144 unsafe_interval - rest >= ten_kappa && // Negated condition 2 |
145 unsafe_interval - rest >= ten_kappa && // Negated condition 2 | 145 (rest + ten_kappa < small_distance || // buffer{-1} > w_high |
146 (rest + ten_kappa < small_distance || // buffer{-1} > w_high | 146 small_distance - rest >= rest + ten_kappa - small_distance)) { |
147 small_distance - rest >= rest + ten_kappa - small_distance)) { | 147 buffer[length - 1]--; |
148 buffer[length - 1]--; | 148 rest += ten_kappa; |
149 rest += ten_kappa; | 149 } |
150 } | 150 |
151 | 151 // We have approached w+ as much as possible. We now test if approaching w- |
152 // We have approached w+ as much as possible. We now test if approaching
w- | 152 // would require changing the buffer. If yes, then we have two possible |
153 // would require changing the buffer. If yes, then we have two possible | 153 // representations close to w, but we cannot decide which one is closer. |
154 // representations close to w, but we cannot decide which one is closer. | 154 if (rest < big_distance && unsafe_interval - rest >= ten_kappa && |
155 if (rest < big_distance && | 155 (rest + ten_kappa < big_distance || |
156 unsafe_interval - rest >= ten_kappa && | 156 big_distance - rest > rest + ten_kappa - big_distance)) { |
157 (rest + ten_kappa < big_distance || | 157 return false; |
158 big_distance - rest > rest + ten_kappa - big_distance)) { | 158 } |
159 return false; | 159 |
160 } | 160 // Weeding test. |
161 | 161 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp] |
162 // Weeding test. | 162 // Since too_low = too_high - unsafe_interval this is equivalent to |
163 // The safe interval is [too_low + 2 ulp; too_high - 2 ulp] | 163 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp] |
164 // Since too_low = too_high - unsafe_interval this is equivalent to | 164 // Conceptually we have: rest ~= too_high - buffer |
165 // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp] | 165 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit); |
166 // Conceptually we have: rest ~= too_high - buffer | 166 } |
167 return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit); | 167 |
| 168 // Rounds the buffer upwards if the result is closer to v by possibly adding |
| 169 // 1 to the buffer. If the precision of the calculation is not sufficient to |
| 170 // round correctly, return false. |
| 171 // The rounding might shift the whole buffer in which case the kappa is |
| 172 // adjusted. For example "99", kappa = 3 might become "10", kappa = 4. |
| 173 // |
| 174 // If 2*rest > ten_kappa then the buffer needs to be round up. |
| 175 // rest can have an error of +/- 1 unit. This function accounts for the |
| 176 // imprecision and returns false, if the rounding direction cannot be |
| 177 // unambiguously determined. |
| 178 // |
| 179 // Precondition: rest < ten_kappa. |
| 180 static bool RoundWeedCounted(Vector<char> buffer, |
| 181 int length, |
| 182 uint64_t rest, |
| 183 uint64_t ten_kappa, |
| 184 uint64_t unit, |
| 185 int* kappa) { |
| 186 ASSERT(rest < ten_kappa); |
| 187 // The following tests are done in a specific order to avoid overflows. They |
| 188 // will work correctly with any uint64 values of rest < ten_kappa and unit. |
| 189 // |
| 190 // If the unit is too big, then we don't know which way to round. For example |
| 191 // a unit of 50 means that the real number lies within rest +/- 50. If |
| 192 // 10^kappa == 40 then there is no way to tell which way to round. |
| 193 if (unit >= ten_kappa) |
| 194 return false; |
| 195 // Even if unit is just half the size of 10^kappa we are already completely |
| 196 // lost. (And after the previous test we know that the expression will not |
| 197 // over/underflow.) |
| 198 if (ten_kappa - unit <= unit) |
| 199 return false; |
| 200 // If 2 * (rest + unit) <= 10^kappa we can safely round down. |
| 201 if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) { |
| 202 return true; |
| 203 } |
| 204 // If 2 * (rest - unit) >= 10^kappa, then we can safely round up. |
| 205 if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) { |
| 206 // Increment the last digit recursively until we find a non '9' digit. |
| 207 buffer[length - 1]++; |
| 208 for (int i = length - 1; i > 0; --i) { |
| 209 if (buffer[i] != '0' + 10) |
| 210 break; |
| 211 buffer[i] = '0'; |
| 212 buffer[i - 1]++; |
168 } | 213 } |
169 | 214 // If the first digit is now '0'+ 10 we had a buffer with all '9's. With the |
170 | 215 // exception of the first digit all digits are now '0'. Simply switch the |
171 // Rounds the buffer upwards if the result is closer to v by possibly adding | 216 // first digit to '1' and adjust the kappa. Example: "99" becomes "10" and |
172 // 1 to the buffer. If the precision of the calculation is not sufficient to | 217 // the power (the kappa) is increased. |
173 // round correctly, return false. | 218 if (buffer[0] == '0' + 10) { |
174 // The rounding might shift the whole buffer in which case the kappa is | 219 buffer[0] = '1'; |
175 // adjusted. For example "99", kappa = 3 might become "10", kappa = 4. | 220 (*kappa) += 1; |
176 // | |
177 // If 2*rest > ten_kappa then the buffer needs to be round up. | |
178 // rest can have an error of +/- 1 unit. This function accounts for the | |
179 // imprecision and returns false, if the rounding direction cannot be | |
180 // unambiguously determined. | |
181 // | |
182 // Precondition: rest < ten_kappa. | |
183 static bool RoundWeedCounted(Vector<char> buffer, | |
184 int length, | |
185 uint64_t rest, | |
186 uint64_t ten_kappa, | |
187 uint64_t unit, | |
188 int* kappa) { | |
189 ASSERT(rest < ten_kappa); | |
190 // The following tests are done in a specific order to avoid overflows.
They | |
191 // will work correctly with any uint64 values of rest < ten_kappa and un
it. | |
192 // | |
193 // If the unit is too big, then we don't know which way to round. For ex
ample | |
194 // a unit of 50 means that the real number lies within rest +/- 50. If | |
195 // 10^kappa == 40 then there is no way to tell which way to round. | |
196 if (unit >= ten_kappa) return false; | |
197 // Even if unit is just half the size of 10^kappa we are already complet
ely | |
198 // lost. (And after the previous test we know that the expression will n
ot | |
199 // over/underflow.) | |
200 if (ten_kappa - unit <= unit) return false; | |
201 // If 2 * (rest + unit) <= 10^kappa we can safely round down. | |
202 if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) { | |
203 return true; | |
204 } | |
205 // If 2 * (rest - unit) >= 10^kappa, then we can safely round up. | |
206 if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) { | |
207 // Increment the last digit recursively until we find a non '9' digi
t. | |
208 buffer[length - 1]++; | |
209 for (int i = length - 1; i > 0; --i) { | |
210 if (buffer[i] != '0' + 10) break; | |
211 buffer[i] = '0'; | |
212 buffer[i - 1]++; | |
213 } | |
214 // If the first digit is now '0'+ 10 we had a buffer with all '9's.
With the | |
215 // exception of the first digit all digits are now '0'. Simply switc
h the | |
216 // first digit to '1' and adjust the kappa. Example: "99" becomes "1
0" and | |
217 // the power (the kappa) is increased. | |
218 if (buffer[0] == '0' + 10) { | |
219 buffer[0] = '1'; | |
220 (*kappa) += 1; | |
221 } | |
222 return true; | |
223 } | |
224 return false; | |
225 } | 221 } |
226 | 222 return true; |
227 | 223 } |
228 static const uint32_t kTen4 = 10000; | 224 return false; |
229 static const uint32_t kTen5 = 100000; | 225 } |
230 static const uint32_t kTen6 = 1000000; | 226 |
231 static const uint32_t kTen7 = 10000000; | 227 static const uint32_t kTen4 = 10000; |
232 static const uint32_t kTen8 = 100000000; | 228 static const uint32_t kTen5 = 100000; |
233 static const uint32_t kTen9 = 1000000000; | 229 static const uint32_t kTen6 = 1000000; |
234 | 230 static const uint32_t kTen7 = 10000000; |
235 // Returns the biggest power of ten that is less than or equal to the given | 231 static const uint32_t kTen8 = 100000000; |
236 // number. We furthermore receive the maximum number of bits 'number' has. | 232 static const uint32_t kTen9 = 1000000000; |
237 // If number_bits == 0 then 0^-1 is returned | 233 |
238 // The number of bits must be <= 32. | 234 // Returns the biggest power of ten that is less than or equal to the given |
239 // Precondition: number < (1 << (number_bits + 1)). | 235 // number. We furthermore receive the maximum number of bits 'number' has. |
240 static void BiggestPowerTen(uint32_t number, | 236 // If number_bits == 0 then 0^-1 is returned |
241 int number_bits, | 237 // The number of bits must be <= 32. |
242 uint32_t* power, | 238 // Precondition: number < (1 << (number_bits + 1)). |
243 int* exponent) { | 239 static void BiggestPowerTen(uint32_t number, |
244 ASSERT(number < (uint32_t)(1 << (number_bits + 1))); | 240 int number_bits, |
245 | 241 uint32_t* power, |
246 switch (number_bits) { | 242 int* exponent) { |
247 case 32: | 243 ASSERT(number < (uint32_t)(1 << (number_bits + 1))); |
248 case 31: | 244 |
249 case 30: | 245 switch (number_bits) { |
250 if (kTen9 <= number) { | 246 case 32: |
251 *power = kTen9; | 247 case 31: |
252 *exponent = 9; | 248 case 30: |
253 break; | 249 if (kTen9 <= number) { |
254 } // else fallthrough | 250 *power = kTen9; |
255 case 29: | 251 *exponent = 9; |
256 case 28: | 252 break; |
257 case 27: | 253 } // else fallthrough |
258 if (kTen8 <= number) { | 254 case 29: |
259 *power = kTen8; | 255 case 28: |
260 *exponent = 8; | 256 case 27: |
261 break; | 257 if (kTen8 <= number) { |
262 } // else fallthrough | 258 *power = kTen8; |
263 case 26: | 259 *exponent = 8; |
264 case 25: | 260 break; |
265 case 24: | 261 } // else fallthrough |
266 if (kTen7 <= number) { | 262 case 26: |
267 *power = kTen7; | 263 case 25: |
268 *exponent = 7; | 264 case 24: |
269 break; | 265 if (kTen7 <= number) { |
270 } // else fallthrough | 266 *power = kTen7; |
271 case 23: | 267 *exponent = 7; |
272 case 22: | 268 break; |
273 case 21: | 269 } // else fallthrough |
274 case 20: | 270 case 23: |
275 if (kTen6 <= number) { | 271 case 22: |
276 *power = kTen6; | 272 case 21: |
277 *exponent = 6; | 273 case 20: |
278 break; | 274 if (kTen6 <= number) { |
279 } // else fallthrough | 275 *power = kTen6; |
280 case 19: | 276 *exponent = 6; |
281 case 18: | 277 break; |
282 case 17: | 278 } // else fallthrough |
283 if (kTen5 <= number) { | 279 case 19: |
284 *power = kTen5; | 280 case 18: |
285 *exponent = 5; | 281 case 17: |
286 break; | 282 if (kTen5 <= number) { |
287 } // else fallthrough | 283 *power = kTen5; |
288 case 16: | 284 *exponent = 5; |
289 case 15: | 285 break; |
290 case 14: | 286 } // else fallthrough |
291 if (kTen4 <= number) { | 287 case 16: |
292 *power = kTen4; | 288 case 15: |
293 *exponent = 4; | 289 case 14: |
294 break; | 290 if (kTen4 <= number) { |
295 } // else fallthrough | 291 *power = kTen4; |
296 case 13: | 292 *exponent = 4; |
297 case 12: | 293 break; |
298 case 11: | 294 } // else fallthrough |
299 case 10: | 295 case 13: |
300 if (1000 <= number) { | 296 case 12: |
301 *power = 1000; | 297 case 11: |
302 *exponent = 3; | 298 case 10: |
303 break; | 299 if (1000 <= number) { |
304 } // else fallthrough | 300 *power = 1000; |
305 case 9: | 301 *exponent = 3; |
306 case 8: | 302 break; |
307 case 7: | 303 } // else fallthrough |
308 if (100 <= number) { | 304 case 9: |
309 *power = 100; | 305 case 8: |
310 *exponent = 2; | 306 case 7: |
311 break; | 307 if (100 <= number) { |
312 } // else fallthrough | 308 *power = 100; |
313 case 6: | 309 *exponent = 2; |
314 case 5: | 310 break; |
315 case 4: | 311 } // else fallthrough |
316 if (10 <= number) { | 312 case 6: |
317 *power = 10; | 313 case 5: |
318 *exponent = 1; | 314 case 4: |
319 break; | 315 if (10 <= number) { |
320 } // else fallthrough | 316 *power = 10; |
321 case 3: | 317 *exponent = 1; |
322 case 2: | 318 break; |
323 case 1: | 319 } // else fallthrough |
324 if (1 <= number) { | 320 case 3: |
325 *power = 1; | 321 case 2: |
326 *exponent = 0; | 322 case 1: |
327 break; | 323 if (1 <= number) { |
328 } // else fallthrough | 324 *power = 1; |
329 case 0: | 325 *exponent = 0; |
330 *power = 0; | 326 break; |
331 *exponent = -1; | 327 } // else fallthrough |
332 break; | 328 case 0: |
333 default: | 329 *power = 0; |
334 // Following assignments are here to silence compiler warnings. | 330 *exponent = -1; |
335 *power = 0; | 331 break; |
336 *exponent = 0; | 332 default: |
337 UNREACHABLE(); | 333 // Following assignments are here to silence compiler warnings. |
338 } | 334 *power = 0; |
| 335 *exponent = 0; |
| 336 UNREACHABLE(); |
| 337 } |
| 338 } |
| 339 |
| 340 // Generates the digits of input number w. |
| 341 // w is a floating-point number (DiyFp), consisting of a significand and an |
| 342 // exponent. Its exponent is bounded by kMinimalTargetExponent and |
| 343 // kMaximalTargetExponent. |
| 344 // Hence -60 <= w.e() <= -32. |
| 345 // |
| 346 // Returns false if it fails, in which case the generated digits in the buffer |
| 347 // should not be used. |
| 348 // Preconditions: |
| 349 // * low, w and high are correct up to 1 ulp (unit in the last place). That |
| 350 // is, their error must be less than a unit of their last digits. |
| 351 // * low.e() == w.e() == high.e() |
| 352 // * low < w < high, and taking into account their error: low~ <= high~ |
| 353 // * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent |
| 354 // Postconditions: returns false if procedure fails. |
| 355 // otherwise: |
| 356 // * buffer is not null-terminated, but len contains the number of digits. |
| 357 // * buffer contains the shortest possible decimal digit-sequence |
| 358 // such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the |
| 359 // correct values of low and high (without their error). |
| 360 // * if more than one decimal representation gives the minimal number of |
| 361 // decimal digits then the one closest to W (where W is the correct value |
| 362 // of w) is chosen. |
| 363 // Remark: this procedure takes into account the imprecision of its input |
| 364 // numbers. If the precision is not enough to guarantee all the postconditions |
| 365 // then false is returned. This usually happens rarely (~0.5%). |
| 366 // |
| 367 // Say, for the sake of example, that |
| 368 // w.e() == -48, and w.f() == 0x1234567890abcdef |
| 369 // w's value can be computed by w.f() * 2^w.e() |
| 370 // We can obtain w's integral digits by simply shifting w.f() by -w.e(). |
| 371 // -> w's integral part is 0x1234 |
| 372 // w's fractional part is therefore 0x567890abcdef. |
| 373 // Printing w's integral part is easy (simply print 0x1234 in decimal). |
| 374 // In order to print its fraction we repeatedly multiply the fraction by 10 and |
| 375 // get each digit. Example the first digit after the point would be computed by |
| 376 // (0x567890abcdef * 10) >> 48. -> 3 |
| 377 // The whole thing becomes slightly more complicated because we want to stop |
| 378 // once we have enough digits. That is, once the digits inside the buffer |
| 379 // represent 'w' we can stop. Everything inside the interval low - high |
| 380 // represents w. However we have to pay attention to low, high and w's |
| 381 // imprecision. |
| 382 static bool DigitGen(DiyFp low, |
| 383 DiyFp w, |
| 384 DiyFp high, |
| 385 Vector<char> buffer, |
| 386 int* length, |
| 387 int* kappa) { |
| 388 ASSERT(low.e() == w.e() && w.e() == high.e()); |
| 389 ASSERT(low.f() + 1 <= high.f() - 1); |
| 390 ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent); |
| 391 // low, w and high are imprecise, but by less than one ulp (unit in the last |
| 392 // place). |
| 393 // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that |
| 394 // the new numbers are outside of the interval we want the final |
| 395 // representation to lie in. |
| 396 // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield |
| 397 // numbers that are certain to lie in the interval. We will use this fact |
| 398 // later on. |
| 399 // We will now start by generating the digits within the uncertain |
| 400 // interval. Later we will weed out representations that lie outside the safe |
| 401 // interval and thus _might_ lie outside the correct interval. |
| 402 uint64_t unit = 1; |
| 403 DiyFp too_low = DiyFp(low.f() - unit, low.e()); |
| 404 DiyFp too_high = DiyFp(high.f() + unit, high.e()); |
| 405 // too_low and too_high are guaranteed to lie outside the interval we want the |
| 406 // generated number in. |
| 407 DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low); |
| 408 // We now cut the input number into two parts: the integral digits and the |
| 409 // fractionals. We will not write any decimal separator though, but adapt |
| 410 // kappa instead. |
| 411 // Reminder: we are currently computing the digits (stored inside the buffer) |
| 412 // such that: too_low < buffer * 10^kappa < too_high |
| 413 // We use too_high for the digit_generation and stop as soon as possible. |
| 414 // If we stop early we effectively round down. |
| 415 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e()); |
| 416 // Division by one is a shift. |
| 417 uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e()); |
| 418 // Modulo by one is an and. |
| 419 uint64_t fractionals = too_high.f() & (one.f() - 1); |
| 420 uint32_t divisor; |
| 421 int divisor_exponent; |
| 422 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), &divisor, |
| 423 &divisor_exponent); |
| 424 *kappa = divisor_exponent + 1; |
| 425 *length = 0; |
| 426 // Loop invariant: buffer = too_high / 10^kappa (integer division) |
| 427 // The invariant holds for the first iteration: kappa has been initialized |
| 428 // with the divisor exponent + 1. And the divisor is the biggest power of ten |
| 429 // that is smaller than integrals. |
| 430 while (*kappa > 0) { |
| 431 char digit = static_cast<char>(integrals / divisor); |
| 432 buffer[*length] = '0' + digit; |
| 433 (*length)++; |
| 434 integrals %= divisor; |
| 435 (*kappa)--; |
| 436 // Note that kappa now equals the exponent of the divisor and that the |
| 437 // invariant thus holds again. |
| 438 uint64_t rest = |
| 439 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals; |
| 440 // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e()) |
| 441 // Reminder: unsafe_interval.e() == one.e() |
| 442 if (rest < unsafe_interval.f()) { |
| 443 // Rounding down (by not emitting the remaining digits) yields a number |
| 444 // that lies within the unsafe interval. |
| 445 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(), |
| 446 unsafe_interval.f(), rest, |
| 447 static_cast<uint64_t>(divisor) << -one.e(), unit); |
339 } | 448 } |
340 | 449 divisor /= 10; |
341 | 450 } |
342 // Generates the digits of input number w. | 451 |
343 // w is a floating-point number (DiyFp), consisting of a significand and an | 452 // The integrals have been generated. We are at the point of the decimal |
344 // exponent. Its exponent is bounded by kMinimalTargetExponent and | 453 // separator. In the following loop we simply multiply the remaining digits by |
345 // kMaximalTargetExponent. | 454 // 10 and divide by one. We just need to pay attention to multiply associated |
346 // Hence -60 <= w.e() <= -32. | 455 // data (like the interval or 'unit'), too. |
347 // | 456 // Note that the multiplication by 10 does not overflow, because w.e >= -60 |
348 // Returns false if it fails, in which case the generated digits in the buff
er | 457 // and thus one.e >= -60. |
349 // should not be used. | 458 ASSERT(one.e() >= -60); |
350 // Preconditions: | 459 ASSERT(fractionals < one.f()); |
351 // * low, w and high are correct up to 1 ulp (unit in the last place). That | 460 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); |
352 // is, their error must be less than a unit of their last digits. | 461 while (true) { |
353 // * low.e() == w.e() == high.e() | 462 fractionals *= 10; |
354 // * low < w < high, and taking into account their error: low~ <= high~ | 463 unit *= 10; |
355 // * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent | 464 unsafe_interval.set_f(unsafe_interval.f() * 10); |
356 // Postconditions: returns false if procedure fails. | 465 // Integer division by one. |
357 // otherwise: | 466 char digit = static_cast<char>(fractionals >> -one.e()); |
358 // * buffer is not null-terminated, but len contains the number of digit
s. | 467 buffer[*length] = '0' + digit; |
359 // * buffer contains the shortest possible decimal digit-sequence | 468 (*length)++; |
360 // such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are th
e | 469 fractionals &= one.f() - 1; // Modulo by one. |
361 // correct values of low and high (without their error). | 470 (*kappa)--; |
362 // * if more than one decimal representation gives the minimal number of | 471 if (fractionals < unsafe_interval.f()) { |
363 // decimal digits then the one closest to W (where W is the correct va
lue | 472 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit, |
364 // of w) is chosen. | 473 unsafe_interval.f(), fractionals, one.f(), unit); |
365 // Remark: this procedure takes into account the imprecision of its input | |
366 // numbers. If the precision is not enough to guarantee all the postcondit
ions | |
367 // then false is returned. This usually happens rarely (~0.5%). | |
368 // | |
369 // Say, for the sake of example, that | |
370 // w.e() == -48, and w.f() == 0x1234567890abcdef | |
371 // w's value can be computed by w.f() * 2^w.e() | |
372 // We can obtain w's integral digits by simply shifting w.f() by -w.e(). | |
373 // -> w's integral part is 0x1234 | |
374 // w's fractional part is therefore 0x567890abcdef. | |
375 // Printing w's integral part is easy (simply print 0x1234 in decimal). | |
376 // In order to print its fraction we repeatedly multiply the fraction by 10
and | |
377 // get each digit. Example the first digit after the point would be computed
by | |
378 // (0x567890abcdef * 10) >> 48. -> 3 | |
379 // The whole thing becomes slightly more complicated because we want to stop | |
380 // once we have enough digits. That is, once the digits inside the buffer | |
381 // represent 'w' we can stop. Everything inside the interval low - high | |
382 // represents w. However we have to pay attention to low, high and w's | |
383 // imprecision. | |
384 static bool DigitGen(DiyFp low, | |
385 DiyFp w, | |
386 DiyFp high, | |
387 Vector<char> buffer, | |
388 int* length, | |
389 int* kappa) { | |
390 ASSERT(low.e() == w.e() && w.e() == high.e()); | |
391 ASSERT(low.f() + 1 <= high.f() - 1); | |
392 ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponen
t); | |
393 // low, w and high are imprecise, but by less than one ulp (unit in the
last | |
394 // place). | |
395 // If we remove (resp. add) 1 ulp from low (resp. high) we are certain t
hat | |
396 // the new numbers are outside of the interval we want the final | |
397 // representation to lie in. | |
398 // Inversely adding (resp. removing) 1 ulp from low (resp. high) would y
ield | |
399 // numbers that are certain to lie in the interval. We will use this fac
t | |
400 // later on. | |
401 // We will now start by generating the digits within the uncertain | |
402 // interval. Later we will weed out representations that lie outside the
safe | |
403 // interval and thus _might_ lie outside the correct interval. | |
404 uint64_t unit = 1; | |
405 DiyFp too_low = DiyFp(low.f() - unit, low.e()); | |
406 DiyFp too_high = DiyFp(high.f() + unit, high.e()); | |
407 // too_low and too_high are guaranteed to lie outside the interval we wa
nt the | |
408 // generated number in. | |
409 DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low); | |
410 // We now cut the input number into two parts: the integral digits and t
he | |
411 // fractionals. We will not write any decimal separator though, but adap
t | |
412 // kappa instead. | |
413 // Reminder: we are currently computing the digits (stored inside the bu
ffer) | |
414 // such that: too_low < buffer * 10^kappa < too_high | |
415 // We use too_high for the digit_generation and stop as soon as possible
. | |
416 // If we stop early we effectively round down. | |
417 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e()); | |
418 // Division by one is a shift. | |
419 uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e()); | |
420 // Modulo by one is an and. | |
421 uint64_t fractionals = too_high.f() & (one.f() - 1); | |
422 uint32_t divisor; | |
423 int divisor_exponent; | |
424 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), | |
425 &divisor, &divisor_exponent); | |
426 *kappa = divisor_exponent + 1; | |
427 *length = 0; | |
428 // Loop invariant: buffer = too_high / 10^kappa (integer division) | |
429 // The invariant holds for the first iteration: kappa has been initializ
ed | |
430 // with the divisor exponent + 1. And the divisor is the biggest power o
f ten | |
431 // that is smaller than integrals. | |
432 while (*kappa > 0) { | |
433 char digit = static_cast<char>(integrals / divisor); | |
434 buffer[*length] = '0' + digit; | |
435 (*length)++; | |
436 integrals %= divisor; | |
437 (*kappa)--; | |
438 // Note that kappa now equals the exponent of the divisor and that t
he | |
439 // invariant thus holds again. | |
440 uint64_t rest = | |
441 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals; | |
442 // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e()) | |
443 // Reminder: unsafe_interval.e() == one.e() | |
444 if (rest < unsafe_interval.f()) { | |
445 // Rounding down (by not emitting the remaining digits) yields a
number | |
446 // that lies within the unsafe interval. | |
447 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(), | |
448 unsafe_interval.f(), rest, | |
449 static_cast<uint64_t>(divisor) << -one.e(), uni
t); | |
450 } | |
451 divisor /= 10; | |
452 } | |
453 | |
454 // The integrals have been generated. We are at the point of the decimal | |
455 // separator. In the following loop we simply multiply the remaining dig
its by | |
456 // 10 and divide by one. We just need to pay attention to multiply assoc
iated | |
457 // data (like the interval or 'unit'), too. | |
458 // Note that the multiplication by 10 does not overflow, because w.e >=
-60 | |
459 // and thus one.e >= -60. | |
460 ASSERT(one.e() >= -60); | |
461 ASSERT(fractionals < one.f()); | |
462 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); | |
463 while (true) { | |
464 fractionals *= 10; | |
465 unit *= 10; | |
466 unsafe_interval.set_f(unsafe_interval.f() * 10); | |
467 // Integer division by one. | |
468 char digit = static_cast<char>(fractionals >> -one.e()); | |
469 buffer[*length] = '0' + digit; | |
470 (*length)++; | |
471 fractionals &= one.f() - 1; // Modulo by one. | |
472 (*kappa)--; | |
473 if (fractionals < unsafe_interval.f()) { | |
474 return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f()
* unit, | |
475 unsafe_interval.f(), fractionals, one.f(), unit
); | |
476 } | |
477 } | |
478 } | 474 } |
479 | 475 } |
480 | 476 } |
481 | 477 |
482 // Generates (at most) requested_digits digits of input number w. | 478 // Generates (at most) requested_digits digits of input number w. |
483 // w is a floating-point number (DiyFp), consisting of a significand and an | 479 // w is a floating-point number (DiyFp), consisting of a significand and an |
484 // exponent. Its exponent is bounded by kMinimalTargetExponent and | 480 // exponent. Its exponent is bounded by kMinimalTargetExponent and |
485 // kMaximalTargetExponent. | 481 // kMaximalTargetExponent. |
486 // Hence -60 <= w.e() <= -32. | 482 // Hence -60 <= w.e() <= -32. |
487 // | 483 // |
488 // Returns false if it fails, in which case the generated digits in the buff
er | 484 // Returns false if it fails, in which case the generated digits in the buffer |
489 // should not be used. | 485 // should not be used. |
490 // Preconditions: | 486 // Preconditions: |
491 // * w is correct up to 1 ulp (unit in the last place). That | 487 // * w is correct up to 1 ulp (unit in the last place). That |
492 // is, its error must be strictly less than a unit of its last digit. | 488 // is, its error must be strictly less than a unit of its last digit. |
493 // * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent | 489 // * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent |
494 // | 490 // |
495 // Postconditions: returns false if procedure fails. | 491 // Postconditions: returns false if procedure fails. |
496 // otherwise: | 492 // otherwise: |
497 // * buffer is not null-terminated, but length contains the number of | 493 // * buffer is not null-terminated, but length contains the number of |
498 // digits. | 494 // digits. |
499 // * the representation in buffer is the most precise representation of | 495 // * the representation in buffer is the most precise representation of |
500 // requested_digits digits. | 496 // requested_digits digits. |
501 // * buffer contains at most requested_digits digits of w. If there are
less | 497 // * buffer contains at most requested_digits digits of w. If there are less |
502 // than requested_digits digits then some trailing '0's have been remo
ved. | 498 // than requested_digits digits then some trailing '0's have been removed. |
503 // * kappa is such that | 499 // * kappa is such that |
504 // w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2. | 500 // w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2. |
505 // | 501 // |
506 // Remark: This procedure takes into account the imprecision of its input | 502 // Remark: This procedure takes into account the imprecision of its input |
507 // numbers. If the precision is not enough to guarantee all the postcondit
ions | 503 // numbers. If the precision is not enough to guarantee all the postconditions |
508 // then false is returned. This usually happens rarely, but the failure-ra
te | 504 // then false is returned. This usually happens rarely, but the failure-rate |
509 // increases with higher requested_digits. | 505 // increases with higher requested_digits. |
510 static bool DigitGenCounted(DiyFp w, | 506 static bool DigitGenCounted(DiyFp w, |
511 int requested_digits, | 507 int requested_digits, |
512 Vector<char> buffer, | 508 Vector<char> buffer, |
513 int* length, | 509 int* length, |
514 int* kappa) { | 510 int* kappa) { |
515 ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponen
t); | 511 ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent); |
516 ASSERT(kMinimalTargetExponent >= -60); | 512 ASSERT(kMinimalTargetExponent >= -60); |
517 ASSERT(kMaximalTargetExponent <= -32); | 513 ASSERT(kMaximalTargetExponent <= -32); |
518 // w is assumed to have an error less than 1 unit. Whenever w is scaled
we | 514 // w is assumed to have an error less than 1 unit. Whenever w is scaled we |
519 // also scale its error. | 515 // also scale its error. |
520 uint64_t w_error = 1; | 516 uint64_t w_error = 1; |
521 // We cut the input number into two parts: the integral digits and the | 517 // We cut the input number into two parts: the integral digits and the |
522 // fractional digits. We don't emit any decimal separator, but adapt kap
pa | 518 // fractional digits. We don't emit any decimal separator, but adapt kappa |
523 // instead. Example: instead of writing "1.2" we put "12" into the buffe
r and | 519 // instead. Example: instead of writing "1.2" we put "12" into the buffer and |
524 // increase kappa by 1. | 520 // increase kappa by 1. |
525 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e()); | 521 DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e()); |
526 // Division by one is a shift. | 522 // Division by one is a shift. |
527 uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e()); | 523 uint32_t integrals = static_cast<uint32_t>(w.f() >> -one.e()); |
528 // Modulo by one is an and. | 524 // Modulo by one is an and. |
529 uint64_t fractionals = w.f() & (one.f() - 1); | 525 uint64_t fractionals = w.f() & (one.f() - 1); |
530 uint32_t divisor; | 526 uint32_t divisor; |
531 int divisor_exponent; | 527 int divisor_exponent; |
532 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), | 528 BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), &divisor, |
533 &divisor, &divisor_exponent); | 529 &divisor_exponent); |
534 *kappa = divisor_exponent + 1; | 530 *kappa = divisor_exponent + 1; |
535 *length = 0; | 531 *length = 0; |
536 | 532 |
537 // Loop invariant: buffer = w / 10^kappa (integer division) | 533 // Loop invariant: buffer = w / 10^kappa (integer division) |
538 // The invariant holds for the first iteration: kappa has been initializ
ed | 534 // The invariant holds for the first iteration: kappa has been initialized |
539 // with the divisor exponent + 1. And the divisor is the biggest power o
f ten | 535 // with the divisor exponent + 1. And the divisor is the biggest power of ten |
540 // that is smaller than 'integrals'. | 536 // that is smaller than 'integrals'. |
541 while (*kappa > 0) { | 537 while (*kappa > 0) { |
542 char digit = static_cast<char>(integrals / divisor); | 538 char digit = static_cast<char>(integrals / divisor); |
543 buffer[*length] = '0' + digit; | 539 buffer[*length] = '0' + digit; |
544 (*length)++; | 540 (*length)++; |
545 requested_digits--; | 541 requested_digits--; |
546 integrals %= divisor; | 542 integrals %= divisor; |
547 (*kappa)--; | 543 (*kappa)--; |
548 // Note that kappa now equals the exponent of the divisor and that t
he | 544 // Note that kappa now equals the exponent of the divisor and that the |
549 // invariant thus holds again. | 545 // invariant thus holds again. |
550 if (requested_digits == 0) break; | 546 if (requested_digits == 0) |
551 divisor /= 10; | 547 break; |
552 } | 548 divisor /= 10; |
553 | 549 } |
554 if (requested_digits == 0) { | 550 |
555 uint64_t rest = | 551 if (requested_digits == 0) { |
556 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals; | 552 uint64_t rest = |
557 return RoundWeedCounted(buffer, *length, rest, | 553 (static_cast<uint64_t>(integrals) << -one.e()) + fractionals; |
558 static_cast<uint64_t>(divisor) << -one.e(),
w_error, | 554 return RoundWeedCounted(buffer, *length, rest, |
559 kappa); | 555 static_cast<uint64_t>(divisor) << -one.e(), w_error, |
560 } | 556 kappa); |
561 | 557 } |
562 // The integrals have been generated. We are at the point of the decimal | 558 |
563 // separator. In the following loop we simply multiply the remaining dig
its by | 559 // The integrals have been generated. We are at the point of the decimal |
564 // 10 and divide by one. We just need to pay attention to multiply assoc
iated | 560 // separator. In the following loop we simply multiply the remaining digits by |
565 // data (the 'unit'), too. | 561 // 10 and divide by one. We just need to pay attention to multiply associated |
566 // Note that the multiplication by 10 does not overflow, because w.e >=
-60 | 562 // data (the 'unit'), too. |
567 // and thus one.e >= -60. | 563 // Note that the multiplication by 10 does not overflow, because w.e >= -60 |
568 ASSERT(one.e() >= -60); | 564 // and thus one.e >= -60. |
569 ASSERT(fractionals < one.f()); | 565 ASSERT(one.e() >= -60); |
570 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); | 566 ASSERT(fractionals < one.f()); |
571 while (requested_digits > 0 && fractionals > w_error) { | 567 ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); |
572 fractionals *= 10; | 568 while (requested_digits > 0 && fractionals > w_error) { |
573 w_error *= 10; | 569 fractionals *= 10; |
574 // Integer division by one. | 570 w_error *= 10; |
575 char digit = static_cast<char>(fractionals >> -one.e()); | 571 // Integer division by one. |
576 buffer[*length] = '0' + digit; | 572 char digit = static_cast<char>(fractionals >> -one.e()); |
577 (*length)++; | 573 buffer[*length] = '0' + digit; |
578 requested_digits--; | 574 (*length)++; |
579 fractionals &= one.f() - 1; // Modulo by one. | 575 requested_digits--; |
580 (*kappa)--; | 576 fractionals &= one.f() - 1; // Modulo by one. |
581 } | 577 (*kappa)--; |
582 if (requested_digits != 0) return false; | 578 } |
583 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error, | 579 if (requested_digits != 0) |
584 kappa); | 580 return false; |
585 } | 581 return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error, |
586 | 582 kappa); |
587 | 583 } |
588 // Provides a decimal representation of v. | 584 |
589 // Returns true if it succeeds, otherwise the result cannot be trusted. | 585 // Provides a decimal representation of v. |
590 // There will be *length digits inside the buffer (not null-terminated). | 586 // Returns true if it succeeds, otherwise the result cannot be trusted. |
591 // If the function returns true then | 587 // There will be *length digits inside the buffer (not null-terminated). |
592 // v == (double) (buffer * 10^decimal_exponent). | 588 // If the function returns true then |
593 // The digits in the buffer are the shortest representation possible: no | 589 // v == (double) (buffer * 10^decimal_exponent). |
594 // 0.09999999999999999 instead of 0.1. The shorter representation will even
be | 590 // The digits in the buffer are the shortest representation possible: no |
595 // chosen even if the longer one would be closer to v. | 591 // 0.09999999999999999 instead of 0.1. The shorter representation will even be |
596 // The last digit will be closest to the actual v. That is, even if several | 592 // chosen even if the longer one would be closer to v. |
597 // digits might correctly yield 'v' when read again, the closest will be | 593 // The last digit will be closest to the actual v. That is, even if several |
598 // computed. | 594 // digits might correctly yield 'v' when read again, the closest will be |
599 static bool Grisu3(double v, | 595 // computed. |
600 Vector<char> buffer, | 596 static bool Grisu3(double v, |
601 int* length, | 597 Vector<char> buffer, |
602 int* decimal_exponent) { | 598 int* length, |
603 DiyFp w = Double(v).AsNormalizedDiyFp(); | 599 int* decimal_exponent) { |
604 // boundary_minus and boundary_plus are the boundaries between v and its | 600 DiyFp w = Double(v).AsNormalizedDiyFp(); |
605 // closest floating-point neighbors. Any number strictly between | 601 // boundary_minus and boundary_plus are the boundaries between v and its |
606 // boundary_minus and boundary_plus will round to v when convert to a do
uble. | 602 // closest floating-point neighbors. Any number strictly between |
607 // Grisu3 will never output representations that lie exactly on a bounda
ry. | 603 // boundary_minus and boundary_plus will round to v when convert to a double. |
608 DiyFp boundary_minus, boundary_plus; | 604 // Grisu3 will never output representations that lie exactly on a boundary. |
609 Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus); | 605 DiyFp boundary_minus, boundary_plus; |
610 ASSERT(boundary_plus.e() == w.e()); | 606 Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus); |
611 DiyFp ten_mk; // Cached power of ten: 10^-k | 607 ASSERT(boundary_plus.e() == w.e()); |
612 int mk; // -k | 608 DiyFp ten_mk; // Cached power of ten: 10^-k |
613 int ten_mk_minimal_binary_exponent = | 609 int mk; // -k |
614 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); | 610 int ten_mk_minimal_binary_exponent = |
615 int ten_mk_maximal_binary_exponent = | 611 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); |
616 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); | 612 int ten_mk_maximal_binary_exponent = |
617 PowersOfTenCache::GetCachedPowerForBinaryExponentRange( | 613 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); |
618 ten_mk_minimal_bi
nary_exponent, | 614 PowersOfTenCache::GetCachedPowerForBinaryExponentRange( |
619 ten_mk_maximal_bi
nary_exponent, | 615 ten_mk_minimal_binary_exponent, ten_mk_maximal_binary_exponent, &ten_mk, |
620 &ten_mk, &mk); | 616 &mk); |
621 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() + | 617 ASSERT( |
622 DiyFp::kSignificandSize) && | 618 (kMinimalTargetExponent <= |
623 (kMaximalTargetExponent >= w.e() + ten_mk.e() + | 619 w.e() + ten_mk.e() + DiyFp::kSignificandSize) && |
624 DiyFp::kSignificandSize)); | 620 (kMaximalTargetExponent >= w.e() + ten_mk.e() + DiyFp::kSignificandSize)); |
625 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only cont
ains a | 621 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a |
626 // 64 bit significand and ten_mk is thus only precise up to 64 bits. | 622 // 64 bit significand and ten_mk is thus only precise up to 64 bits. |
627 | 623 |
628 // The DiyFp::Times procedure rounds its result, and ten_mk is approxima
ted | 624 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated |
629 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) ar
e now | 625 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now |
630 // off by a small amount. | 626 // off by a small amount. |
631 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_
w. | 627 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w. |
632 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then | 628 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then |
633 // (f-1) * 2^e < w*10^k < (f+1) * 2^e | 629 // (f-1) * 2^e < w*10^k < (f+1) * 2^e |
634 DiyFp scaled_w = DiyFp::Times(w, ten_mk); | 630 DiyFp scaled_w = DiyFp::Times(w, ten_mk); |
635 ASSERT(scaled_w.e() == | 631 ASSERT(scaled_w.e() == |
636 boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize); | 632 boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize); |
637 // In theory it would be possible to avoid some recomputations by comput
ing | 633 // In theory it would be possible to avoid some recomputations by computing |
638 // the difference between w and boundary_minus/plus (a power of 2) and t
o | 634 // the difference between w and boundary_minus/plus (a power of 2) and to |
639 // compute scaled_boundary_minus/plus by subtracting/adding from | 635 // compute scaled_boundary_minus/plus by subtracting/adding from |
640 // scaled_w. However the code becomes much less readable and the speed | 636 // scaled_w. However the code becomes much less readable and the speed |
641 // enhancements are not terriffic. | 637 // enhancements are not terriffic. |
642 DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk); | 638 DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk); |
643 DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk); | 639 DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk); |
644 | 640 |
645 // DigitGen will generate the digits of scaled_w. Therefore we have | 641 // DigitGen will generate the digits of scaled_w. Therefore we have |
646 // v == (double) (scaled_w * 10^-mk). | 642 // v == (double) (scaled_w * 10^-mk). |
647 // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is n
ot an | 643 // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an |
648 // integer than it will be updated. For instance if scaled_w == 1.23 the
n | 644 // integer than it will be updated. For instance if scaled_w == 1.23 then |
649 // the buffer will be filled with "123" und the decimal_exponent will be | 645 // the buffer will be filled with "123" und the decimal_exponent will be |
650 // decreased by 2. | 646 // decreased by 2. |
651 int kappa; | 647 int kappa; |
652 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_
plus, | 648 bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus, |
653 buffer, length, &kappa); | 649 buffer, length, &kappa); |
654 *decimal_exponent = -mk + kappa; | 650 *decimal_exponent = -mk + kappa; |
655 return result; | 651 return result; |
656 } | 652 } |
657 | 653 |
658 | 654 // The "counted" version of grisu3 (see above) only generates requested_digits |
659 // The "counted" version of grisu3 (see above) only generates requested_digi
ts | 655 // number of digits. This version does not generate the shortest representation, |
660 // number of digits. This version does not generate the shortest representat
ion, | 656 // and with enough requested digits 0.1 will at some point print as 0.9999999... |
661 // and with enough requested digits 0.1 will at some point print as 0.999999
9... | 657 // Grisu3 is too imprecise for real halfway cases (1.5 will not work) and |
662 // Grisu3 is too imprecise for real halfway cases (1.5 will not work) and | 658 // therefore the rounding strategy for halfway cases is irrelevant. |
663 // therefore the rounding strategy for halfway cases is irrelevant. | 659 static bool Grisu3Counted(double v, |
664 static bool Grisu3Counted(double v, | 660 int requested_digits, |
665 int requested_digits, | 661 Vector<char> buffer, |
666 Vector<char> buffer, | 662 int* length, |
667 int* length, | 663 int* decimal_exponent) { |
668 int* decimal_exponent) { | 664 DiyFp w = Double(v).AsNormalizedDiyFp(); |
669 DiyFp w = Double(v).AsNormalizedDiyFp(); | 665 DiyFp ten_mk; // Cached power of ten: 10^-k |
670 DiyFp ten_mk; // Cached power of ten: 10^-k | 666 int mk; // -k |
671 int mk; // -k | 667 int ten_mk_minimal_binary_exponent = |
672 int ten_mk_minimal_binary_exponent = | 668 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); |
673 kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); | 669 int ten_mk_maximal_binary_exponent = |
674 int ten_mk_maximal_binary_exponent = | 670 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); |
675 kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); | 671 PowersOfTenCache::GetCachedPowerForBinaryExponentRange( |
676 PowersOfTenCache::GetCachedPowerForBinaryExponentRange( | 672 ten_mk_minimal_binary_exponent, ten_mk_maximal_binary_exponent, &ten_mk, |
677 ten_mk_minimal_bi
nary_exponent, | 673 &mk); |
678 ten_mk_maximal_bi
nary_exponent, | 674 ASSERT( |
679 &ten_mk, &mk); | 675 (kMinimalTargetExponent <= |
680 ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() + | 676 w.e() + ten_mk.e() + DiyFp::kSignificandSize) && |
681 DiyFp::kSignificandSize) && | 677 (kMaximalTargetExponent >= w.e() + ten_mk.e() + DiyFp::kSignificandSize)); |
682 (kMaximalTargetExponent >= w.e() + ten_mk.e() + | 678 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a |
683 DiyFp::kSignificandSize)); | 679 // 64 bit significand and ten_mk is thus only precise up to 64 bits. |
684 // Note that ten_mk is only an approximation of 10^-k. A DiyFp only cont
ains a | 680 |
685 // 64 bit significand and ten_mk is thus only precise up to 64 bits. | 681 // The DiyFp::Times procedure rounds its result, and ten_mk is approximated |
686 | 682 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now |
687 // The DiyFp::Times procedure rounds its result, and ten_mk is approxima
ted | 683 // off by a small amount. |
688 // too. The variable scaled_w (as well as scaled_boundary_minus/plus) ar
e now | 684 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w. |
689 // off by a small amount. | 685 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then |
690 // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_
w. | 686 // (f-1) * 2^e < w*10^k < (f+1) * 2^e |
691 // In other words: let f = scaled_w.f() and e = scaled_w.e(), then | 687 DiyFp scaled_w = DiyFp::Times(w, ten_mk); |
692 // (f-1) * 2^e < w*10^k < (f+1) * 2^e | 688 |
693 DiyFp scaled_w = DiyFp::Times(w, ten_mk); | 689 // We now have (double) (scaled_w * 10^-mk). |
694 | 690 // DigitGen will generate the first requested_digits digits of scaled_w and |
695 // We now have (double) (scaled_w * 10^-mk). | 691 // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It |
696 // DigitGen will generate the first requested_digits digits of scaled_w
and | 692 // will not always be exactly the same since DigitGenCounted only produces a |
697 // return together with a kappa such that scaled_w ~= buffer * 10^kappa.
(It | 693 // limited number of digits.) |
698 // will not always be exactly the same since DigitGenCounted only produc
es a | 694 int kappa; |
699 // limited number of digits.) | 695 bool result = |
700 int kappa; | 696 DigitGenCounted(scaled_w, requested_digits, buffer, length, &kappa); |
701 bool result = DigitGenCounted(scaled_w, requested_digits, | 697 *decimal_exponent = -mk + kappa; |
702 buffer, length, &kappa); | 698 return result; |
703 *decimal_exponent = -mk + kappa; | 699 } |
704 return result; | 700 |
705 } | 701 bool FastDtoa(double v, |
706 | 702 FastDtoaMode mode, |
707 | 703 int requested_digits, |
708 bool FastDtoa(double v, | 704 Vector<char> buffer, |
709 FastDtoaMode mode, | 705 int* length, |
710 int requested_digits, | 706 int* decimal_point) { |
711 Vector<char> buffer, | 707 ASSERT(v > 0); |
712 int* length, | 708 ASSERT(!Double(v).IsSpecial()); |
713 int* decimal_point) { | 709 |
714 ASSERT(v > 0); | 710 bool result = false; |
715 ASSERT(!Double(v).IsSpecial()); | 711 int decimal_exponent = 0; |
716 | 712 switch (mode) { |
717 bool result = false; | 713 case FAST_DTOA_SHORTEST: |
718 int decimal_exponent = 0; | 714 result = Grisu3(v, buffer, length, &decimal_exponent); |
719 switch (mode) { | 715 break; |
720 case FAST_DTOA_SHORTEST: | 716 case FAST_DTOA_PRECISION: |
721 result = Grisu3(v, buffer, length, &decimal_exponent); | 717 result = |
722 break; | 718 Grisu3Counted(v, requested_digits, buffer, length, &decimal_exponent); |
723 case FAST_DTOA_PRECISION: | 719 break; |
724 result = Grisu3Counted(v, requested_digits, | 720 default: |
725 buffer, length, &decimal_exponent); | 721 UNREACHABLE(); |
726 break; | 722 } |
727 default: | 723 if (result) { |
728 UNREACHABLE(); | 724 *decimal_point = *length + decimal_exponent; |
729 } | 725 buffer[*length] = '\0'; |
730 if (result) { | 726 } |
731 *decimal_point = *length + decimal_exponent; | 727 return result; |
732 buffer[*length] = '\0'; | 728 } |
733 } | |
734 return result; | |
735 } | |
736 | 729 |
737 } // namespace double_conversion | 730 } // namespace double_conversion |
738 | 731 |
739 } // namespace WTF | 732 } // namespace WTF |
OLD | NEW |