]> git.tdb.fi Git - ext/openal.git/blob - common/alnumeric.h
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / common / alnumeric.h
1 #ifndef AL_NUMERIC_H
2 #define AL_NUMERIC_H
3
4 #include <algorithm>
5 #include <cmath>
6 #include <cstddef>
7 #include <cstdint>
8 #ifdef HAVE_INTRIN_H
9 #include <intrin.h>
10 #endif
11 #ifdef HAVE_SSE_INTRINSICS
12 #include <xmmintrin.h>
13 #endif
14
15 #include "altraits.h"
16 #include "opthelpers.h"
17
18
19 inline constexpr int64_t operator "" _i64(unsigned long long int n) noexcept { return static_cast<int64_t>(n); }
20 inline constexpr uint64_t operator "" _u64(unsigned long long int n) noexcept { return static_cast<uint64_t>(n); }
21
22
23 constexpr inline float minf(float a, float b) noexcept
24 { return ((a > b) ? b : a); }
25 constexpr inline float maxf(float a, float b) noexcept
26 { return ((a > b) ? a : b); }
27 constexpr inline float clampf(float val, float min, float max) noexcept
28 { return minf(max, maxf(min, val)); }
29
30 constexpr inline double mind(double a, double b) noexcept
31 { return ((a > b) ? b : a); }
32 constexpr inline double maxd(double a, double b) noexcept
33 { return ((a > b) ? a : b); }
34 constexpr inline double clampd(double val, double min, double max) noexcept
35 { return mind(max, maxd(min, val)); }
36
37 constexpr inline unsigned int minu(unsigned int a, unsigned int b) noexcept
38 { return ((a > b) ? b : a); }
39 constexpr inline unsigned int maxu(unsigned int a, unsigned int b) noexcept
40 { return ((a > b) ? a : b); }
41 constexpr inline unsigned int clampu(unsigned int val, unsigned int min, unsigned int max) noexcept
42 { return minu(max, maxu(min, val)); }
43
44 constexpr inline int mini(int a, int b) noexcept
45 { return ((a > b) ? b : a); }
46 constexpr inline int maxi(int a, int b) noexcept
47 { return ((a > b) ? a : b); }
48 constexpr inline int clampi(int val, int min, int max) noexcept
49 { return mini(max, maxi(min, val)); }
50
51 constexpr inline int64_t mini64(int64_t a, int64_t b) noexcept
52 { return ((a > b) ? b : a); }
53 constexpr inline int64_t maxi64(int64_t a, int64_t b) noexcept
54 { return ((a > b) ? a : b); }
55 constexpr inline int64_t clampi64(int64_t val, int64_t min, int64_t max) noexcept
56 { return mini64(max, maxi64(min, val)); }
57
58 constexpr inline uint64_t minu64(uint64_t a, uint64_t b) noexcept
59 { return ((a > b) ? b : a); }
60 constexpr inline uint64_t maxu64(uint64_t a, uint64_t b) noexcept
61 { return ((a > b) ? a : b); }
62 constexpr inline uint64_t clampu64(uint64_t val, uint64_t min, uint64_t max) noexcept
63 { return minu64(max, maxu64(min, val)); }
64
65 constexpr inline size_t minz(size_t a, size_t b) noexcept
66 { return ((a > b) ? b : a); }
67 constexpr inline size_t maxz(size_t a, size_t b) noexcept
68 { return ((a > b) ? a : b); }
69 constexpr inline size_t clampz(size_t val, size_t min, size_t max) noexcept
70 { return minz(max, maxz(min, val)); }
71
72
73 constexpr inline float lerpf(float val1, float val2, float mu) noexcept
74 { return val1 + (val2-val1)*mu; }
75 constexpr inline float cubic(float val1, float val2, float val3, float val4, float mu) noexcept
76 {
77     const float mu2{mu*mu}, mu3{mu2*mu};
78     const float a0{-0.5f*mu3 +       mu2 + -0.5f*mu};
79     const float a1{ 1.5f*mu3 + -2.5f*mu2            + 1.0f};
80     const float a2{-1.5f*mu3 +  2.0f*mu2 +  0.5f*mu};
81     const float a3{ 0.5f*mu3 + -0.5f*mu2};
82     return val1*a0 + val2*a1 + val3*a2 + val4*a3;
83 }
84
85
86 /** Find the next power-of-2 for non-power-of-2 numbers. */
87 inline uint32_t NextPowerOf2(uint32_t value) noexcept
88 {
89     if(value > 0)
90     {
91         value--;
92         value |= value>>1;
93         value |= value>>2;
94         value |= value>>4;
95         value |= value>>8;
96         value |= value>>16;
97     }
98     return value+1;
99 }
100
101 /**
102  * If the value is not already a multiple of r, round down to the next
103  * multiple.
104  */
105 template<typename T>
106 constexpr T RoundDown(T value, al::type_identity_t<T> r) noexcept
107 { return value - (value%r); }
108
109 /**
110  * If the value is not already a multiple of r, round up to the next multiple.
111  */
112 template<typename T>
113 constexpr T RoundUp(T value, al::type_identity_t<T> r) noexcept
114 { return RoundDown(value + r-1, r); }
115
116
117 /**
118  * Fast float-to-int conversion. No particular rounding mode is assumed; the
119  * IEEE-754 default is round-to-nearest with ties-to-even, though an app could
120  * change it on its own threads. On some systems, a truncating conversion may
121  * always be the fastest method.
122  */
123 inline int fastf2i(float f) noexcept
124 {
125 #if defined(HAVE_SSE_INTRINSICS)
126     return _mm_cvt_ss2si(_mm_set_ss(f));
127
128 #elif defined(_MSC_VER) && defined(_M_IX86_FP)
129
130     int i;
131     __asm fld f
132     __asm fistp i
133     return i;
134
135 #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
136
137     int i;
138 #ifdef __SSE_MATH__
139     __asm__("cvtss2si %1, %0" : "=r"(i) : "x"(f));
140 #else
141     __asm__ __volatile__("fistpl %0" : "=m"(i) : "t"(f) : "st");
142 #endif
143     return i;
144
145 #else
146
147     return static_cast<int>(f);
148 #endif
149 }
150 inline unsigned int fastf2u(float f) noexcept
151 { return static_cast<unsigned int>(fastf2i(f)); }
152
153 /** Converts float-to-int using standard behavior (truncation). */
154 inline int float2int(float f) noexcept
155 {
156 #if defined(HAVE_SSE_INTRINSICS)
157     return _mm_cvtt_ss2si(_mm_set_ss(f));
158
159 #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 0) \
160     || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
161         && !defined(__SSE_MATH__))
162     int sign, shift, mant;
163     union {
164         float f;
165         int i;
166     } conv;
167
168     conv.f = f;
169     sign = (conv.i>>31) | 1;
170     shift = ((conv.i>>23)&0xff) - (127+23);
171
172     /* Over/underflow */
173     if(shift >= 31 || shift < -23) UNLIKELY
174         return 0;
175
176     mant = (conv.i&0x7fffff) | 0x800000;
177     if(shift < 0) LIKELY
178         return (mant >> -shift) * sign;
179     return (mant << shift) * sign;
180
181 #else
182
183     return static_cast<int>(f);
184 #endif
185 }
186 inline unsigned int float2uint(float f) noexcept
187 { return static_cast<unsigned int>(float2int(f)); }
188
189 /** Converts double-to-int using standard behavior (truncation). */
190 inline int double2int(double d) noexcept
191 {
192 #if defined(HAVE_SSE_INTRINSICS)
193     return _mm_cvttsd_si32(_mm_set_sd(d));
194
195 #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP < 2) \
196     || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
197         && !defined(__SSE2_MATH__))
198     int sign, shift;
199     int64_t mant;
200     union {
201         double d;
202         int64_t i64;
203     } conv;
204
205     conv.d = d;
206     sign = (conv.i64 >> 63) | 1;
207     shift = ((conv.i64 >> 52) & 0x7ff) - (1023 + 52);
208
209     /* Over/underflow */
210     if(shift >= 63 || shift < -52) UNLIKELY
211         return 0;
212
213     mant = (conv.i64 & 0xfffffffffffff_i64) | 0x10000000000000_i64;
214     if(shift < 0) LIKELY
215         return (int)(mant >> -shift) * sign;
216     return (int)(mant << shift) * sign;
217
218 #else
219
220     return static_cast<int>(d);
221 #endif
222 }
223
224 /**
225  * Rounds a float to the nearest integral value, according to the current
226  * rounding mode. This is essentially an inlined version of rintf, although
227  * makes fewer promises (e.g. -0 or -0.25 rounded to 0 may result in +0).
228  */
229 inline float fast_roundf(float f) noexcept
230 {
231 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
232     && !defined(__SSE_MATH__)
233
234     float out;
235     __asm__ __volatile__("frndint" : "=t"(out) : "0"(f));
236     return out;
237
238 #elif (defined(__GNUC__) || defined(__clang__)) && defined(__aarch64__)
239
240     float out;
241     __asm__ volatile("frintx %s0, %s1" : "=w"(out) : "w"(f));
242     return out;
243
244 #else
245
246     /* Integral limit, where sub-integral precision is not available for
247      * floats.
248      */
249     static const float ilim[2]{
250          8388608.0f /*  0x1.0p+23 */,
251         -8388608.0f /* -0x1.0p+23 */
252     };
253     unsigned int sign, expo;
254     union {
255         float f;
256         unsigned int i;
257     } conv;
258
259     conv.f = f;
260     sign = (conv.i>>31)&0x01;
261     expo = (conv.i>>23)&0xff;
262
263     if(expo >= 150/*+23*/) UNLIKELY
264     {
265         /* An exponent (base-2) of 23 or higher is incapable of sub-integral
266          * precision, so it's already an integral value. We don't need to worry
267          * about infinity or NaN here.
268          */
269         return f;
270     }
271     /* Adding the integral limit to the value (with a matching sign) forces a
272      * result that has no sub-integral precision, and is consequently forced to
273      * round to an integral value. Removing the integral limit then restores
274      * the initial value rounded to the integral. The compiler should not
275      * optimize this out because of non-associative rules on floating-point
276      * math (as long as you don't use -fassociative-math,
277      * -funsafe-math-optimizations, -ffast-math, or -Ofast, in which case this
278      * may break).
279      */
280     f += ilim[sign];
281     return f - ilim[sign];
282 #endif
283 }
284
285
286 template<typename T>
287 constexpr const T& clamp(const T& value, const T& min_value, const T& max_value) noexcept
288 {
289     return std::min(std::max(value, min_value), max_value);
290 }
291
292 // Converts level (mB) to gain.
293 inline float level_mb_to_gain(float x)
294 {
295     if(x <= -10'000.0f)
296         return 0.0f;
297     return std::pow(10.0f, x / 2'000.0f);
298 }
299
300 // Converts gain to level (mB).
301 inline float gain_to_level_mb(float x)
302 {
303     if (x <= 0.0f)
304         return -10'000.0f;
305     return maxf(std::log10(x) * 2'000.0f, -10'000.0f);
306 }
307
308 #endif /* AL_NUMERIC_H */