Commit b688f9bc authored by Tomasz Wlostowski's avatar Tomasz Wlostowski Committed by Maciej Suminski

math_util: speed up rescaling on 64-bit platforms by using native 128-bit types

parent 655721d9
...@@ -27,20 +27,24 @@ ...@@ -27,20 +27,24 @@
#include <cstdlib> #include <cstdlib>
#include <climits> #include <climits>
#include <math/math_util.h> #include <math/math_util.h>
#include <stdint.h>
template <> template<>
int rescale( int aNumerator, int aValue, int aDenominator ) int rescale( int aNumerator, int aValue, int aDenominator )
{ {
return (int) ( (int64_t) aNumerator * (int64_t) aValue / (int64_t) aDenominator ); return (int) ( (int64_t) aNumerator * (int64_t) aValue / (int64_t) aDenominator );
} }
template <> template<>
int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator ) int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
{ {
#ifdef __x86_64__
return ( (__int128_t) aNumerator * (__int128_t) aValue ) / aDenominator;
#else
int64_t r = 0; int64_t r = 0;
int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) * int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
( aValue < 0 ? -1 : 1 ); ( aValue < 0 ? -1 : 1 );
int64_t a = std::abs( aNumerator ); int64_t a = std::abs( aNumerator );
int64_t b = std::abs( aValue ); int64_t b = std::abs( aValue );
...@@ -84,4 +88,5 @@ int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator ) ...@@ -84,4 +88,5 @@ int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
return t1 * sign; return t1 * sign;
} }
#endif
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment