]> git.sesse.net Git - movit/commitdiff
Use the F16C instruction set when available.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 23 Feb 2015 19:17:49 +0000 (20:17 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 23 Feb 2015 19:17:49 +0000 (20:17 +0100)
For most users, this is mostly theoretical, as it requires compiling
with -march=native or similar. And these are definitely meant for
vectorizing, although it's still 2-3x as fast to use them as our own
software fallback.

These are supported starting from Haswell, and also by some AMD CPUs.

fp16.cpp
fp16.h

index 3738f5c4360bd055cdd5a39ed32d9aecdb8a3158..fc5800e4a964855c0cfb00efa29c0c7d96d94a31 100644 (file)
--- a/fp16.cpp
+++ b/fp16.cpp
@@ -209,6 +209,8 @@ const int FP16_MAX_EXPONENT = (1 << FP16_EXPONENT_BITS) - 1;
 
 }  // namespace
 
 
 }  // namespace
 
+#ifndef __F16C__
+
 double fp16_to_fp64(fp16_int_t x)
 {
        return fp_upconvert<fp16_int_t,
 double fp16_to_fp64(fp16_int_t x)
 {
        return fp_upconvert<fp16_int_t,
@@ -223,6 +225,8 @@ fp16_int_t fp64_to_fp16(double x)
               FP64_BIAS, FP64_MANTISSA_BITS, FP64_EXPONENT_BITS, FP64_MAX_EXPONENT>(x);
 }
 
               FP64_BIAS, FP64_MANTISSA_BITS, FP64_EXPONENT_BITS, FP64_MAX_EXPONENT>(x);
 }
 
+#endif
+
 double fp32_to_fp64(fp32_int_t x)
 {
        return fp_upconvert<fp32_int_t,
 double fp32_to_fp64(fp32_int_t x)
 {
        return fp_upconvert<fp32_int_t,
diff --git a/fp16.h b/fp16.h
index 4e6652f689fb79affad6bb5d0c3cc0765afa5a3c..5417e020b82cdfc039e78a1124b3c98833604e77 100644 (file)
--- a/fp16.h
+++ b/fp16.h
@@ -1,6 +1,10 @@
 #ifndef _MOVIT_FP16_H
 #define _MOVIT_FP16_H 1
 
 #ifndef _MOVIT_FP16_H
 #define _MOVIT_FP16_H 1
 
+#ifdef __F16C__
+#include <immintrin.h>
+#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
 // 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
@@ -13,9 +17,29 @@ namespace movit {
 typedef unsigned int fp32_int_t;
 typedef unsigned short fp16_int_t;
 
 typedef unsigned int fp32_int_t;
 typedef unsigned short fp16_int_t;
 
+#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);
+}
+
+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.
+       return _cvtss_sh(x, 0);
+}
+
+#else
+
 double fp16_to_fp64(fp16_int_t x);
 fp16_int_t fp64_to_fp16(double x);
 
 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.
 // 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.