MatchLib
nvhls_array.h
1 /*
2  * Copyright (c) 2016-2024, 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 NVHLS_ARRAY
17 #define NVHLS_ARRAY
18 
19 
20 #include <cstddef>
21 #include <ac_assert.h>
22 #include <list>
23 #include "systemc.h"
24 
25 namespace nvhls {
26 
27 template <size_t W>
28 struct nv_array_pow2;
29 
30 template <>
31 struct nv_array_pow2<1>
32 {
33  static const size_t P = 1;
34 };
35 
36 template <size_t W>
38 {
39  typedef nv_array_pow2<(W>>1)> SUB;
40  static const size_t P = SUB::P << 1;
41 };
42 
43 
44 static const char* make_permanent(const char* nm) {
45 #ifdef __SYNTHESIS__
46  return nm;
47 #else
48  static std::list<std::string> nm_str;
49  nm_str.push_back(std::string(nm));
50  return nm_str.back().c_str();
51 #endif
52 }
53 
54 // nv_array_bank_array_no_assert_base is the base class for banked arrays,
55 // and typically is not directly used in user models.
56 
57 template <typename B, size_t C>
58 class nv_array_bank_array_no_assert_base;
59 
60 template <typename B>
62 {
63  B a;
64 
65 public:
66 
68 
69  nv_array_bank_array_no_assert_base(const char* prefix)
70  : a(make_permanent(sc_gen_unique_name(prefix)))
71  {}
72 
73  B &operator[](size_t idx) { return a; }
74  const B &operator[](size_t idx) const { return a; }
75 };
76 
77 
78 template <typename B, size_t C>
80 {
81  static const size_t W = nv_array_pow2<C-1>::P;
84 public:
85 
86  nv_array_bank_array_no_assert_base() : a0(), a1() {}
87 
88  nv_array_bank_array_no_assert_base(const char* prefix)
89  : a0(make_permanent(sc_gen_unique_name(prefix)))
90  , a1(make_permanent(sc_gen_unique_name(prefix)))
91  {}
92 
93  B &operator[](size_t idx) {
94 #ifndef __SYNTHESIS__
95  assert(idx < C);
96 #endif
97  size_t aidx = idx & (W-1); return idx&W ? a1[aidx] : a0[aidx];
98  }
99 
100  const B &operator[](size_t idx) const {
101 #ifndef __SYNTHESIS__
102  assert(idx < C);
103 #endif
104  size_t aidx = idx & (W-1); return idx&W ? a1[aidx] : a0[aidx];
105  }
106 };
107 
108 
109 
144 template <typename Type, unsigned int VectorLength>
145 class nv_array : public nv_array_bank_array_no_assert_base<Type, VectorLength>
146 {
147 public:
149  nv_array() {}
150  nv_array(const char* prefix) : base_t(prefix) {}
151  nv_array(sc_module_name prefix) : base_t(prefix) {}
152  nv_array(sc_module_name prefix, const unsigned int& id) : base_t(prefix) {}
153 };
154 
155 };
156 
157 #endif
An implementation of array that declares VectorLength variables for array of size VectorLength.
Definition: nvhls_array.h:146