MatchLib
Loading...
Searching...
No Matches
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#pragma map_to_operator [CCORE]
47#pragma ccore_type combinational
48template <unsigned OneHotLen, unsigned BinLen>
49void one_hot_to_bin(const NVUINTW(OneHotLen) & one_hot_in,
50 NVUINTW(BinLen) & bin_out) {
51
52#pragma hls_unroll yes
53 for (unsigned bin = 0; bin < BinLen; bin++) {
54
55 // constructs mask which matches one-hot to binary outputs
56 NVUINTW(OneHotLen) tmp;
57#pragma hls_unroll yes
58 for (unsigned bit = 0; bit < OneHotLen; bit++) {
59 NVUINTW(OneHotLen) ind = bit;
60 tmp[bit] = ind[bin];
61 }
62
63 tmp = (tmp & one_hot_in);
64
65 // Reduction OR
66 NVUINTW(1) bit_tmp = 0;
67#pragma hls_unroll yes
68 for (unsigned i = 0; i < OneHotLen; i++) {
69 bit_tmp = bit_tmp | tmp[i];
70 }
71
72 // Update output
73 bin_out[bin] = bit_tmp;
74 }
75}
76
77#endif
#define NVUINTW(width)
Definition nvhls_types.h:35
void one_hot_to_bin(const NVUINTW(OneHotLen) &one_hot_in, NVUINTW(BinLen) &bin_out)
One hot to binary conversion.