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
Line 
1#ifndef Morpheo_BitManipulation_h
2#define Morpheo_BitManipulation_h
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
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  template <typename T>
45  T gen_mask_not   (uint32_t size)
46  { 
47    return gen_mask_not<T>(size-1,0);
48  };
49
50  //............................................................................
51  // mask, mask_not ............................................................
52  //............................................................................
53  template <typename T>
54  T mask           (T data, uint32_t index_max, uint32_t index_min) 
55  {
56    return gen_mask    <T>(index_max,index_min) & data;
57  }
58
59  template <typename T>
60  T mask_not       (T data, uint32_t index_max, uint32_t index_min) 
61  {
62    return gen_mask_not<T>(index_max,index_min) & data;
63  }
64
65  //............................................................................
66  // shift_left_logic, shift_right_logic .......................................
67  //............................................................................
68  template <typename T>
69  T shift_logic_left (uint32_t size, T data, T value) 
70  {
71    T mask = gen_mask<T> (size);
72
73    return (mask & ((mask & data) << value));
74  }
75
76  template <typename T>
77  T shift_logic_right (uint32_t size, T data, T value) 
78  {
79    T mask = gen_mask<T> (size);
80
81    return (mask & ((mask & data) >> value));
82  }
83
84  //............................................................................
85  // shift_logic ...............................................................
86  //............................................................................
87  template <typename T>
88  T shift_logic      (uint32_t size, T data, T value, bool is_direction_left)
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
96  //............................................................................
97  // shift_left_arithmetic, shift_right_arithmetic .............................
98  //............................................................................
99  template <typename T>
100  T shift_arithmetic_left (uint32_t size, T data, T value) 
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>
118  T shift_arithmetic_right (uint32_t size, T data, T value) 
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
134  //............................................................................
135  // shift_arithmetic ..........................................................
136  //............................................................................
137  template <typename T>
138  T shift_arithmetic      (uint32_t size, T data, T value, bool is_direction_left)
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
146  //............................................................................
147  // shift .....................................................................
148  //............................................................................
149  template <typename T>
150  T shift            (uint32_t size, T data, T value, bool is_direction_left, bool is_shift_arithmetic)
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
158  //............................................................................
159  // rotate_left, rotate_right .................................................
160  //............................................................................
161  template <typename T>
162  T rotate_left    (uint32_t size, T data, T value) 
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>
171  T rotate_right    (uint32_t size, T data, T value) 
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
179  //............................................................................
180  // rotate ....................................................................
181  //............................................................................
182  template <typename T>
183  T rotate         (uint32_t size, T data, T value, bool is_direction_left) 
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
191  //............................................................................
192  // range .....................................................................
193  //............................................................................
194  template <typename T>
195  T range          (T data, uint32_t index_max, uint32_t index_min) 
196  {
197    return (mask<T>(data,index_max,index_min) >> index_min);
198  }
199
200  template <typename T>
201  T range          (T data, uint32_t nb_bits) 
202  {
203    return gen_mask<T>(nb_bits) & data;
204  }
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
253}; // end namespace morpheo             
254
255#endif
Note: See TracBrowser for help on using the repository browser.