+// 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 float fp16_to_fp32(fp16_int_t x)
+{
+ return _cvtsh_ss(x.val);
+}
+
+static inline fp16_int_t fp32_to_fp16(float x)
+{
+ fp16_int_t ret;
+ ret.val = _cvtss_sh(x, 0);
+ return ret;
+}
+
+#else
+
+float fp16_to_fp32(fp16_int_t x);
+fp16_int_t fp32_to_fp16(float x);
+
+#endif
+
+// Overloads for use in templates.
+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<class T> inline T from_fp32(float x);
+template<> inline double from_fp32<double>(float x) { return x; }
+template<> inline float from_fp32<float>(float x) { return x; }
+template<> inline fp16_int_t from_fp32<fp16_int_t>(float x) { return fp32_to_fp16(x); }