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