diff options
author | Raulshc <[email protected]> | 2019-08-18 19:12:38 +0200 |
---|---|---|
committer | Raulshc <[email protected]> | 2019-08-18 19:12:38 +0200 |
commit | 550f1dce1fa00bc084e6dacbb40fdafb6867a015 (patch) | |
tree | 75b8af0e8e993540a8bfb32379a621f2efb174f4 | |
parent | ff66061091baedaa7d677ede65aba592c8f8aa5d (diff) |
Move double2int function
Move inline double2int function to alnumeric.h from pshifter.cpp
-rw-r--r-- | alc/effects/pshifter.cpp | 35 | ||||
-rw-r--r-- | common/alnumeric.h | 35 |
2 files changed, 36 insertions, 34 deletions
diff --git a/alc/effects/pshifter.cpp b/alc/effects/pshifter.cpp index 819e510c..0ba0b496 100644 --- a/alc/effects/pshifter.cpp +++ b/alc/effects/pshifter.cpp @@ -34,6 +34,7 @@ #include "alcmain.h" #include "alcomplex.h" #include "alcontext.h" +#include "alnumeric.h" #include "alu.h" @@ -48,40 +49,6 @@ using complex_d = std::complex<double>; #define STFT_STEP (STFT_SIZE / OVERSAMP) #define FIFO_LATENCY (STFT_STEP * (OVERSAMP-1)) -inline int double2int(double d) -{ -#if defined(HAVE_SSE_INTRINSICS) - return _mm_cvttsd_si32(_mm_set_sd(d)); - -#elif ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) && \ - !defined(__SSE2_MATH__)) || (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP < 2) - - int sign, shift; - int64_t mant; - union { - double d; - int64_t i64; - } conv; - - conv.d = d; - sign = (conv.i64>>63) | 1; - shift = ((conv.i64>>52)&0x7ff) - (1023+52); - - /* Over/underflow */ - if UNLIKELY(shift >= 63 || shift < -52) - return 0; - - mant = (conv.i64&0xfffffffffffff_i64) | 0x10000000000000_i64; - if LIKELY(shift < 0) - return (int)(mant >> -shift) * sign; - return (int)(mant << shift) * sign; - -#else - - return static_cast<int>(d); -#endif -} - /* Define a Hann window, used to filter the STFT input and output. */ /* Making this constexpr seems to require C++14. */ std::array<ALdouble,STFT_SIZE> InitHannWindow() diff --git a/common/alnumeric.h b/common/alnumeric.h index b51893d6..0a05f4bf 100644 --- a/common/alnumeric.h +++ b/common/alnumeric.h @@ -250,6 +250,41 @@ inline int float2int(float f) noexcept #endif } +/** Converts double-to-int using standard behavior (truncation). */ +inline int double2int(double d) noexcept +{ +#if defined(HAVE_SSE_INTRINSICS) + return _mm_cvttsd_si32(_mm_set_sd(d)); + +#elif ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) && \ + !defined(__SSE2_MATH__)) || (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP < 2) + + int sign, shift; + int64_t mant; + union { + double d; + int64_t i64; + } conv; + + conv.d = d; + sign = (conv.i64 >> 63) | 1; + shift = ((conv.i64 >> 52) & 0x7ff) - (1023 + 52); + + /* Over/underflow */ + if UNLIKELY(shift >= 63 || shift < -52) + return 0; + + mant = (conv.i64 & 0xfffffffffffff_i64) | 0x10000000000000_i64; + if LIKELY(shift < 0) + return (int)(mant >> -shift) * sign; + return (int)(mant << shift) * sign; + +#else + + return static_cast<int>(d); +#endif +} + /** * Rounds a float to the nearest integral value, according to the current * rounding mode. This is essentially an inlined version of rintf, although |