Skip to content

Commit

Permalink
refactor(util): modernize math.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Jul 15, 2022
1 parent f03b4b8 commit 6d7c776
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/util/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@
#include "util/assert.h"
#include "util/fpclassify.h"

#if __has_include(<bit>)
#include <bit>
#endif

// If we don't do this then we get the C90 fabs from the global namespace which
// is only defined for double.
using std::fabs;

#define math_max std::max
#define math_min std::min
#define math_max3(a, b, c) math_max(math_max((a), (b)), (c))
#define math_min3(a, b, c) math_min(math_min((a), (b)), (c))
#define math_max3(a, b, c) std::max({a, b, c});
#define math_min3(a, b, c) std::min({a, b, c});

// 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) {
template<typename T>
constexpr T math_clamp(T value, T min, T 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));
return std::clamp(value, min, max);
}

// NOTE(rryan): It is an error to call even() on a floating point number. Do not
// hack this to support floating point values! The programmer should be required
// to manually convert so they are aware of the conversion.
template <typename T>
inline bool even(T value) {
template<typename T>
constexpr bool even(T value) {
// since we also want to this to work on size_t and ptrdiff_t, is_integer would be too strict.
static_assert(std::is_arithmetic_v<T> && !std::is_floating_point_v<T>,
"even only supports integral types");
return value % 2 == 0;
}

Expand All @@ -38,36 +45,38 @@ inline bool even(T value) {
#pragma intrinsic(fabs)
#endif

inline unsigned int roundUpToPowerOf2(int v) {
constexpr unsigned int roundUpToPowerOf2(int v) {
DEBUG_ASSERT(v >= 0);
#ifdef __cpp_lib_bitops
const auto uv = static_cast<unsigned int>(v);
return std::bit_ceil(uv);
#else
unsigned int power = 1;
while (power <= v && power > 0) {
while (power < v && power > 0) {
power *= 2;
}
return power;
#endif
}

// TODO (XXX): make this constexpr once <cmath> has constexpr support
inline double roundToFraction(double value, int denominator) {
int wholePart = static_cast<int>(value);
double fractionPart = value - wholePart;
double numerator = std::round(fractionPart * denominator);
return wholePart + numerator / denominator;
}

template <typename T>
// TODO (XXX): make this constexpr once <cmath> has constexpr support
template<typename T>
inline const T ratio2db(const T a) {
static_assert(std::is_same<float, T>::value ||
std::is_same<double, T>::value ||
std::is_same<long double, T>::value,
"ratio2db works only for floating point types");
static_assert(std::is_floating_point_v<T>, "ratio2db works only for floating point types");
return log10(a) * 20;
}

template <typename T>
// TODO (XXX): make this constexpr once <cmath> has constexpr support
template<typename T>
inline const T db2ratio(const T a) {
static_assert(std::is_same<float, T>::value ||
std::is_same<double, T>::value ||
std::is_same<long double, T>::value,
"db2ratio works only for floating point types");
static_assert(std::is_floating_point_v<T>, "db2ratio works only for floating point type");
return static_cast<T>(pow(10, a / 20));
}

0 comments on commit 6d7c776

Please sign in to comment.