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 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #define FP16_ONE     ((uint16_t) 0x3c00)
37 #define FP16_ZERO    ((uint16_t) 0)
38 
39 uint16_t _mesa_float_to_half(float val);
40 float _mesa_half_to_float(uint16_t val);
41 uint8_t _mesa_half_to_unorm8(uint16_t v);
42 uint16_t _mesa_uint16_div_64k_to_half(uint16_t v);
43 
44 /*
45  * _mesa_float_to_float16_rtz is no more than a wrapper to the counterpart
46  * softfloat.h call. Still, softfloat.h conversion API is meant to be kept
47  * private. In other words, only use the API published here, instead of
48  * calling directly the softfloat.h one.
49  */
50 uint16_t _mesa_float_to_float16_rtz(float val);
51 
52 static inline uint16_t
_mesa_float_to_float16_rtne(float val)53 _mesa_float_to_float16_rtne(float val)
54 {
55    return _mesa_float_to_half(val);
56 }
57 
58 static inline bool
_mesa_half_is_negative(uint16_t h)59 _mesa_half_is_negative(uint16_t h)
60 {
61    return !!(h & 0x8000);
62 }
63 
64 
65 #ifdef __cplusplus
66 
67 /* Helper class for disambiguating fp16 from uint16_t in C++ overloads */
68 
69 struct float16_t {
70    uint16_t bits;
float16_tfloat16_t71    float16_t(float f) : bits(_mesa_float_to_half(f)) {}
float16_tfloat16_t72    float16_t(double d) : bits(_mesa_float_to_half(d)) {}
float16_tfloat16_t73    float16_t(uint16_t bits) : bits(bits) {}
onefloat16_t74    static float16_t one() { return float16_t(FP16_ONE); }
zerofloat16_t75    static float16_t zero() { return float16_t(FP16_ZERO); }
76 };
77 
78 #endif
79 
80 
81 #ifdef __cplusplus
82 } /* extern C */
83 #endif
84 
85 #endif /* _HALF_FLOAT_H_ */
86