source: trunk/IPs/systemC/processor/Morpheo/Common/include/BitManipulation.h @ 71

Last change on this file since 71 was 71, checked in by rosiere, 16 years ago

Modification of Statisctics
Add a new systemC component : Load_Store_Queue (tested with one benchmark and one configuration). Store don't supported the Data Buss Error (Load is supported)

File size: 8.3 KB
Line 
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
14namespace morpheo              {
15
16  //............................................................................
17  // gen_mask ..................................................................
18  //............................................................................
19 
20  template <typename T>
21  T gen_mask       (uint32_t size) 
22  { 
23    T mask = 0; 
24    for (uint32_t i=0; i<size; i++)
25      {
26        mask <<= 1;
27        mask  |= 1;
28      }
29    return mask;
30  };
31
32  template <typename T>
33  T gen_mask       (uint32_t index_max, uint32_t index_min) 
34  { 
35    return  (gen_mask<T>(index_max-index_min+1)<<index_min);
36  };
37
38  template <typename T>
39  T gen_mask_not   (uint32_t index_max, uint32_t index_min) 
40  { 
41    return ~(gen_mask<T>(index_max-index_min+1)<<index_min);
42  };
43
44  //............................................................................
45  // mask, mask_not ............................................................
46  //............................................................................
47  template <typename T>
48  T mask           (T data, uint32_t index_max, uint32_t index_min) 
49  {
50    return gen_mask    <T>(index_max,index_min) & data;
51  }
52
53  template <typename T>
54  T mask_not       (T data, uint32_t index_max, uint32_t index_min) 
55  {
56    return gen_mask_not<T>(index_max,index_min) & data;
57  }
58
59  //............................................................................
60  // shift_left_logic, shift_right_logic .......................................
61  //............................................................................
62  template <typename T>
63  T shift_logic_left (uint32_t size, T data, T value) 
64  {
65    T mask = gen_mask<T> (size);
66
67    return (mask & ((mask & data) << value));
68  }
69
70  template <typename T>
71  T shift_logic_right (uint32_t size, T data, T value) 
72  {
73    T mask = gen_mask<T> (size);
74
75    return (mask & ((mask & data) >> value));
76  }
77
78  //............................................................................
79  // shift_logic ...............................................................
80  //............................................................................
81  template <typename T>
82  T shift_logic      (uint32_t size, T data, T value, bool is_direction_left)
83  {
84    if (is_direction_left == true)
85      return shift_logic_left  <T> (size, data, value);
86    else
87      return shift_logic_right <T> (size, data, value);
88  }
89
90  //............................................................................
91  // shift_left_arithmetic, shift_right_arithmetic .............................
92  //............................................................................
93  template <typename T>
94  T shift_arithmetic_left (uint32_t size, T data, T value) 
95  {
96    bool carry = (data&1) != 0;
97
98    if (carry == false)
99      return shift_logic_left <T> (size,data,value);
100    else
101      {
102        if (value > size)
103          return gen_mask<T> (size);
104
105        T mask = gen_mask<T> (value);
106        return shift_logic_left <T> (size,data,value) | mask;
107      }
108  }
109
110
111  template <typename T>
112  T shift_arithmetic_right (uint32_t size, T data, T value) 
113  {
114    bool carry = (data&(1<<(size-1))) != 0;
115
116    if (carry == false)
117      return shift_logic_right <T> (size,data,value);
118    else
119      {
120        if (value > size)
121          return gen_mask<T> (size);
122       
123        T mask = gen_mask<T> (value) << (size-value);
124        return ((shift_logic_right <T> (size,data,value)) | mask);
125      }
126  }
127
128  //............................................................................
129  // shift_arithmetic ..........................................................
130  //............................................................................
131  template <typename T>
132  T shift_arithmetic      (uint32_t size, T data, T value, bool is_direction_left)
133  {
134    if (is_direction_left == true)
135      return shift_arithmetic_left  <T> (size, data, value);
136    else
137      return shift_arithmetic_right <T> (size, data, value);
138  }
139
140  //............................................................................
141  // shift .....................................................................
142  //............................................................................
143  template <typename T>
144  T shift            (uint32_t size, T data, T value, bool is_direction_left, bool is_shift_arithmetic)
145  {
146    if (is_shift_arithmetic == true)
147      return shift_arithmetic <T> (size, data, value, is_direction_left);
148    else
149      return shift_logic      <T> (size, data, value, is_direction_left);
150  }
151
152  //............................................................................
153  // rotate_left, rotate_right .................................................
154  //............................................................................
155  template <typename T>
156  T rotate_left    (uint32_t size, T data, T value) 
157  {
158    T mask        = gen_mask<T> (size);
159    T rotate_mask = gen_mask<T> (value);
160    T rotate      = rotate_mask & shift_logic_right<T>(size,data,size-value);
161    return (mask & (shift_logic_left <T> (size, data, value) | rotate));
162  }
163
164  template <typename T>
165  T rotate_right    (uint32_t size, T data, T value) 
166  {
167    T mask        = gen_mask<T> (size);
168    T rotate_mask = gen_mask<T> (value);
169    T rotate      = shift_logic_left <T> (size,rotate_mask & data,size-value);
170    return (mask & (shift_logic_right<T> (size, data, value) | rotate));
171  }
172
173  //............................................................................
174  // rotate ....................................................................
175  //............................................................................
176  template <typename T>
177  T rotate         (uint32_t size, T data, T value, bool is_direction_left) 
178  {
179    if (is_direction_left == true)
180      return rotate_left  <T> (size, data, value);
181    else
182      return rotate_right <T> (size, data, value);
183  }
184
185  //............................................................................
186  // range .....................................................................
187  //............................................................................
188  template <typename T>
189  T range          (T data, uint32_t index_max, uint32_t index_min) 
190  {
191    return gen_mask<T>(index_max-index_min+1) & (data << index_min);
192  }
193
194  template <typename T>
195  T range          (T data, uint32_t nb_bits) 
196  {
197    return gen_mask<T>(nb_bits) & data;
198  }
199
200  //............................................................................
201  // insert ....................................................................
202  //............................................................................
203  template <typename T>
204  T insert         (T data_old, T data_new, uint32_t index_max, uint32_t index_min) 
205  {
206    return (mask<T>(data_new,index_max,index_min) | mask_not<T>(data_old,index_max,index_min));
207  }
208
209  //............................................................................
210  // extend ....................................................................
211  //............................................................................
212  template <typename T>
213  T extend         (uint32_t size, T data, bool extend_with_sign, uint32_t nb_bits_keep)
214  {
215    if (size < nb_bits_keep)
216      return data;
217
218    if (extend_with_sign and ((data>>(nb_bits_keep-1))&1))
219      return data | (mask<T>(gen_mask<T>(size),size-1, nb_bits_keep));
220    else
221      return data & (mask<T>(gen_mask<T>(size),nb_bits_keep-1, 0));
222  }
223
224  //............................................................................
225  // duplicate..................................................................
226  //............................................................................
227
228  template <typename T>
229  T duplicate (uint32_t size, T data_src, uint32_t nb_bits, uint32_t index_min)
230  {
231    T data_duplicate = mask<T>((data_src)>>index_min, nb_bits-1, 0);
232    T data_dest      = 0;
233   
234    for (uint32_t i=0; i < size; i+=nb_bits)
235      data_dest |= (data_duplicate<<i);
236   
237    return data_dest;
238  }
239
240  template <typename T>
241  T duplicate (uint32_t size, T data_src, uint32_t nb_bits)
242  {
243    return duplicate<T> (size,data_src,nb_bits,0);
244  }
245
246
247}; // end namespace morpheo             
248
249#endif
Note: See TracBrowser for help on using the repository browser.