math_util: Remove the Clamp() function

C++17 adds clamp() to the standard library, so we can remove ours in
favor of it.
This commit is contained in:
Lioncash 2018-04-19 23:01:50 -04:00
parent 17ad56c1dc
commit fae2dd0344
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
5 changed files with 22 additions and 24 deletions

View file

@ -17,11 +17,6 @@ inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start
return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1)); return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1));
} }
template <typename T>
inline T Clamp(const T val, const T& min, const T& max) {
return std::max(min, std::min(max, val));
}
template <class T> template <class T>
struct Rectangle { struct Rectangle {
T left; T left;

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
@ -87,7 +88,7 @@ void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) {
frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us); frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us);
frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime); frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
frame_limiting_delta_err = 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()) { if (frame_limiting_delta_err > microseconds::zero()) {
std::this_thread::sleep_for(frame_limiting_delta_err); std::this_thread::sleep_for(frame_limiting_delta_err);

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
@ -43,8 +44,8 @@ public:
tilt_angle = 0; tilt_angle = 0;
} else { } else {
tilt_direction = mouse_move.Cast<float>(); tilt_direction = mouse_move.Cast<float>();
tilt_angle = MathUtil::Clamp(tilt_direction.Normalize() * sensitivity, 0.0f, tilt_angle =
MathUtil::PI * 0.5f); std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, MathUtil::PI * 0.5f);
} }
} }
} }

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <memory> #include <memory>
#include <string> #include <string>
#include <tuple> #include <tuple>
@ -290,16 +291,16 @@ void RasterizerOpenGL::DrawArrays() {
: (depth_surface == nullptr ? 1u : depth_surface->res_scale); : (depth_surface == nullptr ? 1u : depth_surface->res_scale);
MathUtil::Rectangle<u32> draw_rect{ MathUtil::Rectangle<u32> draw_rect{
static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) + static_cast<u32>(
viewport_rect.left * res_scale, std::clamp<s32>(static_cast<s32>(surfaces_rect.left) + viewport_rect.left * res_scale,
surfaces_rect.left, surfaces_rect.right)), // Left surfaces_rect.left, surfaces_rect.right)), // Left
static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) + static_cast<u32>(
viewport_rect.top * res_scale, std::clamp<s32>(static_cast<s32>(surfaces_rect.bottom) + viewport_rect.top * res_scale,
surfaces_rect.bottom, surfaces_rect.top)), // Top surfaces_rect.bottom, surfaces_rect.top)), // Top
static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) + static_cast<u32>(
viewport_rect.right * res_scale, std::clamp<s32>(static_cast<s32>(surfaces_rect.left) + viewport_rect.right * res_scale,
surfaces_rect.left, surfaces_rect.right)), // Right surfaces_rect.left, surfaces_rect.right)), // Right
static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) + static_cast<u32>(std::clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
viewport_rect.bottom * res_scale, viewport_rect.bottom * res_scale,
surfaces_rect.bottom, surfaces_rect.top))}; // Bottom surfaces_rect.bottom, surfaces_rect.top))}; // Bottom

View file

@ -1086,10 +1086,10 @@ SurfaceSurfaceRect_Tuple RasterizerCacheOpenGL::GetFramebufferSurfaces(
} }
MathUtil::Rectangle<u32> viewport_clamped{ MathUtil::Rectangle<u32> viewport_clamped{
static_cast<u32>(MathUtil::Clamp(viewport.left, 0, static_cast<s32>(config.width))), static_cast<u32>(std::clamp(viewport.left, 0, static_cast<s32>(config.width))),
static_cast<u32>(MathUtil::Clamp(viewport.top, 0, static_cast<s32>(config.height))), static_cast<u32>(std::clamp(viewport.top, 0, static_cast<s32>(config.height))),
static_cast<u32>(MathUtil::Clamp(viewport.right, 0, static_cast<s32>(config.width))), static_cast<u32>(std::clamp(viewport.right, 0, static_cast<s32>(config.width))),
static_cast<u32>(MathUtil::Clamp(viewport.bottom, 0, static_cast<s32>(config.height)))}; static_cast<u32>(std::clamp(viewport.bottom, 0, static_cast<s32>(config.height)))};
// get color and depth surfaces // get color and depth surfaces
SurfaceParams color_params; SurfaceParams color_params;