summaryrefslogtreecommitdiffstats
path: root/src/util/math.h
diff options
context:
space:
mode:
authorRJ Ryan <rryan@mixxx.org>2014-12-17 23:54:33 -0500
committerRJ Ryan <rryan@mixxx.org>2014-12-17 23:55:36 -0500
commit202508fcc37e5ab9ba32baca3acad90e6f37d1e8 (patch)
tree99ba86e5f4a461e9ffabbc2a1afeaa0c23d4167f /src/util/math.h
parentd15a2b97cbabd689a1b43c17c2328749b117fbc9 (diff)
Remove math_clamp_unsafe / math_clamp_safe.
Use DEBUG_ASSERT to check validity of bounds.
Diffstat (limited to 'src/util/math.h')
-rw-r--r--src/util/math.h31
1 files changed, 6 insertions, 25 deletions
diff --git a/src/util/math.h b/src/util/math.h
index 4b317ec724..82afe19696 100644
--- a/src/util/math.h
+++ b/src/util/math.h
@@ -7,7 +7,7 @@
#include <cmath>
#include <algorithm>
-#include <QtDebug>
+#include "util/assert.h"
// If we don't do this then we get the C90 fabs from the global namespace which
// is only defined for double.
@@ -17,32 +17,13 @@ using std::fabs;
#define math_min std::min
#define math_max3(a, b, c) math_max(math_max((a), (b)), (c))
-// Restrict value to the range [min, max]. Undefined behavior
-// if min > max.
-template <typename T>
-inline T math_clamp_unsafe(T value, T min, T max) {
- return math_max(min, math_min(max, value));
-}
-
-// Clamp with bounds checking to avoid undefined behavior
-// on invalid min/max parameters.
-template <typename T>
-inline T math_clamp_safe(T value, T min, T max) {
- if (min <= max) {
- // valid bounds
- return math_clamp_unsafe(value, min, max);
- } else {
- // invalid bounds
- qWarning() << "PROGRAMMING ERROR: math_clamp_safe() called with min > max!"
- << min << ">" << max;
- // simply return the value unchanged
- return value;
- }
-}
-
+// Restrict value to the range [min, max]. Undefined behavior if min > max.
template <typename T>
inline T math_clamp(T value, T min, T max) {
- return math_clamp_safe(value, min, max);
+ // DEBUG_ASSERT compiles out in release builds so it does not affect
+ // vectorization or pipelining of clamping in tight loops.
+ DEBUG_ASSERT(min <= max);
+ return math_max(min, math_min(max, value));
}
// NOTE(rryan): It is an error to call even() on a floating point number. Do not