1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2018-2019 Intel Corporation
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #ifndef _HALF_FLOAT_H_
27 #define _HALF_FLOAT_H_
28
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include "util/u_cpu_detect.h"
33
34 #if defined(USE_X86_64_ASM)
35 #include <xmmintrin.h>
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #define FP16_ONE ((uint16_t) 0x3c00)
43 #define FP16_ZERO ((uint16_t) 0)
44
45 uint16_t _mesa_float_to_half_slow(float val);
46 float _mesa_half_to_float_slow(uint16_t val);
47 uint16_t _mesa_uint16_div_64k_to_half(uint16_t v);
48
49 /*
50 * _mesa_float_to_float16_rtz_slow is no more than a wrapper to the counterpart
51 * softfloat.h call. Still, softfloat.h conversion API is meant to be kept
52 * private. In other words, only use the API published here, instead of
53 * calling directly the softfloat.h one.
54 */
55 uint16_t _mesa_float_to_float16_rtz_slow(float val);
56
57 static inline uint16_t
_mesa_float_to_half(float val)58 _mesa_float_to_half(float val)
59 {
60 #if defined(USE_X86_64_ASM)
61 if (util_get_cpu_caps()->has_f16c) {
62 __m128 in = {val};
63 __m128i out;
64
65 /* $0 = round to nearest */
66 __asm volatile("vcvtps2ph $0, %1, %0" : "=v"(out) : "v"(in));
67 return out[0];
68 }
69 #endif
70 return _mesa_float_to_half_slow(val);
71 }
72
73 static inline float
_mesa_half_to_float(uint16_t val)74 _mesa_half_to_float(uint16_t val)
75 {
76 #if defined(USE_X86_64_ASM)
77 if (util_get_cpu_caps()->has_f16c) {
78 __m128i in = {val};
79 __m128 out;
80
81 __asm volatile("vcvtph2ps %1, %0" : "=v"(out) : "v"(in));
82 return out[0];
83 }
84 #endif
85 return _mesa_half_to_float_slow(val);
86 }
87
88 static inline uint16_t
_mesa_float_to_float16_rtz(float val)89 _mesa_float_to_float16_rtz(float val)
90 {
91 #if defined(USE_X86_64_ASM)
92 if (util_get_cpu_caps()->has_f16c) {
93 __m128 in = {val};
94 __m128i out;
95
96 /* $3 = round towards zero (truncate) */
97 __asm volatile("vcvtps2ph $3, %1, %0" : "=v"(out) : "v"(in));
98 return out[0];
99 }
100 #endif
101 return _mesa_float_to_float16_rtz_slow(val);
102 }
103
104 static inline uint16_t
_mesa_float_to_float16_rtne(float val)105 _mesa_float_to_float16_rtne(float val)
106 {
107 return _mesa_float_to_half(val);
108 }
109
110 static inline bool
_mesa_half_is_negative(uint16_t h)111 _mesa_half_is_negative(uint16_t h)
112 {
113 return !!(h & 0x8000);
114 }
115
116
117 #ifdef __cplusplus
118
119 /* Helper class for disambiguating fp16 from uint16_t in C++ overloads */
120
121 struct float16_t {
122 uint16_t bits;
float16_tfloat16_t123 float16_t(float f) : bits(_mesa_float_to_half(f)) {}
float16_tfloat16_t124 float16_t(double d) : bits(_mesa_float_to_half(d)) {}
float16_tfloat16_t125 float16_t(uint16_t raw_bits) : bits(raw_bits) {}
onefloat16_t126 static float16_t one() { return float16_t(FP16_ONE); }
zerofloat16_t127 static float16_t zero() { return float16_t(FP16_ZERO); }
128 };
129
130 #endif
131
132
133 #ifdef __cplusplus
134 } /* extern C */
135 #endif
136
137 #endif /* _HALF_FLOAT_H_ */
138