/src/mozilla-central/intl/icu/source/i18n/number_decimalquantity.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2017 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | #include "unicode/utypes.h" |
5 | | |
6 | | #if !UCONFIG_NO_FORMATTING |
7 | | |
8 | | #include <cstdlib> |
9 | | #include <cmath> |
10 | | #include <limits> |
11 | | #include <stdlib.h> |
12 | | |
13 | | #include "unicode/plurrule.h" |
14 | | #include "cmemory.h" |
15 | | #include "number_decnum.h" |
16 | | #include "putilimp.h" |
17 | | #include "number_decimalquantity.h" |
18 | | #include "number_roundingutils.h" |
19 | | #include "double-conversion.h" |
20 | | #include "charstr.h" |
21 | | #include "number_utils.h" |
22 | | #include "uassert.h" |
23 | | |
24 | | using namespace icu; |
25 | | using namespace icu::number; |
26 | | using namespace icu::number::impl; |
27 | | |
28 | | using icu::double_conversion::DoubleToStringConverter; |
29 | | using icu::double_conversion::StringToDoubleConverter; |
30 | | |
31 | | namespace { |
32 | | |
33 | | int8_t NEGATIVE_FLAG = 1; |
34 | | int8_t INFINITY_FLAG = 2; |
35 | | int8_t NAN_FLAG = 4; |
36 | | |
37 | | /** Helper function for safe subtraction (no overflow). */ |
38 | 0 | inline int32_t safeSubtract(int32_t a, int32_t b) { |
39 | 0 | // Note: In C++, signed integer subtraction is undefined behavior. |
40 | 0 | int32_t diff = static_cast<int32_t>(static_cast<uint32_t>(a) - static_cast<uint32_t>(b)); |
41 | 0 | if (b < 0 && diff < a) { return INT32_MAX; } |
42 | 0 | if (b > 0 && diff > a) { return INT32_MIN; } |
43 | 0 | return diff; |
44 | 0 | } |
45 | | |
46 | | static double DOUBLE_MULTIPLIERS[] = { |
47 | | 1e0, |
48 | | 1e1, |
49 | | 1e2, |
50 | | 1e3, |
51 | | 1e4, |
52 | | 1e5, |
53 | | 1e6, |
54 | | 1e7, |
55 | | 1e8, |
56 | | 1e9, |
57 | | 1e10, |
58 | | 1e11, |
59 | | 1e12, |
60 | | 1e13, |
61 | | 1e14, |
62 | | 1e15, |
63 | | 1e16, |
64 | | 1e17, |
65 | | 1e18, |
66 | | 1e19, |
67 | | 1e20, |
68 | | 1e21}; |
69 | | |
70 | | } // namespace |
71 | | |
72 | 0 | icu::IFixedDecimal::~IFixedDecimal() = default; |
73 | | |
74 | 0 | DecimalQuantity::DecimalQuantity() { |
75 | 0 | setBcdToZero(); |
76 | 0 | flags = 0; |
77 | 0 | } |
78 | | |
79 | 0 | DecimalQuantity::~DecimalQuantity() { |
80 | 0 | if (usingBytes) { |
81 | 0 | uprv_free(fBCD.bcdBytes.ptr); |
82 | 0 | fBCD.bcdBytes.ptr = nullptr; |
83 | 0 | usingBytes = false; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | DecimalQuantity::DecimalQuantity(const DecimalQuantity &other) { |
88 | 0 | *this = other; |
89 | 0 | } |
90 | | |
91 | 0 | DecimalQuantity::DecimalQuantity(DecimalQuantity&& src) U_NOEXCEPT { |
92 | 0 | *this = std::move(src); |
93 | 0 | } |
94 | | |
95 | 0 | DecimalQuantity &DecimalQuantity::operator=(const DecimalQuantity &other) { |
96 | 0 | if (this == &other) { |
97 | 0 | return *this; |
98 | 0 | } |
99 | 0 | copyBcdFrom(other); |
100 | 0 | copyFieldsFrom(other); |
101 | 0 | return *this; |
102 | 0 | } |
103 | | |
104 | 0 | DecimalQuantity& DecimalQuantity::operator=(DecimalQuantity&& src) U_NOEXCEPT { |
105 | 0 | if (this == &src) { |
106 | 0 | return *this; |
107 | 0 | } |
108 | 0 | moveBcdFrom(src); |
109 | 0 | copyFieldsFrom(src); |
110 | 0 | return *this; |
111 | 0 | } |
112 | | |
113 | 0 | void DecimalQuantity::copyFieldsFrom(const DecimalQuantity& other) { |
114 | 0 | bogus = other.bogus; |
115 | 0 | lOptPos = other.lOptPos; |
116 | 0 | lReqPos = other.lReqPos; |
117 | 0 | rReqPos = other.rReqPos; |
118 | 0 | rOptPos = other.rOptPos; |
119 | 0 | scale = other.scale; |
120 | 0 | precision = other.precision; |
121 | 0 | flags = other.flags; |
122 | 0 | origDouble = other.origDouble; |
123 | 0 | origDelta = other.origDelta; |
124 | 0 | isApproximate = other.isApproximate; |
125 | 0 | } |
126 | | |
127 | 0 | void DecimalQuantity::clear() { |
128 | 0 | lOptPos = INT32_MAX; |
129 | 0 | lReqPos = 0; |
130 | 0 | rReqPos = 0; |
131 | 0 | rOptPos = INT32_MIN; |
132 | 0 | flags = 0; |
133 | 0 | setBcdToZero(); // sets scale, precision, hasDouble, origDouble, origDelta, and BCD data |
134 | 0 | } |
135 | | |
136 | 0 | void DecimalQuantity::setIntegerLength(int32_t minInt, int32_t maxInt) { |
137 | 0 | // Validation should happen outside of DecimalQuantity, e.g., in the Precision class. |
138 | 0 | U_ASSERT(minInt >= 0); |
139 | 0 | U_ASSERT(maxInt >= minInt); |
140 | 0 |
|
141 | 0 | // Special behavior: do not set minInt to be less than what is already set. |
142 | 0 | // This is so significant digits rounding can set the integer length. |
143 | 0 | if (minInt < lReqPos) { |
144 | 0 | minInt = lReqPos; |
145 | 0 | } |
146 | 0 |
|
147 | 0 | // Save values into internal state |
148 | 0 | // Negation is safe for minFrac/maxFrac because -Integer.MAX_VALUE > Integer.MIN_VALUE |
149 | 0 | lOptPos = maxInt; |
150 | 0 | lReqPos = minInt; |
151 | 0 | } |
152 | | |
153 | 0 | void DecimalQuantity::setFractionLength(int32_t minFrac, int32_t maxFrac) { |
154 | 0 | // Validation should happen outside of DecimalQuantity, e.g., in the Precision class. |
155 | 0 | U_ASSERT(minFrac >= 0); |
156 | 0 | U_ASSERT(maxFrac >= minFrac); |
157 | 0 |
|
158 | 0 | // Save values into internal state |
159 | 0 | // Negation is safe for minFrac/maxFrac because -Integer.MAX_VALUE > Integer.MIN_VALUE |
160 | 0 | rReqPos = -minFrac; |
161 | 0 | rOptPos = -maxFrac; |
162 | 0 | } |
163 | | |
164 | 0 | uint64_t DecimalQuantity::getPositionFingerprint() const { |
165 | 0 | uint64_t fingerprint = 0; |
166 | 0 | fingerprint ^= lOptPos; |
167 | 0 | fingerprint ^= (lReqPos << 16); |
168 | 0 | fingerprint ^= (static_cast<uint64_t>(rReqPos) << 32); |
169 | 0 | fingerprint ^= (static_cast<uint64_t>(rOptPos) << 48); |
170 | 0 | return fingerprint; |
171 | 0 | } |
172 | | |
173 | | void DecimalQuantity::roundToIncrement(double roundingIncrement, RoundingMode roundingMode, |
174 | 0 | int32_t maxFrac, UErrorCode& status) { |
175 | 0 | // TODO(13701): This is innefficient. Improve? |
176 | 0 | // TODO(13701): Should we convert to decNumber instead? |
177 | 0 | roundToInfinity(); |
178 | 0 | double temp = toDouble(); |
179 | 0 | temp /= roundingIncrement; |
180 | 0 | // Use another DecimalQuantity to perform the actual rounding... |
181 | 0 | DecimalQuantity dq; |
182 | 0 | dq.setToDouble(temp); |
183 | 0 | dq.roundToMagnitude(0, roundingMode, status); |
184 | 0 | temp = dq.toDouble(); |
185 | 0 | temp *= roundingIncrement; |
186 | 0 | setToDouble(temp); |
187 | 0 | // Since we reset the value to a double, we need to specify the rounding boundary |
188 | 0 | // in order to get the DecimalQuantity out of approximation mode. |
189 | 0 | // NOTE: In Java, we have minMaxFrac, but in C++, the two are differentiated. |
190 | 0 | roundToMagnitude(-maxFrac, roundingMode, status); |
191 | 0 | } |
192 | | |
193 | 0 | void DecimalQuantity::multiplyBy(const DecNum& multiplicand, UErrorCode& status) { |
194 | 0 | if (isInfinite() || isZero() || isNaN()) { |
195 | 0 | return; |
196 | 0 | } |
197 | 0 | // Convert to DecNum, multiply, and convert back. |
198 | 0 | DecNum decnum; |
199 | 0 | toDecNum(decnum, status); |
200 | 0 | if (U_FAILURE(status)) { return; } |
201 | 0 | decnum.multiplyBy(multiplicand, status); |
202 | 0 | if (U_FAILURE(status)) { return; } |
203 | 0 | setToDecNum(decnum, status); |
204 | 0 | } |
205 | | |
206 | 0 | void DecimalQuantity::divideBy(const DecNum& divisor, UErrorCode& status) { |
207 | 0 | if (isInfinite() || isZero() || isNaN()) { |
208 | 0 | return; |
209 | 0 | } |
210 | 0 | // Convert to DecNum, multiply, and convert back. |
211 | 0 | DecNum decnum; |
212 | 0 | toDecNum(decnum, status); |
213 | 0 | if (U_FAILURE(status)) { return; } |
214 | 0 | decnum.divideBy(divisor, status); |
215 | 0 | if (U_FAILURE(status)) { return; } |
216 | 0 | setToDecNum(decnum, status); |
217 | 0 | } |
218 | | |
219 | 0 | void DecimalQuantity::negate() { |
220 | 0 | flags ^= NEGATIVE_FLAG; |
221 | 0 | } |
222 | | |
223 | 0 | int32_t DecimalQuantity::getMagnitude() const { |
224 | 0 | U_ASSERT(precision != 0); |
225 | 0 | return scale + precision - 1; |
226 | 0 | } |
227 | | |
228 | 0 | bool DecimalQuantity::adjustMagnitude(int32_t delta) { |
229 | 0 | if (precision != 0) { |
230 | 0 | // i.e., scale += delta; origDelta += delta |
231 | 0 | bool overflow = uprv_add32_overflow(scale, delta, &scale); |
232 | 0 | overflow = uprv_add32_overflow(origDelta, delta, &origDelta) || overflow; |
233 | 0 | // Make sure that precision + scale won't overflow, either |
234 | 0 | int32_t dummy; |
235 | 0 | overflow = overflow || uprv_add32_overflow(scale, precision, &dummy); |
236 | 0 | return overflow; |
237 | 0 | } |
238 | 0 | return false; |
239 | 0 | } |
240 | | |
241 | 0 | double DecimalQuantity::getPluralOperand(PluralOperand operand) const { |
242 | 0 | // If this assertion fails, you need to call roundToInfinity() or some other rounding method. |
243 | 0 | // See the comment at the top of this file explaining the "isApproximate" field. |
244 | 0 | U_ASSERT(!isApproximate); |
245 | 0 |
|
246 | 0 | switch (operand) { |
247 | 0 | case PLURAL_OPERAND_I: |
248 | 0 | // Invert the negative sign if necessary |
249 | 0 | return static_cast<double>(isNegative() ? -toLong(true) : toLong(true)); |
250 | 0 | case PLURAL_OPERAND_F: |
251 | 0 | return static_cast<double>(toFractionLong(true)); |
252 | 0 | case PLURAL_OPERAND_T: |
253 | 0 | return static_cast<double>(toFractionLong(false)); |
254 | 0 | case PLURAL_OPERAND_V: |
255 | 0 | return fractionCount(); |
256 | 0 | case PLURAL_OPERAND_W: |
257 | 0 | return fractionCountWithoutTrailingZeros(); |
258 | 0 | default: |
259 | 0 | return std::abs(toDouble()); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | 0 | bool DecimalQuantity::hasIntegerValue() const { |
264 | 0 | return scale >= 0; |
265 | 0 | } |
266 | | |
267 | 0 | int32_t DecimalQuantity::getUpperDisplayMagnitude() const { |
268 | 0 | // If this assertion fails, you need to call roundToInfinity() or some other rounding method. |
269 | 0 | // See the comment in the header file explaining the "isApproximate" field. |
270 | 0 | U_ASSERT(!isApproximate); |
271 | 0 |
|
272 | 0 | int32_t magnitude = scale + precision; |
273 | 0 | int32_t result = (lReqPos > magnitude) ? lReqPos : (lOptPos < magnitude) ? lOptPos : magnitude; |
274 | 0 | return result - 1; |
275 | 0 | } |
276 | | |
277 | 0 | int32_t DecimalQuantity::getLowerDisplayMagnitude() const { |
278 | 0 | // If this assertion fails, you need to call roundToInfinity() or some other rounding method. |
279 | 0 | // See the comment in the header file explaining the "isApproximate" field. |
280 | 0 | U_ASSERT(!isApproximate); |
281 | 0 |
|
282 | 0 | int32_t magnitude = scale; |
283 | 0 | int32_t result = (rReqPos < magnitude) ? rReqPos : (rOptPos > magnitude) ? rOptPos : magnitude; |
284 | 0 | return result; |
285 | 0 | } |
286 | | |
287 | 0 | int8_t DecimalQuantity::getDigit(int32_t magnitude) const { |
288 | 0 | // If this assertion fails, you need to call roundToInfinity() or some other rounding method. |
289 | 0 | // See the comment at the top of this file explaining the "isApproximate" field. |
290 | 0 | U_ASSERT(!isApproximate); |
291 | 0 |
|
292 | 0 | return getDigitPos(magnitude - scale); |
293 | 0 | } |
294 | | |
295 | 0 | int32_t DecimalQuantity::fractionCount() const { |
296 | 0 | return -getLowerDisplayMagnitude(); |
297 | 0 | } |
298 | | |
299 | 0 | int32_t DecimalQuantity::fractionCountWithoutTrailingZeros() const { |
300 | 0 | return -scale > 0 ? -scale : 0; // max(-scale, 0) |
301 | 0 | } |
302 | | |
303 | 0 | bool DecimalQuantity::isNegative() const { |
304 | 0 | return (flags & NEGATIVE_FLAG) != 0; |
305 | 0 | } |
306 | | |
307 | 0 | int8_t DecimalQuantity::signum() const { |
308 | 0 | return isNegative() ? -1 : isZero() ? 0 : 1; |
309 | 0 | } |
310 | | |
311 | 0 | bool DecimalQuantity::isInfinite() const { |
312 | 0 | return (flags & INFINITY_FLAG) != 0; |
313 | 0 | } |
314 | | |
315 | 0 | bool DecimalQuantity::isNaN() const { |
316 | 0 | return (flags & NAN_FLAG) != 0; |
317 | 0 | } |
318 | | |
319 | 0 | bool DecimalQuantity::isZero() const { |
320 | 0 | return precision == 0; |
321 | 0 | } |
322 | | |
323 | 0 | DecimalQuantity &DecimalQuantity::setToInt(int32_t n) { |
324 | 0 | setBcdToZero(); |
325 | 0 | flags = 0; |
326 | 0 | if (n == INT32_MIN) { |
327 | 0 | flags |= NEGATIVE_FLAG; |
328 | 0 | // leave as INT32_MIN; handled below in _setToInt() |
329 | 0 | } else if (n < 0) { |
330 | 0 | flags |= NEGATIVE_FLAG; |
331 | 0 | n = -n; |
332 | 0 | } |
333 | 0 | if (n != 0) { |
334 | 0 | _setToInt(n); |
335 | 0 | compact(); |
336 | 0 | } |
337 | 0 | return *this; |
338 | 0 | } |
339 | | |
340 | 0 | void DecimalQuantity::_setToInt(int32_t n) { |
341 | 0 | if (n == INT32_MIN) { |
342 | 0 | readLongToBcd(-static_cast<int64_t>(n)); |
343 | 0 | } else { |
344 | 0 | readIntToBcd(n); |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | DecimalQuantity &DecimalQuantity::setToLong(int64_t n) { |
349 | 0 | setBcdToZero(); |
350 | 0 | flags = 0; |
351 | 0 | if (n < 0 && n > INT64_MIN) { |
352 | 0 | flags |= NEGATIVE_FLAG; |
353 | 0 | n = -n; |
354 | 0 | } |
355 | 0 | if (n != 0) { |
356 | 0 | _setToLong(n); |
357 | 0 | compact(); |
358 | 0 | } |
359 | 0 | return *this; |
360 | 0 | } |
361 | | |
362 | 0 | void DecimalQuantity::_setToLong(int64_t n) { |
363 | 0 | if (n == INT64_MIN) { |
364 | 0 | DecNum decnum; |
365 | 0 | UErrorCode localStatus = U_ZERO_ERROR; |
366 | 0 | decnum.setTo("9.223372036854775808E+18", localStatus); |
367 | 0 | if (U_FAILURE(localStatus)) { return; } // unexpected |
368 | 0 | flags |= NEGATIVE_FLAG; |
369 | 0 | readDecNumberToBcd(decnum); |
370 | 0 | } else if (n <= INT32_MAX) { |
371 | 0 | readIntToBcd(static_cast<int32_t>(n)); |
372 | 0 | } else { |
373 | 0 | readLongToBcd(n); |
374 | 0 | } |
375 | 0 | } |
376 | | |
377 | 0 | DecimalQuantity &DecimalQuantity::setToDouble(double n) { |
378 | 0 | setBcdToZero(); |
379 | 0 | flags = 0; |
380 | 0 | // signbit() from <math.h> handles +0.0 vs -0.0 |
381 | 0 | if (std::signbit(n)) { |
382 | 0 | flags |= NEGATIVE_FLAG; |
383 | 0 | n = -n; |
384 | 0 | } |
385 | 0 | if (std::isnan(n) != 0) { |
386 | 0 | flags |= NAN_FLAG; |
387 | 0 | } else if (std::isfinite(n) == 0) { |
388 | 0 | flags |= INFINITY_FLAG; |
389 | 0 | } else if (n != 0) { |
390 | 0 | _setToDoubleFast(n); |
391 | 0 | compact(); |
392 | 0 | } |
393 | 0 | return *this; |
394 | 0 | } |
395 | | |
396 | 0 | void DecimalQuantity::_setToDoubleFast(double n) { |
397 | 0 | isApproximate = true; |
398 | 0 | origDouble = n; |
399 | 0 | origDelta = 0; |
400 | 0 |
|
401 | 0 | // Make sure the double is an IEEE 754 double. If not, fall back to the slow path right now. |
402 | 0 | // TODO: Make a fast path for other types of doubles. |
403 | 0 | if (!std::numeric_limits<double>::is_iec559) { |
404 | 0 | convertToAccurateDouble(); |
405 | 0 | // Turn off the approximate double flag, since the value is now exact. |
406 | 0 | isApproximate = false; |
407 | 0 | origDouble = 0.0; |
408 | 0 | return; |
409 | 0 | } |
410 | 0 | |
411 | 0 | // To get the bits from the double, use memcpy, which takes care of endianness. |
412 | 0 | uint64_t ieeeBits; |
413 | 0 | uprv_memcpy(&ieeeBits, &n, sizeof(n)); |
414 | 0 | int32_t exponent = static_cast<int32_t>((ieeeBits & 0x7ff0000000000000L) >> 52) - 0x3ff; |
415 | 0 |
|
416 | 0 | // Not all integers can be represented exactly for exponent > 52 |
417 | 0 | if (exponent <= 52 && static_cast<int64_t>(n) == n) { |
418 | 0 | _setToLong(static_cast<int64_t>(n)); |
419 | 0 | return; |
420 | 0 | } |
421 | 0 | |
422 | 0 | // 3.3219... is log2(10) |
423 | 0 | auto fracLength = static_cast<int32_t> ((52 - exponent) / 3.32192809489); |
424 | 0 | if (fracLength >= 0) { |
425 | 0 | int32_t i = fracLength; |
426 | 0 | // 1e22 is the largest exact double. |
427 | 0 | for (; i >= 22; i -= 22) n *= 1e22; |
428 | 0 | n *= DOUBLE_MULTIPLIERS[i]; |
429 | 0 | } else { |
430 | 0 | int32_t i = fracLength; |
431 | 0 | // 1e22 is the largest exact double. |
432 | 0 | for (; i <= -22; i += 22) n /= 1e22; |
433 | 0 | n /= DOUBLE_MULTIPLIERS[-i]; |
434 | 0 | } |
435 | 0 | auto result = static_cast<int64_t>(std::round(n)); |
436 | 0 | if (result != 0) { |
437 | 0 | _setToLong(result); |
438 | 0 | scale -= fracLength; |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | 0 | void DecimalQuantity::convertToAccurateDouble() { |
443 | 0 | U_ASSERT(origDouble != 0); |
444 | 0 | int32_t delta = origDelta; |
445 | 0 |
|
446 | 0 | // Call the slow oracle function (Double.toString in Java, DoubleToAscii in C++). |
447 | 0 | char buffer[DoubleToStringConverter::kBase10MaximalLength + 1]; |
448 | 0 | bool sign; // unused; always positive |
449 | 0 | int32_t length; |
450 | 0 | int32_t point; |
451 | 0 | DoubleToStringConverter::DoubleToAscii( |
452 | 0 | origDouble, |
453 | 0 | DoubleToStringConverter::DtoaMode::SHORTEST, |
454 | 0 | 0, |
455 | 0 | buffer, |
456 | 0 | sizeof(buffer), |
457 | 0 | &sign, |
458 | 0 | &length, |
459 | 0 | &point |
460 | 0 | ); |
461 | 0 |
|
462 | 0 | setBcdToZero(); |
463 | 0 | readDoubleConversionToBcd(buffer, length, point); |
464 | 0 | scale += delta; |
465 | 0 | explicitExactDouble = true; |
466 | 0 | } |
467 | | |
468 | 0 | DecimalQuantity &DecimalQuantity::setToDecNumber(StringPiece n, UErrorCode& status) { |
469 | 0 | setBcdToZero(); |
470 | 0 | flags = 0; |
471 | 0 |
|
472 | 0 | // Compute the decNumber representation |
473 | 0 | DecNum decnum; |
474 | 0 | decnum.setTo(n, status); |
475 | 0 |
|
476 | 0 | _setToDecNum(decnum, status); |
477 | 0 | return *this; |
478 | 0 | } |
479 | | |
480 | 0 | DecimalQuantity& DecimalQuantity::setToDecNum(const DecNum& decnum, UErrorCode& status) { |
481 | 0 | setBcdToZero(); |
482 | 0 | flags = 0; |
483 | 0 |
|
484 | 0 | _setToDecNum(decnum, status); |
485 | 0 | return *this; |
486 | 0 | } |
487 | | |
488 | 0 | void DecimalQuantity::_setToDecNum(const DecNum& decnum, UErrorCode& status) { |
489 | 0 | if (U_FAILURE(status)) { return; } |
490 | 0 | if (decnum.isNegative()) { |
491 | 0 | flags |= NEGATIVE_FLAG; |
492 | 0 | } |
493 | 0 | if (!decnum.isZero()) { |
494 | 0 | readDecNumberToBcd(decnum); |
495 | 0 | compact(); |
496 | 0 | } |
497 | 0 | } |
498 | | |
499 | 0 | int64_t DecimalQuantity::toLong(bool truncateIfOverflow) const { |
500 | 0 | // NOTE: Call sites should be guarded by fitsInLong(), like this: |
501 | 0 | // if (dq.fitsInLong()) { /* use dq.toLong() */ } else { /* use some fallback */ } |
502 | 0 | // Fallback behavior upon truncateIfOverflow is to truncate at 17 digits. |
503 | 0 | uint64_t result = 0L; |
504 | 0 | int32_t upperMagnitude = std::min(scale + precision, lOptPos) - 1; |
505 | 0 | if (truncateIfOverflow) { |
506 | 0 | upperMagnitude = std::min(upperMagnitude, 17); |
507 | 0 | } |
508 | 0 | for (int32_t magnitude = upperMagnitude; magnitude >= 0; magnitude--) { |
509 | 0 | result = result * 10 + getDigitPos(magnitude - scale); |
510 | 0 | } |
511 | 0 | if (isNegative()) { |
512 | 0 | return static_cast<int64_t>(0LL - result); // i.e., -result |
513 | 0 | } |
514 | 0 | return static_cast<int64_t>(result); |
515 | 0 | } |
516 | | |
517 | 0 | uint64_t DecimalQuantity::toFractionLong(bool includeTrailingZeros) const { |
518 | 0 | uint64_t result = 0L; |
519 | 0 | int32_t magnitude = -1; |
520 | 0 | int32_t lowerMagnitude = std::max(scale, rOptPos); |
521 | 0 | if (includeTrailingZeros) { |
522 | 0 | lowerMagnitude = std::min(lowerMagnitude, rReqPos); |
523 | 0 | } |
524 | 0 | for (; magnitude >= lowerMagnitude && result <= 1e18L; magnitude--) { |
525 | 0 | result = result * 10 + getDigitPos(magnitude - scale); |
526 | 0 | } |
527 | 0 | // Remove trailing zeros; this can happen during integer overflow cases. |
528 | 0 | if (!includeTrailingZeros) { |
529 | 0 | while (result > 0 && (result % 10) == 0) { |
530 | 0 | result /= 10; |
531 | 0 | } |
532 | 0 | } |
533 | 0 | return result; |
534 | 0 | } |
535 | | |
536 | 0 | bool DecimalQuantity::fitsInLong(bool ignoreFraction) const { |
537 | 0 | if (isZero()) { |
538 | 0 | return true; |
539 | 0 | } |
540 | 0 | if (scale < 0 && !ignoreFraction) { |
541 | 0 | return false; |
542 | 0 | } |
543 | 0 | int magnitude = getMagnitude(); |
544 | 0 | if (magnitude < 18) { |
545 | 0 | return true; |
546 | 0 | } |
547 | 0 | if (magnitude > 18) { |
548 | 0 | return false; |
549 | 0 | } |
550 | 0 | // Hard case: the magnitude is 10^18. |
551 | 0 | // The largest int64 is: 9,223,372,036,854,775,807 |
552 | 0 | for (int p = 0; p < precision; p++) { |
553 | 0 | int8_t digit = getDigit(18 - p); |
554 | 0 | static int8_t INT64_BCD[] = { 9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8 }; |
555 | 0 | if (digit < INT64_BCD[p]) { |
556 | 0 | return true; |
557 | 0 | } else if (digit > INT64_BCD[p]) { |
558 | 0 | return false; |
559 | 0 | } |
560 | 0 | } |
561 | 0 | // Exactly equal to max long plus one. |
562 | 0 | return isNegative(); |
563 | 0 | } |
564 | | |
565 | 0 | double DecimalQuantity::toDouble() const { |
566 | 0 | // If this assertion fails, you need to call roundToInfinity() or some other rounding method. |
567 | 0 | // See the comment in the header file explaining the "isApproximate" field. |
568 | 0 | U_ASSERT(!isApproximate); |
569 | 0 |
|
570 | 0 | if (isNaN()) { |
571 | 0 | return NAN; |
572 | 0 | } else if (isInfinite()) { |
573 | 0 | return isNegative() ? -INFINITY : INFINITY; |
574 | 0 | } |
575 | 0 |
|
576 | 0 | // We are processing well-formed input, so we don't need any special options to StringToDoubleConverter. |
577 | 0 | StringToDoubleConverter converter(0, 0, 0, "", ""); |
578 | 0 | UnicodeString numberString = this->toScientificString(); |
579 | 0 | int32_t count; |
580 | 0 | return converter.StringToDouble( |
581 | 0 | reinterpret_cast<const uint16_t*>(numberString.getBuffer()), |
582 | 0 | numberString.length(), |
583 | 0 | &count); |
584 | 0 | } |
585 | | |
586 | 0 | void DecimalQuantity::toDecNum(DecNum& output, UErrorCode& status) const { |
587 | 0 | // Special handling for zero |
588 | 0 | if (precision == 0) { |
589 | 0 | output.setTo("0", status); |
590 | 0 | } |
591 | 0 |
|
592 | 0 | // Use the BCD constructor. We need to do a little bit of work to convert, though. |
593 | 0 | // The decNumber constructor expects most-significant first, but we store least-significant first. |
594 | 0 | MaybeStackArray<uint8_t, 20> ubcd(precision); |
595 | 0 | for (int32_t m = 0; m < precision; m++) { |
596 | 0 | ubcd[precision - m - 1] = static_cast<uint8_t>(getDigitPos(m)); |
597 | 0 | } |
598 | 0 | output.setTo(ubcd.getAlias(), precision, scale, isNegative(), status); |
599 | 0 | } |
600 | | |
601 | 0 | void DecimalQuantity::truncate() { |
602 | 0 | if (scale < 0) { |
603 | 0 | shiftRight(-scale); |
604 | 0 | scale = 0; |
605 | 0 | compact(); |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | 0 | void DecimalQuantity::roundToMagnitude(int32_t magnitude, RoundingMode roundingMode, UErrorCode& status) { |
610 | 0 | // The position in the BCD at which rounding will be performed; digits to the right of position |
611 | 0 | // will be rounded away. |
612 | 0 | // TODO: Andy: There was a test failure because of integer overflow here. Should I do |
613 | 0 | // "safe subtraction" everywhere in the code? What's the nicest way to do it? |
614 | 0 | int position = safeSubtract(magnitude, scale); |
615 | 0 |
|
616 | 0 | if (position <= 0 && !isApproximate) { |
617 | 0 | // All digits are to the left of the rounding magnitude. |
618 | 0 | } else if (precision == 0) { |
619 | 0 | // No rounding for zero. |
620 | 0 | } else { |
621 | 0 | // Perform rounding logic. |
622 | 0 | // "leading" = most significant digit to the right of rounding |
623 | 0 | // "trailing" = least significant digit to the left of rounding |
624 | 0 | int8_t leadingDigit = getDigitPos(safeSubtract(position, 1)); |
625 | 0 | int8_t trailingDigit = getDigitPos(position); |
626 | 0 |
|
627 | 0 | // Compute which section of the number we are in. |
628 | 0 | // EDGE means we are at the bottom or top edge, like 1.000 or 1.999 (used by doubles) |
629 | 0 | // LOWER means we are between the bottom edge and the midpoint, like 1.391 |
630 | 0 | // MIDPOINT means we are exactly in the middle, like 1.500 |
631 | 0 | // UPPER means we are between the midpoint and the top edge, like 1.916 |
632 | 0 | roundingutils::Section section = roundingutils::SECTION_MIDPOINT; |
633 | 0 | if (!isApproximate) { |
634 | 0 | if (leadingDigit < 5) { |
635 | 0 | section = roundingutils::SECTION_LOWER; |
636 | 0 | } else if (leadingDigit > 5) { |
637 | 0 | section = roundingutils::SECTION_UPPER; |
638 | 0 | } else { |
639 | 0 | for (int p = safeSubtract(position, 2); p >= 0; p--) { |
640 | 0 | if (getDigitPos(p) != 0) { |
641 | 0 | section = roundingutils::SECTION_UPPER; |
642 | 0 | break; |
643 | 0 | } |
644 | 0 | } |
645 | 0 | } |
646 | 0 | } else { |
647 | 0 | int32_t p = safeSubtract(position, 2); |
648 | 0 | int32_t minP = uprv_max(0, precision - 14); |
649 | 0 | if (leadingDigit == 0) { |
650 | 0 | section = roundingutils::SECTION_LOWER_EDGE; |
651 | 0 | for (; p >= minP; p--) { |
652 | 0 | if (getDigitPos(p) != 0) { |
653 | 0 | section = roundingutils::SECTION_LOWER; |
654 | 0 | break; |
655 | 0 | } |
656 | 0 | } |
657 | 0 | } else if (leadingDigit == 4) { |
658 | 0 | for (; p >= minP; p--) { |
659 | 0 | if (getDigitPos(p) != 9) { |
660 | 0 | section = roundingutils::SECTION_LOWER; |
661 | 0 | break; |
662 | 0 | } |
663 | 0 | } |
664 | 0 | } else if (leadingDigit == 5) { |
665 | 0 | for (; p >= minP; p--) { |
666 | 0 | if (getDigitPos(p) != 0) { |
667 | 0 | section = roundingutils::SECTION_UPPER; |
668 | 0 | break; |
669 | 0 | } |
670 | 0 | } |
671 | 0 | } else if (leadingDigit == 9) { |
672 | 0 | section = roundingutils::SECTION_UPPER_EDGE; |
673 | 0 | for (; p >= minP; p--) { |
674 | 0 | if (getDigitPos(p) != 9) { |
675 | 0 | section = roundingutils::SECTION_UPPER; |
676 | 0 | break; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | } else if (leadingDigit < 5) { |
680 | 0 | section = roundingutils::SECTION_LOWER; |
681 | 0 | } else { |
682 | 0 | section = roundingutils::SECTION_UPPER; |
683 | 0 | } |
684 | 0 |
|
685 | 0 | bool roundsAtMidpoint = roundingutils::roundsAtMidpoint(roundingMode); |
686 | 0 | if (safeSubtract(position, 1) < precision - 14 || |
687 | 0 | (roundsAtMidpoint && section == roundingutils::SECTION_MIDPOINT) || |
688 | 0 | (!roundsAtMidpoint && section < 0 /* i.e. at upper or lower edge */)) { |
689 | 0 | // Oops! This means that we have to get the exact representation of the double, because |
690 | 0 | // the zone of uncertainty is along the rounding boundary. |
691 | 0 | convertToAccurateDouble(); |
692 | 0 | roundToMagnitude(magnitude, roundingMode, status); // start over |
693 | 0 | return; |
694 | 0 | } |
695 | 0 | |
696 | 0 | // Turn off the approximate double flag, since the value is now confirmed to be exact. |
697 | 0 | isApproximate = false; |
698 | 0 | origDouble = 0.0; |
699 | 0 | origDelta = 0; |
700 | 0 |
|
701 | 0 | if (position <= 0) { |
702 | 0 | // All digits are to the left of the rounding magnitude. |
703 | 0 | return; |
704 | 0 | } |
705 | 0 | |
706 | 0 | // Good to continue rounding. |
707 | 0 | if (section == -1) { section = roundingutils::SECTION_LOWER; } |
708 | 0 | if (section == -2) { section = roundingutils::SECTION_UPPER; } |
709 | 0 | } |
710 | 0 |
|
711 | 0 | bool roundDown = roundingutils::getRoundingDirection((trailingDigit % 2) == 0, |
712 | 0 | isNegative(), |
713 | 0 | section, |
714 | 0 | roundingMode, |
715 | 0 | status); |
716 | 0 | if (U_FAILURE(status)) { |
717 | 0 | return; |
718 | 0 | } |
719 | 0 | |
720 | 0 | // Perform truncation |
721 | 0 | if (position >= precision) { |
722 | 0 | setBcdToZero(); |
723 | 0 | scale = magnitude; |
724 | 0 | } else { |
725 | 0 | shiftRight(position); |
726 | 0 | } |
727 | 0 |
|
728 | 0 | // Bubble the result to the higher digits |
729 | 0 | if (!roundDown) { |
730 | 0 | if (trailingDigit == 9) { |
731 | 0 | int bubblePos = 0; |
732 | 0 | // Note: in the long implementation, the most digits BCD can have at this point is 15, |
733 | 0 | // so bubblePos <= 15 and getDigitPos(bubblePos) is safe. |
734 | 0 | for (; getDigitPos(bubblePos) == 9; bubblePos++) {} |
735 | 0 | shiftRight(bubblePos); // shift off the trailing 9s |
736 | 0 | } |
737 | 0 | int8_t digit0 = getDigitPos(0); |
738 | 0 | U_ASSERT(digit0 != 9); |
739 | 0 | setDigitPos(0, static_cast<int8_t>(digit0 + 1)); |
740 | 0 | precision += 1; // in case an extra digit got added |
741 | 0 | } |
742 | 0 |
|
743 | 0 | compact(); |
744 | 0 | } |
745 | 0 | } |
746 | | |
747 | 0 | void DecimalQuantity::roundToInfinity() { |
748 | 0 | if (isApproximate) { |
749 | 0 | convertToAccurateDouble(); |
750 | 0 | } |
751 | 0 | } |
752 | | |
753 | 0 | void DecimalQuantity::appendDigit(int8_t value, int32_t leadingZeros, bool appendAsInteger) { |
754 | 0 | U_ASSERT(leadingZeros >= 0); |
755 | 0 |
|
756 | 0 | // Zero requires special handling to maintain the invariant that the least-significant digit |
757 | 0 | // in the BCD is nonzero. |
758 | 0 | if (value == 0) { |
759 | 0 | if (appendAsInteger && precision != 0) { |
760 | 0 | scale += leadingZeros + 1; |
761 | 0 | } |
762 | 0 | return; |
763 | 0 | } |
764 | 0 |
|
765 | 0 | // Deal with trailing zeros |
766 | 0 | if (scale > 0) { |
767 | 0 | leadingZeros += scale; |
768 | 0 | if (appendAsInteger) { |
769 | 0 | scale = 0; |
770 | 0 | } |
771 | 0 | } |
772 | 0 |
|
773 | 0 | // Append digit |
774 | 0 | shiftLeft(leadingZeros + 1); |
775 | 0 | setDigitPos(0, value); |
776 | 0 |
|
777 | 0 | // Fix scale if in integer mode |
778 | 0 | if (appendAsInteger) { |
779 | 0 | scale += leadingZeros + 1; |
780 | 0 | } |
781 | 0 | } |
782 | | |
783 | 0 | UnicodeString DecimalQuantity::toPlainString() const { |
784 | 0 | U_ASSERT(!isApproximate); |
785 | 0 | UnicodeString sb; |
786 | 0 | if (isNegative()) { |
787 | 0 | sb.append(u'-'); |
788 | 0 | } |
789 | 0 | if (precision == 0 || getMagnitude() < 0) { |
790 | 0 | sb.append(u'0'); |
791 | 0 | } |
792 | 0 | for (int m = getUpperDisplayMagnitude(); m >= getLowerDisplayMagnitude(); m--) { |
793 | 0 | if (m == -1) { sb.append(u'.'); } |
794 | 0 | sb.append(getDigit(m) + u'0'); |
795 | 0 | } |
796 | 0 | return sb; |
797 | 0 | } |
798 | | |
799 | 0 | UnicodeString DecimalQuantity::toScientificString() const { |
800 | 0 | U_ASSERT(!isApproximate); |
801 | 0 | UnicodeString result; |
802 | 0 | if (isNegative()) { |
803 | 0 | result.append(u'-'); |
804 | 0 | } |
805 | 0 | if (precision == 0) { |
806 | 0 | result.append(u"0E+0", -1); |
807 | 0 | return result; |
808 | 0 | } |
809 | 0 | // NOTE: It is not safe to add to lOptPos (aka maxInt) or subtract from |
810 | 0 | // rOptPos (aka -maxFrac) due to overflow. |
811 | 0 | int32_t upperPos = std::min(precision + scale, lOptPos) - scale - 1; |
812 | 0 | int32_t lowerPos = std::max(scale, rOptPos) - scale; |
813 | 0 | int32_t p = upperPos; |
814 | 0 | result.append(u'0' + getDigitPos(p)); |
815 | 0 | if ((--p) >= lowerPos) { |
816 | 0 | result.append(u'.'); |
817 | 0 | for (; p >= lowerPos; p--) { |
818 | 0 | result.append(u'0' + getDigitPos(p)); |
819 | 0 | } |
820 | 0 | } |
821 | 0 | result.append(u'E'); |
822 | 0 | int32_t _scale = upperPos + scale; |
823 | 0 | if (_scale < 0) { |
824 | 0 | _scale *= -1; |
825 | 0 | result.append(u'-'); |
826 | 0 | } else { |
827 | 0 | result.append(u'+'); |
828 | 0 | } |
829 | 0 | if (_scale == 0) { |
830 | 0 | result.append(u'0'); |
831 | 0 | } |
832 | 0 | int32_t insertIndex = result.length(); |
833 | 0 | while (_scale > 0) { |
834 | 0 | std::div_t res = std::div(_scale, 10); |
835 | 0 | result.insert(insertIndex, u'0' + res.rem); |
836 | 0 | _scale = res.quot; |
837 | 0 | } |
838 | 0 | return result; |
839 | 0 | } |
840 | | |
841 | | //////////////////////////////////////////////////// |
842 | | /// End of DecimalQuantity_AbstractBCD.java /// |
843 | | /// Start of DecimalQuantity_DualStorageBCD.java /// |
844 | | //////////////////////////////////////////////////// |
845 | | |
846 | 0 | int8_t DecimalQuantity::getDigitPos(int32_t position) const { |
847 | 0 | if (usingBytes) { |
848 | 0 | if (position < 0 || position >= precision) { return 0; } |
849 | 0 | return fBCD.bcdBytes.ptr[position]; |
850 | 0 | } else { |
851 | 0 | if (position < 0 || position >= 16) { return 0; } |
852 | 0 | return (int8_t) ((fBCD.bcdLong >> (position * 4)) & 0xf); |
853 | 0 | } |
854 | 0 | } |
855 | | |
856 | 0 | void DecimalQuantity::setDigitPos(int32_t position, int8_t value) { |
857 | 0 | U_ASSERT(position >= 0); |
858 | 0 | if (usingBytes) { |
859 | 0 | ensureCapacity(position + 1); |
860 | 0 | fBCD.bcdBytes.ptr[position] = value; |
861 | 0 | } else if (position >= 16) { |
862 | 0 | switchStorage(); |
863 | 0 | ensureCapacity(position + 1); |
864 | 0 | fBCD.bcdBytes.ptr[position] = value; |
865 | 0 | } else { |
866 | 0 | int shift = position * 4; |
867 | 0 | fBCD.bcdLong = (fBCD.bcdLong & ~(0xfL << shift)) | ((long) value << shift); |
868 | 0 | } |
869 | 0 | } |
870 | | |
871 | 0 | void DecimalQuantity::shiftLeft(int32_t numDigits) { |
872 | 0 | if (!usingBytes && precision + numDigits > 16) { |
873 | 0 | switchStorage(); |
874 | 0 | } |
875 | 0 | if (usingBytes) { |
876 | 0 | ensureCapacity(precision + numDigits); |
877 | 0 | int i = precision + numDigits - 1; |
878 | 0 | for (; i >= numDigits; i--) { |
879 | 0 | fBCD.bcdBytes.ptr[i] = fBCD.bcdBytes.ptr[i - numDigits]; |
880 | 0 | } |
881 | 0 | for (; i >= 0; i--) { |
882 | 0 | fBCD.bcdBytes.ptr[i] = 0; |
883 | 0 | } |
884 | 0 | } else { |
885 | 0 | fBCD.bcdLong <<= (numDigits * 4); |
886 | 0 | } |
887 | 0 | scale -= numDigits; |
888 | 0 | precision += numDigits; |
889 | 0 | } |
890 | | |
891 | 0 | void DecimalQuantity::shiftRight(int32_t numDigits) { |
892 | 0 | if (usingBytes) { |
893 | 0 | int i = 0; |
894 | 0 | for (; i < precision - numDigits; i++) { |
895 | 0 | fBCD.bcdBytes.ptr[i] = fBCD.bcdBytes.ptr[i + numDigits]; |
896 | 0 | } |
897 | 0 | for (; i < precision; i++) { |
898 | 0 | fBCD.bcdBytes.ptr[i] = 0; |
899 | 0 | } |
900 | 0 | } else { |
901 | 0 | fBCD.bcdLong >>= (numDigits * 4); |
902 | 0 | } |
903 | 0 | scale += numDigits; |
904 | 0 | precision -= numDigits; |
905 | 0 | } |
906 | | |
907 | 0 | void DecimalQuantity::setBcdToZero() { |
908 | 0 | if (usingBytes) { |
909 | 0 | uprv_free(fBCD.bcdBytes.ptr); |
910 | 0 | fBCD.bcdBytes.ptr = nullptr; |
911 | 0 | usingBytes = false; |
912 | 0 | } |
913 | 0 | fBCD.bcdLong = 0L; |
914 | 0 | scale = 0; |
915 | 0 | precision = 0; |
916 | 0 | isApproximate = false; |
917 | 0 | origDouble = 0; |
918 | 0 | origDelta = 0; |
919 | 0 | } |
920 | | |
921 | 0 | void DecimalQuantity::readIntToBcd(int32_t n) { |
922 | 0 | U_ASSERT(n != 0); |
923 | 0 | // ints always fit inside the long implementation. |
924 | 0 | uint64_t result = 0L; |
925 | 0 | int i = 16; |
926 | 0 | for (; n != 0; n /= 10, i--) { |
927 | 0 | result = (result >> 4) + ((static_cast<uint64_t>(n) % 10) << 60); |
928 | 0 | } |
929 | 0 | U_ASSERT(!usingBytes); |
930 | 0 | fBCD.bcdLong = result >> (i * 4); |
931 | 0 | scale = 0; |
932 | 0 | precision = 16 - i; |
933 | 0 | } |
934 | | |
935 | 0 | void DecimalQuantity::readLongToBcd(int64_t n) { |
936 | 0 | U_ASSERT(n != 0); |
937 | 0 | if (n >= 10000000000000000L) { |
938 | 0 | ensureCapacity(); |
939 | 0 | int i = 0; |
940 | 0 | for (; n != 0L; n /= 10L, i++) { |
941 | 0 | fBCD.bcdBytes.ptr[i] = static_cast<int8_t>(n % 10); |
942 | 0 | } |
943 | 0 | U_ASSERT(usingBytes); |
944 | 0 | scale = 0; |
945 | 0 | precision = i; |
946 | 0 | } else { |
947 | 0 | uint64_t result = 0L; |
948 | 0 | int i = 16; |
949 | 0 | for (; n != 0L; n /= 10L, i--) { |
950 | 0 | result = (result >> 4) + ((n % 10) << 60); |
951 | 0 | } |
952 | 0 | U_ASSERT(i >= 0); |
953 | 0 | U_ASSERT(!usingBytes); |
954 | 0 | fBCD.bcdLong = result >> (i * 4); |
955 | 0 | scale = 0; |
956 | 0 | precision = 16 - i; |
957 | 0 | } |
958 | 0 | } |
959 | | |
960 | 0 | void DecimalQuantity::readDecNumberToBcd(const DecNum& decnum) { |
961 | 0 | const decNumber* dn = decnum.getRawDecNumber(); |
962 | 0 | if (dn->digits > 16) { |
963 | 0 | ensureCapacity(dn->digits); |
964 | 0 | for (int32_t i = 0; i < dn->digits; i++) { |
965 | 0 | fBCD.bcdBytes.ptr[i] = dn->lsu[i]; |
966 | 0 | } |
967 | 0 | } else { |
968 | 0 | uint64_t result = 0L; |
969 | 0 | for (int32_t i = 0; i < dn->digits; i++) { |
970 | 0 | result |= static_cast<uint64_t>(dn->lsu[i]) << (4 * i); |
971 | 0 | } |
972 | 0 | fBCD.bcdLong = result; |
973 | 0 | } |
974 | 0 | scale = dn->exponent; |
975 | 0 | precision = dn->digits; |
976 | 0 | } |
977 | | |
978 | | void DecimalQuantity::readDoubleConversionToBcd( |
979 | 0 | const char* buffer, int32_t length, int32_t point) { |
980 | 0 | // NOTE: Despite the fact that double-conversion's API is called |
981 | 0 | // "DoubleToAscii", they actually use '0' (as opposed to u8'0'). |
982 | 0 | if (length > 16) { |
983 | 0 | ensureCapacity(length); |
984 | 0 | for (int32_t i = 0; i < length; i++) { |
985 | 0 | fBCD.bcdBytes.ptr[i] = buffer[length-i-1] - '0'; |
986 | 0 | } |
987 | 0 | } else { |
988 | 0 | uint64_t result = 0L; |
989 | 0 | for (int32_t i = 0; i < length; i++) { |
990 | 0 | result |= static_cast<uint64_t>(buffer[length-i-1] - '0') << (4 * i); |
991 | 0 | } |
992 | 0 | fBCD.bcdLong = result; |
993 | 0 | } |
994 | 0 | scale = point - length; |
995 | 0 | precision = length; |
996 | 0 | } |
997 | | |
998 | 0 | void DecimalQuantity::compact() { |
999 | 0 | if (usingBytes) { |
1000 | 0 | int32_t delta = 0; |
1001 | 0 | for (; delta < precision && fBCD.bcdBytes.ptr[delta] == 0; delta++); |
1002 | 0 | if (delta == precision) { |
1003 | 0 | // Number is zero |
1004 | 0 | setBcdToZero(); |
1005 | 0 | return; |
1006 | 0 | } else { |
1007 | 0 | // Remove trailing zeros |
1008 | 0 | shiftRight(delta); |
1009 | 0 | } |
1010 | 0 |
|
1011 | 0 | // Compute precision |
1012 | 0 | int32_t leading = precision - 1; |
1013 | 0 | for (; leading >= 0 && fBCD.bcdBytes.ptr[leading] == 0; leading--); |
1014 | 0 | precision = leading + 1; |
1015 | 0 |
|
1016 | 0 | // Switch storage mechanism if possible |
1017 | 0 | if (precision <= 16) { |
1018 | 0 | switchStorage(); |
1019 | 0 | } |
1020 | 0 |
|
1021 | 0 | } else { |
1022 | 0 | if (fBCD.bcdLong == 0L) { |
1023 | 0 | // Number is zero |
1024 | 0 | setBcdToZero(); |
1025 | 0 | return; |
1026 | 0 | } |
1027 | 0 | |
1028 | 0 | // Compact the number (remove trailing zeros) |
1029 | 0 | // TODO: Use a more efficient algorithm here and below. There is a logarithmic one. |
1030 | 0 | int32_t delta = 0; |
1031 | 0 | for (; delta < precision && getDigitPos(delta) == 0; delta++); |
1032 | 0 | fBCD.bcdLong >>= delta * 4; |
1033 | 0 | scale += delta; |
1034 | 0 |
|
1035 | 0 | // Compute precision |
1036 | 0 | int32_t leading = precision - 1; |
1037 | 0 | for (; leading >= 0 && getDigitPos(leading) == 0; leading--); |
1038 | 0 | precision = leading + 1; |
1039 | 0 | } |
1040 | 0 | } |
1041 | | |
1042 | 0 | void DecimalQuantity::ensureCapacity() { |
1043 | 0 | ensureCapacity(40); |
1044 | 0 | } |
1045 | | |
1046 | 0 | void DecimalQuantity::ensureCapacity(int32_t capacity) { |
1047 | 0 | if (capacity == 0) { return; } |
1048 | 0 | int32_t oldCapacity = usingBytes ? fBCD.bcdBytes.len : 0; |
1049 | 0 | if (!usingBytes) { |
1050 | 0 | // TODO: There is nothing being done to check for memory allocation failures. |
1051 | 0 | // TODO: Consider indexing by nybbles instead of bytes in C++, so that we can |
1052 | 0 | // make these arrays half the size. |
1053 | 0 | fBCD.bcdBytes.ptr = static_cast<int8_t*>(uprv_malloc(capacity * sizeof(int8_t))); |
1054 | 0 | fBCD.bcdBytes.len = capacity; |
1055 | 0 | // Initialize the byte array to zeros (this is done automatically in Java) |
1056 | 0 | uprv_memset(fBCD.bcdBytes.ptr, 0, capacity * sizeof(int8_t)); |
1057 | 0 | } else if (oldCapacity < capacity) { |
1058 | 0 | auto bcd1 = static_cast<int8_t*>(uprv_malloc(capacity * 2 * sizeof(int8_t))); |
1059 | 0 | uprv_memcpy(bcd1, fBCD.bcdBytes.ptr, oldCapacity * sizeof(int8_t)); |
1060 | 0 | // Initialize the rest of the byte array to zeros (this is done automatically in Java) |
1061 | 0 | uprv_memset(bcd1 + oldCapacity, 0, (capacity - oldCapacity) * sizeof(int8_t)); |
1062 | 0 | uprv_free(fBCD.bcdBytes.ptr); |
1063 | 0 | fBCD.bcdBytes.ptr = bcd1; |
1064 | 0 | fBCD.bcdBytes.len = capacity * 2; |
1065 | 0 | } |
1066 | 0 | usingBytes = true; |
1067 | 0 | } |
1068 | | |
1069 | 0 | void DecimalQuantity::switchStorage() { |
1070 | 0 | if (usingBytes) { |
1071 | 0 | // Change from bytes to long |
1072 | 0 | uint64_t bcdLong = 0L; |
1073 | 0 | for (int i = precision - 1; i >= 0; i--) { |
1074 | 0 | bcdLong <<= 4; |
1075 | 0 | bcdLong |= fBCD.bcdBytes.ptr[i]; |
1076 | 0 | } |
1077 | 0 | uprv_free(fBCD.bcdBytes.ptr); |
1078 | 0 | fBCD.bcdBytes.ptr = nullptr; |
1079 | 0 | fBCD.bcdLong = bcdLong; |
1080 | 0 | usingBytes = false; |
1081 | 0 | } else { |
1082 | 0 | // Change from long to bytes |
1083 | 0 | // Copy the long into a local variable since it will get munged when we allocate the bytes |
1084 | 0 | uint64_t bcdLong = fBCD.bcdLong; |
1085 | 0 | ensureCapacity(); |
1086 | 0 | for (int i = 0; i < precision; i++) { |
1087 | 0 | fBCD.bcdBytes.ptr[i] = static_cast<int8_t>(bcdLong & 0xf); |
1088 | 0 | bcdLong >>= 4; |
1089 | 0 | } |
1090 | 0 | U_ASSERT(usingBytes); |
1091 | 0 | } |
1092 | 0 | } |
1093 | | |
1094 | 0 | void DecimalQuantity::copyBcdFrom(const DecimalQuantity &other) { |
1095 | 0 | setBcdToZero(); |
1096 | 0 | if (other.usingBytes) { |
1097 | 0 | ensureCapacity(other.precision); |
1098 | 0 | uprv_memcpy(fBCD.bcdBytes.ptr, other.fBCD.bcdBytes.ptr, other.precision * sizeof(int8_t)); |
1099 | 0 | } else { |
1100 | 0 | fBCD.bcdLong = other.fBCD.bcdLong; |
1101 | 0 | } |
1102 | 0 | } |
1103 | | |
1104 | 0 | void DecimalQuantity::moveBcdFrom(DecimalQuantity &other) { |
1105 | 0 | setBcdToZero(); |
1106 | 0 | if (other.usingBytes) { |
1107 | 0 | usingBytes = true; |
1108 | 0 | fBCD.bcdBytes.ptr = other.fBCD.bcdBytes.ptr; |
1109 | 0 | fBCD.bcdBytes.len = other.fBCD.bcdBytes.len; |
1110 | 0 | // Take ownership away from the old instance: |
1111 | 0 | other.fBCD.bcdBytes.ptr = nullptr; |
1112 | 0 | other.usingBytes = false; |
1113 | 0 | } else { |
1114 | 0 | fBCD.bcdLong = other.fBCD.bcdLong; |
1115 | 0 | } |
1116 | 0 | } |
1117 | | |
1118 | 0 | const char16_t* DecimalQuantity::checkHealth() const { |
1119 | 0 | if (usingBytes) { |
1120 | 0 | if (precision == 0) { return u"Zero precision but we are in byte mode"; } |
1121 | 0 | int32_t capacity = fBCD.bcdBytes.len; |
1122 | 0 | if (precision > capacity) { return u"Precision exceeds length of byte array"; } |
1123 | 0 | if (getDigitPos(precision - 1) == 0) { return u"Most significant digit is zero in byte mode"; } |
1124 | 0 | if (getDigitPos(0) == 0) { return u"Least significant digit is zero in long mode"; } |
1125 | 0 | for (int i = 0; i < precision; i++) { |
1126 | 0 | if (getDigitPos(i) >= 10) { return u"Digit exceeding 10 in byte array"; } |
1127 | 0 | if (getDigitPos(i) < 0) { return u"Digit below 0 in byte array"; } |
1128 | 0 | } |
1129 | 0 | for (int i = precision; i < capacity; i++) { |
1130 | 0 | if (getDigitPos(i) != 0) { return u"Nonzero digits outside of range in byte array"; } |
1131 | 0 | } |
1132 | 0 | } else { |
1133 | 0 | if (precision == 0 && fBCD.bcdLong != 0) { |
1134 | 0 | return u"Value in bcdLong even though precision is zero"; |
1135 | 0 | } |
1136 | 0 | if (precision > 16) { return u"Precision exceeds length of long"; } |
1137 | 0 | if (precision != 0 && getDigitPos(precision - 1) == 0) { |
1138 | 0 | return u"Most significant digit is zero in long mode"; |
1139 | 0 | } |
1140 | 0 | if (precision != 0 && getDigitPos(0) == 0) { |
1141 | 0 | return u"Least significant digit is zero in long mode"; |
1142 | 0 | } |
1143 | 0 | for (int i = 0; i < precision; i++) { |
1144 | 0 | if (getDigitPos(i) >= 10) { return u"Digit exceeding 10 in long"; } |
1145 | 0 | if (getDigitPos(i) < 0) { return u"Digit below 0 in long (?!)"; } |
1146 | 0 | } |
1147 | 0 | for (int i = precision; i < 16; i++) { |
1148 | 0 | if (getDigitPos(i) != 0) { return u"Nonzero digits outside of range in long"; } |
1149 | 0 | } |
1150 | 0 | } |
1151 | 0 |
|
1152 | 0 | // No error |
1153 | 0 | return nullptr; |
1154 | 0 | } |
1155 | | |
1156 | 0 | bool DecimalQuantity::operator==(const DecimalQuantity& other) const { |
1157 | 0 | // FIXME: Make a faster implementation. |
1158 | 0 | return toString() == other.toString(); |
1159 | 0 | } |
1160 | | |
1161 | 0 | UnicodeString DecimalQuantity::toString() const { |
1162 | 0 | MaybeStackArray<char, 30> digits(precision + 1); |
1163 | 0 | for (int32_t i = 0; i < precision; i++) { |
1164 | 0 | digits[i] = getDigitPos(precision - i - 1) + '0'; |
1165 | 0 | } |
1166 | 0 | digits[precision] = 0; // terminate buffer |
1167 | 0 | char buffer8[100]; |
1168 | 0 | snprintf( |
1169 | 0 | buffer8, |
1170 | 0 | sizeof(buffer8), |
1171 | 0 | "<DecimalQuantity %d:%d:%d:%d %s %s%s%s%d>", |
1172 | 0 | (lOptPos > 999 ? 999 : lOptPos), |
1173 | 0 | lReqPos, |
1174 | 0 | rReqPos, |
1175 | 0 | (rOptPos < -999 ? -999 : rOptPos), |
1176 | 0 | (usingBytes ? "bytes" : "long"), |
1177 | 0 | (isNegative() ? "-" : ""), |
1178 | 0 | (precision == 0 ? "0" : digits.getAlias()), |
1179 | 0 | "E", |
1180 | 0 | scale); |
1181 | 0 | return UnicodeString(buffer8, -1, US_INV); |
1182 | 0 | } |
1183 | | |
1184 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |