2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2018-2019 Intel Corporation
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #ifndef _HALF_FLOAT_H_
27 #define _HALF_FLOAT_H_
36 #define FP16_ONE ((uint16_t) 0x3c00)
37 #define FP16_ZERO ((uint16_t) 0)
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
);
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.
50 uint16_t _mesa_float_to_float16_rtz(float val
);
52 static inline uint16_t
53 _mesa_float_to_float16_rtne(float val
)
55 return _mesa_float_to_half(val
);
59 _mesa_half_is_negative(uint16_t h
)
61 return !!(h
& 0x8000);
67 /* Helper class for disambiguating fp16 from uint16_t in C++ overloads */
71 float16_t(float f
) : bits(_mesa_float_to_half(f
)) {}
72 float16_t(double d
) : bits(_mesa_float_to_half(d
)) {}
73 float16_t(uint16_t bits
) : bits(bits
) {}
74 static float16_t
one() { return float16_t(FP16_ONE
); }
75 static float16_t
zero() { return float16_t(FP16_ZERO
); }
85 #endif /* _HALF_FLOAT_H_ */