Both x_value and y_value must be >= 0
| 66 | |
| 67 | /// Both x_value and y_value must be >= 0 |
| 68 | static BasicDecimal128 AddLargePositive(const BasicDecimalScalar128& x, |
| 69 | const BasicDecimalScalar128& y, |
| 70 | int32_t out_scale) { |
| 71 | DCHECK_GE(x.value(), 0); |
| 72 | DCHECK_GE(y.value(), 0); |
| 73 | |
| 74 | // separate out whole/fractions. |
| 75 | BasicDecimal128 x_left, x_right, y_left, y_right; |
| 76 | x.value().GetWholeAndFraction(x.scale(), &x_left, &x_right); |
| 77 | y.value().GetWholeAndFraction(y.scale(), &y_left, &y_right); |
| 78 | |
| 79 | // Adjust fractional parts to higher scale. |
| 80 | auto higher_scale = std::max(x.scale(), y.scale()); |
| 81 | auto x_right_scaled = CheckAndIncreaseScale(x_right, higher_scale - x.scale()); |
| 82 | auto y_right_scaled = CheckAndIncreaseScale(y_right, higher_scale - y.scale()); |
| 83 | |
| 84 | BasicDecimal128 right; |
| 85 | BasicDecimal128 carry_to_left; |
| 86 | auto multiplier = BasicDecimal128::GetScaleMultiplier(higher_scale); |
| 87 | if (x_right_scaled >= multiplier - y_right_scaled) { |
| 88 | right = x_right_scaled - (multiplier - y_right_scaled); |
| 89 | carry_to_left = 1; |
| 90 | } else { |
| 91 | right = x_right_scaled + y_right_scaled; |
| 92 | carry_to_left = 0; |
| 93 | } |
| 94 | right = CheckAndReduceScale(right, higher_scale - out_scale); |
| 95 | |
| 96 | auto left = x_left + y_left + carry_to_left; |
| 97 | return (left * BasicDecimal128::GetScaleMultiplier(out_scale)) + right; |
| 98 | } |
| 99 | |
| 100 | /// x_value and y_value cannot be 0, and one must be positive and the other negative. |
| 101 | static BasicDecimal128 AddLargeNegative(const BasicDecimalScalar128& x, |
no test coverage detected