RESTinio
Toggle main menu visibility
Loading...
Searching...
No Matches
restinio
utils
impl
bitops.hpp
Go to the documentation of this file.
1
/*
2
* restinio
3
*/
4
5
/*!
6
* Helpers for extraction group of bits from numbers.
7
*/
8
9
#
pragma
once
10
11
#
include
<
cstdint
>
12
13
namespace
restinio
{
14
15
namespace
utils
{
16
17
namespace
impl
{
18
19
namespace
bitops
{
20
21
namespace
details
{
22
23
template
<
typename
T >
24
constexpr
T
mask
(
unsigned
bits_to_extract )
25
{
26
return
bits_to_extract <= 1u ? T{1} :
27
static_cast
<T>((mask<T>(bits_to_extract-1) << 1) | T{1});
28
}
29
30
template
<
typename
T >
31
struct
bits_count
;
32
33
template
<>
34
struct
bits_count
<std::
uint8_t
> {
static
constexpr
unsigned
count
= 8u; };
35
36
template
<>
37
struct
bits_count
<std::
int8_t
> {
static
constexpr
unsigned
count
= 8u; };
38
39
template
<>
40
struct
bits_count
<
char
> {
static
constexpr
unsigned
count
= 8u; };
41
42
template
<>
43
struct
bits_count
<std::
uint16_t
> {
static
constexpr
unsigned
count
= 16u; };
44
45
template
<>
46
struct
bits_count
<std::
int16_t
> {
static
constexpr
unsigned
count
= 16u; };
47
48
template
<>
49
struct
bits_count
<std::
uint32_t
> {
static
constexpr
unsigned
count
= 32u; };
50
51
template
<>
52
struct
bits_count
<std::
int32_t
> {
static
constexpr
unsigned
count
= 32u; };
53
54
template
<>
55
struct
bits_count
<std::
uint64_t
> {
static
constexpr
unsigned
count
= 64u; };
56
57
template
<>
58
struct
bits_count
<std::
int64_t
> {
static
constexpr
unsigned
count
= 64u; };
59
60
}
/* namespace details */
61
62
/*!
63
* \brief Extract N bits from a bigger integer value.
64
*
65
* Usage example:
66
* \code
67
* // Extract 8 bits as unsigned char from bits 24..31 in uint32_t.
68
* const std::uint32_t v1 = some_uint_value();
69
* const auto u8 = n_bits_from<std::uint8_t, 24>(v1);
70
*
71
* // Extract 6 bits as char from bits 12..17 in uint32_t.
72
* const auto ch = n_bits_from<char, 12, 6>(v1);
73
*
74
* // Extract 4 bits as unsigned int from bits 32..35 in uint64_t.
75
* const std::uint64_t v2 = some_uint64_value();
76
* const auto ui = n_bits_from<unsigned int, 32, 4>(v2);
77
* \endcode
78
*
79
*/
80
template
<
81
typename
T,
82
unsigned
Shift,
83
unsigned
Bits_To_Extract =
details
::
bits_count
<T>::count,
84
typename
F =
unsigned
int
>
85
T
86
n_bits_from
( F value )
87
{
88
return
static_cast
<T>(value >> Shift) &
details
::mask<T>(Bits_To_Extract);
89
}
90
91
}
/* namespace bitops */
92
93
}
/* namespace impl */
94
95
}
/* namespace utils */
96
97
}
/* namespace restinio */
restinio::utils::impl::bitops::details
Definition
bitops.hpp:21
restinio::utils::impl::bitops::details::mask
constexpr T mask(unsigned bits_to_extract)
Definition
bitops.hpp:24
restinio::utils::impl::bitops
Definition
bitops.hpp:19
restinio::utils::impl::bitops::n_bits_from
T n_bits_from(F value)
Extract N bits from a bigger integer value.
Definition
bitops.hpp:86
restinio::utils::impl
Definition
bitops.hpp:17
restinio::utils
Definition
from_string_details.ipp:5
restinio
Definition
sendfile_operation_default.ipp:12
restinio::utils::impl::bitops::details::bits_count< char >
Definition
bitops.hpp:40
restinio::utils::impl::bitops::details::bits_count< char >::count
static constexpr unsigned count
Definition
bitops.hpp:40
restinio::utils::impl::bitops::details::bits_count< std::int16_t >
Definition
bitops.hpp:46
restinio::utils::impl::bitops::details::bits_count< std::int16_t >::count
static constexpr unsigned count
Definition
bitops.hpp:46
restinio::utils::impl::bitops::details::bits_count< std::int32_t >
Definition
bitops.hpp:52
restinio::utils::impl::bitops::details::bits_count< std::int32_t >::count
static constexpr unsigned count
Definition
bitops.hpp:52
restinio::utils::impl::bitops::details::bits_count< std::int64_t >
Definition
bitops.hpp:58
restinio::utils::impl::bitops::details::bits_count< std::int64_t >::count
static constexpr unsigned count
Definition
bitops.hpp:58
restinio::utils::impl::bitops::details::bits_count< std::int8_t >
Definition
bitops.hpp:37
restinio::utils::impl::bitops::details::bits_count< std::int8_t >::count
static constexpr unsigned count
Definition
bitops.hpp:37
restinio::utils::impl::bitops::details::bits_count< std::uint16_t >
Definition
bitops.hpp:43
restinio::utils::impl::bitops::details::bits_count< std::uint16_t >::count
static constexpr unsigned count
Definition
bitops.hpp:43
restinio::utils::impl::bitops::details::bits_count< std::uint32_t >
Definition
bitops.hpp:49
restinio::utils::impl::bitops::details::bits_count< std::uint32_t >::count
static constexpr unsigned count
Definition
bitops.hpp:49
restinio::utils::impl::bitops::details::bits_count< std::uint64_t >
Definition
bitops.hpp:55
restinio::utils::impl::bitops::details::bits_count< std::uint64_t >::count
static constexpr unsigned count
Definition
bitops.hpp:55
restinio::utils::impl::bitops::details::bits_count< std::uint8_t >
Definition
bitops.hpp:34
restinio::utils::impl::bitops::details::bits_count< std::uint8_t >::count
static constexpr unsigned count
Definition
bitops.hpp:34
restinio::utils::impl::bitops::details::bits_count
Definition
bitops.hpp:31
Generated by
1.17.0