aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Alc/alc.cpp2
-rw-r--r--Alc/backends/alsa.cpp2
-rw-r--r--Alc/effects/pshifter.cpp2
-rw-r--r--Alc/logging.h10
-rw-r--r--Alc/mixer/hrtf_inc.cpp2
-rw-r--r--Alc/mixer/mixer_c.cpp70
-rw-r--r--Alc/mixer/mixer_sse.cpp106
-rw-r--r--Alc/mixer/mixer_sse2.cpp40
-rw-r--r--Alc/mixer/mixer_sse41.cpp40
-rw-r--r--CMakeLists.txt1
-rw-r--r--OpenAL32/Include/alMain.h67
-rw-r--r--OpenAL32/alAuxEffectSlot.cpp2
-rw-r--r--OpenAL32/alBuffer.cpp10
-rw-r--r--OpenAL32/alEffect.cpp10
-rw-r--r--OpenAL32/alFilter.cpp10
-rw-r--r--OpenAL32/alSource.cpp16
-rw-r--r--common/opthelpers.h39
17 files changed, 199 insertions, 230 deletions
diff --git a/Alc/alc.cpp b/Alc/alc.cpp
index 0c3e56c1..e44b42c8 100644
--- a/Alc/alc.cpp
+++ b/Alc/alc.cpp
@@ -2102,7 +2102,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
ALsizei idx = CTZ64(usemask);
ALsource *source = sublist.Sources + idx;
- usemask &= ~(U64(1) << idx);
+ usemask &= ~(1_u64 << idx);
if(old_sends != device->NumAuxSends)
{
diff --git a/Alc/backends/alsa.cpp b/Alc/backends/alsa.cpp
index aee9c1d3..33b3ae49 100644
--- a/Alc/backends/alsa.cpp
+++ b/Alc/backends/alsa.cpp
@@ -695,7 +695,7 @@ ALCboolean AlsaPlayback::reset()
bool allowmmap{!!GetConfigValueBool(mDevice->DeviceName.c_str(), "alsa", "mmap", 1)};
ALuint periods{mDevice->NumUpdates};
- ALuint periodLen{static_cast<ALuint>(mDevice->UpdateSize * U64(1000000) / mDevice->Frequency)};
+ ALuint periodLen{static_cast<ALuint>(mDevice->UpdateSize * 1000000_u64 / mDevice->Frequency)};
ALuint bufferLen{periodLen * periods};
ALuint rate{mDevice->Frequency};
diff --git a/Alc/effects/pshifter.cpp b/Alc/effects/pshifter.cpp
index 8d82b07e..7c6fb51e 100644
--- a/Alc/effects/pshifter.cpp
+++ b/Alc/effects/pshifter.cpp
@@ -65,7 +65,7 @@ inline int double2int(double d)
if(UNLIKELY(shift >= 63 || shift < -52))
return 0;
- mant = (conv.i64&I64(0xfffffffffffff)) | I64(0x10000000000000);
+ mant = (conv.i64&0xfffffffffffff_i64) | 0x10000000000000_i64;
if(LIKELY(shift < 0))
return (ALint)(mant >> -shift) * sign;
return (ALint)(mant << shift) * sign;
diff --git a/Alc/logging.h b/Alc/logging.h
index ae70e598..6c2c1c94 100644
--- a/Alc/logging.h
+++ b/Alc/logging.h
@@ -3,6 +3,8 @@
#include <stdio.h>
+#include "opthelpers.h"
+
#if defined(_WIN64)
#define SZFMT "%I64u"
@@ -46,24 +48,24 @@ enum LogLevel {
extern LogLevel gLogLevel;
#define TRACEREF(...) do { \
- if(gLogLevel >= LogRef) \
+ if(UNLIKELY(gLogLevel >= LogRef)) \
AL_PRINT("(--)", __VA_ARGS__); \
} while(0)
#define TRACE(...) do { \
- if(gLogLevel >= LogTrace) \
+ if(UNLIKELY(gLogLevel >= LogTrace)) \
AL_PRINT("(II)", __VA_ARGS__); \
LOG_ANDROID(ANDROID_LOG_DEBUG, __VA_ARGS__); \
} while(0)
#define WARN(...) do { \
- if(gLogLevel >= LogWarning) \
+ if(UNLIKELY(gLogLevel >= LogWarning)) \
AL_PRINT("(WW)", __VA_ARGS__); \
LOG_ANDROID(ANDROID_LOG_WARN, __VA_ARGS__); \
} while(0)
#define ERR(...) do { \
- if(gLogLevel >= LogError) \
+ if(UNLIKELY(gLogLevel >= LogError)) \
AL_PRINT("(EE)", __VA_ARGS__); \
LOG_ANDROID(ANDROID_LOG_ERROR, __VA_ARGS__); \
} while(0)
diff --git a/Alc/mixer/hrtf_inc.cpp b/Alc/mixer/hrtf_inc.cpp
index 6c0824dc..594c6119 100644
--- a/Alc/mixer/hrtf_inc.cpp
+++ b/Alc/mixer/hrtf_inc.cpp
@@ -155,7 +155,7 @@ void MixDirectHrtf(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
const ALfloat (*data)[BUFFERSIZE], DirectHrtfState *State,
const ALsizei NumChans, const ALsizei BufferSize)
{
- ASSUME(NumChans >= 0);
+ ASSUME(NumChans > 0);
ASSUME(BufferSize > 0);
const ALsizei IrSize{State->IrSize};
diff --git a/Alc/mixer/mixer_c.cpp b/Alc/mixer/mixer_c.cpp
index 22d3642e..31a5cee4 100644
--- a/Alc/mixer/mixer_c.cpp
+++ b/Alc/mixer/mixer_c.cpp
@@ -11,35 +11,31 @@
#include "defs.h"
-static inline ALfloat do_point(const InterpState*, const ALfloat *RESTRICT vals, ALsizei) noexcept
+static inline ALfloat do_point(const InterpState&, const ALfloat *RESTRICT vals, const ALsizei) noexcept
{ return vals[0]; }
-static inline ALfloat do_lerp(const InterpState*, const ALfloat *RESTRICT vals, ALsizei frac) noexcept
+static inline ALfloat do_lerp(const InterpState&, const ALfloat *RESTRICT vals, const ALsizei frac) noexcept
{ return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
-static inline ALfloat do_cubic(const InterpState*, const ALfloat *RESTRICT vals, ALsizei frac) noexcept
+static inline ALfloat do_cubic(const InterpState&, const ALfloat *RESTRICT vals, const ALsizei frac) noexcept
{ return cubic(vals[0], vals[1], vals[2], vals[3], frac * (1.0f/FRACTIONONE)); }
-static inline ALfloat do_bsinc(const InterpState *state, const ALfloat *RESTRICT vals, ALsizei frac) noexcept
+static inline ALfloat do_bsinc(const InterpState &istate, const ALfloat *RESTRICT vals, const ALsizei frac) noexcept
{
- const ALfloat *fil, *scd, *phd, *spd;
- ALsizei j_f, pi;
- ALfloat pf, r;
-
- ASSUME(state->bsinc.m > 0);
+ ASSUME(istate.bsinc.m > 0);
// Calculate the phase index and factor.
#define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
- pi = frac >> FRAC_PHASE_BITDIFF;
- pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
+ const ALsizei pi{frac >> FRAC_PHASE_BITDIFF};
+ const ALfloat pf{(frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF))};
#undef FRAC_PHASE_BITDIFF
- fil = state->bsinc.filter + state->bsinc.m*pi*4;
- scd = fil + state->bsinc.m;
- phd = scd + state->bsinc.m;
- spd = phd + state->bsinc.m;
+ const ALfloat *fil{istate.bsinc.filter + istate.bsinc.m*pi*4};
+ const ALfloat *scd{fil + istate.bsinc.m};
+ const ALfloat *phd{scd + istate.bsinc.m};
+ const ALfloat *spd{phd + istate.bsinc.m};
// Apply the scale and phase interpolated filter.
- r = 0.0f;
- for(j_f = 0;j_f < state->bsinc.m;j_f++)
- r += (fil[j_f] + state->bsinc.sf*scd[j_f] + pf*(phd[j_f] + state->bsinc.sf*spd[j_f])) * vals[j_f];
+ ALfloat r{0.0f};
+ for(ALsizei j_f{0};j_f < istate.bsinc.m;j_f++)
+ r += (fil[j_f] + istate.bsinc.sf*scd[j_f] + pf*(phd[j_f] + istate.bsinc.sf*spd[j_f])) * vals[j_f];
return r;
}
@@ -57,7 +53,7 @@ const ALfloat *Resample_copy_C(const InterpState* UNUSED(state),
return dst;
}
-template<ALfloat Sampler(const InterpState*, const ALfloat*RESTRICT, ALsizei) noexcept>
+template<ALfloat Sampler(const InterpState&, const ALfloat*RESTRICT, const ALsizei) noexcept>
static const ALfloat *DoResample(const InterpState *state, const ALfloat *RESTRICT src,
ALsizei frac, ALint increment, ALfloat *RESTRICT dst,
ALsizei numsamples)
@@ -66,11 +62,11 @@ static const ALfloat *DoResample(const InterpState *state, const ALfloat *RESTRI
ASSUME(increment > 0);
ASSUME(frac >= 0);
- const InterpState istate = *state;
+ const InterpState istate{*state};
std::generate_n<ALfloat*RESTRICT>(dst, numsamples,
[&src,&frac,istate,increment]() noexcept -> ALfloat
{
- ALfloat ret{Sampler(&istate, src, frac)};
+ ALfloat ret{Sampler(istate, src, frac)};
frac += increment;
src += frac>>FRACTIONBITS;
@@ -138,23 +134,21 @@ void Mix_C(const ALfloat *data, ALsizei OutChans, ALfloat (*RESTRICT OutBuffer)[
ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
ALsizei BufferSize)
{
- const ALfloat delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
- ALsizei c;
-
ASSUME(OutChans > 0);
ASSUME(BufferSize > 0);
- for(c = 0;c < OutChans;c++)
+ const ALfloat delta{(Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f};
+ for(ALsizei c{0};c < OutChans;c++)
{
- ALsizei pos = 0;
- ALfloat gain = CurrentGains[c];
- const ALfloat diff = TargetGains[c] - gain;
+ ALsizei pos{0};
+ ALfloat gain{CurrentGains[c]};
- if(fabsf(diff) > std::numeric_limits<float>::epsilon())
+ const ALfloat diff{TargetGains[c] - gain};
+ if(std::fabs(diff) > std::numeric_limits<float>::epsilon())
{
- ALsizei minsize = mini(BufferSize, Counter);
- const ALfloat step = diff * delta;
- ALfloat step_count = 0.0f;
+ ALsizei minsize{mini(BufferSize, Counter)};
+ const ALfloat step{diff * delta};
+ ALfloat step_count{0.0f};
for(;pos < minsize;pos++)
{
OutBuffer[c][OutPos+pos] += data[pos] * (gain + step*step_count);
@@ -167,7 +161,7 @@ void Mix_C(const ALfloat *data, ALsizei OutChans, ALfloat (*RESTRICT OutBuffer)[
CurrentGains[c] = gain;
}
- if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
+ if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
continue;
for(;pos < BufferSize;pos++)
OutBuffer[c][OutPos+pos] += data[pos]*gain;
@@ -182,18 +176,16 @@ void Mix_C(const ALfloat *data, ALsizei OutChans, ALfloat (*RESTRICT OutBuffer)[
*/
void MixRow_C(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*RESTRICT data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
{
- ALsizei c, i;
-
ASSUME(InChans > 0);
ASSUME(BufferSize > 0);
- for(c = 0;c < InChans;c++)
+ for(ALsizei c{0};c < InChans;c++)
{
- const ALfloat gain = Gains[c];
- if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
+ const ALfloat gain{Gains[c]};
+ if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
continue;
- for(i = 0;i < BufferSize;i++)
+ for(ALsizei i{0};i < BufferSize;i++)
OutBuffer[i] += data[c][InPos+i] * gain;
}
}
diff --git a/Alc/mixer/mixer_sse.cpp b/Alc/mixer/mixer_sse.cpp
index 2637883b..df5270e7 100644
--- a/Alc/mixer/mixer_sse.cpp
+++ b/Alc/mixer/mixer_sse.cpp
@@ -18,13 +18,9 @@ const ALfloat *Resample_bsinc_SSE(const InterpState *state, const ALfloat *RESTR
ALsizei frac, ALint increment, ALfloat *RESTRICT dst,
ALsizei dstlen)
{
- const ALfloat *const filter = state->bsinc.filter;
- const __m128 sf4 = _mm_set1_ps(state->bsinc.sf);
- const ALsizei m = state->bsinc.m;
- const __m128 *fil, *scd, *phd, *spd;
- ALsizei pi, i, j, offset;
- ALfloat pf;
- __m128 r4;
+ const ALfloat *const filter{state->bsinc.filter};
+ const __m128 sf4{_mm_set1_ps(state->bsinc.sf)};
+ const ALsizei m{state->bsinc.m};
ASSUME(m > 0);
ASSUME(dstlen > 0);
@@ -32,30 +28,30 @@ const ALfloat *Resample_bsinc_SSE(const InterpState *state, const ALfloat *RESTR
ASSUME(frac >= 0);
src -= state->bsinc.l;
- for(i = 0;i < dstlen;i++)
+ for(ALsizei i{0};i < dstlen;i++)
{
// Calculate the phase index and factor.
#define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
- pi = frac >> FRAC_PHASE_BITDIFF;
- pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
+ const ALsizei pi{frac >> FRAC_PHASE_BITDIFF};
+ const ALfloat pf{(frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF))};
#undef FRAC_PHASE_BITDIFF
- offset = m*pi*4;
- fil = (const __m128*)(filter + offset); offset += m;
- scd = (const __m128*)(filter + offset); offset += m;
- phd = (const __m128*)(filter + offset); offset += m;
- spd = (const __m128*)(filter + offset);
+ ALsizei offset{m*pi*4};
+ const __m128 *fil{(const __m128*)(filter + offset)}; offset += m;
+ const __m128 *scd{(const __m128*)(filter + offset)}; offset += m;
+ const __m128 *phd{(const __m128*)(filter + offset)}; offset += m;
+ const __m128 *spd{(const __m128*)(filter + offset)};
// Apply the scale and phase interpolated filter.
- r4 = _mm_setzero_ps();
+ __m128 r4{_mm_setzero_ps()};
{
- const ALsizei count = m >> 2;
- const __m128 pf4 = _mm_set1_ps(pf);
+ const ALsizei count{m >> 2};
+ const __m128 pf4{_mm_set1_ps(pf)};
ASSUME(count > 0);
#define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
- for(j = 0;j < count;j++)
+ for(ALsizei j{0};j < count;j++)
{
/* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
const __m128 f4 = MLA4(
@@ -83,9 +79,7 @@ static inline void ApplyCoeffs(ALsizei Offset, ALfloat (&Values)[HRIR_LENGTH][2]
const ALsizei IrSize, const ALfloat (&Coeffs)[HRIR_LENGTH][2],
const ALfloat left, const ALfloat right)
{
- const __m128 lrlr = _mm_setr_ps(left, right, left, right);
- __m128 vals = _mm_setzero_ps();
- __m128 coeffs;
+ const __m128 lrlr{_mm_setr_ps(left, right, left, right)};
ASSUME(IrSize >= 2);
ASSUME(&Values != &Coeffs);
@@ -97,8 +91,8 @@ static inline void ApplyCoeffs(ALsizei Offset, ALfloat (&Values)[HRIR_LENGTH][2]
ASSUME(count >= 1);
__m128 imp0, imp1;
- coeffs = _mm_load_ps(&Coeffs[0][0]);
- vals = _mm_loadl_pi(vals, (__m64*)&Values[Offset][0]);
+ __m128 coeffs{_mm_load_ps(&Coeffs[0][0])};
+ __m128 vals{_mm_loadl_pi(_mm_setzero_ps(), (__m64*)&Values[Offset][0])};
imp0 = _mm_mul_ps(lrlr, coeffs);
vals = _mm_add_ps(imp0, vals);
_mm_storel_pi((__m64*)&Values[Offset][0], vals);
@@ -135,8 +129,8 @@ static inline void ApplyCoeffs(ALsizei Offset, ALfloat (&Values)[HRIR_LENGTH][2]
{
for(;i < count;i += 2)
{
- coeffs = _mm_load_ps(&Coeffs[i][0]);
- vals = _mm_load_ps(&Values[Offset][0]);
+ __m128 coeffs{_mm_load_ps(&Coeffs[i][0])};
+ __m128 vals{_mm_load_ps(&Values[Offset][0])};
vals = _mm_add_ps(vals, _mm_mul_ps(lrlr, coeffs));
_mm_store_ps(&Values[Offset][0], vals);
Offset += 2;
@@ -159,34 +153,32 @@ void Mix_SSE(const ALfloat *data, ALsizei OutChans, ALfloat (*RESTRICT OutBuffer
ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
ALsizei BufferSize)
{
- const ALfloat delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
- ALsizei c;
-
ASSUME(OutChans > 0);
ASSUME(BufferSize > 0);
- for(c = 0;c < OutChans;c++)
+ const ALfloat delta{(Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f};
+ for(ALsizei c{0};c < OutChans;c++)
{
- ALsizei pos = 0;
- ALfloat gain = CurrentGains[c];
- const ALfloat diff = TargetGains[c] - gain;
+ ALsizei pos{0};
+ ALfloat gain{CurrentGains[c]};
+ const ALfloat diff{TargetGains[c] - gain};
- if(fabsf(diff) > std::numeric_limits<float>::epsilon())
+ if(std::fabs(diff) > std::numeric_limits<float>::epsilon())
{
- ALsizei minsize = mini(BufferSize, Counter);
- const ALfloat step = diff * delta;
- ALfloat step_count = 0.0f;
+ ALsizei minsize{mini(BufferSize, Counter)};
+ const ALfloat step{diff * delta};
+ ALfloat step_count{0.0f};
/* Mix with applying gain steps in aligned multiples of 4. */
if(LIKELY(minsize > 3))
{
- const __m128 four4 = _mm_set1_ps(4.0f);
- const __m128 step4 = _mm_set1_ps(step);
- const __m128 gain4 = _mm_set1_ps(gain);
- __m128 step_count4 = _mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f);
- ALsizei todo = minsize >> 2;
+ const __m128 four4{_mm_set1_ps(4.0f)};
+ const __m128 step4{_mm_set1_ps(step)};
+ const __m128 gain4{_mm_set1_ps(gain)};
+ __m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)};
+ ALsizei todo{minsize >> 2};
do {
- const __m128 val4 = _mm_load_ps(&data[pos]);
- __m128 dry4 = _mm_load_ps(&OutBuffer[c][OutPos+pos]);
+ const __m128 val4{_mm_load_ps(&data[pos])};
+ __m128 dry4{_mm_load_ps(&OutBuffer[c][OutPos+pos])};
#define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
/* dry += val * (gain + step*step_count) */
dry4 = MLA4(dry4, val4, MLA4(gain4, step4, step_count4));
@@ -219,15 +211,15 @@ void Mix_SSE(const ALfloat *data, ALsizei OutChans, ALfloat (*RESTRICT OutBuffer
OutBuffer[c][OutPos+pos] += data[pos]*gain;
}
- if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
+ if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
continue;
if(LIKELY(BufferSize-pos > 3))
{
- ALsizei todo = (BufferSize-pos) >> 2;
- const __m128 gain4 = _mm_set1_ps(gain);
+ ALsizei todo{(BufferSize-pos) >> 2};
+ const __m128 gain4{_mm_set1_ps(gain)};
do {
- const __m128 val4 = _mm_load_ps(&data[pos]);
- __m128 dry4 = _mm_load_ps(&OutBuffer[c][OutPos+pos]);
+ const __m128 val4{_mm_load_ps(&data[pos])};
+ __m128 dry4{_mm_load_ps(&OutBuffer[c][OutPos+pos])};
dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
_mm_store_ps(&OutBuffer[c][OutPos+pos], dry4);
pos += 4;
@@ -240,25 +232,23 @@ void Mix_SSE(const ALfloat *data, ALsizei OutChans, ALfloat (*RESTRICT OutBuffer
void MixRow_SSE(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*RESTRICT data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
{
- ALsizei c;
-
ASSUME(InChans > 0);
ASSUME(BufferSize > 0);
- for(c = 0;c < InChans;c++)
+ for(ALsizei c{0};c < InChans;c++)
{
- ALsizei pos = 0;
- const ALfloat gain = Gains[c];
- if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
+ const ALfloat gain{Gains[c]};
+ if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
continue;
+ ALsizei pos{0};
if(LIKELY(BufferSize > 3))
{
- ALsizei todo = BufferSize >> 2;
+ ALsizei todo{BufferSize >> 2};
const __m128 gain4 = _mm_set1_ps(gain);
do {
- const __m128 val4 = _mm_load_ps(&data[c][InPos+pos]);
- __m128 dry4 = _mm_load_ps(&OutBuffer[pos]);
+ const __m128 val4{_mm_load_ps(&data[c][InPos+pos])};
+ __m128 dry4{_mm_load_ps(&OutBuffer[pos])};
dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
_mm_store_ps(&OutBuffer[pos], dry4);
pos += 4;
diff --git a/Alc/mixer/mixer_sse2.cpp b/Alc/mixer/mixer_sse2.cpp
index 26fe26ba..6408c8f9 100644
--- a/Alc/mixer/mixer_sse2.cpp
+++ b/Alc/mixer/mixer_sse2.cpp
@@ -31,35 +31,33 @@ const ALfloat *Resample_lerp_SSE2(const InterpState* UNUSED(state),
const ALfloat *RESTRICT src, ALsizei frac, ALint increment,
ALfloat *RESTRICT dst, ALsizei numsamples)
{
- const __m128i increment4 = _mm_set1_epi32(increment*4);
- const __m128 fracOne4 = _mm_set1_ps(1.0f/FRACTIONONE);
- const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
- alignas(16) ALsizei pos_[4], frac_[4];
- __m128i frac4, pos4;
- ALsizei todo, pos, i;
+ const __m128i increment4{_mm_set1_epi32(increment*4)};
+ const __m128 fracOne4{_mm_set1_ps(1.0f/FRACTIONONE)};
+ const __m128i fracMask4{_mm_set1_epi32(FRACTIONMASK)};
ASSUME(numsamples > 0);
ASSUME(increment > 0);
ASSUME(frac >= 0);
+ alignas(16) ALsizei pos_[4], frac_[4];
InitiatePositionArrays(frac, increment, frac_, pos_, 4);
- frac4 = _mm_setr_epi32(frac_[0], frac_[1], frac_[2], frac_[3]);
- pos4 = _mm_setr_epi32(pos_[0], pos_[1], pos_[2], pos_[3]);
+ __m128i frac4{_mm_setr_epi32(frac_[0], frac_[1], frac_[2], frac_[3])};
+ __m128i pos4{_mm_setr_epi32(pos_[0], pos_[1], pos_[2], pos_[3])};
- todo = numsamples & ~3;
- for(i = 0;i < todo;i += 4)
+ const ALsizei todo{numsamples & ~3};
+ for(ALsizei i{0};i < todo;i += 4)
{
- const int pos0 = _mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(0, 0, 0, 0)));
- const int pos1 = _mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(1, 1, 1, 1)));
- const int pos2 = _mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(2, 2, 2, 2)));
- const int pos3 = _mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(3, 3, 3, 3)));
- const __m128 val1 = _mm_setr_ps(src[pos0 ], src[pos1 ], src[pos2 ], src[pos3 ]);
- const __m128 val2 = _mm_setr_ps(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1]);
+ const int pos0{_mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(0, 0, 0, 0)))};
+ const int pos1{_mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(1, 1, 1, 1)))};
+ const int pos2{_mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(2, 2, 2, 2)))};
+ const int pos3{_mm_cvtsi128_si32(_mm_shuffle_epi32(pos4, _MM_SHUFFLE(3, 3, 3, 3)))};
+ const __m128 val1{_mm_setr_ps(src[pos0 ], src[pos1 ], src[pos2 ], src[pos3 ])};
+ const __m128 val2{_mm_setr_ps(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1])};
/* val1 + (val2-val1)*mu */
- const __m128 r0 = _mm_sub_ps(val2, val1);
- const __m128 mu = _mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4);
- const __m128 out = _mm_add_ps(val1, _mm_mul_ps(mu, r0));
+ const __m128 r0{_mm_sub_ps(val2, val1)};
+ const __m128 mu{_mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4)};
+ const __m128 out{_mm_add_ps(val1, _mm_mul_ps(mu, r0))};
_mm_store_ps(&dst[i], out);
@@ -71,10 +69,10 @@ const ALfloat *Resample_lerp_SSE2(const InterpState* UNUSED(state),
/* NOTE: These four elements represent the position *after* the last four
* samples, so the lowest element is the next position to resample.
*/
- pos = _mm_cvtsi128_si32(pos4);
+ ALsizei pos{_mm_cvtsi128_si32(pos4)};
frac = _mm_cvtsi128_si32(frac4);
- for(;i < numsamples;++i)
+ for(ALsizei i{todo};i < numsamples;++i)
{
dst[i] = lerp(src[pos], src[pos+1], frac * (1.0f/FRACTIONONE));
diff --git a/Alc/mixer/mixer_sse41.cpp b/Alc/mixer/mixer_sse41.cpp
index cfda905b..fea03764 100644
--- a/Alc/mixer/mixer_sse41.cpp
+++ b/Alc/mixer/mixer_sse41.cpp
@@ -32,35 +32,33 @@ const ALfloat *Resample_lerp_SSE41(const InterpState* UNUSED(state),
const ALfloat *RESTRICT src, ALsizei frac, ALint increment,
ALfloat *RESTRICT dst, ALsizei numsamples)
{
- const __m128i increment4 = _mm_set1_epi32(increment*4);
- const __m128 fracOne4 = _mm_set1_ps(1.0f/FRACTIONONE);
- const __m128i fracMask4 = _mm_set1_epi32(FRACTIONMASK);
- alignas(16) ALsizei pos_[4], frac_[4];
- __m128i frac4, pos4;
- ALsizei todo, pos, i;
+ const __m128i increment4{_mm_set1_epi32(increment*4)};
+ const __m128 fracOne4{_mm_set1_ps(1.0f/FRACTIONONE)};
+ const __m128i fracMask4{_mm_set1_epi32(FRACTIONMASK)};
ASSUME(numsamples > 0);
ASSUME(increment > 0);
ASSUME(frac >= 0);
+ alignas(16) ALsizei pos_[4], frac_[4];
InitiatePositionArrays(frac, increment, frac_, pos_, 4);
- frac4 = _mm_setr_epi32(frac_[0], frac_[1], frac_[2], frac_[3]);
- pos4 = _mm_setr_epi32(pos_[0], pos_[1], pos_[2], pos_[3]);
+ __m128i frac4{_mm_setr_epi32(frac_[0], frac_[1], frac_[2], frac_[3])};
+ __m128i pos4{_mm_setr_epi32(pos_[0], pos_[1], pos_[2], pos_[3])};
- todo = numsamples & ~3;
- for(i = 0;i < todo;i += 4)
+ const ALsizei todo{numsamples & ~3};
+ for(ALsizei i{0};i < todo;i += 4)
{
- const int pos0 = _mm_extract_epi32(pos4, 0);
- const int pos1 = _mm_extract_epi32(pos4, 1);
- const int pos2 = _mm_extract_epi32(pos4, 2);
- const int pos3 = _mm_extract_epi32(pos4, 3);
- const __m128 val1 = _mm_setr_ps(src[pos0 ], src[pos1 ], src[pos2 ], src[pos3 ]);
- const __m128 val2 = _mm_setr_ps(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1]);
+ const int pos0{_mm_extract_epi32(pos4, 0)};
+ const int pos1{_mm_extract_epi32(pos4, 1)};
+ const int pos2{_mm_extract_epi32(pos4, 2)};
+ const int pos3{_mm_extract_epi32(pos4, 3)};
+ const __m128 val1{_mm_setr_ps(src[pos0 ], src[pos1 ], src[pos2 ], src[pos3 ])};
+ const __m128 val2{_mm_setr_ps(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1])};
/* val1 + (val2-val1)*mu */
- const __m128 r0 = _mm_sub_ps(val2, val1);
- const __m128 mu = _mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4);
- const __m128 out = _mm_add_ps(val1, _mm_mul_ps(mu, r0));
+ const __m128 r0{_mm_sub_ps(val2, val1)};
+ const __m128 mu{_mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4)};
+ const __m128 out{_mm_add_ps(val1, _mm_mul_ps(mu, r0))};
_mm_store_ps(&dst[i], out);
@@ -72,10 +70,10 @@ const ALfloat *Resample_lerp_SSE41(const InterpState* UNUSED(state),
/* NOTE: These four elements represent the position *after* the last four
* samples, so the lowest element is the next position to resample.
*/
- pos = _mm_cvtsi128_si32(pos4);
+ ALsizei pos{_mm_cvtsi128_si32(pos4)};
frac = _mm_cvtsi128_si32(frac4);
- for(;i < numsamples;++i)
+ for(ALsizei i{todo};i < numsamples;++i)
{
dst[i] = lerp(src[pos], src[pos+1], frac * (1.0f/FRACTIONONE));
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f0161048..cdbf4855 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -696,6 +696,7 @@ SET(COMMON_OBJS
common/almalloc.h
common/atomic.h
common/math_defs.h
+ common/opthelpers.h
common/threads.cpp
common/threads.h
common/vecmat.h
diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h
index 9fdec5ac..f0771386 100644
--- a/OpenAL32/Include/alMain.h
+++ b/OpenAL32/Include/alMain.h
@@ -32,6 +32,7 @@
#include "almalloc.h"
#include "threads.h"
#include "ambidefs.h"
+#include "opthelpers.h"
template<typename T, size_t N>
@@ -40,41 +41,6 @@ constexpr inline size_t countof(const T(&)[N]) noexcept
#define COUNTOF countof
-#ifdef __has_builtin
-#define HAS_BUILTIN __has_builtin
-#else
-#define HAS_BUILTIN(x) (0)
-#endif
-
-#ifdef __GNUC__
-/* LIKELY optimizes the case where the condition is true. The condition is not
- * required to be true, but it can result in more optimal code for the true
- * path at the expense of a less optimal false path.
- */
-#define LIKELY(x) __builtin_expect(!!(x), !0)
-/* The opposite of LIKELY, optimizing the case where the condition is false. */
-#define UNLIKELY(x) __builtin_expect(!!(x), 0)
-/* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes
- * undefined behavior. It's essentially an assert without actually checking the
- * condition at run-time, allowing for stronger optimizations than LIKELY.
- */
-#if HAS_BUILTIN(__builtin_assume)
-#define ASSUME __builtin_assume
-#else
-#define ASSUME(x) do { if(!(x)) __builtin_unreachable(); } while(0)
-#endif
-
-#else
-
-#define LIKELY(x) (!!(x))
-#define UNLIKELY(x) (!!(x))
-#ifdef _MSC_VER
-#define ASSUME __assume
-#else
-#define ASSUME(x) ((void)0)
-#endif
-#endif
-
#ifndef UNUSED
#if defined(__cplusplus)
#define UNUSED(x)
@@ -97,25 +63,8 @@ constexpr inline size_t countof(const T(&)[N]) noexcept
using ALint64 = ALint64SOFT;
using ALuint64 = ALuint64SOFT;
-#ifndef U64
-#if defined(_MSC_VER)
-#define U64(x) ((ALuint64)(x##ui64))
-#elif SIZEOF_LONG == 8
-#define U64(x) ((ALuint64)(x##ul))
-#elif SIZEOF_LONG_LONG == 8
-#define U64(x) ((ALuint64)(x##ull))
-#endif
-#endif
-
-#ifndef I64
-#if defined(_MSC_VER)
-#define I64(x) ((ALint64)(x##i64))
-#elif SIZEOF_LONG == 8
-#define I64(x) ((ALint64)(x##l))
-#elif SIZEOF_LONG_LONG == 8
-#define I64(x) ((ALint64)(x##ll))
-#endif
-#endif
+inline constexpr int64_t operator "" _i64(unsigned long long int n) noexcept { return static_cast<int64_t>(n); }
+inline constexpr uint64_t operator "" _u64(unsigned long long int n) noexcept { return static_cast<uint64_t>(n); }
/* Define CTZ macros (count trailing zeros), and POPCNT macros (population
* count/count 1 bits), for 32- and 64-bit integers. The CTZ macros' results
@@ -208,10 +157,10 @@ inline int fallback_ctz32(ALuint value)
inline int fallback_popcnt64(ALuint64 v)
{
- v = v - ((v >> 1) & U64(0x5555555555555555));
- v = (v & U64(0x3333333333333333)) + ((v >> 2) & U64(0x3333333333333333));
- v = (v + (v >> 4)) & U64(0x0f0f0f0f0f0f0f0f);
- return (int)((v * U64(0x0101010101010101)) >> 56);
+ v = v - ((v >> 1) & 0x5555555555555555_u64);
+ v = (v & 0x3333333333333333_u64) + ((v >> 2) & 0x3333333333333333_u64);
+ v = (v + (v >> 4)) & 0x0f0f0f0f0f0f0f0f_u64;
+ return (int)((v * 0x0101010101010101_u64) >> 56);
}
#define POPCNT64 fallback_popcnt64
inline int fallback_ctz64(ALuint64 value)
@@ -809,7 +758,7 @@ struct ALCdevice {
/* Nanosecond resolution for the device clock time. */
-#define DEVICE_CLOCK_RES U64(1000000000)
+#define DEVICE_CLOCK_RES 1000000000_u64
/* Must be less than 15 characters (16 including terminating null) for
diff --git a/OpenAL32/alAuxEffectSlot.cpp b/OpenAL32/alAuxEffectSlot.cpp
index 0c2d7bec..55f5649c 100644
--- a/OpenAL32/alAuxEffectSlot.cpp
+++ b/OpenAL32/alAuxEffectSlot.cpp
@@ -58,7 +58,7 @@ inline ALeffect *LookupEffect(ALCdevice *device, ALuint id) noexcept
if(UNLIKELY(lidx >= device->EffectList.size()))
return nullptr;
EffectSubList &sublist = device->EffectList[lidx];
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Effects + slidx;
}
diff --git a/OpenAL32/alBuffer.cpp b/OpenAL32/alBuffer.cpp
index 2f4b9d4d..7f2085d8 100644
--- a/OpenAL32/alBuffer.cpp
+++ b/OpenAL32/alBuffer.cpp
@@ -79,7 +79,7 @@ ALbuffer *AllocBuffer(ALCcontext *context)
}
device->BufferList.emplace_back();
sublist = device->BufferList.end() - 1;
- sublist->FreeMask = ~U64(0);
+ sublist->FreeMask = ~0_u64;
sublist->Buffers = reinterpret_cast<ALbuffer*>(al_calloc(16, sizeof(ALbuffer)*64));
if(UNLIKELY(!sublist->Buffers))
{
@@ -96,7 +96,7 @@ ALbuffer *AllocBuffer(ALCcontext *context)
/* Add 1 to avoid buffer ID 0. */
buffer->id = ((lidx<<6) | slidx) + 1;
- sublist->FreeMask &= ~(U64(1)<<slidx);
+ sublist->FreeMask &= ~(1_u64 << slidx);
return buffer;
}
@@ -109,7 +109,7 @@ void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
buffer->~ALbuffer();
- device->BufferList[lidx].FreeMask |= U64(1) << slidx;
+ device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
}
inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
@@ -120,7 +120,7 @@ inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
if(UNLIKELY(lidx >= device->BufferList.size()))
return nullptr;
BufferSubList &sublist = device->BufferList[lidx];
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Buffers + slidx;
}
@@ -1174,7 +1174,7 @@ BufferSubList::~BufferSubList()
{
ALsizei idx{CTZ64(usemask)};
Buffers[idx].~ALbuffer();
- usemask &= ~(U64(1) << idx);
+ usemask &= ~(1_u64 << idx);
}
FreeMask = ~usemask;
al_free(Buffers);
diff --git a/OpenAL32/alEffect.cpp b/OpenAL32/alEffect.cpp
index 884b0acd..498c04e8 100644
--- a/OpenAL32/alEffect.cpp
+++ b/OpenAL32/alEffect.cpp
@@ -236,7 +236,7 @@ ALeffect *AllocEffect(ALCcontext *context)
}
device->EffectList.emplace_back();
sublist = device->EffectList.end() - 1;
- sublist->FreeMask = ~U64(0);
+ sublist->FreeMask = ~0_u64;
sublist->Effects = static_cast<ALeffect*>(al_calloc(16, sizeof(ALeffect)*64));
if(UNLIKELY(!sublist->Effects))
{
@@ -255,7 +255,7 @@ ALeffect *AllocEffect(ALCcontext *context)
/* Add 1 to avoid effect ID 0. */
effect->id = ((lidx<<6) | slidx) + 1;
- sublist->FreeMask &= ~(U64(1)<<slidx);
+ sublist->FreeMask &= ~(1_u64 << slidx);
return effect;
}
@@ -268,7 +268,7 @@ void FreeEffect(ALCdevice *device, ALeffect *effect)
effect->~ALeffect();
- device->EffectList[lidx].FreeMask |= U64(1) << slidx;
+ device->EffectList[lidx].FreeMask |= 1_u64 << slidx;
}
inline ALeffect *LookupEffect(ALCdevice *device, ALuint id)
@@ -279,7 +279,7 @@ inline ALeffect *LookupEffect(ALCdevice *device, ALuint id)
if(UNLIKELY(lidx >= device->EffectList.size()))
return nullptr;
EffectSubList &sublist = device->EffectList[lidx];
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Effects + slidx;
}
@@ -574,7 +574,7 @@ EffectSubList::~EffectSubList()
{
ALsizei idx = CTZ64(usemask);
Effects[idx].~ALeffect();
- usemask &= ~(U64(1) << idx);
+ usemask &= ~(1_u64 << idx);
}
FreeMask = ~usemask;
al_free(Effects);
diff --git a/OpenAL32/alFilter.cpp b/OpenAL32/alFilter.cpp
index 69a148b6..b3aaa0ee 100644
--- a/OpenAL32/alFilter.cpp
+++ b/OpenAL32/alFilter.cpp
@@ -296,7 +296,7 @@ ALfilter *AllocFilter(ALCcontext *context)
}
device->FilterList.emplace_back();
sublist = device->FilterList.end() - 1;
- sublist->FreeMask = ~U64(0);
+ sublist->FreeMask = ~0_u64;
sublist->Filters = static_cast<ALfilter*>(al_calloc(16, sizeof(ALfilter)*64));
if(UNLIKELY(!sublist->Filters))
{
@@ -315,7 +315,7 @@ ALfilter *AllocFilter(ALCcontext *context)
/* Add 1 to avoid filter ID 0. */
filter->id = ((lidx<<6) | slidx) + 1;
- sublist->FreeMask &= ~(U64(1)<<slidx);
+ sublist->FreeMask &= ~(1_u64 << slidx);
return filter;
}
@@ -328,7 +328,7 @@ void FreeFilter(ALCdevice *device, ALfilter *filter)
filter->~ALfilter();
- device->FilterList[lidx].FreeMask |= U64(1) << slidx;
+ device->FilterList[lidx].FreeMask |= 1_u64 << slidx;
}
@@ -340,7 +340,7 @@ inline ALfilter *LookupFilter(ALCdevice *device, ALuint id)
if(UNLIKELY(lidx >= device->FilterList.size()))
return nullptr;
FilterSubList &sublist = device->FilterList[lidx];
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Filters + slidx;
}
@@ -625,7 +625,7 @@ FilterSubList::~FilterSubList()
{
ALsizei idx = CTZ64(usemask);
Filters[idx].~ALfilter();
- usemask &= ~(U64(1) << idx);
+ usemask &= ~(1_u64 << idx);
}
FreeMask = ~usemask;
al_free(Filters);
diff --git a/OpenAL32/alSource.cpp b/OpenAL32/alSource.cpp
index 5ce14bbf..639d1c37 100644
--- a/OpenAL32/alSource.cpp
+++ b/OpenAL32/alSource.cpp
@@ -188,7 +188,7 @@ ALint64 GetSourceSampleOffset(ALsource *Source, ALCcontext *context, std::chrono
readPos += (ALuint64)BufferList->max_samples << 32;
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
- readPos = minu64(readPos, U64(0x7fffffffffffffff));
+ readPos = minu64(readPos, 0x7fffffffffffffff_u64);
}
return (ALint64)readPos;
@@ -495,7 +495,7 @@ ALsource *AllocSource(ALCcontext *context)
context->SourceList.emplace_back();
sublist = context->SourceList.end() - 1;
- sublist->FreeMask = ~U64(0);
+ sublist->FreeMask = ~0_u64;
sublist->Sources = static_cast<ALsource*>(al_calloc(16, sizeof(ALsource)*64));
if(UNLIKELY(!sublist->Sources))
{
@@ -514,7 +514,7 @@ ALsource *AllocSource(ALCcontext *context)
source->id = ((lidx<<6) | slidx) + 1;
context->NumSources += 1;
- sublist->FreeMask &= ~(U64(1)<<slidx);
+ sublist->FreeMask &= ~(1_u64 << slidx);
return source;
}
@@ -536,7 +536,7 @@ void FreeSource(ALCcontext *context, ALsource *source)
source->~ALsource();
- context->SourceList[lidx].FreeMask |= U64(1) << slidx;
+ context->SourceList[lidx].FreeMask |= 1_u64 << slidx;
context->NumSources--;
}
@@ -549,7 +549,7 @@ inline ALsource *LookupSource(ALCcontext *context, ALuint id) noexcept
if(UNLIKELY(lidx >= context->SourceList.size()))
return nullptr;
SourceSubList &sublist{context->SourceList[lidx]};
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Sources + slidx;
}
@@ -562,7 +562,7 @@ inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) noexcept
if(UNLIKELY(lidx >= device->BufferList.size()))
return nullptr;
BufferSubList &sublist = device->BufferList[lidx];
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Buffers + slidx;
}
@@ -575,7 +575,7 @@ inline ALfilter *LookupFilter(ALCdevice *device, ALuint id) noexcept
if(UNLIKELY(lidx >= device->FilterList.size()))
return nullptr;
FilterSubList &sublist = device->FilterList[lidx];
- if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
+ if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
return nullptr;
return sublist.Filters + slidx;
}
@@ -3401,7 +3401,7 @@ SourceSubList::~SourceSubList()
{
ALsizei idx{CTZ64(usemask)};
Sources[idx].~ALsource();
- usemask &= ~(U64(1) << idx);
+ usemask &= ~(1_u64 << idx);
}
FreeMask = ~usemask;
al_free(Sources);
diff --git a/common/opthelpers.h b/common/opthelpers.h
new file mode 100644
index 00000000..b496942c
--- /dev/null
+++ b/common/opthelpers.h
@@ -0,0 +1,39 @@
+#ifndef OPTHELPERS_H
+#define OPTHELPERS_H
+
+#ifdef __has_builtin
+#define HAS_BUILTIN __has_builtin
+#else
+#define HAS_BUILTIN(x) (0)
+#endif
+
+#ifdef __GNUC__
+/* LIKELY optimizes the case where the condition is true. The condition is not
+ * required to be true, but it can result in more optimal code for the true
+ * path at the expense of a less optimal false path.
+ */
+#define LIKELY(x) __builtin_expect(!!(x), !0)
+/* The opposite of LIKELY, optimizing the case where the condition is false. */
+#define UNLIKELY(x) __builtin_expect(!!(x), 0)
+/* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes
+ * undefined behavior. It's essentially an assert without actually checking the
+ * condition at run-time, allowing for stronger optimizations than LIKELY.
+ */
+#if HAS_BUILTIN(__builtin_assume)
+#define ASSUME __builtin_assume
+#else
+#define ASSUME(x) do { if(!(x)) __builtin_unreachable(); } while(0)
+#endif
+
+#else /* __GNUC__ */
+
+#define LIKELY(x) (!!(x))
+#define UNLIKELY(x) (!!(x))
+#ifdef _MSC_VER
+#define ASSUME __assume
+#else
+#define ASSUME(x) ((void)0)
+#endif /* _MSC_VER */
+#endif /* __GNUC__ */
+
+#endif /* OPTHELPERS_H */