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 "type_traits.h"
36#include "binary.h"
37#include "integral_limits.h"
38#include "endianness.h"
39#include "type_traits.h"
40
41#include <string.h>
42
43#if ETL_USING_CPP20 && ETL_USING_STL
44 #include <bit>
45#endif
46
47namespace etl
48{
49 //***************************************************************************
51 //***************************************************************************
52 template <typename TDestination, typename TSource>
53 ETL_NODISCARD
55 (sizeof(TDestination) == sizeof(TSource)) &&
58 bit_cast(const TSource& source) ETL_NOEXCEPT
59 {
61
63
64 return destination;
65 }
66
67 //***************************************************************************
69 //***************************************************************************
70 template <typename TDestination, typename TSource>
71 ETL_NODISCARD
72 ETL_CONSTEXPR14
74 (sizeof(TDestination) == sizeof(TSource)), TDestination>::type
75 bit_cast(const TSource& source) ETL_NOEXCEPT
76 {
77 return static_cast<TDestination>(source);
78 }
79
80 //***************************************************************************
82 //***************************************************************************
83 template <typename T>
84 ETL_CONSTEXPR14
86 byteswap(T value) ETL_NOEXCEPT
87 {
88 return etl::reverse_bytes(value);
89 }
90
91 //***************************************************************************
93 //***************************************************************************
94 template <typename T>
95 ETL_NODISCARD ETL_CONSTEXPR14
96 typename etl::enable_if<etl::is_unsigned<T>::value, bool>::type has_single_bit(T value) ETL_NOEXCEPT
97 {
98 return (value & (value - 1)) == 0;
99 }
100 //***************************************************************************
102 //***************************************************************************
103 template <typename T>
104 ETL_NODISCARD ETL_CONSTEXPR14
106 countl_zero(T value) ETL_NOEXCEPT
107 {
108 return etl::count_leading_zeros(value);
109 }
110
111 //***************************************************************************
113 //***************************************************************************
114 template <typename T>
115 ETL_NODISCARD ETL_CONSTEXPR14
117 countl_one(T value) ETL_NOEXCEPT
118 {
119 return etl::count_leading_ones(value);
120 }
121
122 //***************************************************************************
124 //***************************************************************************
125 template <typename T>
126 ETL_NODISCARD ETL_CONSTEXPR14
128 countr_zero(T value) ETL_NOEXCEPT
129 {
130 return etl::count_trailing_zeros(value);
131 }
132
133 //***************************************************************************
135 //***************************************************************************
136 template <typename T>
137 ETL_NODISCARD ETL_CONSTEXPR14
139 countr_one(T value) ETL_NOEXCEPT
140 {
141 return etl::count_trailing_ones(value);;
142 }
143
144
145 //***************************************************************************
147 //***************************************************************************
148 template <typename T>
149 ETL_CONSTEXPR14
151 bit_width(T value) ETL_NOEXCEPT
152 {
153#if ETL_USING_CPP20 && ETL_USING_STL
154 return std::bit_width(value);
155#else
157#endif
158 }
159
160 //***************************************************************************
162 //***************************************************************************
163 template <typename T>
164 ETL_NODISCARD ETL_CONSTEXPR14
166 bit_ceil(T value)
167 {
168#if ETL_USING_CPP20 && ETL_USING_STL
169 return std::bit_ceil(value);
170#else
171 if (value == T(0))
172 {
173 return T(1);
174 }
175 else
176 {
177 return T(1) << etl::bit_width(T(value - T(1)));
178 }
179#endif
180 }
181
182 //***************************************************************************
184 //***************************************************************************
185 template <typename T>
186 ETL_NODISCARD ETL_CONSTEXPR14
188 bit_floor(T value) ETL_NOEXCEPT
189 {
190#if ETL_USING_CPP20 && ETL_USING_STL
191 return std::bit_floor(value);
192#else
193 if (value == T(0))
194 {
195 return T(0);
196 }
197 else
198 {
199 return T(1) << (etl::bit_width(value) - T(1));
200 }
201#endif
202 }
203
204 //***************************************************************************
206 //***************************************************************************
207 template <typename T>
208 ETL_NODISCARD ETL_CONSTEXPR14
210 rotl(T value, int n) ETL_NOEXCEPT
211 {
212 if (n < 0)
213 {
214 return etl::rotate_right(value, -n);
215 }
216 else
217 {
218 return etl::rotate_left(value, n);
219 }
220 }
221
222 //***************************************************************************
224 //***************************************************************************
225 template <typename T>
226 ETL_NODISCARD ETL_CONSTEXPR14
228 rotr(T value, int n) ETL_NOEXCEPT
229 {
230 if (n < 0)
231 {
232 return etl::rotate_left(value, -n);
233 }
234 else
235 {
236 return etl::rotate_right(value, n);
237 }
238 }
239
240 //***************************************************************************
242 //***************************************************************************
243 template <typename T>
244 ETL_NODISCARD ETL_CONSTEXPR14
246 popcount(T value) ETL_NOEXCEPT
247 {
248 return etl::count_bits(value);
249 }
250}
251
252#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:1387
ETL_CONSTEXPR14 T rotate_left(T value)
Definition binary.h:116
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:922
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:1141
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:1873
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:1627
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:739
ETL_CONSTEXPR14 T rotate_right(T value)
Definition binary.h:161
Definition integral_limits.h:516
enable_if
Definition type_traits_generator.h:1186
is_integral
Definition type_traits_generator.h:996
bitset_ext
Definition absolute.h:38
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:228
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:117
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:210
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:188
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type byteswap(T value) ETL_NOEXCEPT
byteswap
Definition bit.h:86
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, int >::type popcount(T value) ETL_NOEXCEPT
popcount
Definition bit.h:246
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:96
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:139
ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_width(T value) ETL_NOEXCEPT
bit_width
Definition bit.h:151
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:128
ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if< etl::is_unsigned< T >::value, T >::type bit_ceil(T value)
bit_ceil
Definition bit.h:166
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:106
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:58
Definition type_traits_generator.h:2110
pair holds two objects of arbitrary type
Definition utility.h:164