1 | #ifndef morpheo_BitManipulation |
---|
2 | #define morpheo_BitManipulation |
---|
3 | |
---|
4 | /* |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * [ Description ] |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | #include <stdint.h> |
---|
12 | #include <iostream> |
---|
13 | using namespace std; |
---|
14 | |
---|
15 | namespace morpheo { |
---|
16 | |
---|
17 | // gen_mask .................................................................. |
---|
18 | |
---|
19 | template <typename T> |
---|
20 | T gen_mask (uint32_t size) |
---|
21 | { |
---|
22 | T mask = 0; |
---|
23 | for (uint32_t i=0; i<size; i++) |
---|
24 | { |
---|
25 | mask <<= 1; |
---|
26 | mask |= 1; |
---|
27 | } |
---|
28 | return mask; |
---|
29 | }; |
---|
30 | |
---|
31 | // mask, mask_not ............................................................ |
---|
32 | template <typename T> |
---|
33 | T mask (uint32_t data, uint32_t index_max, uint32_t index_min) |
---|
34 | { |
---|
35 | return (gen_mask<T>(index_max-index_min+1)<<index_min) & data; |
---|
36 | } |
---|
37 | |
---|
38 | template <typename T> |
---|
39 | T mask_not (uint32_t data, uint32_t index_max, uint32_t index_min) |
---|
40 | { |
---|
41 | return (~(gen_mask<T>(index_max-index_min+1)<<index_min)) & data; |
---|
42 | } |
---|
43 | |
---|
44 | // shift_left_logic, shift_right_logic ....................................... |
---|
45 | template <typename T> |
---|
46 | T shift_logic_left (uint32_t size, uint32_t data, uint32_t value) |
---|
47 | { |
---|
48 | T mask = gen_mask<T> (size); |
---|
49 | |
---|
50 | return (mask & ((mask & data) << value)); |
---|
51 | } |
---|
52 | |
---|
53 | template <typename T> |
---|
54 | T shift_logic_right (uint32_t size, uint32_t data, uint32_t value) |
---|
55 | { |
---|
56 | T mask = gen_mask<T> (size); |
---|
57 | |
---|
58 | return (mask & ((mask & data) >> value)); |
---|
59 | } |
---|
60 | |
---|
61 | // shift_logic ............................................................... |
---|
62 | template <typename T> |
---|
63 | T shift_logic (uint32_t size, uint32_t data, uint32_t value, bool is_direction_left) |
---|
64 | { |
---|
65 | if (is_direction_left == true) |
---|
66 | return shift_logic_left <T> (size, data, value); |
---|
67 | else |
---|
68 | return shift_logic_right <T> (size, data, value); |
---|
69 | } |
---|
70 | |
---|
71 | // shift_left_arithmetic, shift_right_arithmetic ............................. |
---|
72 | template <typename T> |
---|
73 | T shift_arithmetic_left (uint32_t size, uint32_t data, uint32_t value) |
---|
74 | { |
---|
75 | bool carry = (data&1) != 0; |
---|
76 | |
---|
77 | if (carry == false) |
---|
78 | return shift_logic_left <T> (size,data,value); |
---|
79 | else |
---|
80 | { |
---|
81 | if (value > size) |
---|
82 | return gen_mask<T> (size); |
---|
83 | |
---|
84 | T mask = gen_mask<T> (value); |
---|
85 | return shift_logic_left <T> (size,data,value) | mask; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | template <typename T> |
---|
91 | T shift_arithmetic_right (uint32_t size, uint32_t data, uint32_t value) |
---|
92 | { |
---|
93 | bool carry = (data&(1<<(size-1))) != 0; |
---|
94 | |
---|
95 | if (carry == false) |
---|
96 | return shift_logic_right <T> (size,data,value); |
---|
97 | else |
---|
98 | { |
---|
99 | if (value > size) |
---|
100 | return gen_mask<T> (size); |
---|
101 | |
---|
102 | T mask = gen_mask<T> (value) << (size-value); |
---|
103 | return ((shift_logic_right <T> (size,data,value)) | mask); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | // shift_arithmetic .......................................................... |
---|
108 | template <typename T> |
---|
109 | T shift_arithmetic (uint32_t size, uint32_t data, uint32_t value, bool is_direction_left) |
---|
110 | { |
---|
111 | if (is_direction_left == true) |
---|
112 | return shift_arithmetic_left <T> (size, data, value); |
---|
113 | else |
---|
114 | return shift_arithmetic_right <T> (size, data, value); |
---|
115 | } |
---|
116 | |
---|
117 | // shift ..................................................................... |
---|
118 | template <typename T> |
---|
119 | T shift (uint32_t size, uint32_t data, uint32_t value, bool is_direction_left, bool is_shift_arithmetic) |
---|
120 | { |
---|
121 | if (is_shift_arithmetic == true) |
---|
122 | return shift_arithmetic <T> (size, data, value, is_direction_left); |
---|
123 | else |
---|
124 | return shift_logic <T> (size, data, value, is_direction_left); |
---|
125 | } |
---|
126 | |
---|
127 | // rotate_left, rotate_right ................................................. |
---|
128 | |
---|
129 | template <typename T> |
---|
130 | T rotate_left (uint32_t size, uint32_t data, uint32_t value) |
---|
131 | { |
---|
132 | T mask = gen_mask<T> (size); |
---|
133 | T rotate_mask = gen_mask<T> (value); |
---|
134 | T rotate = rotate_mask & shift_logic_right<T>(size,data,size-value); |
---|
135 | return (mask & (shift_logic_left <T> (size, data, value) | rotate)); |
---|
136 | } |
---|
137 | |
---|
138 | template <typename T> |
---|
139 | T rotate_right (uint32_t size, uint32_t data, uint32_t value) |
---|
140 | { |
---|
141 | T mask = gen_mask<T> (size); |
---|
142 | T rotate_mask = gen_mask<T> (value); |
---|
143 | T rotate = shift_logic_left <T> (size,rotate_mask & data,size-value); |
---|
144 | return (mask & (shift_logic_right<T> (size, data, value) | rotate)); |
---|
145 | } |
---|
146 | |
---|
147 | // rotate .................................................................... |
---|
148 | template <typename T> |
---|
149 | T rotate (uint32_t size, uint32_t data, uint32_t value, bool is_direction_left) |
---|
150 | { |
---|
151 | if (is_direction_left == true) |
---|
152 | return rotate_left <T> (size, data, value); |
---|
153 | else |
---|
154 | return rotate_right <T> (size, data, value); |
---|
155 | } |
---|
156 | |
---|
157 | // range ..................................................................... |
---|
158 | template <typename T> |
---|
159 | T range (uint32_t data, uint32_t index_max, uint32_t index_min) |
---|
160 | { |
---|
161 | return gen_mask<T>(index_max-index_min+1) & (data << index_min); |
---|
162 | } |
---|
163 | |
---|
164 | template <typename T> |
---|
165 | T range (uint32_t data, uint32_t nb_bits) |
---|
166 | { |
---|
167 | return gen_mask<T>(nb_bits) & data; |
---|
168 | } |
---|
169 | }; // end namespace morpheo |
---|
170 | |
---|
171 | #endif |
---|