util: add a resource wrapper to get resource samples
[mesa.git] / 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 #include "util/half_float.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /*
40 * References for float <-> half conversions
41 *
42 * http://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
43 * https://gist.github.com/2156668
44 * https://gist.github.com/2144712
45 */
46
47 static inline uint16_t
48 util_float_to_half(float f)
49 {
50 return _mesa_float_to_half(f);
51 }
52
53 static inline uint16_t
54 util_float_to_half_rtz(float f)
55 {
56 uint32_t sign_mask = 0x80000000;
57 uint32_t round_mask = ~0xfff;
58 uint32_t f32inf = 0xff << 23;
59 uint32_t f16inf = 0x1f << 23;
60 uint32_t sign;
61 union fi magic;
62 union fi f32;
63 uint16_t f16;
64
65 magic.ui = 0xf << 23;
66
67 f32.f = f;
68
69 /* Sign */
70 sign = f32.ui & sign_mask;
71 f32.ui ^= sign;
72
73 if (f32.ui == f32inf) {
74 /* Inf */
75 f16 = 0x7c00;
76 } else if (f32.ui > f32inf) {
77 /* NaN */
78 f16 = 0x7e00;
79 } else {
80 /* Number */
81 f32.ui &= round_mask;
82 f32.f *= magic.f;
83 f32.ui -= round_mask;
84 /*
85 * XXX: The magic mul relies on denorms being available, otherwise
86 * all f16 denorms get flushed to zero - hence when this is used
87 * for tgsi_exec in softpipe we won't get f16 denorms.
88 */
89 /*
90 * Clamp to max finite value if overflowed.
91 * OpenGL has completely undefined rounding behavior for float to
92 * half-float conversions, and this matches what is mandated for float
93 * to fp11/fp10, which recommend round-to-nearest-finite too.
94 * (d3d10 is deeply unhappy about flushing such values to infinity, and
95 * while it also mandates round-to-zero it doesn't care nearly as much
96 * about that.)
97 */
98 if (f32.ui > f16inf)
99 f32.ui = f16inf - 1;
100
101 f16 = f32.ui >> 13;
102 }
103
104 /* Sign */
105 f16 |= sign >> 16;
106
107 return f16;
108 }
109
110 static inline float
111 util_half_to_float(uint16_t f16)
112 {
113 union fi infnan;
114 union fi magic;
115 union fi f32;
116
117 infnan.ui = 0x8f << 23;
118 infnan.f = 65536.0f;
119 magic.ui = 0xef << 23;
120
121 /* Exponent / Mantissa */
122 f32.ui = (f16 & 0x7fff) << 13;
123
124 /* Adjust */
125 f32.f *= magic.f;
126 /* XXX: The magic mul relies on denorms being available */
127
128 /* Inf / NaN */
129 if (f32.f >= infnan.f)
130 f32.ui |= 0xff << 23;
131
132 /* Sign */
133 f32.ui |= (uint32_t)(f16 & 0x8000) << 16;
134
135 return f32.f;
136 }
137
138 #ifdef __cplusplus
139 }
140 #endif
141
142 #endif /* U_HALF_H */
143