wsi: add minImageCount override
[mesa.git] / src / util / half_float.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright 2015 Philip Taylor <philip@zaynar.co.uk>
6 * Copyright 2018 Advanced Micro Devices, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include <math.h>
28 #include <assert.h>
29 #include "half_float.h"
30 #include "util/u_half.h"
31 #include "rounding.h"
32 #include "macros.h"
33
34 typedef union { float f; int32_t i; uint32_t u; } fi_type;
35
36 /**
37 * Convert a 4-byte float to a 2-byte half float.
38 *
39 * Not all float32 values can be represented exactly as a float16 value. We
40 * round such intermediate float32 values to the nearest float16. When the
41 * float32 lies exactly between to float16 values, we round to the one with
42 * an even mantissa.
43 *
44 * This rounding behavior has several benefits:
45 * - It has no sign bias.
46 *
47 * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
48 * GPU ISA.
49 *
50 * - By reproducing the behavior of the GPU (at least on Intel hardware),
51 * compile-time evaluation of constant packHalf2x16 GLSL expressions will
52 * result in the same value as if the expression were executed on the GPU.
53 */
54 uint16_t
55 _mesa_float_to_half(float val)
56 {
57 const fi_type fi = {val};
58 const int flt_m = fi.i & 0x7fffff;
59 const int flt_e = (fi.i >> 23) & 0xff;
60 const int flt_s = (fi.i >> 31) & 0x1;
61 int s, e, m = 0;
62 uint16_t result;
63
64 /* sign bit */
65 s = flt_s;
66
67 /* handle special cases */
68 if ((flt_e == 0) && (flt_m == 0)) {
69 /* zero */
70 /* m = 0; - already set */
71 e = 0;
72 }
73 else if ((flt_e == 0) && (flt_m != 0)) {
74 /* denorm -- denorm float maps to 0 half */
75 /* m = 0; - already set */
76 e = 0;
77 }
78 else if ((flt_e == 0xff) && (flt_m == 0)) {
79 /* infinity */
80 /* m = 0; - already set */
81 e = 31;
82 }
83 else if ((flt_e == 0xff) && (flt_m != 0)) {
84 /* NaN */
85 m = 1;
86 e = 31;
87 }
88 else {
89 /* regular number */
90 const int new_exp = flt_e - 127;
91 if (new_exp < -14) {
92 /* The float32 lies in the range (0.0, min_normal16) and is rounded
93 * to a nearby float16 value. The result will be either zero, subnormal,
94 * or normal.
95 */
96 e = 0;
97 m = _mesa_lroundevenf((1 << 24) * fabsf(fi.f));
98 }
99 else if (new_exp > 15) {
100 /* map this value to infinity */
101 /* m = 0; - already set */
102 e = 31;
103 }
104 else {
105 /* The float32 lies in the range
106 * [min_normal16, max_normal16 + max_step16)
107 * and is rounded to a nearby float16 value. The result will be
108 * either normal or infinite.
109 */
110 e = new_exp + 15;
111 m = _mesa_lroundevenf(flt_m / (float) (1 << 13));
112 }
113 }
114
115 assert(0 <= m && m <= 1024);
116 if (m == 1024) {
117 /* The float32 was rounded upwards into the range of the next exponent,
118 * so bump the exponent. This correctly handles the case where f32
119 * should be rounded up to float16 infinity.
120 */
121 ++e;
122 m = 0;
123 }
124
125 result = (s << 15) | (e << 10) | m;
126 return result;
127 }
128
129
130 /**
131 * Convert a 2-byte half float to a 4-byte float.
132 * Based on code from:
133 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
134 */
135 float
136 _mesa_half_to_float(uint16_t val)
137 {
138 return util_half_to_float(val);
139 }
140
141 /**
142 * Convert 0.0 to 0x00, 1.0 to 0xff.
143 * Values outside the range [0.0, 1.0] will give undefined results.
144 */
145 uint8_t _mesa_half_to_unorm8(uint16_t val)
146 {
147 const int m = val & 0x3ff;
148 const int e = (val >> 10) & 0x1f;
149 ASSERTED const int s = (val >> 15) & 0x1;
150
151 /* v = round_to_nearest(1.mmmmmmmmmm * 2^(e-15) * 255)
152 * = round_to_nearest((1.mmmmmmmmmm * 255) * 2^(e-15))
153 * = round_to_nearest((1mmmmmmmmmm * 255) * 2^(e-25))
154 * = round_to_zero((1mmmmmmmmmm * 255) * 2^(e-25) + 0.5)
155 * = round_to_zero(((1mmmmmmmmmm * 255) * 2^(e-24) + 1) / 2)
156 *
157 * This happens to give the correct answer for zero/subnormals too
158 */
159 assert(s == 0 && val <= FP16_ONE); /* check 0 <= this <= 1 */
160 /* (implies e <= 15, which means the bit-shifts below are safe) */
161
162 uint32_t v = ((1 << 10) | m) * 255;
163 v = ((v >> (24 - e)) + 1) >> 1;
164 return v;
165 }
166
167 /**
168 * Takes a uint16_t, divides by 65536, converts the infinite-precision
169 * result to fp16 with round-to-zero. Used by the ASTC decoder.
170 */
171 uint16_t _mesa_uint16_div_64k_to_half(uint16_t v)
172 {
173 /* Zero or subnormal. Set the mantissa to (v << 8) and return. */
174 if (v < 4)
175 return v << 8;
176
177 /* Count the leading 0s in the uint16_t */
178 #ifdef HAVE___BUILTIN_CLZ
179 int n = __builtin_clz(v) - 16;
180 #else
181 int n = 16;
182 for (int i = 15; i >= 0; i--) {
183 if (v & (1 << i)) {
184 n = 15 - i;
185 break;
186 }
187 }
188 #endif
189
190 /* Shift the mantissa up so bit 16 is the hidden 1 bit,
191 * mask it off, then shift back down to 10 bits
192 */
193 int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;
194
195 /* (0{n} 1 X{15-n}) * 2^-16
196 * = 1.X * 2^(15-n-16)
197 * = 1.X * 2^(14-n - 15)
198 * which is the FP16 form with e = 14 - n
199 */
200 int e = 14 - n;
201
202 assert(e >= 1 && e <= 30);
203 assert(m >= 0 && m < 0x400);
204
205 return (e << 10) | m;
206 }