2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "half_float.h"
30 typedef union { float f
; int32_t i
; uint32_t u
; } fi_type
;
33 * Convert a 4-byte float to a 2-byte half float.
35 * Not all float32 values can be represented exactly as a float16 value. We
36 * round such intermediate float32 values to the nearest float16. When the
37 * float32 lies exactly between to float16 values, we round to the one with
40 * This rounding behavior has several benefits:
41 * - It has no sign bias.
43 * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
46 * - By reproducing the behavior of the GPU (at least on Intel hardware),
47 * compile-time evaluation of constant packHalf2x16 GLSL expressions will
48 * result in the same value as if the expression were executed on the GPU.
51 _mesa_float_to_half(float val
)
53 const fi_type fi
= {val
};
54 const int flt_m
= fi
.i
& 0x7fffff;
55 const int flt_e
= (fi
.i
>> 23) & 0xff;
56 const int flt_s
= (fi
.i
>> 31) & 0x1;
63 /* handle special cases */
64 if ((flt_e
== 0) && (flt_m
== 0)) {
66 /* m = 0; - already set */
69 else if ((flt_e
== 0) && (flt_m
!= 0)) {
70 /* denorm -- denorm float maps to 0 half */
71 /* m = 0; - already set */
74 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
76 /* m = 0; - already set */
79 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
86 const int new_exp
= flt_e
- 127;
88 /* The float32 lies in the range (0.0, min_normal16) and is rounded
89 * to a nearby float16 value. The result will be either zero, subnormal,
93 m
= _mesa_lroundevenf((1 << 24) * fabsf(fi
.f
));
95 else if (new_exp
> 15) {
96 /* map this value to infinity */
97 /* m = 0; - already set */
101 /* The float32 lies in the range
102 * [min_normal16, max_normal16 + max_step16)
103 * and is rounded to a nearby float16 value. The result will be
104 * either normal or infinite.
107 m
= _mesa_lroundevenf(flt_m
/ (float) (1 << 13));
111 assert(0 <= m
&& m
<= 1024);
113 /* The float32 was rounded upwards into the range of the next exponent,
114 * so bump the exponent. This correctly handles the case where f32
115 * should be rounded up to float16 infinity.
121 result
= (s
<< 15) | (e
<< 10) | m
;
127 * Convert a 2-byte half float to a 4-byte float.
128 * Based on code from:
129 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
132 _mesa_half_to_float(uint16_t val
)
134 /* XXX could also use a 64K-entry lookup table */
135 const int m
= val
& 0x3ff;
136 const int e
= (val
>> 10) & 0x1f;
137 const int s
= (val
>> 15) & 0x1;
138 int flt_m
, flt_e
, flt_s
;
145 /* handle special cases */
146 if ((e
== 0) && (m
== 0)) {
151 else if ((e
== 0) && (m
!= 0)) {
152 /* denorm -- denorm half will fit in non-denorm single */
153 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
154 float mantissa
= ((float) (m
)) / 1024.0f
;
155 float sign
= s
? -1.0f
: 1.0f
;
156 return sign
* mantissa
* half_denorm
;
158 else if ((e
== 31) && (m
== 0)) {
163 else if ((e
== 31) && (m
!= 0)) {
174 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;