diff --git a/Source/Core/AudioCommon/DPL2Decoder.cpp b/Source/Core/AudioCommon/DPL2Decoder.cpp index b1b63250e4..036f0e96c8 100644 --- a/Source/Core/AudioCommon/DPL2Decoder.cpp +++ b/Source/Core/AudioCommon/DPL2Decoder.cpp @@ -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); diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp index d582f5596f..1bebe88595 100644 --- a/Source/Core/AudioCommon/Mixer.cpp +++ b/Source/Core/AudioCommon/Mixer.cpp @@ -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; } diff --git a/Source/Core/Common/MathUtil.h b/Source/Core/Common/MathUtil.h index c9bd3e09b4..dbe7a3e6df 100644 --- a/Source/Core/Common/MathUtil.h +++ b/Source/Core/Common/MathUtil.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include @@ -12,20 +13,9 @@ namespace MathUtil { template -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 -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); } }; diff --git a/Source/Core/Core/DSP/DSPAccelerator.cpp b/Source/Core/Core/DSP/DSPAccelerator.cpp index d7be9d61f4..6a6ce0ffb4 100644 --- a/Source/Core/Core/DSP/DSPAccelerator.cpp +++ b/Source/Core/Core/DSP/DSPAccelerator.cpp @@ -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; diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp index 059226d55e..c5784bffb1 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp @@ -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); diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h b/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h index 706c94de80..1ea4ba7491 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h @@ -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; diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp index a3c9731db8..a3c9818750 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AXWii.cpp @@ -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); } } diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp index 4c552948da..4d2c52a85a 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp @@ -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(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; diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h index 780cc89708..b3e6e7caf5 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h +++ b/Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h @@ -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 @@ -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); } } diff --git a/Source/Core/Core/HW/StreamADPCM.cpp b/Source/Core/Core/HW/StreamADPCM.cpp index aeb201bde8..2d1175bc67 100644 --- a/Source/Core/Core/HW/StreamADPCM.cpp +++ b/Source/Core/Core/HW/StreamADPCM.cpp @@ -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; } diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStorePaired.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStorePaired.cpp index 4e3d467541..d280faca21 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStorePaired.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter_LoadStorePaired.cpp @@ -53,8 +53,8 @@ template SType ScaleAndClamp(double ps, u32 stScale) float convPS = (float)ps * m_quantizeTable[stScale]; float min = (float)std::numeric_limits::min(); float max = (float)std::numeric_limits::max(); - MathUtil::Clamp(&convPS, min, max); - return (SType)convPS; + + return (SType)MathUtil::Clamp(convPS, min, max); } template static T ReadUnpaired(u32 addr); diff --git a/Source/Core/DolphinWX/FrameAui.cpp b/Source/Core/DolphinWX/FrameAui.cpp index 346fb03d63..5ac605e101 100644 --- a/Source/Core/DolphinWX/FrameAui.cpp +++ b/Source/Core/DolphinWX/FrameAui.cpp @@ -677,8 +677,8 @@ void CFrame::SetPaneSize() H = Perspectives[ActivePerspective].Height[j]; // Check limits - MathUtil::Clamp(&W, 5, 95); - MathUtil::Clamp(&H, 5, 95); + W = MathUtil::Clamp(W, 5, 95); + H = MathUtil::Clamp(H, 5, 95); // Convert percentages to pixel lengths W = (W * iClientX) / 100; diff --git a/Source/Core/VideoBackends/Software/TransformUnit.cpp b/Source/Core/VideoBackends/Software/TransformUnit.cpp index c72fd0f8f0..edfd3d8852 100644 --- a/Source/Core/VideoBackends/Software/TransformUnit.cpp +++ b/Source/Core/VideoBackends/Software/TransformUnit.cpp @@ -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(lightCol.x), 0, 255); + int light_y = MathUtil::Clamp(static_cast(lightCol.y), 0, 255); + int light_z = MathUtil::Clamp(static_cast(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(lightCol), 0, 255); chancolor[0] = (matcolor[0] * (light_a + (light_a >> 7))) >> 8; } else