From 3f67ba14a50dff960d43813c0d5d459e78e75fe6 Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Sun, 14 Jul 2024 19:46:15 +0200 Subject: [PATCH] Use GetDivMod --- src/common/operator/cast_operators.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/common/operator/cast_operators.cpp b/src/common/operator/cast_operators.cpp index 0a57872e82a3..0459ef82bfa8 100644 --- a/src/common/operator/cast_operators.cpp +++ b/src/common/operator/cast_operators.cpp @@ -2687,6 +2687,17 @@ hugeint_t GetPowerOfTen(hugeint_t input, uint8_t scale) { return Hugeint::POWERS_OF_TEN[scale]; } +template +static void GetDivMod(SRC lhs, SRC rhs, SRC &div, SRC &mod) { + div = lhs / rhs; + mod = lhs % rhs; +} + +template <> +void GetDivMod(hugeint_t lhs, hugeint_t rhs, hugeint_t &div, hugeint_t &mod) { + div = Hugeint::DivMod(lhs, rhs, mod); +} + template bool TryCastDecimalToFloatingPoint(SRC input, DST &result, uint8_t scale) { if (IsRepresentableExactly(input, DST(0.0)) || scale == 0) { @@ -2695,8 +2706,13 @@ bool TryCastDecimalToFloatingPoint(SRC input, DST &result, uint8_t scale) { return true; } auto power_of_ten = GetPowerOfTen(input, scale); - result = Cast::Operation(input / power_of_ten) + - Cast::Operation(input % power_of_ten) / DST(NumericHelper::DOUBLE_POWERS_OF_TEN[scale]); + + SRC div = 0; + SRC mod = 0; + GetDivMod(input, power_of_ten, div, mod); + + result = Cast::Operation(div) + + Cast::Operation(mod) / DST(NumericHelper::DOUBLE_POWERS_OF_TEN[scale]); return true; }