1 #ifndef _CMPH_BITBOOL_H__
2 #define _CMPH_BITBOOL_H__
3 #include "cmph_types.h"
4
5 static const cmph_uint8 bitmask[] = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 };
6
7 static const cmph_uint32 bitmask32[] = { 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7,
8 1 << 8, 1 << 9, 1 << 10, 1 << 11, 1 << 12, 1 << 13, 1 << 14, 1 << 15,
9 1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 21, 1 << 22, 1 << 23,
10 1 << 24, 1 << 25, 1 << 26, 1 << 27, 1 << 28, 1 << 29, 1 << 30, 1U << 31
11 };
12
13 static const cmph_uint8 valuemask[] = { 0xfc, 0xf3, 0xcf, 0x3f};
14
15
16 /** \def GETBIT(array, i)
17 * \brief get the value of an 1-bit integer stored in an array.
18 * \param array to get 1-bit integer values from
19 * \param i is the index in array to get the 1-bit integer value from
20 *
21 * GETBIT(array, i) is a macro that gets the value of an 1-bit integer stored in array.
22 */
23 #define GETBIT(array, i) ((array[i >> 3] & bitmask[i & 0x00000007]) >> (i & 0x00000007))
24
25 /** \def SETBIT(array, i)
26 * \brief set 1 to an 1-bit integer stored in an array.
27 * \param array to store 1-bit integer values
28 * \param i is the index in array to set the the bit to 1
29 *
30 * SETBIT(array, i) is a macro that sets 1 to an 1-bit integer stored in an array.
31 */
32 #define SETBIT(array, i) (array[i >> 3] |= bitmask[i & 0x00000007])
33
34 //#define GETBIT(array, i) (array[(i) / 8] & bitmask[(i) % 8])
35 //#define SETBIT(array, i) (array[(i) / 8] |= bitmask[(i) % 8])
36 //#define UNSETBIT(array, i) (array[(i) / 8] ^= ((bitmask[(i) % 8])))
37
38
39 /** \def SETVALUE1(array, i, v)
40 * \brief set a value for a 2-bit integer stored in an array initialized with 1s.
41 * \param array to store 2-bit integer values
42 * \param i is the index in array to set the value v
43 * \param v is the value to be set
44 *
45 * SETVALUE1(array, i, v) is a macro that set a value for a 2-bit integer stored in an array.
46 * The array should be initialized with all bits set to 1. For example:
47 * memset(array, 0xff, arraySize);
48 */
49 #define SETVALUE1(array, i, v) (array[i >> 2] &= (cmph_uint8)((v << ((i & 0x00000003) << 1)) | valuemask[i & 0x00000003]))
50
51 /** \def SETVALUE0(array, i, v)
52 * \brief set a value for a 2-bit integer stored in an array initialized with 0s.
53 * \param array to store 2-bit integer values
54 * \param i is the index in array to set the value v
55 * \param v is the value to be set
56 *
57 * SETVALUE0(array, i, v) is a macro that set a value for a 2-bit integer stored in an array.
58 * The array should be initialized with all bits set to 0. For example:
59 * memset(array, 0, arraySize);
60 */
61 #define SETVALUE0(array, i, v) (array[i >> 2] |= (cmph_uint8)(v << ((i & 0x00000003) << 1)))
62
63
64 /** \def GETVALUE(array, i)
65 * \brief get a value for a 2-bit integer stored in an array.
66 * \param array to get 2-bit integer values from
67 * \param i is the index in array to get the value from
68 *
69 * GETVALUE(array, i) is a macro that get a value for a 2-bit integer stored in an array.
70 */
71 #define GETVALUE(array, i) ((cmph_uint8)((array[i >> 2] >> ((i & 0x00000003U) << 1U)) & 0x00000003U))
72
73
74
75 /** \def SETBIT32(array, i)
76 * \brief set 1 to an 1-bit integer stored in an array of 32-bit words.
77 * \param array to store 1-bit integer values. The entries are 32-bit words.
78 * \param i is the index in array to set the the bit to 1
79 *
80 * SETBIT32(array, i) is a macro that sets 1 to an 1-bit integer stored in an array of 32-bit words.
81 */
82 #define SETBIT32(array, i) (array[i >> 5] |= bitmask32[i & 0x0000001f])
83
84 /** \def GETBIT32(array, i)
85 * \brief get the value of an 1-bit integer stored in an array of 32-bit words.
86 * \param array to get 1-bit integer values from. The entries are 32-bit words.
87 * \param i is the index in array to get the 1-bit integer value from
88 *
89 * GETBIT32(array, i) is a macro that gets the value of an 1-bit integer stored in an array of 32-bit words.
90 */
91 #define GETBIT32(array, i) (array[i >> 5] & bitmask32[i & 0x0000001f])
92
93 /** \def UNSETBIT32(array, i)
94 * \brief set 0 to an 1-bit integer stored in an array of 32-bit words.
95 * \param array to store 1-bit integer values. The entries ar 32-bit words
96 * \param i is the index in array to set the the bit to 0
97 *
98 * UNSETBIT32(array, i) is a macro that sets 0 to an 1-bit integer stored in an array of 32-bit words.
99 */
100 #define UNSETBIT32(array, i) (array[i >> 5] ^= ((bitmask32[i & 0x0000001f])))
101
102 #define BITS_TABLE_SIZE(n, bits_length) ((n * bits_length + 31) >> 5)
103
set_bits_value(cmph_uint32 * bits_table,cmph_uint32 index,cmph_uint32 bits_string,cmph_uint32 string_length,cmph_uint32 string_mask)104 static inline void set_bits_value(cmph_uint32 * bits_table, cmph_uint32 index, cmph_uint32 bits_string,
105 cmph_uint32 string_length, cmph_uint32 string_mask)
106 {
107 register cmph_uint32 bit_idx = index * string_length;
108 register cmph_uint32 word_idx = bit_idx >> 5;
109 register cmph_uint32 shift1 = bit_idx & 0x0000001f;
110 register cmph_uint32 shift2 = 32 - shift1;
111
112 bits_table[word_idx] &= ~((string_mask) << shift1);
113 bits_table[word_idx] |= bits_string << shift1;
114
115 if(shift2 < string_length)
116 {
117 bits_table[word_idx+1] &= ~((string_mask) >> shift2);
118 bits_table[word_idx+1] |= bits_string >> shift2;
119 };
120 };
121
get_bits_value(cmph_uint32 * bits_table,cmph_uint32 index,cmph_uint32 string_length,cmph_uint32 string_mask)122 static inline cmph_uint32 get_bits_value(cmph_uint32 * bits_table,cmph_uint32 index, cmph_uint32 string_length, cmph_uint32 string_mask)
123 {
124 register cmph_uint32 bit_idx = index * string_length;
125 register cmph_uint32 word_idx = bit_idx >> 5;
126 register cmph_uint32 shift1 = bit_idx & 0x0000001f;
127 register cmph_uint32 shift2 = 32-shift1;
128 register cmph_uint32 bits_string;
129
130 bits_string = (bits_table[word_idx] >> shift1) & string_mask;
131
132 if(shift2 < string_length)
133 bits_string |= (bits_table[word_idx+1] << shift2) & string_mask;
134
135 return bits_string;
136 };
137
set_bits_at_pos(cmph_uint32 * bits_table,cmph_uint32 pos,cmph_uint32 bits_string,cmph_uint32 string_length)138 static inline void set_bits_at_pos(cmph_uint32 * bits_table, cmph_uint32 pos, cmph_uint32 bits_string, cmph_uint32 string_length)
139 {
140 register cmph_uint32 word_idx = pos >> 5;
141 register cmph_uint32 shift1 = pos & 0x0000001f;
142 register cmph_uint32 shift2 = 32-shift1;
143 register cmph_uint32 string_mask = (1U << string_length) - 1;
144
145 bits_table[word_idx] &= ~((string_mask) << shift1);
146 bits_table[word_idx] |= bits_string << shift1;
147 if(shift2 < string_length)
148 {
149 bits_table[word_idx+1] &= ~((string_mask) >> shift2);
150 bits_table[word_idx+1] |= bits_string >> shift2;
151 }
152 };
153
get_bits_at_pos(cmph_uint32 * bits_table,cmph_uint32 pos,cmph_uint32 string_length)154 static inline cmph_uint32 get_bits_at_pos(cmph_uint32 * bits_table,cmph_uint32 pos,cmph_uint32 string_length)
155 {
156 register cmph_uint32 word_idx = pos >> 5;
157 register cmph_uint32 shift1 = pos & 0x0000001f;
158 register cmph_uint32 shift2 = 32 - shift1;
159 register cmph_uint32 string_mask = (1U << string_length) - 1;
160 register cmph_uint32 bits_string;
161
162 bits_string = (bits_table[word_idx] >> shift1) & string_mask;
163
164 if(shift2 < string_length)
165 bits_string |= (bits_table[word_idx+1] << shift2) & string_mask;
166 return bits_string;
167 }
168
169
170 #endif
171