distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
log2u.hpp
Go to the documentation of this file.
1 
12 #ifndef ESTD_LOG2U_HPP_
13 #define ESTD_LOG2U_HPP_
14 
15 #include <climits>
16 
23 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
24 
25 namespace estd
26 {
27 
38 constexpr int log2u(const unsigned int value)
39 {
40  return value != 0 ? (sizeof(value) * CHAR_BIT - 1) - __builtin_clz(value) : 0;
41 }
42 
43 STATIC_ASSERT(log2u(0) == 0);
44 STATIC_ASSERT(log2u(1) == 0);
45 STATIC_ASSERT(log2u(2) == 1);
46 STATIC_ASSERT(log2u(4) == 2);
47 STATIC_ASSERT(log2u(8) == 3);
48 STATIC_ASSERT(log2u(15) == 3);
49 STATIC_ASSERT(log2u(16) == 4);
50 STATIC_ASSERT(log2u(17) == 4);
51 STATIC_ASSERT(log2u(127) == 6);
52 STATIC_ASSERT(log2u(128) == 7);
53 STATIC_ASSERT(log2u(129) == 7);
54 STATIC_ASSERT(log2u(UINT_MAX) == sizeof(UINT_MAX) * CHAR_BIT - 1);
55 
56 } // namespace estd
57 
58 #endif // ESTD_LOG2U_HPP_
Collection of useful templates.
Definition: DmaChannel.hpp:121
constexpr int log2u(const unsigned int value)
log2()-like constexpr function for unsigned int.
Definition: log2u.hpp:38
#define STATIC_ASSERT(...)
Produces a static_assert() where the expression is also used as the message.
Definition: log2u.hpp:23