Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
bitfield.hpp
1
10#ifndef QLIBS_BITFIELD
11#define QLIBS_BITFIELD
12
13#include <include/qlibs_types.hpp>
14
18namespace qlibs {
19
30 template <size_t N>
31 using bitArea = uint8_t[ 4U*( ( ( N - 1U )/32U ) + 1U ) ];
32
36 class bitfield : private nonCopyable {
37 private:
38 uint32_t *field{ nullptr };
39 size_t size{ 0U };
40 size_t nSlots{ 0U };
41 static const size_t LBit;
42 inline uint32_t mask( const size_t index ) noexcept
43 {
44 return static_cast<uint32_t>( 1U ) << ( index % LBit );
45 }
46 inline size_t slot( const size_t index ) const noexcept
47 {
48 return index/LBit;
49 }
50 inline uint32_t get( const size_t index ) const noexcept
51 {
52 const size_t s = slot( index );
53 return ( field[ s ] >> ( index % LBit ) ) & 1U;
54 }
55 inline void set( const size_t index ) noexcept
56 {
57 const size_t s = slot( index );
58 field[ s ] |= mask( index );
59 }
60 inline void clear( const size_t index ) noexcept
61 {
62 const size_t s = slot( index );
63
64 field[ s ] &= ~mask( index );
65 }
66 inline void toggle( const size_t index ) noexcept
67 {
68 const size_t s = slot( index );
69
70 field[ s ] ^= mask( index );
71 }
72 inline uint32_t safeMask( const uint32_t val,
73 const size_t x,
74 const size_t nbits ) const noexcept
75 {
76 return val >> ( static_cast<uint32_t>( x - nbits ) );
77 }
78 inline size_t offset( const size_t index ) const noexcept
79 {
80 return index & static_cast<size_t>( 31U );
81 }
82 inline uint32_t maskMerge( const uint32_t w,
83 const uint32_t value,
84 const uint32_t mask ) noexcept
85 {
86 return value ^ ( ( w ^ value ) & mask );
87 }
88
89 uint32_t read_uint32( const size_t index ) const noexcept;
90 void write_uint32( const size_t index,
91 const uint32_t value ) noexcept;
92
93 public:
94 bitfield() = default;
95 virtual ~bitfield() {}
96
105 bool setup( void * const area,
106 const size_t area_size ) noexcept;
107
115 template<size_t area_size>
116 bool setup( uint8_t ( &area )[ area_size ] )
117 {
118 return setup( area, area_size );
119 }
120
125 bool clearAll( void ) noexcept;
126
131 bool setAll( void ) noexcept;
132
138 bool setBit( const size_t index ) noexcept;
139
145 bool clearBit( const size_t index ) noexcept;
146
152 bool toggleBit( const size_t index ) noexcept;
153
159 bool readBit( const size_t index ) const noexcept;
160
167 bool writeBit( const size_t index,
168 const bool value ) noexcept;
169
176 uint32_t readUINTn( const size_t index,
177 const size_t xBits ) const noexcept;
178
186 bool writeUINTn( const size_t index,
187 const size_t xBits,
188 uint32_t value ) noexcept;
189
195 float readFloat( const size_t index ) const noexcept;
196
203 bool writeFloat( const size_t index,
204 const float value ) noexcept;
205
214 void* dump( void * const dst,
215 const size_t n ) noexcept;
216 };
217
219}
220
221
222#endif /*QLIBS_BITFIELD*/
A BitField object.
Definition bitfield.hpp:36
uint32_t readUINTn(const size_t index, const size_t xBits) const noexcept
Reads an unsigned 32-bit value from the BitField.
Definition bitfield.cpp:113
bool clearAll(void) noexcept
Clear all the bits in the BitField.
Definition bitfield.cpp:25
bool clearBit(const size_t index) noexcept
Clears one bit in a BitField.
Definition bitfield.cpp:60
bool setup(uint8_t(&area)[area_size])
Setup a initialize a BitField instance.
Definition bitfield.hpp:116
bool readBit(const size_t index) const noexcept
Retrieve the state of a bit in a bitfield.
Definition bitfield.cpp:84
bool setup(void *const area, const size_t area_size) noexcept
Setup a initialize a BitField instance.
Definition bitfield.cpp:8
bool writeFloat(const size_t index, const float value) noexcept
Writes a 32-bit floating point value to the BitField.
Definition bitfield.cpp:177
void * dump(void *const dst, const size_t n) noexcept
Copies n bytes from the bit-field instance to a designed memory area.
Definition bitfield.cpp:193
bool writeBit(const size_t index, const bool value) noexcept
Writes one bit in a bitfield.
Definition bitfield.cpp:95
float readFloat(const size_t index) const noexcept
Reads a 32-bit floating point value from the BitField.
Definition bitfield.cpp:163
virtual ~bitfield()
Definition bitfield.hpp:95
bool toggleBit(const size_t index) noexcept
Toggles (i.e. reverses the state of) a bit in a BitField.
Definition bitfield.cpp:72
bool setAll(void) noexcept
Set all the bits in the BitField.
Definition bitfield.cpp:36
bool writeUINTn(const size_t index, const size_t xBits, uint32_t value) noexcept
Writes an unsigned n-bit value from the BitField.
Definition bitfield.cpp:134
bitfield()=default
bool setBit(const size_t index) noexcept
Sets one bit in a BitField.
Definition bitfield.cpp:48
uint8_t[4U *(((N - 1U)/32U)+1U)] bitArea
Variable that allocates block of bytes to hold N bits for a BitField .
Definition bitfield.hpp:31
The qLibs++ library namespace.
Definition fp16.cpp:4