M5Utility 0.3.0 git rev:41a629c
Loading...
Searching...
No Matches
misc.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_MISC_HPP
11#define M5_UTILITY_MISC_HPP
12
13#include <cstdint>
14#include <type_traits>
15
16namespace m5 {
17namespace utility {
18
20inline bool isValidI2CAddress(const uint16_t addr)
21{
22 if (addr <= 0x7F) { // 7 bit
23 return (addr >= 0x08 && addr <= 0x77);
24 }
25 return addr <= 0x3FF; // 10 bit
26}
27
29namespace detail {
35inline uint8_t reverseBitOrderPortable(const uint8_t u8)
36{
37 uint8_t v{u8};
38 v = ((v & 0xF0) >> 4) | ((v & 0x0F) << 4);
39 v = ((v & 0xCC) >> 2) | ((v & 0x33) << 2);
40 v = ((v & 0xAA) >> 1) | ((v & 0x55) << 1);
41 return v;
42}
43
45inline uint16_t reverseBitOrderPortable(const uint16_t u16)
46{
47 uint16_t v{u16};
48 v = ((v & 0xFF00) >> 8) | ((v & 0x00FF) << 8);
49 v = ((v & 0xF0F0) >> 4) | ((v & 0x0F0F) << 4);
50 v = ((v & 0xCCCC) >> 2) | ((v & 0x3333) << 2);
51 v = ((v & 0xAAAA) >> 1) | ((v & 0x5555) << 1);
52 return v;
53}
59inline bool parityPortable(const uint8_t u8)
60{
61 uint8_t v{u8};
62 v ^= static_cast<uint8_t>(v >> 4);
63 v ^= static_cast<uint8_t>(v >> 2);
64 v ^= static_cast<uint8_t>(v >> 1);
65 return (v & 0x01) != 0;
66}
67} // namespace detail
69
71inline uint8_t reverseBitOrder(const uint8_t u8)
72{
73#if defined(__clang__)
74 return __builtin_bitreverse8(u8);
75#else
76 return detail::reverseBitOrderPortable(u8);
77#endif
78}
79
81inline uint16_t reverseBitOrder(const uint16_t u16)
82{
83#if defined(__clang__)
84 return __builtin_bitreverse16(u16);
85#else
86 return detail::reverseBitOrderPortable(u16);
87#endif
88}
89
95inline bool parity(const uint8_t u8)
96{
97#if defined(__GNUC__) || defined(__clang__)
98 return __builtin_parity(u8) != 0;
99#else
100 return detail::parityPortable(u8);
101#endif
102}
103
110inline uint8_t oddParityBit(const uint8_t u8)
111{
112 return parity(u8) ? 0 : 1;
113}
114
121template <uint32_t N>
123 static_assert(N >= 1, "N must be >= 1");
124#if defined(__SIZEOF_INT128__)
125 static_assert(N <= 128, "N must be <= 128");
126 using type = //
127 typename std::conditional< //
128 (N <= 8), uint8_t, //
129 typename std::conditional< //
130 (N <= 16), uint16_t, //
131 typename std::conditional< //
132 (N <= 32), uint32_t, //
133 typename std::conditional< //
134 (N <= 64), uint64_t, //
135 unsigned __int128 //
136 >::type //
137 >::type //
138 >::type //
139 >::type;
140#else
141 static_assert(N <= 64, "N must be <= 64");
142 using type = //
143 typename std::conditional< //
144 (N <= 8), uint8_t, //
145 typename std::conditional< //
146 (N <= 16), uint16_t, //
147 typename std::conditional< //
148 (N <= 32), uint32_t, //
149 uint64_t //
150 >::type //
151 >::type //
152 >::type;
153#endif
154};
155
156} // namespace utility
157} // namespace m5
158#endif
bool parity(const uint8_t u8)
Is the number of set bits odd?
Definition misc.hpp:95
uint8_t oddParityBit(const uint8_t u8)
Gets the bit that makes the byte odd parity.
Definition misc.hpp:110
bool isValidI2CAddress(const uint16_t addr)
Valid I2C address?
Definition misc.hpp:20
uint8_t reverseBitOrder(const uint8_t u8)
Reversing the bit order.
Definition misc.hpp:71
Top level namespace of M5.
Definition base64.cpp:39
For utilities.
Gets the smallest unsigned integer type that can store N bit.
Definition misc.hpp:122