#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
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);
+#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.