Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_nibble.h
Go to the documentation of this file.
1
14#ifndef VL_NIBBLE_H
15#define VL_NIBBLE_H
16
17#include <vl/vl_numtypes.h>
18
28typedef vl_uint8_t vl_nibble_t;
29
30#define VL_NIBBLE_MASK_HIGH 0xF0
31#define VL_NIBBLE_MASK_LOW 0x0F
32#define VL_NIBBLE_MAX 0xF
33#define VL_NIBBLE_MIN 0x0
34
39static inline vl_nibble_t vlNibbleLow(vl_uint8_t x) { return x & 0x0F; }
40
45static inline vl_nibble_t vlNibbleHigh(vl_uint8_t x) { return (x >> 4) & 0x0F; }
46
51static inline vl_uint8_t vlNibblePack(vl_nibble_t high, vl_nibble_t low)
52{
53 return (vl_uint8_t)((high << 4) | (low & 0x0F));
54}
55
60static inline vl_nibble_t vlNibbleAdd(vl_nibble_t a, vl_nibble_t b) { return (vl_nibble_t)((a + b) & 0x0F); }
61
66static inline vl_nibble_t vlNibbleAddSat(vl_nibble_t a, vl_nibble_t b)
67{
68 vl_uint8_t sum = a + b;
69 return (sum > 0x0F) ? 0x0F : (vl_nibble_t)sum;
70}
71
76static inline vl_nibble_t vlNibbleSub(vl_nibble_t a, vl_nibble_t b) { return (vl_nibble_t)((a - b) & 0x0F); }
77
82static inline vl_nibble_t vlNibbleSubSat(vl_nibble_t a, vl_nibble_t b) { return (b > a) ? 0 : (vl_nibble_t)(a - b); }
83
88static inline void vlNibbleSetHigh(vl_uint8_t* dst, vl_nibble_t v) { *dst = (*dst & 0x0F) | (v << 4); }
89
94static inline void vlNibbleSetLow(vl_uint8_t* dst, vl_nibble_t v) { *dst = (*dst & 0xF0) | (v & 0x0F); }
95
96#endif // VL_NIBBLE_H