From c72779588111e194476c6b9d996f576eac19c0ac Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sat, 26 Aug 2023 09:56:50 +0200 Subject: [PATCH] input: clamp 0-1 in NormalizeDirectedInput If you called this function with a value smaller than threshold, you would get an overflow. This never happened, because we always passed values bigger than threshold. Let's better fix this anyway. --- rpcs3/Emu/Io/PadHandler.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/rpcs3/Emu/Io/PadHandler.cpp b/rpcs3/Emu/Io/PadHandler.cpp index 40eb6dc000..b04e2b6f79 100644 --- a/rpcs3/Emu/Io/PadHandler.cpp +++ b/rpcs3/Emu/Io/PadHandler.cpp @@ -55,15 +55,14 @@ u16 PadHandlerBase::NormalizeTriggerInput(u16 value, int threshold) const { return static_cast(0); } - else if (threshold <= trigger_min) + + if (threshold <= trigger_min) { return static_cast(ScaledInput(value, trigger_min, trigger_max)); } - else - { - const s32 val = static_cast(static_cast(trigger_max) * (value - threshold) / (trigger_max - threshold)); - return static_cast(ScaledInput(val, trigger_min, trigger_max)); - } + + const s32 val = static_cast(static_cast(trigger_max) * (value - threshold) / (trigger_max - threshold)); + return static_cast(ScaledInput(val, trigger_min, trigger_max)); } // normalizes a directed input, meaning it will correspond to a single "button" and not an axis with two directions @@ -81,11 +80,9 @@ u16 PadHandlerBase::NormalizeDirectedInput(s32 raw_value, s32 threshold, s32 max { return static_cast(255.0f * val); } - else - { - const f32 thresh = static_cast(threshold) / maximum; // threshold converted to [0, 1] - return static_cast(255.0f * std::min(1.0f, (val - thresh) / (1.0f - thresh))); - } + + const f32 thresh = static_cast(threshold) / maximum; // threshold converted to [0, 1] + return static_cast(255.0f * std::clamp((val - thresh) / (1.0f - thresh), 0.0f, 1.0f)); } u16 PadHandlerBase::NormalizeStickInput(u16 raw_value, int threshold, int multiplier, bool ignore_threshold) const @@ -96,10 +93,8 @@ u16 PadHandlerBase::NormalizeStickInput(u16 raw_value, int threshold, int multip { return static_cast(ScaledInput(scaled_value, 0, thumb_max)); } - else - { - return NormalizeDirectedInput(scaled_value, threshold, thumb_max); - } + + return NormalizeDirectedInput(scaled_value, threshold, thumb_max); } // This function normalizes stick deadzone based on the DS3's deadzone, which is ~13%