X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=fp16.h;h=5f71fec593d8924deaee981a6aa3100d035171c1;hp=c21153b3466fe71424283063cb7cf9b775a89153;hb=7cfa62b39fb56fb1e5f094fa11bfe9d84445c80f;hpb=6c954b4f0bff0743e13ce6ddcee8bda15b3af234 diff --git a/fp16.h b/fp16.h index c21153b..5f71fec 100644 --- a/fp16.h +++ b/fp16.h @@ -10,7 +10,11 @@ // handling of NaNs and infinities). This is needed because some OpenGL // drivers don't properly round off when asked to convert data themselves. // -// These routines are not particularly fast. +// These routines are originally written by Fabian Giesen, and released by +// him into the public domain; +// see https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/. +// They are quite fast, and can be vectorized if need be; of course, using +// the f16c instructions (see below) will be faster still. namespace movit { @@ -26,15 +30,13 @@ struct fp16_int_t { // 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) +static inline float fp16_to_fp32(fp16_int_t x) { return _cvtsh_ss(x.val); } -static inline fp16_int_t fp64_to_fp16(double x) +static inline fp16_int_t fp32_to_fp16(float 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; @@ -42,29 +44,23 @@ static inline fp16_int_t fp64_to_fp16(double x) #else -double fp16_to_fp64(fp16_int_t x); -fp16_int_t fp64_to_fp16(double x); +float fp16_to_fp32(fp16_int_t x); +fp16_int_t fp32_to_fp16(float 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); } +static inline float to_fp32(double x) { return x; } +static inline float to_fp32(float x) { return x; } +static inline float to_fp32(fp16_int_t x) { return fp16_to_fp32(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 T from_fp32(float x); +template<> inline double from_fp32(float x) { return x; } +template<> inline float from_fp32(float x) { return x; } +template<> inline fp16_int_t from_fp32(float x) { return fp32_to_fp16(x); } template -inline To convert_float(From x) { return from_fp64(to_fp64(x)); } +inline To convert_float(From x) { return from_fp32(to_fp32(x)); } template inline Same convert_float(Same x) { return x; }