Skip to content

Commit 332e3e3

Browse files
committed
Override pointer comparison in nnet_types.h
1 parent 269b96c commit 332e3e3

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#ifndef NNET_TYPES_H_
2+
#define NNET_TYPES_H_
3+
4+
#include <assert.h>
5+
#include <cstddef>
6+
#include <cstdio>
7+
8+
namespace nnet {
9+
10+
// Fixed-size array
11+
template <typename T, unsigned N> struct array {
12+
typedef T value_type;
13+
static const unsigned size = N;
14+
15+
T data[N];
16+
17+
T &operator[](size_t pos) { return data[pos]; }
18+
19+
const T &operator[](size_t pos) const { return data[pos]; }
20+
21+
array &operator=(const array &other) {
22+
// if (&other == this)
23+
// return *this;
24+
25+
assert(N == other.size && "Array sizes must match.");
26+
27+
for (unsigned i = 0; i < N; i++) {
28+
#pragma HLS UNROLL
29+
data[i] = other[i];
30+
}
31+
return *this;
32+
}
33+
};
34+
35+
// Generic lookup-table implementation, for use in approximations of math functions
36+
template <typename T, unsigned N, T (*func)(T)> class lookup_table {
37+
public:
38+
lookup_table(T from, T to) : range_start(from), range_end(to), base_div(ap_uint<16>(N) / T(to - from)) {
39+
T step = (range_end - range_start) / ap_uint<16>(N);
40+
for (size_t i = 0; i < N; i++) {
41+
T num = range_start + ap_uint<16>(i) * step;
42+
T sample = func(num);
43+
samples[i] = sample;
44+
}
45+
}
46+
47+
T operator()(T n) const {
48+
int index = (n - range_start) * base_div;
49+
if (index < 0)
50+
index = 0;
51+
else if (index > N - 1)
52+
index = N - 1;
53+
return samples[index];
54+
}
55+
56+
private:
57+
T samples[N];
58+
const T range_start, range_end;
59+
ap_fixed<20, 16> base_div;
60+
};
61+
62+
} // namespace nnet
63+
64+
#endif

0 commit comments

Comments
 (0)