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 /*
39  * References for float <-> half conversions
40  *
41  *  http://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
42  *  https://gist.github.com/2156668
43  *  https://gist.github.com/2144712
44  */
45 
46 static INLINE uint16_t
util_float_to_half(float f)47 util_float_to_half(float f)
48 {
49    uint32_t sign_mask  = 0x80000000;
50    uint32_t round_mask = ~0xfff;
51    uint32_t f32inf = 0xff << 23;
52    uint32_t f16inf = 0x1f << 23;
53    uint32_t sign;
54    union fi magic;
55    union fi f32;
56    uint16_t f16;
57 
58    magic.ui = 0xf << 23;
59 
60    f32.f = f;
61 
62    /* Sign */
63    sign = f32.ui & sign_mask;
64    f32.ui ^= sign;
65 
66    if (f32.ui == f32inf) {
67       /* Inf */
68       f16 = 0x7c00;
69    } else if (f32.ui > f32inf) {
70       /* NaN */
71       f16 = 0x7e00;
72    } else {
73       /* Number */
74       f32.ui &= round_mask;
75       f32.f  *= magic.f;
76       f32.ui -= round_mask;
77 
78       /* Clamp to infinity if overflowed */
79       if (f32.ui > f16inf)
80          f32.ui = f16inf;
81 
82       f16 = f32.ui >> 13;
83    }
84 
85    /* Sign */
86    f16 |= sign >> 16;
87 
88    return f16;
89 }
90 
91 static INLINE float
util_half_to_float(uint16_t f16)92 util_half_to_float(uint16_t f16)
93 {
94    union fi infnan;
95    union fi magic;
96    union fi f32;
97 
98    infnan.ui = 0x8f << 23;
99    infnan.f = 65536.0f;
100    magic.ui  = 0xef << 23;
101 
102    /* Exponent / Mantissa */
103    f32.ui = (f16 & 0x7fff) << 13;
104 
105    /* Adjust */
106    f32.f *= magic.f;
107 
108    /* Inf / NaN */
109    if (f32.f >= infnan.f)
110       f32.ui |= 0xff << 23;
111 
112    /* Sign */
113    f32.ui |= (f16 & 0x8000) << 16;
114 
115    return f32.f;
116 }
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* U_HALF_H */
123 
124