[OPENGL]
[reactos.git] / reactos / dll / opengl / mesa / src / gallium / auxiliary / util / u_half.h
1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27
28 #ifndef U_HALF_H
29 #define U_HALF_H
30
31 #include "pipe/p_compiler.h"
32 #include "util/u_math.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 extern const uint32_t util_half_to_float_mantissa_table[2048];
39 extern const uint32_t util_half_to_float_exponent_table[64];
40 extern const uint32_t util_half_to_float_offset_table[64];
41 extern const uint16_t util_float_to_half_base_table[512];
42 extern const uint8_t util_float_to_half_shift_table[512];
43
44 /*
45 * Note that if the half float is a signaling NaN, the x87 FPU will turn
46 * it into a quiet NaN immediately upon loading into a float.
47 *
48 * Additionally, denormals may be flushed to zero.
49 *
50 * To avoid this, use the floatui functions instead of the float ones
51 * when just doing conversion rather than computation on the resulting
52 * floats.
53 */
54
55 static INLINE uint32_t
56 util_half_to_floatui(uint16_t h)
57 {
58 unsigned exp = h >> 10;
59 return util_half_to_float_mantissa_table[util_half_to_float_offset_table[exp] + (h & 0x3ff)] + util_half_to_float_exponent_table[exp];
60 }
61
62 static INLINE float
63 util_half_to_float(uint16_t h)
64 {
65 union fi r;
66 r.ui = util_half_to_floatui(h);
67 return r.f;
68 }
69
70 static INLINE uint16_t
71 util_floatui_to_half(uint32_t v)
72 {
73 unsigned signexp = v >> 23;
74 return util_float_to_half_base_table[signexp] + ((v & 0x007fffff) >> util_float_to_half_shift_table[signexp]);
75 }
76
77 static INLINE uint16_t
78 util_float_to_half(float f)
79 {
80 union fi i;
81 i.f = f;
82 return util_floatui_to_half(i.ui);
83 }
84
85 #ifdef __cplusplus
86 }
87 #endif
88
89 #endif /* U_HALF_H */
90