summaryrefslogtreecommitdiff
path: root/src/util/half_float.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/half_float.h')
-rw-r--r--src/util/half_float.h46
1 files changed, 42 insertions, 4 deletions
diff --git a/src/util/half_float.h b/src/util/half_float.h
index c9fad9a9400..6f9a405613b 100644
--- a/src/util/half_float.h
+++ b/src/util/half_float.h
@@ -28,6 +28,14 @@
#include <stdbool.h>
#include <stdint.h>
+#include <string.h>
+#include "util/u_cpu_detect.h"
+
+#ifdef USE_F16C
+#include <immintrin.h>
+#define F16C_NEAREST 0
+#define F16C_TRUNCATE 3
+#endif
#ifdef __cplusplus
extern "C" {
@@ -36,18 +44,48 @@ extern "C" {
#define FP16_ONE ((uint16_t) 0x3c00)
#define FP16_ZERO ((uint16_t) 0)
-uint16_t _mesa_float_to_half(float val);
-float _mesa_half_to_float(uint16_t val);
+uint16_t _mesa_float_to_half_slow(float val);
+float _mesa_half_to_float_slow(uint16_t val);
uint8_t _mesa_half_to_unorm8(uint16_t v);
uint16_t _mesa_uint16_div_64k_to_half(uint16_t v);
/*
- * _mesa_float_to_float16_rtz is no more than a wrapper to the counterpart
+ * _mesa_float_to_float16_rtz_slow is no more than a wrapper to the counterpart
* softfloat.h call. Still, softfloat.h conversion API is meant to be kept
* private. In other words, only use the API published here, instead of
* calling directly the softfloat.h one.
*/
-uint16_t _mesa_float_to_float16_rtz(float val);
+uint16_t _mesa_float_to_float16_rtz_slow(float val);
+
+static inline uint16_t
+_mesa_float_to_half(float val)
+{
+#ifdef USE_F16C
+ if (util_cpu_caps.has_f16c)
+ return _cvtss_sh(val, F16C_NEAREST);
+#endif
+ return _mesa_float_to_half_slow(val);
+}
+
+static inline float
+_mesa_half_to_float(uint16_t val)
+{
+#ifdef USE_F16C
+ if (util_cpu_caps.has_f16c)
+ return _cvtsh_ss(val);
+#endif
+ return _mesa_half_to_float_slow(val);
+}
+
+static inline uint16_t
+_mesa_float_to_float16_rtz(float val)
+{
+#ifdef USE_F16C
+ if (util_cpu_caps.has_f16c)
+ return _cvtss_sh(val, F16C_TRUNCATE);
+#endif
+ return _mesa_float_to_float16_rtz_slow(val);
+}
static inline uint16_t
_mesa_float_to_float16_rtne(float val)