From a342bb9ab39270f9f25498165823107a1f7a5210 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Thu, 7 Jan 2021 11:21:05 -0800 Subject: [PATCH] Standardize rounding in 64-bit rescale 32-bit rescale still uses truncation until we can determine how this affects poly_grid_partition --- libs/kimath/src/math/util.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/kimath/src/math/util.cpp b/libs/kimath/src/math/util.cpp index 51a09e1d46..4ace387b8d 100644 --- a/libs/kimath/src/math/util.cpp +++ b/libs/kimath/src/math/util.cpp @@ -31,15 +31,21 @@ template<> int rescale( int aNumerator, int aValue, int aDenominator ) { - return (int) ( (int64_t) aNumerator * (int64_t) aValue / (int64_t) aDenominator ); + int64_t numerator = (int64_t) aNumerator * (int64_t) aValue; + return numerator / aDenominator; } template<> int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator ) { -#ifdef __x86_64__ - return ( (__int128_t) aNumerator * (__int128_t) aValue ) / aDenominator; +#ifdef __SIZEOF_INT128__ + __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue; + + if( ( numerator < 0 ) ^ ( aDenominator < 0 ) ) + return ( numerator - aDenominator / 2 ) / aDenominator; + else + return ( numerator + aDenominator / 2 ) / aDenominator; #else int64_t r = 0; int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *