PcmUtils.hxx 1.58 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20 21
#ifndef MPD_PCM_UTILS_H
#define MPD_PCM_UTILS_H
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "Compiler.h"
24

25 26
#include <limits>

27
#include <stdint.h>
28

29 30 31
enum class SampleFormat : uint8_t;
template<SampleFormat F> struct SampleTraits;

32 33 34 35
/**
 * Check if the value is within the range of the provided bit size,
 * and caps it if necessary.
 */
36
template<SampleFormat F, class Traits=SampleTraits<F>>
37
gcc_const
38
static inline typename Traits::value_type
39
PcmClamp(typename Traits::long_type x) noexcept
40
{
41
	typedef typename Traits::value_type T;
42 43

	typedef std::numeric_limits<T> limits;
44 45
	static_assert(Traits::MIN >= limits::min(), "out of range");
	static_assert(Traits::MAX <= limits::max(), "out of range");
46

47 48
	if (gcc_unlikely(x < Traits::MIN))
		return T(Traits::MIN);
49

50 51
	if (gcc_unlikely(x > Traits::MAX))
		return T(Traits::MAX);
52 53 54 55

	return T(x);
}

Warren Dukes's avatar
Warren Dukes committed
56
#endif