diff --git a/maths.cpp b/maths.cpp index b33c64c7..9e89ff88 100644 --- a/maths.cpp +++ b/maths.cpp @@ -182,5 +182,7 @@ template int sign (int); // so it's easier to instantiate early and check for broken code at library // build time. +template float limit (float, float, float); + template float smoothstep (float, float, float); template double smoothstep (double, double, double); diff --git a/maths.hpp b/maths.hpp index a7c5b756..d5f54b1c 100644 --- a/maths.hpp +++ b/maths.hpp @@ -276,10 +276,18 @@ max [[gnu::pure]] (const T a, const U b, Args ...args) //----------------------------------------------------------------------------- +// Limiting functions + +// min/max clamping template T -limit [[gnu::pure]] (const T val, const U hi, const V lo) +limit [[gnu::pure]] (const T val, const U lo, const V hi) { + CHECK_LE( + decltype (lo+hi) (lo), + decltype (hi+lo) (hi) + ); + return val > hi ? hi: val < lo ? lo: val;