2 * Mesa 3-D graphics library
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 * Copyright (C) 2018-2019 Intel Corporation
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
30 #include "half_float.h"
32 #include "softfloat.h"
36 typedef union { float f; int32_t i; uint32_t u; } fi_type;
39 * Convert a 4-byte float to a 2-byte half float.
41 * Not all float32 values can be represented exactly as a float16 value. We
42 * round such intermediate float32 values to the nearest float16. When the
43 * float32 lies exactly between to float16 values, we round to the one with
46 * This rounding behavior has several benefits:
47 * - It has no sign bias.
49 * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
52 * - By reproducing the behavior of the GPU (at least on Intel hardware),
53 * compile-time evaluation of constant packHalf2x16 GLSL expressions will
54 * result in the same value as if the expression were executed on the GPU.
57 _mesa_float_to_half_slow(float val)
59 const fi_type fi = {val};
60 const int flt_m = fi.i & 0x7fffff;
61 const int flt_e = (fi.i >> 23) & 0xff;
62 const int flt_s = (fi.i >> 31) & 0x1;
69 /* handle special cases */
70 if ((flt_e == 0) && (flt_m == 0)) {
72 /* m = 0; - already set */
75 else if ((flt_e == 0) && (flt_m != 0)) {
76 /* denorm -- denorm float maps to 0 half */
77 /* m = 0; - already set */
80 else if ((flt_e == 0xff) && (flt_m == 0)) {
82 /* m = 0; - already set */
85 else if ((flt_e == 0xff) && (flt_m != 0)) {
86 /* Retain the top bits of a NaN to make sure that the quiet/signaling
87 * status stays the same.
96 const int new_exp = flt_e - 127;
98 /* The float32 lies in the range (0.0, min_normal16) and is rounded
99 * to a nearby float16 value. The result will be either zero, subnormal,
103 m = _mesa_lroundevenf((1 << 24) * fabsf(fi.f));
105 else if (new_exp > 15) {
106 /* map this value to infinity */
107 /* m = 0; - already set */
111 /* The float32 lies in the range
112 * [min_normal16, max_normal16 + max_step16)
113 * and is rounded to a nearby float16 value. The result will be
114 * either normal or infinite.
117 m = _mesa_lroundevenf(flt_m / (float) (1 << 13));
121 assert(0 <= m && m <= 1024);
123 /* The float32 was rounded upwards into the range of the next exponent,
124 * so bump the exponent. This correctly handles the case where f32
125 * should be rounded up to float16 infinity.
131 result = (s << 15) | (e << 10) | m;
136 _mesa_float_to_float16_rtz_slow(float val)
138 return _mesa_float_to_half_rtz_slow(val);
142 * Convert a 2-byte half float to a 4-byte float.
143 * Based on code from:
144 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
147 _mesa_half_to_float_slow(uint16_t val)
153 infnan.ui = 0x8f << 23;
155 magic.ui = 0xef << 23;
157 /* Exponent / Mantissa */
158 f32.ui = (val & 0x7fff) << 13;
162 /* XXX: The magic mul relies on denorms being available */
165 if (f32.f >= infnan.f)
166 f32.ui |= 0xff << 23;
169 f32.ui |= (uint32_t)(val & 0x8000) << 16;
175 * Takes a uint16_t, divides by 65536, converts the infinite-precision
176 * result to fp16 with round-to-zero. Used by the ASTC decoder.
178 uint16_t _mesa_uint16_div_64k_to_half(uint16_t v)
180 /* Zero or subnormal. Set the mantissa to (v << 8) and return. */
184 /* Count the leading 0s in the uint16_t */
185 #ifdef HAVE___BUILTIN_CLZ
186 int n = __builtin_clz(v) - 16;
189 for (int i = 15; i >= 0; i--) {
197 /* Shift the mantissa up so bit 16 is the hidden 1 bit,
198 * mask it off, then shift back down to 10 bits
200 int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;
202 /* (0{n} 1 X{15-n}) * 2^-16
203 * = 1.X * 2^(15-n-16)
204 * = 1.X * 2^(14-n - 15)
205 * which is the FP16 form with e = 14 - n
209 assert(e >= 1 && e <= 30);
210 assert(m >= 0 && m < 0x400);
212 return (e << 10) | m;