X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=fp16.h;h=c21153b3466fe71424283063cb7cf9b775a89153;hp=9f7eff926fca594d54198b14b3ec30d834124508;hb=825c90789c229f502520bf0b665596d473f2636d;hpb=82464681eca54bf11be3ca53eed20841ff6eb204 diff --git a/fp16.h b/fp16.h index 9f7eff9..c21153b 100644 --- a/fp16.h +++ b/fp16.h @@ -1,6 +1,10 @@ #ifndef _MOVIT_FP16_H #define _MOVIT_FP16_H 1 +#ifdef __F16C__ +#include +#endif + // Code for converting to and from fp16 (from fp64), without any particular // machine support, with proper IEEE round-to-even behavior (and correct // handling of NaNs and infinities). This is needed because some OpenGL @@ -10,18 +14,61 @@ namespace movit { -typedef unsigned int fp32_int_t; -typedef unsigned short fp16_int_t; +// structs instead of ints, so that they are not implicitly convertible. +struct fp32_int_t { + unsigned int val; +}; +struct fp16_int_t { + unsigned short val; +}; + +#ifdef __F16C__ + +// Use the f16c instructions from Haswell if available (and we know that they +// are at compile time). +static inline double fp16_to_fp64(fp16_int_t x) +{ + return _cvtsh_ss(x.val); +} + +static inline fp16_int_t fp64_to_fp16(double x) +{ + // NOTE: Strictly speaking, there are some select values where this isn't correct, + // since we first round to fp32 and then to fp16. + fp16_int_t ret; + ret.val = _cvtss_sh(x, 0); + return ret; +} + +#else double fp16_to_fp64(fp16_int_t x); fp16_int_t fp64_to_fp16(double x); +#endif + // These are not very useful by themselves, but are implemented using the same // code as the fp16 ones (just with different constants), so they are useful // for verifying against the FPU in unit tests. double fp32_to_fp64(fp32_int_t x); fp32_int_t fp64_to_fp32(double x); +// Overloads for use in templates. +static inline double to_fp64(double x) { return x; } +static inline double to_fp64(float x) { return x; } +static inline double to_fp64(fp16_int_t x) { return fp16_to_fp64(x); } + +template inline T from_fp64(double x); +template<> inline double from_fp64(double x) { return x; } +template<> inline float from_fp64(double x) { return x; } +template<> inline fp16_int_t from_fp64(double x) { return fp64_to_fp16(x); } + +template +inline To convert_float(From x) { return from_fp64(to_fp64(x)); } + +template +inline Same convert_float(Same x) { return x; } + } // namespace movit #endif // _MOVIT_FP16_H