M5Utility
0.0.8 git rev:f3e2efe
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
16
namespace
m5
{
17
namespace
utility
{
18
20
inline
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
29
inline
uint8_t
reverseBitOrder
(
const
uint8_t u8)
30
{
31
#if defined(__clang__)
32
return
__builtin_bitreverse8(u8);
33
#else
34
uint8_t v{u8};
35
v = ((v & 0xF0) >> 4) | ((v & 0x0F) << 4);
36
v = ((v & 0xCC) >> 2) | ((v & 0x33) << 2);
37
v = ((v & 0xAA) >> 1) | ((v & 0x55) << 1);
38
return
v;
39
#endif
40
}
41
43
inline
uint16_t
reverseBitOrder
(
const
uint16_t u16)
44
{
45
#if defined(__clang__)
46
return
__builtin_bitreverse16(u16);
47
#else
48
uint16_t v{u16};
49
v = ((v & 0xFF00) >> 8) | ((v & 0x00FF) << 8);
50
v = ((v & 0xF0F0) >> 4) | ((v & 0x0F0F) << 4);
51
v = ((v & 0xCCCC) >> 2) | ((v & 0x3333) << 2);
52
v = ((v & 0xAAAA) >> 1) | ((v & 0x5555) << 1);
53
return
v;
54
#endif
55
}
56
63
template
<u
int
32_t N>
64
struct
uint_least_for_bits
{
65
static_assert
(N >= 1,
"N must be >= 1"
);
66
#ifdef __SIZEOF_INT128__
67
static_assert
(N <= 128,
"N must be <= 128"
);
68
using
type =
//
69
typename
std::conditional<
//
70
(N <= 8), uint8_t,
//
71
typename
std::conditional<
//
72
(N <= 16), uint16_t,
//
73
typename
std::conditional<
//
74
(N <= 32), uint32_t,
//
75
typename
std::conditional<
//
76
(N <= 64), uint64_t,
//
77
__uint128_t
//
78
>::type
//
79
>::type
//
80
>::type
//
81
>::type;
82
#else
83
static_assert
(N <= 64,
"N must be <= 64m"
);
84
using
type =
//
85
typename
std::conditional<
//
86
(N <= 8), uint8_t,
//
87
typename
std::conditional<
//
88
(N <= 16), uint16_t,
//
89
typename
std::conditional<
//
90
(N <= 32), uint32_t,
//
91
uint64_t
//
92
>::type
//
93
>::type
//
94
>::type;
95
#endif
96
};
97
98
}
// namespace utility
99
}
// namespace m5
100
#endif
m5::utility::isValidI2CAddress
bool isValidI2CAddress(const uint16_t addr)
Valid I2C address?
Definition
misc.hpp:20
m5::utility::reverseBitOrder
uint8_t reverseBitOrder(const uint8_t u8)
Reversing the bit order.
Definition
misc.hpp:29
m5
Top level namespace of M5.
Definition
bit_segment.hpp:17
utility
For utilities.
m5::utility::uint_least_for_bits
Gets the smallest unsigned integer type that can store N bit.
Definition
misc.hpp:64
src
m5_utility
misc.hpp
Generated by
1.11.0