aboutsummaryrefslogtreecommitdiffstats
path: root/src/PcmUtils.hxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-31 22:07:10 +0100
committerMax Kellermann <max@duempel.org>2013-02-01 13:47:29 +0100
commit86a0f9cf9b9cddb60a4fb58c7ef187d220e8abfd (patch)
treef1bb6df451bc8480862b4168d0ea2d2c83f33c4f /src/PcmUtils.hxx
parente6a91d291d74bb341b9f7e47ac576fa97533ceb1 (diff)
downloadmpd-86a0f9cf9b9cddb60a4fb58c7ef187d220e8abfd.tar.gz
mpd-86a0f9cf9b9cddb60a4fb58c7ef187d220e8abfd.tar.xz
mpd-86a0f9cf9b9cddb60a4fb58c7ef187d220e8abfd.zip
PcmFormat: merge duplicate code with a template
Diffstat (limited to '')
-rw-r--r--src/PcmUtils.hxx39
1 files changed, 24 insertions, 15 deletions
diff --git a/src/PcmUtils.hxx b/src/PcmUtils.hxx
index a95af7e68..5683a167d 100644
--- a/src/PcmUtils.hxx
+++ b/src/PcmUtils.hxx
@@ -22,6 +22,8 @@
#include "gcc.h"
+#include <limits>
+
#include <stdint.h>
/**
@@ -64,32 +66,39 @@ pcm_range_64(int64_t sample, unsigned bits)
return sample;
}
+template<typename T, typename U, unsigned bits>
gcc_const
-static inline int16_t
-pcm_clamp_16(int x)
+static inline T
+PcmClamp(U x)
{
- static const int32_t MIN_VALUE = -(1 << 15);
- static const int32_t MAX_VALUE = (1 << 15) - 1;
+ constexpr U MIN_VALUE = -(1 << (bits - 1));
+ constexpr U MAX_VALUE = (1 << (bits - 1)) - 1;
+
+ typedef std::numeric_limits<T> limits;
+ static_assert(MIN_VALUE >= limits::min(), "out of range");
+ static_assert(MAX_VALUE <= limits::max(), "out of range");
if (gcc_unlikely(x < MIN_VALUE))
- return MIN_VALUE;
+ return T(MIN_VALUE);
+
if (gcc_unlikely(x > MAX_VALUE))
- return MAX_VALUE;
- return x;
+ return T(MAX_VALUE);
+
+ return T(x);
+}
+
+gcc_const
+static inline int16_t
+pcm_clamp_16(int x)
+{
+ return PcmClamp<int16_t, int, 16>(x);
}
gcc_const
static inline int32_t
pcm_clamp_24(int x)
{
- static const int32_t MIN_VALUE = -(1 << 23);
- static const int32_t MAX_VALUE = (1 << 23) - 1;
-
- if (gcc_unlikely(x < MIN_VALUE))
- return MIN_VALUE;
- if (gcc_unlikely(x > MAX_VALUE))
- return MAX_VALUE;
- return x;
+ return PcmClamp<int32_t, int, 24>(x);
}
#endif