Embedded Template Library 1.0
Loading...
Searching...
No Matches
bit.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_BIT_INCLUDED
32#define ETL_BIT_INCLUDED
33
34#include "platform.h"
35#include "binary.h"
36#include "endianness.h"
37#include "integral_limits.h"
38#include "type_traits.h"
39
40#include <string.h>
41
42#if ETL_USING_CPP20 && ETL_USING_STL
43 #include <bit>
44#endif
45
46namespace etl
47{
48 //***************************************************************************
50 //***************************************************************************
51 template <typename TDestination, typename TSource>
52 ETL_NODISCARD
53 typename etl::enable_if< !(etl::is_integral<TDestination>::value && etl::is_integral<TSource>::value) && (sizeof(TDestination) == sizeof(TSource))
54 && etl::is_trivially_copyable<TSource>::value && etl::is_trivially_copyable<TDestination>::value,
55 TDestination>::type bit_cast(const TSource& source) ETL_NOEXCEPT
56 {
57 TDestination destination;
58
59 memcpy(&destination, &source, sizeof(TDestination));
60
61 return destination;
62 }
63
64 //***************************************************************************
66 //***************************************************************************
67 template <typename TDestination, typename TSource>
68 ETL_NODISCARD ETL_CONSTEXPR14
69 typename etl::enable_if< (etl::is_integral<TDestination>::value && etl::is_integral<TSource>::value) && (sizeof(TDestination) == sizeof(TSource)),
70 TDestination>::type bit_cast(const TSource& source) ETL_NOEXCEPT
71 {
72 return static_cast<TDestination>(source);
73 }
74
75 //***************************************************************************
77 //***************************************************************************
78 template <typename T>
79 ETL_CONSTEXPR14 typename etl::enable_if<etl::is_integral<T>::value, T>::type byteswap(T value) ETL_NOEXCEPT
80 {
81 return etl::reverse_bytes(value);
82 }
83
84 //***************************************************************************
86 //***************************************************************************
87 template <typename T>
88 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, bool>::type has_single_bit(T value) ETL_NOEXCEPT
89 {
90 return (value & (value - 1)) == 0;
91 }
92 //***************************************************************************
94 //***************************************************************************
95 template <typename T>
96 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countl_zero(T value) ETL_NOEXCEPT
97 {
98 return etl::count_leading_zeros(value);
99 }
100
101 //***************************************************************************
103 //***************************************************************************
104 template <typename T>
105 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countl_one(T value) ETL_NOEXCEPT
106 {
107 return etl::count_leading_ones(value);
108 }
109
110 //***************************************************************************
112 //***************************************************************************
113 template <typename T>
114 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countr_zero(T value) ETL_NOEXCEPT
115 {
116 return etl::count_trailing_zeros(value);
117 }
118
119 //***************************************************************************
121 //***************************************************************************
122 template <typename T>
123 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type countr_one(T value) ETL_NOEXCEPT
124 {
125 return etl::count_trailing_ones(value);
126 }
127
128 //***************************************************************************
130 //***************************************************************************
131 template <typename T>
132 ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type bit_width(T value) ETL_NOEXCEPT
133 {
134#if ETL_USING_CPP20 && ETL_USING_STL
135 return static_cast<T>(std::bit_width(value));
136#else
137 return static_cast<T>(etl::integral_limits<T>::bits - etl::countl_zero(value));
138#endif
139 }
140
141 //***************************************************************************
143 //***************************************************************************
144 template <typename T>
145 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type bit_ceil(T value)
146 {
147#if ETL_USING_CPP20 && ETL_USING_STL
148 return std::bit_ceil(value);
149#else
150 if (value == T(0))
151 {
152 return T(1);
153 }
154 else
155 {
156 return T(1) << etl::bit_width(T(value - T(1)));
157 }
158#endif
159 }
160
161 //***************************************************************************
163 //***************************************************************************
164 template <typename T>
165 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type bit_floor(T value) ETL_NOEXCEPT
166 {
167#if ETL_USING_CPP20 && ETL_USING_STL
168 return std::bit_floor(value);
169#else
170 if (value == T(0))
171 {
172 return T(0);
173 }
174 else
175 {
176 return T(1) << (etl::bit_width(value) - T(1));
177 }
178#endif
179 }
180
181 //***************************************************************************
183 //***************************************************************************
184 template <typename T>
185 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type rotl(T value, int n) ETL_NOEXCEPT
186 {
187 if (n < 0)
188 {
189 return etl::rotate_right(value, static_cast<size_t>(-n));
190 }
191 else
192 {
193 return etl::rotate_left(value, static_cast<size_t>(n));
194 }
195 }
196
197 //***************************************************************************
199 //***************************************************************************
200 template <typename T>
201 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, T>::type rotr(T value, int n) ETL_NOEXCEPT
202 {
203 if (n < 0)
204 {
205 return etl::rotate_left(value, static_cast<size_t>(-n));
206 }
207 else
208 {
209 return etl::rotate_right(value, static_cast<size_t>(n));
210 }
211 }
212
213 //***************************************************************************
215 //***************************************************************************
216 template <typename T>
217 ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if<etl::is_unsigned<T>::value, int>::type popcount(T value) ETL_NOEXCEPT
218 {
219 return etl::count_bits(value);
220 }
221} // namespace etl
222
223#endif
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_CONSTEXPR14 T rotate_left(T value)
Definition binary.h:117
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_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_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_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_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_CONSTEXPR14 T rotate_right(T value)
Definition binary.h:162
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40
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_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_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_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_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type byteswap(T value) ETL_NOEXCEPT
byteswap
Definition bit.h:79
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_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_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_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_width(T value) ETL_NOEXCEPT
bit_width
Definition bit.h:132
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_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_ceil(T value)
bit_ceil
Definition bit.h:145
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_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