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

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

Almost complete design
with Test and test platform

  • Property svn:keywords set to Id
File size: 8.2 KB
RevLine 
[88]1#ifndef Morpheo_BitManipulation_h
2#define Morpheo_BitManipulation_h
[2]3
4/*
5 * $Id: BitManipulation.h 88 2008-12-10 18:31:39Z rosiere $
6 *
7 * [ Description ]
8 *
9 */
10
11#include <stdint.h>
12#include <iostream>
13
14namespace morpheo              {
15
[71]16  //............................................................................
[63]17  // gen_mask ..................................................................
[71]18  //............................................................................
[63]19 
[2]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
[71]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  //............................................................................
[63]45  // mask, mask_not ............................................................
[71]46  //............................................................................
[2]47  template <typename T>
[71]48  T mask           (T data, uint32_t index_max, uint32_t index_min) 
[63]49  {
[71]50    return gen_mask    <T>(index_max,index_min) & data;
[63]51  }
52
53  template <typename T>
[71]54  T mask_not       (T data, uint32_t index_max, uint32_t index_min) 
[63]55  {
[71]56    return gen_mask_not<T>(index_max,index_min) & data;
[63]57  }
58
[71]59  //............................................................................
[63]60  // shift_left_logic, shift_right_logic .......................................
[71]61  //............................................................................
[63]62  template <typename T>
[71]63  T shift_logic_left (uint32_t size, T data, T value) 
[2]64  {
65    T mask = gen_mask<T> (size);
66
67    return (mask & ((mask & data) << value));
68  }
69
70  template <typename T>
[71]71  T shift_logic_right (uint32_t size, T data, T value) 
[2]72  {
73    T mask = gen_mask<T> (size);
74
75    return (mask & ((mask & data) >> value));
76  }
77
[71]78  //............................................................................
[63]79  // shift_logic ...............................................................
[71]80  //............................................................................
[2]81  template <typename T>
[71]82  T shift_logic      (uint32_t size, T data, T value, bool is_direction_left)
[2]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
[71]90  //............................................................................
[63]91  // shift_left_arithmetic, shift_right_arithmetic .............................
[71]92  //............................................................................
[2]93  template <typename T>
[71]94  T shift_arithmetic_left (uint32_t size, T data, T value) 
[2]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>
[71]112  T shift_arithmetic_right (uint32_t size, T data, T value) 
[2]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
[71]128  //............................................................................
[63]129  // shift_arithmetic ..........................................................
[71]130  //............................................................................
[2]131  template <typename T>
[71]132  T shift_arithmetic      (uint32_t size, T data, T value, bool is_direction_left)
[2]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
[71]140  //............................................................................
[63]141  // shift .....................................................................
[71]142  //............................................................................
[2]143  template <typename T>
[71]144  T shift            (uint32_t size, T data, T value, bool is_direction_left, bool is_shift_arithmetic)
[2]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
[71]152  //............................................................................
[63]153  // rotate_left, rotate_right .................................................
[71]154  //............................................................................
[2]155  template <typename T>
[71]156  T rotate_left    (uint32_t size, T data, T value) 
[2]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>
[71]165  T rotate_right    (uint32_t size, T data, T value) 
[2]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
[71]173  //............................................................................
[63]174  // rotate ....................................................................
[71]175  //............................................................................
[2]176  template <typename T>
[71]177  T rotate         (uint32_t size, T data, T value, bool is_direction_left) 
[2]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
[71]185  //............................................................................
[63]186  // range .....................................................................
[71]187  //............................................................................
[2]188  template <typename T>
[71]189  T range          (T data, uint32_t index_max, uint32_t index_min) 
[2]190  {
[72]191    return (mask<T>(data,index_max,index_min) >> index_min);
[2]192  }
193
194  template <typename T>
[71]195  T range          (T data, uint32_t nb_bits) 
[2]196  {
197    return gen_mask<T>(nb_bits) & data;
198  }
[71]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
[2]247}; // end namespace morpheo             
248
249#endif
Note: See TracBrowser for help on using the repository browser.