CMS 3D CMS Logo

inversion.h
Go to the documentation of this file.
1 #ifndef CC_INVERSION_H__
2 #define CC_INVERSION_H__
3 
4 namespace l1ct {
5 
6  constexpr int ceillog2(int x) { return (x <= 2) ? 1 : 1 + ceillog2((x + 1) / 2); }
7 
8  template <class data_T, int N>
9  inline float real_val_from_idx(unsigned i) {
10  // Treat the index as the top N bits
11  static constexpr int NB = ceillog2(N); // number of address bits for table
12  data_T x(0);
13  // The MSB of 1 is implicit in the table
14  x[x.width - 1] = 1;
15  // So we can use the next NB bits for real data
16  x(x.width - 2, x.width - NB - 1) = i;
17  return (float)x;
18  }
19 
20  template <class data_T, int N>
21  inline unsigned idx_from_real_val(data_T x) {
22  // Slice the top N bits to get an index into the table
23  static constexpr int NB = ceillog2(N); // number of address bits for table
24  // Slice the top-1 NB bits of the value
25  // the MSB of '1' is implicit, so only slice below that
26  ap_uint<NB> y = x(x.width - 2, x.width - NB - 1);
27  return (unsigned)y(NB - 1, 0);
28  }
29 
30  template <class data_T, class table_T, int N>
31  void init_invert_table(table_T table_out[N]) {
32  // The template data_T is the data type used to address the table
33  for (unsigned i = 0; i < N; i++) {
34  float x = real_val_from_idx<data_T, N>(i);
35  table_T inv_x = 1 / x;
36  table_out[i] = inv_x;
37  }
38  }
39 
40  template <class in_t, class table_t, int N>
41  table_t invert_with_shift(in_t in) {
42  table_t inv_table[N];
43  init_invert_table<in_t, table_t, N>(inv_table);
44 
45  // find the first '1' in the denominator
46  int msb = 0;
47  for (int b = 0; b < in.width; b++) {
48  // #pragma HLS unroll
49  if (in[b])
50  msb = b;
51  }
52  // shift up the denominator such that the left-most bit (msb) is '1'
53  in_t in_shifted = in << (in.width - msb - 1);
54  // lookup the inverse of the shifted input
55  int idx = idx_from_real_val<in_t, N>(in_shifted);
56  table_t inv_in = inv_table[idx];
57  // shift the output back
58  table_t out = inv_in << (in.width - msb - 1);
59  return out;
60  }
61 
62 } // namespace l1ct
63 #endif
constexpr int ceillog2(int x)
Definition: inversion.h:6
float real_val_from_idx(unsigned i)
Definition: inversion.h:9
void init_invert_table(table_T table_out[N])
Definition: inversion.h:31
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:120
float x
unsigned idx_from_real_val(data_T x)
Definition: inversion.h:21
Definition: datatypes.h:8
table_t invert_with_shift(in_t in)
Definition: inversion.h:41