diff options
author | RJ Ryan <rryan@mixxx.org> | 2014-04-24 17:32:34 -0400 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2014-04-25 10:38:06 -0400 |
commit | 5517d3e4e657354696503e876c023bc1dd6ef549 (patch) | |
tree | cb4210721772aee720828d2666e81c5fc595101d /src/util/math.h | |
parent | 93bd3fe48794d3908f3937bdf3edbfcb21b3039e (diff) |
Refactor use of math and other headers across the codebase.
* Delete mathstuff.h and defs.h.
* Move typedefs into util/types.h.
* Move definitions into util/defs.h.
* Use std::min and std::max for math_min and math_max.
* Replace math_clamp with a template function to prevent repeated sub-expressions.
* Add a Result enum for SoundSource::open() and SoundSource::parseHeader().
* Misc. other header-related cleanups.
Diffstat (limited to 'src/util/math.h')
-rw-r--r-- | src/util/math.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/util/math.h b/src/util/math.h new file mode 100644 index 0000000000..48816118f6 --- /dev/null +++ b/src/util/math.h @@ -0,0 +1,56 @@ +#ifndef MATH_H +#define MATH_H + +// Causes MSVC to define M_PI and friends. +// http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx +#define _USE_MATH_DEFINES +#include <cmath> +#include <algorithm> + +#define math_max std::max +#define math_min std::min +#define math_max3(a, b, c) math_max(math_max((a), (b)), (c)) + +template <typename T> +inline T math_clamp(const T& value, const T& min, const T& max) { + if (value > max) { + return max; + } + if (value < min) { + return min; + } + return value; +} + +// 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 recognize to manually convert to avoid errors. +template <typename T> +inline bool even(const T& value) { + return value % 2 == 0; +} + +#ifdef _MSC_VER +// VC++ uses _isnan() instead of isnan() and !_finite instead of isinf. +#include <float.h> +#define isnan(x) _isnan(x) +#define isinf(x) (!_finite(x)) +#else +// for isnan() everywhere else use cmath.h's version +using std::isnan; +using std::isinf; +#endif + +inline int roundUpToPowerOf2(int v) { + // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return v; +} + +#endif /* MATH_H */ |