-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_ops.h
79 lines (66 loc) · 2.05 KB
/
value_ops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef __VALUE_OPS_H__
#define __VALUE_OPS_H__
#include <stdint.h>
#include <string.h>
#include "gnu_attributes.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define POW_OF_2(exp) (1 << (uint32_t) (exp))
/**
* @brief Generate a number which the lower num bits are all 1
*
* @param num: how many bits need to be set to 1
* @return generated number
*/
#define FILL_BITS(num) (0 == (num) ? 0 : (POW_OF_2(num) - 1))
/**
* @brief Generate a number which the low_bit to high_bit are all 1
*
* @param low_bit: lowest bit index (start from 0)
* @param high_bit: highest bit index (start from 0)
* @return generated number
*/
#define FILL_RANGE(low_bit, high_bit) \
(FILL_BITS(((high_bit) + 1) - (low_bit)) << (low_bit))
#define BYTE_AT(num, index) (((num) >> ((index) * 8)) & 0xFF)
#define BIT_AT(num, index) (((num) & (1 << (index))) != 0)
/**
* @brief Get value inside bit index range [high_bit:low_bit]
*
* @param low_bit: lowest bit index (start from 0)
* @param high_bit: highest bit index (start from 0)
* @return value of expected bit range (closed range)
*/
#define BITS_AT(num, low_bit, high_bit) \
(((num) >> (low_bit)) & FILL_BITS(((high_bit) + 1) - (low_bit)))
/**
* @brief Judge a value is power of 2 or not
*
* @param value: value
* @return true: value is power of 2
* @return false: value is not power of 2
*/
#define IS_POW_OF_2(value) (((value) & ((value) -1)) == 0)
/**
* @brief find first 1
*
* @return u8: first 1 index (in bit)
*/
GNU_PURE uint8_t ff1(size_t num);
/*
@brief 得到位倒序后的输入值
@param num 需要倒序的值
@param bitsOfVar 需要倒序的二进制长度
@return 倒序后的值
*/
GNU_PURE size_t reverseBits(size_t num, uint8_t bitsOfVar);
/**
* @brief
*
* @param pnum: pointer to values
* @param low_bit: low bit index (start from 0)
* @param high_bit: high bit index (start from 0)
* @return uint32_t: value of marked bit range (closed range)
*/
uint32_t valueAt(uint32_t *pnum, uint32_t low_bit, uint32_t high_bit);
#endif // __VALUE_OPS_H__