u_half.h revision 04ea6f5dd84bb91e91225133f3a67e38ed7a3fad
1#ifndef U_HALF_H
2#define U_HALF_H
3
4#include "pipe/p_compiler.h"
5#include "util/u_math.h"
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11extern const uint32_t util_half_to_float_mantissa_table[2048];
12extern const uint32_t util_half_to_float_exponent_table[64];
13extern const uint32_t util_half_to_float_offset_table[64];
14extern const uint16_t util_float_to_half_base_table[512];
15extern const uint8_t util_float_to_half_shift_table[512];
16
17/*
18 * Note that if the half float is a signaling NaN, the x87 FPU will turn
19 * it into a quiet NaN immediately upon loading into a float.
20 *
21 * Additionally, denormals may be flushed to zero.
22 *
23 * To avoid this, use the floatui functions instead of the float ones
24 * when just doing conversion rather than computation on the resulting
25 * floats.
26 */
27
28static INLINE uint32_t
29util_half_to_floatui(half h)
30{
31   unsigned exp = h >> 10;
32   return util_half_to_float_mantissa_table[util_half_to_float_offset_table[exp] + (h & 0x3ff)] + util_half_to_float_exponent_table[exp];
33}
34
35static INLINE float
36util_half_to_float(half h)
37{
38   union fi r;
39   r.ui = util_half_to_floatui(h);
40   return r.f;
41}
42
43static INLINE half
44util_floatui_to_half(uint32_t v)
45{
46   unsigned signexp = v >> 23;
47   return util_float_to_half_base_table[signexp] + ((v & 0x007fffff) >> util_float_to_half_shift_table[signexp]);
48}
49
50static INLINE half
51util_float_to_half(float f)
52{
53   union fi i;
54   i.f = f;
55   return util_floatui_to_half(i.ui);
56}
57
58#ifdef __cplusplus
59}
60#endif
61
62#endif /* U_HALF_H */
63
64