| 209 | } |
| 210 | |
| 211 | BasicDecimal128 Add(const BasicDecimalScalar128& x, const BasicDecimalScalar128& y, |
| 212 | int32_t out_precision, int32_t out_scale) { |
| 213 | if (out_precision < DecimalTypeUtil::kMaxPrecision) { |
| 214 | // fast-path add |
| 215 | return AddFastPath(x, y, out_scale); |
| 216 | } else { |
| 217 | int32_t min_lz = MinLeadingZeros(x, y); |
| 218 | if (min_lz >= 3) { |
| 219 | // If both numbers have at least MIN_LZ leading zeros, we can add them directly |
| 220 | // without the risk of overflow. |
| 221 | // We want the result to have at least 2 leading zeros, which ensures that it fits |
| 222 | // into the maximum decimal because 2^126 - 1 < 10^38 - 1. If both x and y have at |
| 223 | // least 3 leading zeros, then we are guaranteed that the result will have at lest 2 |
| 224 | // leading zeros. |
| 225 | return AddNoOverflow(x, y, out_scale); |
| 226 | } else { |
| 227 | // slower-version : add whole/fraction parts separately, and then, combine. |
| 228 | return AddLarge(x, y, out_scale); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | BasicDecimal128 Subtract(const BasicDecimalScalar128& x, const BasicDecimalScalar128& y, |
| 234 | int32_t out_precision, int32_t out_scale) { |