96int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
98#if defined( _M_X64 ) && ( _MSC_VER >= 1920 )
100 uint64_t productLo =
static_cast<uint64_t
>( _mul128( aNumerator, aValue, &productHi ) );
102 int64_t r = ( ( productHi < 0 ) ^ ( aDenominator < 0 ) ) ? -aDenominator / 2 : aDenominator / 2;
104 uint64_t rLo =
static_cast<uint64_t
>( r );
105 int64_t rHi = r < 0 ? -1ll : 0ll;
108 productHi += rHi + ( productLo < rLo );
113 int64_t
result = _div128( productHi, productLo, aDenominator, &remainder );
117 __except( ( GetExceptionCode() == EXCEPTION_INT_OVERFLOW ) ? EXCEPTION_EXECUTE_HANDLER
118 : EXCEPTION_CONTINUE_SEARCH )
120 kimathLogDebug(
"Overflow in rescale (%lld * %lld + %lld) / %lld", aNumerator, aValue, r,
126#elif defined( __SIZEOF_INT128__ )
127 __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue;
129 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
130 return ( numerator - aDenominator / 2 ) / aDenominator;
132 return ( numerator + aDenominator / 2 ) / aDenominator;
136 int64_t
sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
137 ( aValue < 0 ? -1 : 1 );
141 int64_t c =
std::abs( aDenominator );
145 if( b <= std::numeric_limits<int>::max() && c <= std::numeric_limits<int>::max() )
147 if( a <= std::numeric_limits<int>::max() )
148 return sign * ( ( a * b + r ) / c );
150 return sign * ( a / c * b + ( a % c * b + r ) / c);
154 uint64_t a0 = a & 0xFFFFFFFF;
155 uint64_t a1 = a >> 32;
156 uint64_t b0 = b & 0xFFFFFFFF;
157 uint64_t b1 = b >> 32;
158 uint64_t t1 = a0 * b1 + a1 * b0;
159 uint64_t t1a = t1 << 32;
163 a1 = a1 * b1 + ( t1 >> 32 ) + ( a0 < t1a );
165 a1 += a0 < (uint64_t)r;
167 for( i = 63; i >= 0; i-- )
169 a1 += a1 + ( ( a0 >> i ) & 1 );
172 if( (uint64_t) c <= a1 )
wxString result
Test unit parsing edge cases and error handling.
uint64_t RoundedHypotWide(uint64_t aX, uint64_t aY)
Nearest integer to sqrt( aX^2 + aY^2 ) for magnitudes of at most 2^63, using 128-bit arithmetic.