MatchLib
nvhls_array.h
1 /*
2  * Copyright (c) 2016-2020, 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 #include <boost/preprocessor/arithmetic/add.hpp>
20 #include <boost/preprocessor/punctuation/comma_if.hpp>
21 #include <boost/preprocessor/control/if.hpp>
22 #include <boost/preprocessor/repetition/repeat.hpp>
23 #include <boost/preprocessor/repetition/enum_params.hpp>
24 #include <boost/preprocessor/iteration/local.hpp>
25 
26 #include <nvhls_marshaller.h>
27 #include <nvhls_message.h>
28 #include <nvhls_module.h>
29 
30 namespace nvhls {
60 template <typename Type, unsigned int VectorLength>
61 class nv_array {
62  public:
63  // Alternate array class with no arrays using boost preprocessor to
64  // define template secializations.
65  template <typename A, unsigned int K>
66  class NNode;
67 
68 #define ACCESSOR(Z, N, TEXT) \
69  BOOST_PP_IF(N, else, ) if (idx == N) return data##N;
70 #define NMINIT(Z, N, TEXT) BOOST_PP_COMMA_IF(N) data##N(nvhls_concat(nm, #N))
71 #define NMIDINIT(Z, N, TEXT) \
72  BOOST_PP_COMMA_IF(N) data##N(nvhls_concat(nm, #N), N)
73 #define DECL(Z, N, TEXT) TEXT data##N;
74 
75 #define SPECIALIZATION(Z, N, TEXT) \
76  template <typename A> \
77  class NNode<A, N> { \
78  public: \
79  BOOST_PP_REPEAT(BOOST_PP_ADD(N, 1), DECL, A) \
80  NNode() {} \
81  NNode(sc_module_name nm) \
82  : BOOST_PP_REPEAT(BOOST_PP_ADD(N, 1), NMINIT, BOOST_PP_EMPTY) {} \
83  NNode(sc_module_name nm, const unsigned& id) \
84  : BOOST_PP_REPEAT(BOOST_PP_ADD(N, 1), NMIDINIT, BOOST_PP_EMPTY) {} \
85  A& Get(unsigned int idx) { \
86  BOOST_PP_REPEAT(BOOST_PP_ADD(N, 1), ACCESSOR, BOOST_PP_EMPTY) \
87  else return data0; \
88  } \
89  const A& Get(unsigned int idx) const { \
90  BOOST_PP_REPEAT(BOOST_PP_ADD(N, 1), ACCESSOR, BOOST_PP_EMPTY) \
91  else return data0; \
92  } \
93  };
94 
95 #define MAX_SPECIALIZATIONS 256
96 
97  BOOST_PP_REPEAT(MAX_SPECIALIZATIONS, SPECIALIZATION, BOOST_PP_EMPTY)
98 
99  NNode<Type, VectorLength - 1> array_impl;
100  // END WORKAROUND
101  // Type array_impl[VectorLength];
102 
103  nv_array() {}
104  nv_array(const char* prefix) : array_impl(prefix) {}
105  nv_array(sc_module_name prefix) : array_impl(prefix) {}
106  nv_array(sc_module_name prefix, const unsigned int& id)
107  : array_impl(prefix, id) {}
109 #pragma hls_unroll yes
110  for (unsigned i = 0; i < VectorLength; i++)
111  array_impl.Get(i) = that.array_impl.Get(i);
112  }
113  nv_array(const Type newdata[VectorLength]) {
114 #pragma hls_unroll yes
115  for (unsigned i = 0; i < VectorLength; i++)
116  array_impl.Get(i) = newdata[i];
117  }
118  nv_array<Type, VectorLength>& operator=(
119  const nv_array<Type, VectorLength>& that) {
120 #pragma hls_unroll yes
121  for (unsigned i = 0; i < VectorLength; i++)
122  array_impl.Get(i) = that.array_impl.Get(i);
123  return *this;
124  }
125  inline void copy(nv_array<Type, VectorLength>& out) {
126 #pragma hls_unroll yes
127  for (unsigned i = 0; i < VectorLength; i++)
128  out.array_impl.Get(i) = array_impl.Get(i);
129  }
130  Type& operator[](unsigned int i) {
131  // assert(i<VectorLength);
132  return this->array_impl.Get(i);
133  }
134  const Type& operator[](unsigned int i) const {
135  // assert(i<VectorLength);
136  return this->array_impl.Get(i);
137  }
138  static const unsigned int width = Wrapped<Type>::width * VectorLength;
139  template <unsigned int Size>
140  void Marshall(Marshaller<Size>& m) {
141  for (unsigned int x = 0; x < VectorLength; x++) {
142  m& array_impl.Get(x);
143  }
144  }
145 
146 }; // class nv_array
147 template <typename Type>
148 class nv_array<Type, 0> : public nvhls_message {
149  public:
150  nv_array() {}
151  nv_array(const char* prefix) {}
152  nv_array(sc_module_name prefix) {}
153  nv_array(sc_module_name prefix, const unsigned int& id) {}
154  nv_array(const nv_array<Type, 0>& that) {}
155  nv_array(const Type newdata[0]) {}
156  nv_array<Type, 0>& operator=(const nv_array<Type, 0>& that) { return *this; }
157  inline void copy(nv_array<Type, 0>& out) {}
158  Type& operator[](unsigned int i) {
159  static Type dummy;
160  return dummy;
161  }
162  const Type& operator[](unsigned int i) const { return NULL; }
163  static const unsigned int width = 0;
164  template <unsigned int Size>
165  void Marshall(Marshaller<Size>& m) {}
166 };
167 };
168 
169 #endif
An implementation of array that declares VectorLength variables for array of size VectorLength...
Definition: nvhls_array.h:61