Loading...
Searching...
No Matches
scaled_rounding.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2018 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_SCALED_ROUNDING_INCLUDED
#define ETL_SCALED_ROUNDING_INCLUDED
#include "platform.h"
#include "absolute.h"
#include "type_traits.h"
namespace etl
{
template <typename T>
struct scaled_rounding_t
{
typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type type;
};
//*****************************************************************************
//*****************************************************************************
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_ceiling_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
if (value >= 0)
{
return T((value + scale_t(Scaling - 1U)) / scale_t(Scaling));
}
else
{
return T(value / scale_t(Scaling));
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_ceiling_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return round_ceiling_unscaled<Scaling>(value) * scale_t(Scaling);
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_floor_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
if (value >= 0)
{
return T(value / scale_t(Scaling));
}
else
{
return T((value - scale_t(Scaling - 1)) / scale_t(Scaling));
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_floor_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_floor_unscaled<Scaling>(value) * scale_t(Scaling));
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_up_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
else
{
if (value >= 0)
{
return T((value + scale_t(Scaling / 2U)) / scale_t(Scaling));
}
else
{
return T((value - scale_t(Scaling / 2U)) / scale_t(Scaling));
}
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_up_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_half_up_unscaled<Scaling>(value) * scale_t(Scaling));
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_down_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
if (value >= 0)
{
return T((value + scale_t((Scaling - 1) / 2U)) / scale_t(Scaling));
}
else
{
return T((value - scale_t((Scaling - 1) / 2U)) / scale_t(Scaling));
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_down_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_half_down_unscaled<Scaling>(value) * scale_t(Scaling));
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_zero_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
else
{
return T(value / scale_t(Scaling));
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_zero_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_zero_unscaled<Scaling>(value) * scale_t(Scaling));
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_infinity_unscaled(T value) ETL_NOEXCEPT
{
if (value >= 0)
{
return etl::round_ceiling_unscaled<Scaling>(value);
}
else
{
return etl::round_floor_unscaled<Scaling>(value);
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_infinity_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_infinity_unscaled<Scaling>(value) * scale_t(Scaling));
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
else
{
// Half?
if ((etl::absolute(value) % scale_t(Scaling)) == scale_t(Scaling / 2U))
{
// Odd?
if (static_cast<unsigned int>(value / scale_t(Scaling)) & 1U)
{
return T(round_half_up_unscaled<Scaling>(value));
}
else
{
return T(round_half_down_unscaled<Scaling>(value));
}
}
else
{
return T(round_half_up_unscaled<Scaling>(value));
}
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_half_even_unscaled<Scaling>(value) * scale_t(Scaling));
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_odd_unscaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
if (Scaling == 1)
{
return value;
}
else
{
// Half?
if ((etl::absolute(value) % scale_t(Scaling)) == scale_t(Scaling / 2U))
{
// Odd?
if (static_cast<unsigned int>(value / scale_t(Scaling)) & 1U)
{
return T(round_half_down_unscaled<Scaling>(value));
}
else
{
return T(round_half_up_unscaled<Scaling>(value));
}
}
else
{
return T(round_half_up_unscaled<Scaling>(value));
}
}
}
//***************************************************************************
//***************************************************************************
template <uint32_t Scaling, typename T>
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_odd_scaled(T value) ETL_NOEXCEPT
{
typedef typename scaled_rounding_t<T>::type scale_t;
return T(round_half_odd_unscaled<Scaling>(value) * scale_t(Scaling));
}
} // namespace etl
#endif
absolute.h
etl
Definition absolute.h:40
etl::round_infinity_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_infinity_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:256
etl::round_half_odd_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_odd_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:339
etl::round_floor_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_floor_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:128
etl::round_half_down_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_down_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:210
etl::round_half_down_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_down_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:184
etl::round_half_even_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:325
etl::round_infinity_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_infinity_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:274
etl::round_floor_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_floor_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:104
etl::round_zero_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_zero_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:223
etl::round_zero_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_zero_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:243
etl::round_half_up_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_up_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:142
etl::round_ceiling_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_ceiling_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:91
etl::round_half_up_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_up_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:170
etl::round_half_even_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:288
etl::round_ceiling_unscaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_ceiling_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:66
etl::round_half_odd_scaled
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_odd_scaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:376
platform.h
etl::scaled_rounding_t
Definition scaled_rounding.h:42
type_traits.h