• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <immintrin.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 uint8_t _mesa_half_to_unorm8(uint16_t v);
48 uint16_t _mesa_uint16_div_64k_to_half(uint16_t v);
49 
50 /*
51  * _mesa_float_to_float16_rtz_slow is no more than a wrapper to the counterpart
52  * softfloat.h call. Still, softfloat.h conversion API is meant to be kept
53  * private. In other words, only use the API published here, instead of
54  * calling directly the softfloat.h one.
55  */
56 uint16_t _mesa_float_to_float16_rtz_slow(float val);
57 
58 static inline uint16_t
_mesa_float_to_half(float val)59 _mesa_float_to_half(float val)
60 {
61 #if defined(USE_X86_64_ASM)
62    if (util_get_cpu_caps()->has_f16c) {
63       __m128 in = {val};
64       __m128i out;
65 
66       /* $0 = round to nearest */
67       __asm volatile("vcvtps2ph $0, %1, %0" : "=v"(out) : "v"(in));
68       return out[0];
69    }
70 #endif
71    return _mesa_float_to_half_slow(val);
72 }
73 
74 static inline float
_mesa_half_to_float(uint16_t val)75 _mesa_half_to_float(uint16_t val)
76 {
77 #if defined(USE_X86_64_ASM)
78    if (util_get_cpu_caps()->has_f16c) {
79       __m128i in = {val};
80       __m128 out;
81 
82       __asm volatile("vcvtph2ps %1, %0" : "=v"(out) : "v"(in));
83       return out[0];
84    }
85 #endif
86    return _mesa_half_to_float_slow(val);
87 }
88 
89 static inline uint16_t
_mesa_float_to_float16_rtz(float val)90 _mesa_float_to_float16_rtz(float val)
91 {
92 #if defined(USE_X86_64_ASM)
93    if (util_get_cpu_caps()->has_f16c) {
94       __m128 in = {val};
95       __m128i out;
96 
97       /* $3 = round towards zero (truncate) */
98       __asm volatile("vcvtps2ph $3, %1, %0" : "=v"(out) : "v"(in));
99       return out[0];
100    }
101 #endif
102    return _mesa_float_to_float16_rtz_slow(val);
103 }
104 
105 static inline uint16_t
_mesa_float_to_float16_rtne(float val)106 _mesa_float_to_float16_rtne(float val)
107 {
108    return _mesa_float_to_half(val);
109 }
110 
111 static inline bool
_mesa_half_is_negative(uint16_t h)112 _mesa_half_is_negative(uint16_t h)
113 {
114    return !!(h & 0x8000);
115 }
116 
117 
118 #ifdef __cplusplus
119 
120 /* Helper class for disambiguating fp16 from uint16_t in C++ overloads */
121 
122 struct float16_t {
123    uint16_t bits;
float16_tfloat16_t124    float16_t(float f) : bits(_mesa_float_to_half(f)) {}
float16_tfloat16_t125    float16_t(double d) : bits(_mesa_float_to_half(d)) {}
float16_tfloat16_t126    float16_t(uint16_t raw_bits) : bits(raw_bits) {}
onefloat16_t127    static float16_t one() { return float16_t(FP16_ONE); }
zerofloat16_t128    static float16_t zero() { return float16_t(FP16_ZERO); }
129 };
130 
131 #endif
132 
133 
134 #ifdef __cplusplus
135 } /* extern C */
136 #endif
137 
138 #endif /* _HALF_FLOAT_H_ */
139