-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathap_fp.hpp
43 lines (31 loc) · 1.09 KB
/
ap_fp.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef __AP_FP_HPP__
#define __AP_FP_HPP__
#include "hls/ap_int.hpp"
#define F_M_W 23 // float mantissa width
#define F_E_W 8 // float exponent width
#define D_M_W 52 // double mantissa width
#define D_E_W 11 // double exponent width
#define H_M_W 10 // half mantissa width
#define H_E_W 5 // half exponent width
namespace hls {
// Class for arbitrary precision floating-point numbers.
template <unsigned M_W, unsigned E_W> class ap_fp {
public:
static const unsigned W = M_W + E_W + 1;
ap_fp() : data_() {}
explicit ap_fp(ap_uint<W> val) : data_(val) {}
ap_uint<1> sign() { return data_[W - 1]; }
ap_uint<E_W> exp() { return data_(W - 2, M_W); }
ap_uint<M_W> mantissa() { return data_(M_W - 1, 0); }
ap_uint<W> data() { return data_; }
private:
ap_uint<W> data_;
}; // class ap_fp
ap_fp<D_M_W, D_E_W> double_to_ap_fp(double in) {
return ap_fp<D_M_W, D_E_W>(ap_uint<64>(*(unsigned long long *)&in));
}
ap_fp<F_M_W, F_E_W> float_to_ap_fp(float in) {
return ap_fp<F_M_W, F_E_W>(ap_uint<32>(*(unsigned long *)&in));
}
} // namespace hls
#endif // __AP_FP_HPP__