Skip to content

Commit deb4053

Browse files
committed
Replace std::clamp with MathUtils::SaturatingCast
1 parent 4ff214c commit deb4053

14 files changed

Lines changed: 40 additions & 38 deletions

File tree

Source/Core/AudioCommon/Mixer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ void Mixer::SetGBAInputSampleRate(std::size_t device_number, u32 sample_rate)
334334

335335
void Mixer::SetStreamingVolume(u32 lvolume, u32 rvolume)
336336
{
337-
m_streaming_mixer.SetVolume(std::clamp<u32>(lvolume, 0x00, 0xff),
338-
std::clamp<u32>(rvolume, 0x00, 0xff));
337+
m_streaming_mixer.SetVolume(MathUtil::SaturatingCast<u8>(lvolume),
338+
MathUtil::SaturatingCast<u8>(rvolume));
339339
}
340340

341341
void Mixer::SetWiimoteSpeakerVolume(std::size_t wiimote_index, u32 lvolume, u32 rvolume)

Source/Core/Core/DSP/DSPAccelerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ u16 Accelerator::ReadSample(const s16* coefs)
154154
if (raw_sample >= 8)
155155
raw_sample -= 16;
156156

157-
s32 val32 = (scale * raw_sample) + ((0x400 + coef1 * m_yn1 + coef2 * m_yn2) >> 11);
158-
val = static_cast<s16>(std::clamp<s32>(val32, -0x7FFF, 0x7FFF));
157+
const s32 val32 = (scale * raw_sample) + ((0x400 + coef1 * m_yn1 + coef2 * m_yn2) >> 11);
158+
val = MathUtil::SaturatingCast<s16>(val32);
159159
step_size = 2;
160160

161161
m_yn2 = m_yn1;

Source/Core/Core/HW/DSPHLE/UCodes/AESnd.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ void AESndUCode::DoMixing()
344344
right += m_parameter_block.right;
345345
left += m_parameter_block.left;
346346
// Clamping from set40 mode
347-
right = std::clamp(right, -32768, 32767);
348-
left = std::clamp(left, -32768, 32767);
347+
right = MathUtil::SaturatingCast<s16>(right);
348+
left = MathUtil::SaturatingCast<s16>(left);
349349
m_output_buffer[sample_index * 2 + 0] = right;
350350
m_output_buffer[sample_index * 2 + 1] = left;
351351
sample_index++;
@@ -421,8 +421,8 @@ void AESndUCode::DoMixing()
421421
const s32 mixed_l = (static_cast<s32>(new_l) * m_parameter_block.volume_l) >> 8;
422422
const s32 mixed_r = (static_cast<s32>(new_r) * m_parameter_block.volume_r) >> 8;
423423
// Clamping from set40 mode
424-
m_parameter_block.left = std::clamp(mixed_l, -32768, 32767);
425-
m_parameter_block.right = std::clamp(mixed_r, -32768, 32767);
424+
m_parameter_block.left = MathUtil::SaturatingCast<s16>(mixed_l);
425+
m_parameter_block.right = MathUtil::SaturatingCast<s16>(mixed_r);
426426
}
427427
// no_mix - we don't need to do anything as we modify m_parameter_block.left/right in place
428428
}

Source/Core/Core/HW/DSPHLE/UCodes/ASnd.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ void ASndUCode::DoMixing(u32 return_mail)
338338
// "Mix right sample section"
339339
s32 sample_r = static_cast<s16>(m_output_buffer[buffer_offset]);
340340
sample_r += m_current_voice.right;
341-
sample_r = std::clamp(sample_r, -32768, 32767);
341+
sample_r = MathUtil::SaturatingCast<s16>(sample_r);
342342
m_output_buffer[buffer_offset++] = sample_r;
343343
// "Mix left sample section"
344344
s32 sample_l = static_cast<s16>(m_output_buffer[buffer_offset]);
345345
sample_l += m_current_voice.left;
346-
sample_l = std::clamp(sample_l, -32768, 32767);
346+
sample_l = MathUtil::SaturatingCast<s16>(sample_l);
347347
m_output_buffer[buffer_offset++] = sample_l;
348348
// "adds the counter with the voice frequency and test if it >=48000 to get the next sample"
349349
m_current_voice.counter += m_current_voice.freq;

Source/Core/Core/HW/DSPHLE/UCodes/AXVoice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void GetInputSamples(HLEAccelerator* accelerator, PB_TYPE& pb, s16* samples, u16
356356

357357
s16 ClampS16(s64 sample)
358358
{
359-
return std::clamp<s64>(sample, -0x8000, 0x7FFF);
359+
return MathUtil::SaturatingCast<s16>(sample);
360360
}
361361

362362
// Add samples to an output buffer, with optional volume ramping.

Source/Core/Core/HW/DSPHLE/UCodes/Zelda.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ void ZeldaAudioRenderer::ApplyReverb(bool post_rendering)
11171117
for (u16 j = 0; j < 8; ++j)
11181118
sample += (s32)buffer[i + j] * rpb.filter_coeffs[j];
11191119
sample >>= 15;
1120-
buffer[i] = std::clamp(sample, -0x8000, 0x7FFF);
1120+
buffer[i] = MathUtil::SaturatingCast<s16>(sample);
11211121
}
11221122
};
11231123

@@ -1211,7 +1211,7 @@ void ZeldaAudioRenderer::ApplyLowPassFilter(MixingBuffer* buf, VPB* vpb)
12111211
tmp *= coeff;
12121212
tmp >>= 7;
12131213
tmp += yn1;
1214-
s16 yn0 = std::clamp<s64>(tmp, -0x8000, 0x7FFF);
1214+
s16 yn0 = MathUtil::SaturatingCast<s16>(tmp);
12151215
(*buf)[i] = yn0;
12161216

12171217
yn1 = yn0;
@@ -1237,7 +1237,7 @@ void ZeldaAudioRenderer::ApplyBiquadFilter(MixingBuffer* buf, VPB* vpb)
12371237
tmp += vpb->biquad_bn2 * xn2;
12381238
tmp += vpb->biquad_an1 * yn1;
12391239
tmp += vpb->biquad_an2 * yn2;
1240-
s16 yn0 = std::clamp<s64>(tmp >> 15, -0x8000, 0x7FFF);
1240+
s16 yn0 = MathUtil::SaturatingCast<s16>(tmp >> 15);
12411241
(*buf)[i] = yn0;
12421242

12431243
xn2 = xn1;
@@ -1626,7 +1626,7 @@ void ZeldaAudioRenderer::Resample(VPB* vpb, const s16* src, MixingBuffer* dst)
16261626
dst_sample_unclamped += (s64)2 * coeffs[i] * input[i];
16271627
dst_sample_unclamped >>= 16;
16281628

1629-
dst_sample = (s16)std::clamp<s64>(dst_sample_unclamped, -0x8000, 0x7FFF);
1629+
dst_sample = MathUtil::SaturatingCast<s16>(dst_sample_unclamped);
16301630

16311631
pos += ratio;
16321632
}
@@ -1874,7 +1874,7 @@ void ZeldaAudioRenderer::DecodeAFC(VPB* vpb, s16* dst, size_t block_count)
18741874
{
18751875
s32 sample = delta * nibble + yn1 * m_afc_coeffs[idx * 2] + yn2 * m_afc_coeffs[idx * 2 + 1];
18761876
sample >>= 11;
1877-
sample = std::clamp(sample, -0x8000, 0x7fff);
1877+
sample = MathUtil::SaturatingCast<s16>(sample);
18781878
*dst++ = (s16)sample;
18791879
yn2 = yn1;
18801880
yn1 = sample;

Source/Core/Core/HW/DSPHLE/UCodes/Zelda.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <array>
88

99
#include "Common/CommonTypes.h"
10+
#include "Common/MathUtil.h"
1011
#include "Core/HW/DSPHLE/UCodes/UCodes.h"
1112

1213
namespace Core
@@ -63,7 +64,7 @@ class ZeldaAudioRenderer
6364
s32 tmp = (u32)(*buf)[i] * (u32)vol;
6465
tmp >>= 16 - B;
6566

66-
(*buf)[i] = (s16)std::clamp(tmp, -0x8000, 0x7FFF);
67+
(*buf)[i] = MathUtil::SaturatingCast<s16>(tmp);
6768
}
6869
}
6970
template <size_t N>
@@ -105,7 +106,7 @@ class ZeldaAudioRenderer
105106
while (count--)
106107
{
107108
s32 vol_src = ((s32)*src++ * (s32)vol) >> 15;
108-
*dst++ += std::clamp(vol_src, -0x8000, 0x7FFF);
109+
*dst++ += MathUtil::SaturatingCast<s16>(vol_src);
109110
}
110111
}
111112

Source/Core/Core/HW/SI/SI_DeviceGCSteeringWheel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "Common/CommonTypes.h"
99
#include "Common/Logging/Log.h"
10+
#include "Common/MathUtil.h"
1011
#include "Core/HW/GCPad.h"
1112

1213
namespace SerialInterface
@@ -78,10 +79,10 @@ DataResponse CSIDevice_GCSteeringWheel::GetData(u32& hi, u32& low)
7879
// but we'll have to redesign our GameCube controller input to fix that.
7980

8081
// All 8 bits (Accelerate)
81-
low |= u32(std::clamp(accel_value * 2, 0, 0xff)) << 24;
82+
low |= static_cast<u32>(MathUtil::SaturatingCast<u8>(accel_value * 2)) << 24;
8283

8384
// All 8 bits (Brake)
84-
low |= u32(std::clamp(brake_value * 2, 0, 0xff)) << 16;
85+
low |= static_cast<u32>(MathUtil::SaturatingCast<u8>(brake_value * 2)) << 16;
8586

8687
HandleButtonCombos(pad_status);
8788
}

Source/Core/VideoBackends/Software/Rasterizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static void Draw(s32 x, s32 y, s32 xi, s32 yi)
177177
for (int comp = 0; comp < 4; comp++)
178178
{
179179
const float color = ColorSlopes[i][comp].GetValue(x, y);
180-
tev.Color[i][comp] = (u8)std::clamp<float>(color, 0.0f, 255.0f);
180+
tev.Color[i][comp] = MathUtil::SaturatingCast<u8>(color);
181181
}
182182
}
183183

Source/Core/VideoBackends/Software/SWEfbInterface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ static u32 GammaCorrection(u32 color, const float gamma_rcp)
533533
u8 out_color[4];
534534
for (int i = BLU_C; i <= RED_C; i++)
535535
{
536-
out_color[i] = static_cast<u8>(
537-
std::clamp(std::pow(in_colors[i] / 255.0f, gamma_rcp) * 255.0f, 0.0f, 255.0f));
536+
out_color[i] =
537+
MathUtil::SaturatingCast<u8>(std::pow(in_colors[i] / 255.0f, gamma_rcp) * 255.0f);
538538
}
539539

540540
u32 out_color32;

0 commit comments

Comments
 (0)