From d361f57c8064ce13a0478653050b169dc94e2e99 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 18 Jan 2018 19:35:08 +0100 Subject: compat/util: retire Adjust usages. --- compat/math.hpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 compat/math.hpp (limited to 'compat/math.hpp') diff --git a/compat/math.hpp b/compat/math.hpp new file mode 100644 index 00000000..5d80dace --- /dev/null +++ b/compat/math.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include "macros.hpp" + +#include +#include + +namespace util_detail { + +template +inline auto clamp_float(n val, n min_, n max_) +{ + return std::fmin(std::fmax(val, min_), max_); +} + +template +struct clamp final +{ + static inline auto clamp_(const n& val, const n& min_, const n& max_) + { + if (unlikely(val > max_)) + return max_; + if (unlikely(val < min_)) + return min_; + return val; + } +}; + +template +struct clamp +{ + static inline auto clamp_(float val, float min_, float max_) + { + return clamp_float(val, min_, max_); + } +}; + +template +struct clamp +{ + static inline auto clamp_(double val, double min_, double max_) + { + return clamp_float(val, min_, max_); + } +}; + +} // ns util_detail + +template +inline auto clamp(const t& val, const u& min, const w& max) +{ + using tp = decltype(val + min + max); + return ::util_detail::clamp, tp>::clamp_(val, min, max); +} + +template +inline int iround(const t& val) +{ + return int(std::round(val)); +} + +template +inline unsigned uround(const t& val) +{ + return std::round(std::fmax(t(0), val)); +} + +#include "macros.hpp" + +template +static force_inline constexpr auto signum(T x) +{ + return x < T(0) ? -1 : 1; +} -- cgit v1.2.3