2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 18:06:14 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 18:06:14 +01:00
|
|
|
*/
|
|
|
|
|
2008-10-31 09:19:53 +01:00
|
|
|
#ifndef MPD_PCM_UTILS_H
|
|
|
|
#define MPD_PCM_UTILS_H
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2009-01-07 18:03:53 +01:00
|
|
|
|
2013-01-31 22:07:10 +01:00
|
|
|
#include <limits>
|
|
|
|
|
2008-09-29 15:53:53 +02:00
|
|
|
#include <stdint.h>
|
2007-05-24 23:15:37 +02:00
|
|
|
|
2013-12-02 09:09:19 +01:00
|
|
|
enum class SampleFormat : uint8_t;
|
|
|
|
template<SampleFormat F> struct SampleTraits;
|
|
|
|
|
2009-01-07 18:03:53 +01:00
|
|
|
/**
|
|
|
|
* Check if the value is within the range of the provided bit size,
|
|
|
|
* and caps it if necessary.
|
|
|
|
*/
|
2013-12-02 09:09:19 +01:00
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
2013-01-30 22:53:12 +01:00
|
|
|
gcc_const
|
2013-12-02 09:09:19 +01:00
|
|
|
static inline typename Traits::value_type
|
|
|
|
PcmClamp(typename Traits::long_type x)
|
2011-10-08 10:25:06 +02:00
|
|
|
{
|
2013-12-02 09:09:19 +01:00
|
|
|
typedef typename Traits::value_type T;
|
2013-01-31 22:07:10 +01:00
|
|
|
|
|
|
|
typedef std::numeric_limits<T> limits;
|
2013-12-22 18:52:54 +01:00
|
|
|
static_assert(Traits::MIN >= limits::min(), "out of range");
|
|
|
|
static_assert(Traits::MAX <= limits::max(), "out of range");
|
2011-10-08 10:25:06 +02:00
|
|
|
|
2013-12-22 18:52:54 +01:00
|
|
|
if (gcc_unlikely(x < Traits::MIN))
|
|
|
|
return T(Traits::MIN);
|
2013-01-31 22:07:10 +01:00
|
|
|
|
2013-12-22 18:52:54 +01:00
|
|
|
if (gcc_unlikely(x > Traits::MAX))
|
|
|
|
return T(Traits::MAX);
|
2013-01-31 22:07:10 +01:00
|
|
|
|
|
|
|
return T(x);
|
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|