Merge pull request #3026 from lioncash/constexpr

MathUtil: Make Clamp and IsPow2 constexpr functions.
This commit is contained in:
Ryan Houdek 2015-09-12 02:28:38 -04:00
commit f121d7a8cd
14 changed files with 46 additions and 70 deletions

View File

@ -134,7 +134,8 @@ static float* DesignFIR(unsigned int *n, float* fc, float opt)
// Sanity check
if (*n == 0)
return nullptr;
MathUtil::Clamp(&fc[0], float(0.001), float(1));
fc[0] = MathUtil::Clamp(fc[0], 0.001f, 1.0f);
float *w = (float*)calloc(sizeof(float), *n);

View File

@ -65,16 +65,14 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo
int sampleL = ((l1 << 16) + (l2 - l1) * (u16)m_frac) >> 16;
sampleL = (sampleL * lvolume) >> 8;
sampleL += samples[currentSample + 1];
MathUtil::Clamp(&sampleL, -32767, 32767);
samples[currentSample + 1] = sampleL;
samples[currentSample + 1] = MathUtil::Clamp(sampleL, -32767, 32767);
s16 r1 = Common::swap16(m_buffer[(indexR + 1) & INDEX_MASK]); //current
s16 r2 = Common::swap16(m_buffer[(indexR2 + 1) & INDEX_MASK]); //next
int sampleR = ((r1 << 16) + (r2 - r1) * (u16)m_frac) >> 16;
sampleR = (sampleR * rvolume) >> 8;
sampleR += samples[currentSample];
MathUtil::Clamp(&sampleR, -32767, 32767);
samples[currentSample] = sampleR;
samples[currentSample] = MathUtil::Clamp(sampleR, -32767, 32767);
m_frac += ratio;
indexR += 2 * (u16)(m_frac >> 16);
@ -89,11 +87,10 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo
s[1] = (s[1] * lvolume) >> 8;
for (; currentSample < numSamples * 2; currentSample += 2)
{
int sampleR = s[0] + samples[currentSample];
MathUtil::Clamp(&sampleR, -32767, 32767);
samples[currentSample] = sampleR;
int sampleL = s[1] + samples[currentSample + 1];
MathUtil::Clamp(&sampleL, -32767, 32767);
int sampleR = MathUtil::Clamp(s[0] + samples[currentSample + 0], -32767, 32767);
int sampleL = MathUtil::Clamp(s[1] + samples[currentSample + 1], -32767, 32767);
samples[currentSample + 0] = sampleR;
samples[currentSample + 1] = sampleL;
}

View File

@ -4,6 +4,7 @@
#pragma once
#include <algorithm>
#include <cstdlib>
#include <vector>
@ -12,20 +13,14 @@
namespace MathUtil
{
template<class T>
inline void Clamp(T* val, const T& min, const T& max)
constexpr T Clamp(const T val, const T& min, const T& max)
{
if (*val < min)
*val = min;
else if (*val > max)
*val = max;
return std::max(min, std::min(max, val));
}
template<class T>
inline T Clamp(const T val, const T& min, const T& max)
constexpr bool IsPow2(u32 imm)
{
T ret = val;
Clamp(&ret, min, max);
return ret;
return (imm & (imm - 1)) == 0;
}
// The most significant bit of the fraction is an is-quiet bit on all architectures we care about.
@ -143,20 +138,20 @@ struct Rectangle
// this Clamp.
void ClampLL(T x1, T y1, T x2, T y2)
{
Clamp(&left, x1, x2);
Clamp(&right, x1, x2);
Clamp(&top, y2, y1);
Clamp(&bottom, y2, y1);
left = Clamp(left, x1, x2);
right = Clamp(right, x1, x2);
top = Clamp(top, y2, y1);
bottom = Clamp(bottom, y2, y1);
}
// If the rectangle is in a coordinate system with an upper-left origin,
// use this Clamp.
void ClampUL(T x1, T y1, T x2, T y2)
{
Clamp(&left, x1, x2);
Clamp(&right, x1, x2);
Clamp(&top, y1, y2);
Clamp(&bottom, y1, y2);
left = Clamp(left, x1, x2);
right = Clamp(right, x1, x2);
top = Clamp(top, y1, y2);
bottom = Clamp(bottom, y1, y2);
}
};
@ -167,8 +162,6 @@ float MathFloatVectorSum(const std::vector<float>&);
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
inline bool IsPow2(u32 imm) {return (imm & (imm - 1)) == 0;}
// Rounds down. 0 -> undefined
inline int IntLog2(u64 val)
{

View File

@ -36,8 +36,7 @@ static s16 ADPCM_Step(u32& _rSamplePos)
// 0x400 = 0.5 in 11-bit fixed point
int val = (scale * temp) + ((0x400 + coef1 * (s16)g_dsp.ifx_regs[DSP_YN1] + coef2 * (s16)g_dsp.ifx_regs[DSP_YN2]) >> 11);
MathUtil::Clamp(&val, -0x7FFF, 0x7FFF);
val = MathUtil::Clamp(val, -0x7FFF, 0x7FFF);
g_dsp.ifx_regs[DSP_YN2] = g_dsp.ifx_regs[DSP_YN1];
g_dsp.ifx_regs[DSP_YN1] = val;

View File

@ -503,11 +503,8 @@ void AXUCode::OutputSamples(u32 lr_addr, u32 surround_addr)
// Output samples clamped to 16 bits and interlaced RLRLRLRLRL...
for (u32 i = 0; i < 5 * 32; ++i)
{
int left = m_samples_left[i];
int right = m_samples_right[i];
MathUtil::Clamp(&left, -32767, 32767);
MathUtil::Clamp(&right, -32767, 32767);
int left = MathUtil::Clamp(m_samples_left[i], -32767, 32767);
int right = MathUtil::Clamp(m_samples_right[i], -32767, 32767);
buffer[2 * i + 0] = Common::swap16(right);
buffer[2 * i + 1] = Common::swap16(left);

View File

@ -185,7 +185,7 @@ u16 AcceleratorGetSample()
temp -= 16;
int val = (scale * temp) + ((0x400 + coef1 * acc_pb->adpcm.yn1 + coef2 * acc_pb->adpcm.yn2) >> 11);
MathUtil::Clamp(&val, -0x7FFF, 0x7FFF);
val = MathUtil::Clamp(val, -0x7FFF, 0x7FFF);
acc_pb->adpcm.yn2 = acc_pb->adpcm.yn1;
acc_pb->adpcm.yn1 = val;

View File

@ -618,11 +618,8 @@ void AXWiiUCode::OutputSamples(u32 lr_addr, u32 surround_addr, u16 volume,
left = ((s64)left * volume_ramp[i]) >> 15;
right = ((s64)right * volume_ramp[i]) >> 15;
MathUtil::Clamp(&left, -32767, 32767);
MathUtil::Clamp(&right, -32767, 32767);
m_samples_left[i] = left;
m_samples_right[i] = right;
m_samples_left[i] = MathUtil::Clamp(left, -32767, 32767);
m_samples_right[i] = MathUtil::Clamp(right, -32767, 32767);
}
for (u32 i = 0; i < 3 * 32; ++i)
@ -653,8 +650,7 @@ void AXWiiUCode::OutputWMSamples(u32* addresses)
u16* out = (u16*)HLEMemory_Get_Pointer(addresses[i]);
for (u32 j = 0; j < 3 * 6; ++j)
{
int sample = in[j];
MathUtil::Clamp(&sample, -32767, 32767);
int sample = MathUtil::Clamp(in[j], -32767, 32767);
out[j] = Common::swap16((u16)sample);
}
}

View File

@ -1015,8 +1015,7 @@ void ZeldaAudioRenderer::ApplyReverb(bool post_rendering)
for (u16 j = 0; j < 8; ++j)
sample += (s32)buffer[i + j] * rpb.filter_coeffs[j];
sample >>= 15;
MathUtil::Clamp(&sample, -0x8000, 0x7fff);
buffer[i] = sample;
buffer[i] = MathUtil::Clamp(sample, -0x8000, 0x7FFF);
}
};
@ -1445,8 +1444,8 @@ void ZeldaAudioRenderer::Resample(VPB* vpb, const s16* src, MixingBuffer* dst)
for (size_t i = 0; i < 4; ++i)
dst_sample_unclamped += (s64)2 * coeffs[i] * input[i];
dst_sample_unclamped >>= 16;
MathUtil::Clamp(&dst_sample_unclamped, (s64)-0x8000, (s64)0x7fff);
dst_sample = (s16)dst_sample_unclamped;
dst_sample = (s16)MathUtil::Clamp<s64>(dst_sample_unclamped, -0x8000, 0x7FFF);
pos += ratio;
}
@ -1696,7 +1695,7 @@ void ZeldaAudioRenderer::DecodeAFC(VPB* vpb, s16* dst, size_t block_count)
yn1 * m_afc_coeffs[idx * 2] +
yn2 * m_afc_coeffs[idx * 2 + 1];
sample >>= 11;
MathUtil::Clamp(&sample, -0x8000, 0x7fff);
sample = MathUtil::Clamp(sample, -0x8000, 0x7fff);
*dst++ = (s16)sample;
yn2 = yn1;
yn1 = sample;

View File

@ -46,8 +46,8 @@ private:
{
s32 tmp = (u32)(*buf)[i] * (u32)vol;
tmp >>= 16 - B;
MathUtil::Clamp(&tmp, -0x8000, 0x7fff);
(*buf)[i] = (s16)tmp;
(*buf)[i] = (s16)MathUtil::Clamp(tmp, -0x8000, 0x7FFF);
}
}
template <size_t N>
@ -90,8 +90,7 @@ private:
while (count--)
{
s32 vol_src = ((s32)*src++ * (s32)vol) >> 15;
MathUtil::Clamp(&vol_src, -0x8000, 0x7fff);
*dst++ += vol_src;
*dst++ += MathUtil::Clamp(vol_src, -0x8000, 0x7FFF);
}
}

View File

@ -31,8 +31,7 @@ static s16 ADPDecodeSample(s32 bits, s32 q, s32& hist1, s32& hist2)
hist = (hist1 * 0x62) - (hist2 * 0x37);
break;
}
hist = (hist + 0x20) >> 6;
MathUtil::Clamp(&hist, -0x200000, 0x1fffff);
hist = MathUtil::Clamp((hist + 0x20) >> 6, -0x200000, 0x1fffff);
s32 cur = (((s16)(bits << 12) >> (q & 0xf)) << 6) + hist;
@ -40,7 +39,7 @@ static s16 ADPDecodeSample(s32 bits, s32 q, s32& hist1, s32& hist2)
hist1 = cur;
cur >>= 6;
MathUtil::Clamp(&cur, -0x8000, 0x7fff);
cur = MathUtil::Clamp(cur, -0x8000, 0x7fff);
return (s16)cur;
}

View File

@ -53,8 +53,8 @@ template<typename SType> SType ScaleAndClamp(double ps, u32 stScale)
float convPS = (float)ps * m_quantizeTable[stScale];
float min = (float)std::numeric_limits<SType>::min();
float max = (float)std::numeric_limits<SType>::max();
MathUtil::Clamp(&convPS, min, max);
return (SType)convPS;
return (SType)MathUtil::Clamp(convPS, min, max);
}
template<typename T> static T ReadUnpaired(u32 addr);

View File

@ -924,7 +924,7 @@ void Jit64::MultiplyImmediate(u32 imm, int a, int d, bool overflow)
if (!overflow)
{
// power of 2; just a shift
if (IsPow2(imm))
if (MathUtil::IsPow2(imm))
{
u32 shift = IntLog2(imm);
// use LEA if it saves an op

View File

@ -677,8 +677,8 @@ void CFrame::SetPaneSize()
H = Perspectives[ActivePerspective].Height[j];
// Check limits
MathUtil::Clamp<u32>(&W, 5, 95);
MathUtil::Clamp<u32>(&H, 5, 95);
W = MathUtil::Clamp<u32>(W, 5, 95);
H = MathUtil::Clamp<u32>(H, 5, 95);
// Convert percentages to pixel lengths
W = (W * iClientX) / 100;

View File

@ -336,12 +336,9 @@ void TransformColor(const InputVertexData *src, OutputVertexData *dst)
LightColor(dst->mvPosition, dst->normal[0], i, colorchan, lightCol);
}
int light_x = int(lightCol.x);
int light_y = int(lightCol.y);
int light_z = int(lightCol.z);
MathUtil::Clamp(&light_x, 0, 255);
MathUtil::Clamp(&light_y, 0, 255);
MathUtil::Clamp(&light_z, 0, 255);
int light_x = MathUtil::Clamp(static_cast<int>(lightCol.x), 0, 255);
int light_y = MathUtil::Clamp(static_cast<int>(lightCol.y), 0, 255);
int light_z = MathUtil::Clamp(static_cast<int>(lightCol.z), 0, 255);
chancolor[1] = (matcolor[1] * (light_x + (light_x >> 7))) >> 8;
chancolor[2] = (matcolor[2] * (light_y + (light_y >> 7))) >> 8;
chancolor[3] = (matcolor[3] * (light_z + (light_z >> 7))) >> 8;
@ -373,8 +370,7 @@ void TransformColor(const InputVertexData *src, OutputVertexData *dst)
LightAlpha(dst->mvPosition, dst->normal[0], i, alphachan, lightCol);
}
int light_a = int(lightCol);
MathUtil::Clamp(&light_a, 0, 255);
int light_a = MathUtil::Clamp(static_cast<int>(lightCol), 0, 255);
chancolor[0] = (matcolor[0] * (light_a + (light_a >> 7))) >> 8;
}
else