MatchLib
one_hot_to_bin.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 ONE_HOT_TO_BIN_H
17 #define ONE_HOT_TO_BIN_H
18 
19 #include <nvhls_types.h>
46 template <unsigned OneHotLen, unsigned BinLen>
47 void one_hot_to_bin(const NVUINTW(OneHotLen) & one_hot_in,
48  NVUINTW(BinLen) & bin_out) {
49 
50 #pragma hls_unroll yes
51  for (unsigned bin = 0; bin < BinLen; bin++) {
52 
53  // constructs mask which matches one-hot to binary outputs
54  NVUINTW(OneHotLen) tmp;
55 #pragma hls_unroll yes
56  for (unsigned bit = 0; bit < OneHotLen; bit++) {
57  NVUINTW(OneHotLen) ind = bit;
58  tmp[bit] = ind[bin];
59  }
60 
61  tmp = (tmp & one_hot_in);
62 
63  // Reduction OR
64  NVUINTW(1) bit_tmp = 0;
65 #pragma hls_unroll yes
66  for (unsigned i = 0; i < OneHotLen; i++) {
67  bit_tmp = bit_tmp | tmp[i];
68  }
69 
70  // Update output
71  bin_out[bin] = bit_tmp;
72  }
73 }
74 
75 #endif
NVUINTW(Wrapped< T >::width) TypeToNVUINT(T in)
Convert Type to NVUINT.
Definition: TypeToBits.h:115
void one_hot_to_bin(const NVUINTW(OneHotLen)&one_hot_in, NVUINTW(BinLen)&bin_out)
One hot to binary conversion.