MathUtil: Convert Clamp into a constexpr function
This commit is contained in:
parent
c5685ba53a
commit
b9e360df7b
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
|
@ -12,20 +13,9 @@
|
|||
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;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T Clamp(const T val, const T& min, const T& max)
|
||||
{
|
||||
T ret = val;
|
||||
Clamp(&ret, min, max);
|
||||
return ret;
|
||||
return std::max(min, std::min(max, val));
|
||||
}
|
||||
|
||||
// The most significant bit of the fraction is an is-quiet bit on all architectures we care about.
|
||||
|
@ -143,20 +133,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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue