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

Last change on this file since 113 was 113, checked in by rosiere, 15 years ago

1) Add modelsim simulation systemC
2) Modelsim cosimulation systemC / VHDL is not finish !!!! (cf execute_queue and write_unit)
3) Add multi architecture
5) Add template for comparator, multiplier and divider
6) Change Message
Warning) Various test macro have change, many selftest can't compile

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