2 #define _MOVIT_FP16_H 1
8 // Code for converting to and from fp16 (from fp64), without any particular
9 // machine support, with proper IEEE round-to-even behavior (and correct
10 // handling of NaNs and infinities). This is needed because some OpenGL
11 // drivers don't properly round off when asked to convert data themselves.
13 // These routines are not particularly fast.
17 // structs instead of ints, so that they are not implicitly convertible.
27 // Use the f16c instructions from Haswell if available (and we know that they
28 // are at compile time).
29 static inline double fp16_to_fp64(fp16_int_t x)
31 return _cvtsh_ss(x.val);
34 static inline fp16_int_t fp64_to_fp16(double x)
36 // NOTE: Strictly speaking, there are some select values where this isn't correct,
37 // since we first round to fp32 and then to fp16.
39 ret.val = _cvtss_sh(x, 0);
45 double fp16_to_fp64(fp16_int_t x);
46 fp16_int_t fp64_to_fp16(double x);
50 // These are not very useful by themselves, but are implemented using the same
51 // code as the fp16 ones (just with different constants), so they are useful
52 // for verifying against the FPU in unit tests.
53 double fp32_to_fp64(fp32_int_t x);
54 fp32_int_t fp64_to_fp32(double x);
56 // Overloads for use in templates.
57 static inline double to_fp64(double x) { return x; }
58 static inline double to_fp64(float x) { return x; }
59 static inline double to_fp64(fp16_int_t x) { return fp16_to_fp64(x); }
61 template<class T> inline T from_fp64(double x);
62 template<> inline double from_fp64<double>(double x) { return x; }
63 template<> inline float from_fp64<float>(double x) { return x; }
64 template<> inline fp16_int_t from_fp64<fp16_int_t>(double x) { return fp64_to_fp16(x); }
66 template<class From, class To>
67 inline To convert_float(From x) { return from_fp64<To>(to_fp64(x)); }
70 inline Same convert_float(Same x) { return x; }
74 #endif // _MOVIT_FP16_H