MPD  0.20.15
PcmUtils.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_PCM_UTILS_H
21 #define MPD_PCM_UTILS_H
22 
23 #include "Compiler.h"
24 
25 #include <limits>
26 
27 #include <stdint.h>
28 
29 enum class SampleFormat : uint8_t;
30 template<SampleFormat F> struct SampleTraits;
31 
36 template<SampleFormat F, class Traits=SampleTraits<F>>
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  static_assert(Traits::MIN >= limits::min(), "out of range");
45  static_assert(Traits::MAX <= limits::max(), "out of range");
46 
47  if (gcc_unlikely(x < Traits::MIN))
48  return T(Traits::MIN);
49 
50  if (gcc_unlikely(x > Traits::MAX))
51  return T(Traits::MAX);
52 
53  return T(x);
54 }
55 
56 #endif
static gcc_const Traits::value_type PcmClamp(typename Traits::long_type x) noexcept
Check if the value is within the range of the provided bit size, and caps it if necessary.
Definition: PcmUtils.hxx:39
This template describes the specified SampleFormat.
Definition: PcmUtils.hxx:30
#define gcc_unlikely(x)
Definition: Compiler.h:125
#define gcc_const
Definition: Compiler.h:109
SampleFormat