OS  v1.7.5
Documentation
Loading...
Searching...
No Matches
helper.hpp
1#ifndef QOS_CPP_HELPER
2#define QOS_CPP_HELPER
3
4#include "include/types.hpp"
5
6namespace qOS {
7
15 namespace bits {
20 /*cstat -CERT-INT34-C_a -MISRAC++2008-5-0-10*/
26 template <typename T, typename W>
27 inline void multipleSet( T &dst, W xBits )
28 {
29 dst = dst | static_cast<T>( xBits );
30 }
36 template <typename T, typename W>
37 inline void multipleClear( T &dst, W xBits )
38 {
39 dst = dst & ~static_cast<T>( xBits );
40 }
46 template <typename T>
47 inline void singleSet( T &dst, const size_t xBit )
48 {
49 dst = dst | ( static_cast<T>( 1U ) << static_cast<T>( xBit ) );
50 }
56 template <typename T>
57 inline void singleClear( T &dst, const size_t xBit )
58 {
59 dst = dst & ~( static_cast<T>( 1U ) << static_cast<T>( xBit ) );
60 }
68 template <typename T>
69 inline bool singleRead( T dst, const size_t xBit )
70 {
71 return ( static_cast<T>( 0U ) != ( dst & ( static_cast<T>( 1U ) << static_cast<T>( xBit ) ) ) );
72 }
79 template <typename T>
80 inline void singleToggle( T &dst, const size_t xBit )
81 {
82 dst = dst ^ ( static_cast<T>( 1U ) << static_cast<T>( xBit ) );
83 }
84 /*cstat +CERT-INT34-C_a +MISRAC++2008-5-0-10*/
91 template <typename T>
92 inline void singleWrite( T &dst, const size_t xBit, const bool value )
93 {
94 ( value ) ? bits::singleSet( dst, xBit ) : bits::singleClear( dst, xBit );
95 }
103 template <typename T, typename W>
104 inline bool multipleGet( T reg, W xBits )
105 {
106 return ( 0U != ( reg & xBits) );
107 }
109 }
110
112 template <typename DT, typename ST>
113 inline DT aligned_cast( ST& src )
114 {
115 /*cstat -CERT-EXP33-C_a*/
116 DT dst;
117 (void)memcpy( &dst, &src, sizeof(ST) );
118 return dst;
119 /*cstat +CERT-EXP33-C_a*/
120 }
124 template <typename T>
125 constexpr size_t arraySize( const T& ) noexcept
126 {
127 return 1;
128 }
143 template <typename T, size_t n>
144 constexpr size_t arraySize( const T (&arr)[n] ) noexcept // skipcq: CXX-W2066
145 {
146 return n*arraySize(*arr);
147 }
156 template <typename T>
157 inline T clip( T x, const T Min, const T Max )
158 {
159 return ( x < Min ) ? Min : ( ( x > Max ) ? Max : x );
160 }
167 template <typename T>
168 inline T clipUpper( T x, const T Max )
169 {
170 return ( x > Max ) ? Max : x ;
171 }
178 template <typename T>
179 inline T clipLower( T x, const T Min )
180 {
181 return ( x < Min ) ? Min : x ;
182 }
192 template <typename T>
193 inline T isBetween( T x, const T Low, const T High )
194 {
195 return ( x >= Low ) && ( x <= High );
196 }
202 inline uint8_t byteNibbleHigh( uint8_t x )
203 {
204 return static_cast<uint8_t>( x >> 4U );
205 }
211 inline uint8_t byteNibbleLow( uint8_t x )
212 {
213 return static_cast<uint8_t>( x & 0x0FU );
214 }
221 inline uint8_t byteMergeNibbles( uint8_t h, uint8_t l )
222 {
223 return static_cast<uint8_t>( ( h << 4U ) | ( 0x0FU & l ) );
224 }
230 inline uint16_t wordByteHigh( uint16_t x )
231 {
232 return static_cast<uint16_t>( x >> 8U );
233 }
239 inline uint16_t wordByteLow( uint16_t x )
240 {
241 return static_cast<uint16_t>( x & 0x00FFU );
242 }
249 inline uint16_t wordMergeBytes( uint16_t h, uint16_t l )
250 {
251 return static_cast<uint16_t>( ( h << 8U ) | ( 0x00FFU & l ) );
252 }
258 inline uint32_t dWordWordHigh( uint32_t x )
259 {
260 return static_cast<uint32_t>( x >> 16U );
261 }
267 inline uint32_t dWordWordLow( uint32_t x )
268 {
269 return static_cast<uint32_t>( x & 0xFFFFU );
270 }
277 inline uint32_t dwordMergeWords( uint32_t h, uint32_t l )
278 {
279 return static_cast<uint32_t>( ( h << 16U ) | ( 0xFFFFU & l ) );
280 }
281
283}
284
285#endif /*QOS_CPP_HELPER*/
T clipUpper(T x, const T Max)
Ensures that x is bellow the limits set by Max.
Definition helper.hpp:168
uint8_t byteMergeNibbles(uint8_t h, uint8_t l)
Merges two nibbles to form one byte.
Definition helper.hpp:221
T clipLower(T x, const T Min)
Ensures that x is above the value set by Min.
Definition helper.hpp:179
bool singleRead(T dst, const size_t xBit)
Uses the bitwise AND operator to read the value of a single bit from dst.
Definition helper.hpp:69
void singleClear(T &dst, const size_t xBit)
Uses the bitwise AND operator to clear a single bit into dst.
Definition helper.hpp:57
uint32_t dWordWordLow(uint32_t x)
Read the low-word from x.
Definition helper.hpp:267
uint16_t wordByteLow(uint16_t x)
Read the low-byte from x.
Definition helper.hpp:239
void multipleClear(T &dst, W xBits)
Uses the bitwise AND operator to clear bits into dst.
Definition helper.hpp:37
uint32_t dWordWordHigh(uint32_t x)
Read the high-word from x.
Definition helper.hpp:258
uint8_t byteNibbleHigh(uint8_t x)
Read the high-nibble from x.
Definition helper.hpp:202
void singleToggle(T &dst, const size_t xBit)
Uses the bitwise XOR operator to toggle the value of a single bit from dst.
Definition helper.hpp:80
uint16_t wordMergeBytes(uint16_t h, uint16_t l)
Merges two bytes to form one Word.
Definition helper.hpp:249
constexpr size_t arraySize(const T(&arr)[n]) noexcept
Calculates the size of an array. This function takes a reference to an array and calculates its size....
Definition helper.hpp:144
void multipleSet(T &dst, W xBits)
Uses the bitwise OR operator to set bits into dst.
Definition helper.hpp:27
T isBetween(T x, const T Low, const T High)
Check if the value of x is within the limits defined by Low and High.
Definition helper.hpp:193
bool multipleGet(T reg, W xBits)
Read multiple bits by applying the mask given by xBits to the dst variable.
Definition helper.hpp:104
uint16_t wordByteHigh(uint16_t x)
Read the high-byte from x.
Definition helper.hpp:230
T clip(T x, const T Min, const T Max)
Ensures that x is between the limits set by Min and Max.
Definition helper.hpp:157
uint8_t byteNibbleLow(uint8_t x)
Read the low-nibble from x.
Definition helper.hpp:211
void singleSet(T &dst, const size_t xBit)
Uses the bitwise OR operator to set a single bit into dst.
Definition helper.hpp:47
void singleWrite(T &dst, const size_t xBit, const bool value)
Write the boolean value in a single bit of the dst variable.
Definition helper.hpp:92
uint32_t dwordMergeWords(uint32_t h, uint32_t l)
Merges two words to form one DWord.
Definition helper.hpp:277
OS/Kernel interfaces.
Definition bytebuffer.hpp:7