Compute the rounding delta for the given rounding type.
| 562 | |
| 563 | // Compute the rounding delta for the given rounding type. |
| 564 | static int32_t ComputeRoundingDelta(const BasicDecimal128& x, int32_t x_scale, |
| 565 | int32_t out_scale, RoundType type) { |
| 566 | if (type == kRoundTypeTrunc || // no rounding for this type. |
| 567 | out_scale >= x_scale) { // no digits dropped, so no rounding. |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | int32_t result = 0; |
| 572 | switch (type) { |
| 573 | case kRoundTypeHalfRoundUp: { |
| 574 | auto base = BasicDecimal128::GetScaleMultiplier(x_scale - out_scale); |
| 575 | auto trailing = x % base; |
| 576 | if (trailing == 0) { |
| 577 | result = 0; |
| 578 | } else if (trailing.Abs() < base / 2) { |
| 579 | result = 0; |
| 580 | } else { |
| 581 | result = (x < 0) ? -1 : 1; |
| 582 | } |
| 583 | break; |
| 584 | } |
| 585 | |
| 586 | case kRoundTypeCeil: |
| 587 | if (x < 0) { |
| 588 | // no rounding for -ve |
| 589 | result = 0; |
| 590 | } else { |
| 591 | auto base = BasicDecimal128::GetScaleMultiplier(x_scale - out_scale); |
| 592 | auto trailing = x % base; |
| 593 | result = (trailing == 0) ? 0 : 1; |
| 594 | } |
| 595 | break; |
| 596 | |
| 597 | case kRoundTypeFloor: |
| 598 | if (x > 0) { |
| 599 | // no rounding for +ve |
| 600 | result = 0; |
| 601 | } else { |
| 602 | auto base = BasicDecimal128::GetScaleMultiplier(x_scale - out_scale); |
| 603 | auto trailing = x % base; |
| 604 | result = (trailing == 0) ? 0 : -1; |
| 605 | } |
| 606 | break; |
| 607 | |
| 608 | case kRoundTypeTrunc: |
| 609 | break; |
| 610 | } |
| 611 | return result; |
| 612 | } |
| 613 | |
| 614 | // Modify the scale and round. |
| 615 | static BasicDecimal128 RoundWithPositiveScale(const BasicDecimalScalar128& x, |
no test coverage detected