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