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 #elif defined(USE_AARCH64_ASM)
85 float result;
86 uint16_t in = val;
87
88 __asm volatile(
89 "fcvt %s0, %h1\n"
90 : "=w"(result)
91 : "w"(in)
92 );
93 return result;
94 #endif
95 return _mesa_half_to_float_slow(val);
96 }
97
98 static inline uint16_t
_mesa_float_to_float16_rtz(float val)99 _mesa_float_to_float16_rtz(float val)
100 {
101 #if defined(USE_X86_64_ASM)
102 if (util_get_cpu_caps()->has_f16c) {
103 __m128 in = {val};
104 __m128i out;
105
106 /* $3 = round towards zero (truncate) */
107 __asm volatile("vcvtps2ph $3, %1, %0" : "=v"(out) : "v"(in));
108 return out[0];
109 }
110 #endif
111 return _mesa_float_to_float16_rtz_slow(val);
112 }
113
114 static inline uint16_t
_mesa_float_to_float16_rtne(float val)115 _mesa_float_to_float16_rtne(float val)
116 {
117 return _mesa_float_to_half(val);
118 }
119
120 static inline bool
_mesa_half_is_negative(uint16_t h)121 _mesa_half_is_negative(uint16_t h)
122 {
123 return !!(h & 0x8000);
124 }
125
126
127 #ifdef __cplusplus
128
129 /* Helper class for disambiguating fp16 from uint16_t in C++ overloads */
130
131 struct float16_t {
132 uint16_t bits;
float16_tfloat16_t133 float16_t(float f) : bits(_mesa_float_to_half(f)) {}
float16_tfloat16_t134 float16_t(double d) : bits(_mesa_float_to_half((float)d)) {}
float16_tfloat16_t135 float16_t(uint16_t raw_bits) : bits(raw_bits) {}
onefloat16_t136 static float16_t one() { return float16_t(FP16_ONE); }
zerofloat16_t137 static float16_t zero() { return float16_t(FP16_ZERO); }
138 };
139
140 #endif
141
142
143 #ifdef __cplusplus
144 } /* extern C */
145 #endif
146
147 #endif /* _HALF_FLOAT_H_ */
148