From 7a3e126a4f1b02e62d78814e05172949ae020ab4 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Tue, 24 Jul 2018 19:08:17 +0200 Subject: [PATCH] Replace MathUtil::Clamp with its std counterpart --- src/audio_core/codec.cpp | 3 +-- src/audio_core/hle/filter.cpp | 5 ++-- src/audio_core/hle/mixers.cpp | 3 +-- src/audio_core/interpolate.cpp | 5 ++-- src/audio_core/time_stretch.cpp | 3 +-- src/citra_qt/camera/camera_util.cpp | 8 +++--- src/common/math_util.h | 5 ---- src/core/hw/y2r.cpp | 8 +++--- src/core/perf_stats.cpp | 3 +-- src/input_common/motion_emu.cpp | 2 +- .../renderer_opengl/gl_rasterizer.cpp | 9 +++---- .../renderer_opengl/gl_rasterizer_cache.cpp | 12 +++------ src/video_core/swrasterizer/framebuffer.cpp | 7 +++--- src/video_core/swrasterizer/lighting.cpp | 25 +++++++++---------- src/video_core/swrasterizer/rasterizer.cpp | 7 +++--- src/video_core/swrasterizer/texturing.cpp | 9 +++---- src/video_core/texture/etc1.cpp | 7 +++--- 17 files changed, 49 insertions(+), 72 deletions(-) diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp index b91979c2e..36e70bdab 100644 --- a/src/audio_core/codec.cpp +++ b/src/audio_core/codec.cpp @@ -9,7 +9,6 @@ #include "audio_core/codec.h" #include "common/assert.h" #include "common/common_types.h" -#include "common/math_util.h" namespace AudioCore { namespace Codec { @@ -51,7 +50,7 @@ StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2] int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11; // Clamp to output range. - val = MathUtil::Clamp(val, -32768, 32767); + val = std::clamp(val, -32768, 32767); // Advance output feedback. yn2 = yn1; yn1 = val; diff --git a/src/audio_core/hle/filter.cpp b/src/audio_core/hle/filter.cpp index 9e6ce9196..0413c3b8c 100644 --- a/src/audio_core/hle/filter.cpp +++ b/src/audio_core/hle/filter.cpp @@ -8,7 +8,6 @@ #include "audio_core/hle/filter.h" #include "audio_core/hle/shared_memory.h" #include "common/common_types.h" -#include "common/math_util.h" namespace AudioCore { namespace HLE { @@ -68,7 +67,7 @@ std::array SourceFilters::SimpleFilter::ProcessSample(const std::array y0; for (size_t i = 0; i < 2; i++) { const s32 tmp = (b0 * x0[i] + a1 * y1[i]) >> 15; - y0[i] = MathUtil::Clamp(tmp, -32768, 32767); + y0[i] = std::clamp(tmp, -32768, 32767); } y1 = y0; @@ -102,7 +101,7 @@ std::array SourceFilters::BiquadFilter::ProcessSample(const std::array y0; for (size_t i = 0; i < 2; i++) { const s32 tmp = (b0 * x0[i] + b1 * x1[i] + b2 * x2[i] + a1 * y1[i] + a2 * y2[i]) >> 14; - y0[i] = MathUtil::Clamp(tmp, -32768, 32767); + y0[i] = std::clamp(tmp, -32768, 32767); } x2 = x1; diff --git a/src/audio_core/hle/mixers.cpp b/src/audio_core/hle/mixers.cpp index 1f1f23364..0bd1759d3 100644 --- a/src/audio_core/hle/mixers.cpp +++ b/src/audio_core/hle/mixers.cpp @@ -7,7 +7,6 @@ #include "audio_core/hle/mixers.h" #include "common/assert.h" #include "common/logging/log.h" -#include "common/math_util.h" namespace AudioCore { namespace HLE { @@ -87,7 +86,7 @@ void Mixers::ParseConfig(DspConfiguration& config) { } static s16 ClampToS16(s32 value) { - return static_cast(MathUtil::Clamp(value, -32768, 32767)); + return static_cast(std::clamp(value, -32768, 32767)); } static std::array AddAndClampToS16(const std::array& a, diff --git a/src/audio_core/interpolate.cpp b/src/audio_core/interpolate.cpp index 19ce714e7..bf3696c59 100644 --- a/src/audio_core/interpolate.cpp +++ b/src/audio_core/interpolate.cpp @@ -4,7 +4,6 @@ #include "audio_core/interpolate.h" #include "common/assert.h" -#include "common/math_util.h" namespace AudioCore { namespace AudioInterp { @@ -63,8 +62,8 @@ void Linear(State& state, StereoBuffer16& input, float rate, StereoFrame16& outp StepOverSamples(state, input, rate, output, outputi, [](u64 fraction, const auto& x0, const auto& x1, const auto& x2) { // This is a saturated subtraction. (Verified by black-box fuzzing.) - s64 delta0 = MathUtil::Clamp(x1[0] - x0[0], -32768, 32767); - s64 delta1 = MathUtil::Clamp(x1[1] - x0[1], -32768, 32767); + s64 delta0 = std::clamp(x1[0] - x0[0], -32768, 32767); + s64 delta1 = std::clamp(x1[1] - x0[1], -32768, 32767); return std::array{ static_cast(x0[0] + fraction * delta0 / scale_factor), diff --git a/src/audio_core/time_stretch.cpp b/src/audio_core/time_stretch.cpp index e21bd932a..be6ce8273 100644 --- a/src/audio_core/time_stretch.cpp +++ b/src/audio_core/time_stretch.cpp @@ -10,7 +10,6 @@ #include "audio_core/time_stretch.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "common/math_util.h" using steady_clock = std::chrono::steady_clock; @@ -20,7 +19,7 @@ constexpr double MIN_RATIO = 0.1; constexpr double MAX_RATIO = 100.0; static double ClampRatio(double ratio) { - return MathUtil::Clamp(ratio, MIN_RATIO, MAX_RATIO); + return std::clamp(ratio, MIN_RATIO, MAX_RATIO); } constexpr double MIN_DELAY_TIME = 0.05; // Units: seconds diff --git a/src/citra_qt/camera/camera_util.cpp b/src/citra_qt/camera/camera_util.cpp index 1a3e3b1b0..3d67b1e7a 100644 --- a/src/citra_qt/camera/camera_util.cpp +++ b/src/citra_qt/camera/camera_util.cpp @@ -5,7 +5,6 @@ #include #include #include "citra_qt/camera/camera_util.h" -#include "common/math_util.h" #include "core/frontend/camera/factory.h" #include "core/frontend/camera/interface.h" @@ -191,9 +190,10 @@ std::vector Rgb2Yuv(const QImage& source, int width, int height) { if (write) { pu = (pu + u) / 2; pv = (pv + v) / 2; - using MathUtil::Clamp; - *(dest++) = static_cast(Clamp(py, 0, 0xFF) | (Clamp(pu, 0, 0xFF) << 8)); - *(dest++) = static_cast(Clamp(y, 0, 0xFF) | (Clamp(pv, 0, 0xFF) << 8)); + *(dest++) = + static_cast(std::clamp(py, 0, 0xFF) | (std::clamp(pu, 0, 0xFF) << 8)); + *(dest++) = + static_cast(std::clamp(y, 0, 0xFF) | (std::clamp(pv, 0, 0xFF) << 8)); } else { py = y; pu = u; diff --git a/src/common/math_util.h b/src/common/math_util.h index fa1d61dac..35a1eda75 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -17,11 +17,6 @@ inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1)); } -template -inline T Clamp(const T val, const T& min, const T& max) { - return std::max(min, std::min(max, val)); -} - template struct Rectangle { T left; diff --git a/src/core/hw/y2r.cpp b/src/core/hw/y2r.cpp index 6cee12399..183ce3c70 100644 --- a/src/core/hw/y2r.cpp +++ b/src/core/hw/y2r.cpp @@ -9,7 +9,6 @@ #include "common/assert.h" #include "common/color.h" #include "common/common_types.h" -#include "common/math_util.h" #include "common/vector_math.h" #include "core/hle/service/y2r_u.h" #include "core/hw/y2r.h" @@ -70,10 +69,9 @@ static void ConvertYUVToRGB(InputFormat input_format, const u8* input_Y, const u unsigned int tile = x / 8; unsigned int tile_x = x % 8; u32* out = &output[tile][y * 8 + tile_x]; - - using MathUtil::Clamp; - *out = ((u32)Clamp(r >> 5, 0, 0xFF) << 24) | ((u32)Clamp(g >> 5, 0, 0xFF) << 16) | - ((u32)Clamp(b >> 5, 0, 0xFF) << 8); + *out = ((u32)std::clamp(r >> 5, 0, 0xFF) << 24) | + ((u32)std::clamp(g >> 5, 0, 0xFF) << 16) | + ((u32)std::clamp(b >> 5, 0, 0xFF) << 8); } } } diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cpp index f14e4bbca..0dead3e03 100644 --- a/src/core/perf_stats.cpp +++ b/src/core/perf_stats.cpp @@ -5,7 +5,6 @@ #include #include #include -#include "common/math_util.h" #include "core/hw/gpu.h" #include "core/perf_stats.h" #include "core/settings.h" @@ -92,7 +91,7 @@ void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) { (current_system_time_us - previous_system_time_us) / sleep_scale)); frame_limiting_delta_err -= duration_cast(now - previous_walltime); frame_limiting_delta_err = - MathUtil::Clamp(frame_limiting_delta_err, -max_lag_time_us, max_lag_time_us); + std::clamp(frame_limiting_delta_err, -max_lag_time_us, max_lag_time_us); if (frame_limiting_delta_err > microseconds::zero()) { std::this_thread::sleep_for(frame_limiting_delta_err); diff --git a/src/input_common/motion_emu.cpp b/src/input_common/motion_emu.cpp index 27c81e73b..2a60e37fc 100644 --- a/src/input_common/motion_emu.cpp +++ b/src/input_common/motion_emu.cpp @@ -44,7 +44,7 @@ public: tilt_angle = 0; } else { tilt_direction = mouse_move.Cast(); - tilt_angle = MathUtil::Clamp(tilt_direction.Normalize() * sensitivity, 0.0f, + tilt_angle = std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, MathUtil::PI * this->tilt_clamp / 180.0f); } } diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 734eb4835..eae759b5a 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -10,7 +10,6 @@ #include "common/alignment.h" #include "common/assert.h" #include "common/logging/log.h" -#include "common/math_util.h" #include "common/microprofile.h" #include "common/scope_exit.h" #include "common/vector_math.h" @@ -534,16 +533,16 @@ bool RasterizerOpenGL::Draw(bool accelerate, bool is_indexed) { : (depth_surface == nullptr ? 1u : depth_surface->res_scale); MathUtil::Rectangle draw_rect{ - static_cast(MathUtil::Clamp(static_cast(surfaces_rect.left) + + static_cast(std::clamp(static_cast(surfaces_rect.left) + viewport_rect_unscaled.left * res_scale, surfaces_rect.left, surfaces_rect.right)), // Left - static_cast(MathUtil::Clamp(static_cast(surfaces_rect.bottom) + + static_cast(std::clamp(static_cast(surfaces_rect.bottom) + viewport_rect_unscaled.top * res_scale, surfaces_rect.bottom, surfaces_rect.top)), // Top - static_cast(MathUtil::Clamp(static_cast(surfaces_rect.left) + + static_cast(std::clamp(static_cast(surfaces_rect.left) + viewport_rect_unscaled.right * res_scale, surfaces_rect.left, surfaces_rect.right)), // Right - static_cast(MathUtil::Clamp(static_cast(surfaces_rect.bottom) + + static_cast(std::clamp(static_cast(surfaces_rect.bottom) + viewport_rect_unscaled.bottom * res_scale, surfaces_rect.bottom, surfaces_rect.top))}; // Bottom diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp index f2f1d73c5..b1028f8cb 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp @@ -18,7 +18,6 @@ #include "common/bit_field.h" #include "common/color.h" #include "common/logging/log.h" -#include "common/math_util.h" #include "common/microprofile.h" #include "common/scope_exit.h" #include "common/vector_math.h" @@ -1359,14 +1358,11 @@ SurfaceSurfaceRect_Tuple RasterizerCacheOpenGL::GetFramebufferSurfaces( } MathUtil::Rectangle viewport_clamped{ + static_cast(std::clamp(viewport_rect.left, 0, static_cast(config.GetWidth()))), + static_cast(std::clamp(viewport_rect.top, 0, static_cast(config.GetHeight()))), + static_cast(std::clamp(viewport_rect.right, 0, static_cast(config.GetWidth()))), static_cast( - MathUtil::Clamp(viewport_rect.left, 0, static_cast(config.GetWidth()))), - static_cast( - MathUtil::Clamp(viewport_rect.top, 0, static_cast(config.GetHeight()))), - static_cast( - MathUtil::Clamp(viewport_rect.right, 0, static_cast(config.GetWidth()))), - static_cast( - MathUtil::Clamp(viewport_rect.bottom, 0, static_cast(config.GetHeight())))}; + std::clamp(viewport_rect.bottom, 0, static_cast(config.GetHeight())))}; // get color and depth surfaces SurfaceParams color_params; diff --git a/src/video_core/swrasterizer/framebuffer.cpp b/src/video_core/swrasterizer/framebuffer.cpp index 7e89e9966..0b92dfde1 100644 --- a/src/video_core/swrasterizer/framebuffer.cpp +++ b/src/video_core/swrasterizer/framebuffer.cpp @@ -8,7 +8,6 @@ #include "common/color.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "common/math_util.h" #include "common/vector_math.h" #include "core/hw/gpu.h" #include "core/memory.h" @@ -301,8 +300,8 @@ Math::Vec4 EvaluateBlendEquation(const Math::Vec4& src, const Math::Vec4 UNIMPLEMENTED(); } - return Math::Vec4(MathUtil::Clamp(result.r(), 0, 255), MathUtil::Clamp(result.g(), 0, 255), - MathUtil::Clamp(result.b(), 0, 255), MathUtil::Clamp(result.a(), 0, 255)); + return Math::Vec4(std::clamp(result.r(), 0, 255), std::clamp(result.g(), 0, 255), + std::clamp(result.b(), 0, 255), std::clamp(result.a(), 0, 255)); }; u8 LogicOp(u8 src, u8 dest, FramebufferRegs::LogicOp op) { @@ -400,7 +399,7 @@ void DrawShadowMapPixel(int x, int y, u32 depth, u8 stencil) { float16 linear = float16::FromRaw(shadow.linear); float16 x = float16::FromFloat32(static_cast(depth) / ref_z); float16 stencil_new = float16::FromFloat32(stencil) / (constant + linear * x); - stencil = static_cast(MathUtil::Clamp(stencil_new.ToFloat32(), 0.0f, 255.0f)); + stencil = static_cast(std::clamp(stencil_new.ToFloat32(), 0.0f, 255.0f)); if (stencil < ref_s) EncodeX24S8Shadow(stencil, dst_pixel); diff --git a/src/video_core/swrasterizer/lighting.cpp b/src/video_core/swrasterizer/lighting.cpp index 16491d6ef..e4d4f7aeb 100644 --- a/src/video_core/swrasterizer/lighting.cpp +++ b/src/video_core/swrasterizer/lighting.cpp @@ -2,7 +2,6 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "common/math_util.h" #include "video_core/swrasterizer/lighting.h" namespace Pica { @@ -96,10 +95,10 @@ std::tuple, Math::Vec4> ComputeFragmentsColors( size_t lut = static_cast(LightingRegs::LightingSampler::DistanceAttenuation) + num; - float sample_loc = MathUtil::Clamp(scale * distance + bias, 0.0f, 1.0f); + float sample_loc = std::clamp(scale * distance + bias, 0.0f, 1.0f); u8 lutindex = - static_cast(MathUtil::Clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f)); + static_cast(std::clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f)); float delta = sample_loc * 256 - lutindex; dist_atten = LookupLightingLut(lighting_state, lut, lutindex, delta); } @@ -158,11 +157,11 @@ std::tuple, Math::Vec4> ComputeFragmentsColors( result = std::max(result, 0.0f); float flr = std::floor(result * 256.0f); - index = static_cast(MathUtil::Clamp(flr, 0.0f, 255.0f)); + index = static_cast(std::clamp(flr, 0.0f, 255.0f)); delta = result * 256 - index; } else { float flr = std::floor(result * 128.0f); - s8 signed_index = static_cast(MathUtil::Clamp(flr, -128.0f, 127.0f)); + s8 signed_index = static_cast(std::clamp(flr, -128.0f, 127.0f)); delta = result * 128.0f - signed_index; index = static_cast(signed_index); } @@ -316,15 +315,15 @@ std::tuple, Math::Vec4> ComputeFragmentsColors( diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f); - auto diffuse = Math::MakeVec(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, - MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, - MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, - MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255) + auto diffuse = Math::MakeVec(std::clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, + std::clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, + std::clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, + std::clamp(diffuse_sum.w, 0.0f, 1.0f) * 255) .Cast(); - auto specular = Math::MakeVec(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, - MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, - MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, - MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255) + auto specular = Math::MakeVec(std::clamp(specular_sum.x, 0.0f, 1.0f) * 255, + std::clamp(specular_sum.y, 0.0f, 1.0f) * 255, + std::clamp(specular_sum.z, 0.0f, 1.0f) * 255, + std::clamp(specular_sum.w, 0.0f, 1.0f) * 255) .Cast(); return std::make_tuple(diffuse, specular); } diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp index 2e41e35b8..53ddfd8d5 100644 --- a/src/video_core/swrasterizer/rasterizer.cpp +++ b/src/video_core/swrasterizer/rasterizer.cpp @@ -11,7 +11,6 @@ #include "common/color.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "common/math_util.h" #include "common/microprofile.h" #include "common/quaternion.h" #include "common/vector_math.h" @@ -271,7 +270,7 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve } // Clamp the result - depth = MathUtil::Clamp(depth, 0.0f, 1.0f); + depth = std::clamp(depth, 0.0f, 1.0f); // Perspective correct attribute interpolation: // Attribute values cannot be calculated by simple linear interpolation since @@ -645,11 +644,11 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve } // Generate clamped fog factor from LUT for given fog index - float fog_i = MathUtil::Clamp(floorf(fog_index), 0.0f, 127.0f); + float fog_i = std::clamp(floorf(fog_index), 0.0f, 127.0f); float fog_f = fog_index - fog_i; const auto& fog_lut_entry = g_state.fog.lut[static_cast(fog_i)]; float fog_factor = fog_lut_entry.ToFloat() + fog_lut_entry.DiffToFloat() * fog_f; - fog_factor = MathUtil::Clamp(fog_factor, 0.0f, 1.0f); + fog_factor = std::clamp(fog_factor, 0.0f, 1.0f); // Blend the fog for (unsigned i = 0; i < 3; i++) { diff --git a/src/video_core/swrasterizer/texturing.cpp b/src/video_core/swrasterizer/texturing.cpp index 0d92dbcf9..83d434c94 100644 --- a/src/video_core/swrasterizer/texturing.cpp +++ b/src/video_core/swrasterizer/texturing.cpp @@ -6,7 +6,6 @@ #include "common/assert.h" #include "common/common_types.h" -#include "common/math_util.h" #include "common/vector_math.h" #include "video_core/regs_texturing.h" #include "video_core/swrasterizer/texturing.h" @@ -148,9 +147,9 @@ Math::Vec3 ColorCombine(TevStageConfig::Operation op, const Math::Vec3 i // (byte) 128 is correct auto result = input[0].Cast() + input[1].Cast() - Math::MakeVec(128, 128, 128); - result.r() = MathUtil::Clamp(result.r(), 0, 255); - result.g() = MathUtil::Clamp(result.g(), 0, 255); - result.b() = MathUtil::Clamp(result.b(), 0, 255); + result.r() = std::clamp(result.r(), 0, 255); + result.g() = std::clamp(result.g(), 0, 255); + result.b() = std::clamp(result.b(), 0, 255); return result.Cast(); } @@ -218,7 +217,7 @@ u8 AlphaCombine(TevStageConfig::Operation op, const std::array& input) { case Operation::AddSigned: { // TODO(bunnei): Verify that the color conversion from (float) 0.5f to (byte) 128 is correct auto result = static_cast(input[0]) + static_cast(input[1]) - 128; - return static_cast(MathUtil::Clamp(result, 0, 255)); + return static_cast(std::clamp(result, 0, 255)); } case Operation::Lerp: diff --git a/src/video_core/texture/etc1.cpp b/src/video_core/texture/etc1.cpp index 485ed3934..343105f40 100644 --- a/src/video_core/texture/etc1.cpp +++ b/src/video_core/texture/etc1.cpp @@ -6,7 +6,6 @@ #include "common/bit_field.h" #include "common/color.h" #include "common/common_types.h" -#include "common/math_util.h" #include "common/vector_math.h" #include "video_core/texture/etc1.h" @@ -110,9 +109,9 @@ union ETC1Tile { if (GetNegationFlag(texel)) modifier *= -1; - ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255); - ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255); - ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255); + ret.r() = std::clamp(ret.r() + modifier, 0, 255); + ret.g() = std::clamp(ret.g() + modifier, 0, 255); + ret.b() = std::clamp(ret.b() + modifier, 0, 255); return ret.Cast(); }