Loading...
Searching...
No Matches
bit.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) 2021 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_BIT_INCLUDED
#define ETL_BIT_INCLUDED
#include "platform.h"
#include "binary.h"
#include "endianness.h"
#include "integral_limits.h"
#include "type_traits.h"
#include <string.h>
#if ETL_USING_CPP20 && ETL_USING_STL
#include <bit>
#endif
namespace etl
{
//***************************************************************************
//***************************************************************************
template <typename TDestination, typename TSource>
ETL_NODISCARD
typename etl::enable_if< !(etl::is_integral<TDestination>::value && etl::is_integral<TSource>::value) && (sizeof(TDestination) == sizeof(TSource))
&& etl::is_trivially_copyable<TSource>::value && etl::is_trivially_copyable<TDestination>::value,
TDestination>::type bit_cast(const TSource& source) ETL_NOEXCEPT
{
TDestination destination;
memcpy(&destination, &source, sizeof(TDestination));
return destination;
}
//***************************************************************************
//***************************************************************************
template <typename TDestination, typename TSource>
ETL_NODISCARD ETL_CONSTEXPR14
typename etl::enable_if< (etl::is_integral<TDestination>::value && etl::is_integral<TSource>::value) && (sizeof(TDestination) == sizeof(TSource)),
TDestination>::type bit_cast(const TSource& source) ETL_NOEXCEPT
{
return static_cast<TDestination>(source);
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_CONSTEXPR14 typename etl::enable_if<etl::is_integral<T>::value, T>::type byteswap(T value) ETL_NOEXCEPT
{
return etl::reverse_bytes(value);
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, bool>::type has_single_bit(T value) ETL_NOEXCEPT
{
return (value & (value - 1)) == 0;
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countl_zero(T value) ETL_NOEXCEPT
{
return etl::count_leading_zeros(value);
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countl_one(T value) ETL_NOEXCEPT
{
return etl::count_leading_ones(value);
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countr_zero(T value) ETL_NOEXCEPT
{
return etl::count_trailing_zeros(value);
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countr_one(T value) ETL_NOEXCEPT
{
return etl::count_trailing_ones(value);
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type bit_width(T value) ETL_NOEXCEPT
{
#if ETL_USING_CPP20 && ETL_USING_STL
return static_cast<T>(std::bit_width(value));
#else
return static_cast<T>(etl::integral_limits<T>::bits - etl::countl_zero(value));
#endif
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type bit_ceil(T value)
{
#if ETL_USING_CPP20 && ETL_USING_STL
return std::bit_ceil(value);
#else
if (value == T(0))
{
return T(1);
}
else
{
return T(1) << etl::bit_width(T(value - T(1)));
}
#endif
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type bit_floor(T value) ETL_NOEXCEPT
{
#if ETL_USING_CPP20 && ETL_USING_STL
return std::bit_floor(value);
#else
if (value == T(0))
{
return T(0);
}
else
{
return T(1) << (etl::bit_width(value) - T(1));
}
#endif
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type rotl(T value, int n) ETL_NOEXCEPT
{
if (n < 0)
{
return etl::rotate_right(value, static_cast<size_t>(-n));
}
else
{
return etl::rotate_left(value, static_cast<size_t>(n));
}
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type rotr(T value, int n) ETL_NOEXCEPT
{
if (n < 0)
{
return etl::rotate_left(value, static_cast<size_t>(-n));
}
else
{
return etl::rotate_right(value, static_cast<size_t>(n));
}
}
//***************************************************************************
//***************************************************************************
template <typename T>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type popcount(T value) ETL_NOEXCEPT
{
return etl::count_bits(value);
}
} // namespace etl
#endif
binary.h
endianness.h
etl::count_trailing_ones
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_trailing_ones(T value)
Definition binary.h:1377
etl::rotate_left
ETL_CONSTEXPR14 T rotate_left(T value)
Definition binary.h:117
etl::count_bits
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_bits(T value)
Definition binary.h:918
etl::count_trailing_zeros
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_trailing_zeros(T value)
Definition binary.h:1133
etl::count_leading_ones
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_leading_ones(T value)
Definition binary.h:1859
etl::count_leading_zeros
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_leading_zeros(T value)
Definition binary.h:1615
etl::reverse_bytes
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), T >::type reverse_bytes(T value)
Definition binary.h:745
etl::rotate_right
ETL_CONSTEXPR14 T rotate_right(T value)
Definition binary.h:162
etl::integral_limits
Definition integral_limits.h:517
integral_limits.h
etl
Definition absolute.h:40
etl::rotr
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type rotr(T value, int n) ETL_NOEXCEPT
rotr
Definition bit.h:201
etl::countl_one
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, int >::type countl_one(T value) ETL_NOEXCEPT
countl_one
Definition bit.h:105
etl::rotl
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type rotl(T value, int n) ETL_NOEXCEPT
rotl
Definition bit.h:185
etl::bit_floor
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_floor(T value) ETL_NOEXCEPT
bit_floor
Definition bit.h:165
etl::byteswap
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type byteswap(T value) ETL_NOEXCEPT
byteswap
Definition bit.h:79
etl::popcount
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, int >::type popcount(T value) ETL_NOEXCEPT
popcount
Definition bit.h:217
etl::has_single_bit
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, bool >::type has_single_bit(T value) ETL_NOEXCEPT
has_single_bit
Definition bit.h:88
etl::countr_one
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, int >::type countr_one(T value) ETL_NOEXCEPT
countr_one
Definition bit.h:123
etl::bit_width
ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_width(T value) ETL_NOEXCEPT
bit_width
Definition bit.h:132
etl::countr_zero
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, int >::type countr_zero(T value) ETL_NOEXCEPT
countr_zero
Definition bit.h:114
etl::bit_ceil
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_ceil(T value)
bit_ceil
Definition bit.h:145
etl::countl_zero
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, int >::type countl_zero(T value) ETL_NOEXCEPT
countl_zero
Definition bit.h:96
etl::bit_cast
ETL_NODISCARD etl::enable_if<!(etl::is_integral< TDestination >::value &&etl::is_integral< TSource >::value)&&(sizeof(TDestination)==sizeof(TSource))&&etl::is_trivially_copyable< TSource >::value &&etl::is_trivially_copyable< TDestination >::value, TDestination >::type bit_cast(const TSource &source) ETL_NOEXCEPT
bit_cast - Type to different type.
Definition bit.h:55
platform.h
string.h
etl::type
type_traits.h