MatchLib
crossbar.h
1 /*
2  * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef CROSSBAR_H
17 #define CROSSBAR_H
18 
19 #include "nvhls_int.h"
20 #include "nvhls_types.h"
21 #include <stdio.h>
22 
23 // this section probably deserves it's own h file
24 // in context of crossbar this function is used to zero out bits for any data
25 // type in case the output is invalid. We do not want to deal with Xes.
26 template <class T>
27 T zero_bits() {
28  T t;
29  t = (T){0};
30  return t;
31 }
32 
77 template <typename DataType, unsigned NumInputLanes, unsigned NumOutputLanes>
78 void crossbar(DataType data_in[NumInputLanes], bool valid_in[NumInputLanes],
79  NVUINTW(nvhls::index_width<NumInputLanes >::val) source[NumOutputLanes],
80  bool valid_source[NumOutputLanes],
81  DataType data_out[NumOutputLanes],
82  bool valid_out[NumOutputLanes]) {
83 
85  bool valid_in_tmp;
86  DataType data_in_tmp;
87 
88 #pragma hls_unroll yes
89  for (unsigned dst = 0; dst < NumOutputLanes; dst++) {
90  source_tmp = source[dst];
91  if (!valid_source[dst]) {
92  source_tmp = 0;
93  }
94  valid_in_tmp = valid_in[source_tmp];
95  data_in_tmp = data_in[source_tmp];
96 
97  if (valid_source[dst] && valid_in_tmp) {
98  data_out[dst] = data_in_tmp;
99  valid_out[dst] = true;
100  } else {
101  data_out[dst] = zero_bits<DataType>();
102  valid_out[dst] = false;
103  }
104  }
105 }
106 
111 template <typename DataType, unsigned NumInputLanes, unsigned NumOutputLanes>
112 void crossbar(DataType data_in[NumInputLanes], bool valid_in[NumInputLanes],
113  NVUINTW(nvhls::index_width<NumInputLanes>::val) source[NumOutputLanes],
114  DataType data_out[NumOutputLanes],
115  bool valid_out[NumOutputLanes]) {
116 
117  bool valid_source[NumOutputLanes];
118  for (unsigned i = 0; i < NumOutputLanes; ++i)
119  valid_source[i] = true;
120 
121  crossbar<DataType, NumInputLanes, NumOutputLanes>(
122  data_in, valid_in, source, valid_source, data_out, valid_out);
123 }
124 
129 template <typename DataType, unsigned NumInputLanes, unsigned NumOutputLanes>
130 void crossbar(DataType data_in[NumInputLanes],
131  NVUINTW(nvhls::index_width<NumInputLanes>::val) source[NumOutputLanes],
132  DataType data_out[NumOutputLanes]) {
133 
134  bool valid_out[NumOutputLanes];
135  bool valid_in[NumInputLanes];
136  for (unsigned int i = 0; i < NumInputLanes; ++i)
137  valid_in[i] = true;
138 
139  crossbar<DataType, NumInputLanes, NumOutputLanes>(data_in, valid_in, source,
140  data_out, valid_out);
141 }
142 
143 #endif // CROSSBAR_H
void crossbar(DataType data_in[NumInputLanes], bool valid_in[NumInputLanes], NVUINTW(nvhls::index_width< NumInputLanes >::val) source[NumOutputLanes], bool valid_source[NumOutputLanes], DataType data_out[NumOutputLanes], bool valid_out[NumOutputLanes])
Main entry point for crossbar - most generic implementation.
Definition: crossbar.h:78
NVUINTW(Wrapped< T >::width) TypeToNVUINT(T in)
Convert Type to NVUINT.
Definition: TypeToBits.h:115
Compute index width of a constant.
Definition: nvhls_int.h:285