5fe2e510d223cc22bc61600c8e1fc132d21a3d62
2 * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /* Based on code from The OpenGL Programming Guide / 7th Edition, Appendix J.
25 * Available here: http://www.opengl-redbook.com/appendices/
26 * The algorithm in the book contains a bug though, which is fixed in the code
32 #define UF11(e, m) ((e << 6) | (m))
33 #define UF11_EXPONENT_BIAS 15
34 #define UF11_EXPONENT_BITS 0x1F
35 #define UF11_EXPONENT_SHIFT 6
36 #define UF11_MANTISSA_BITS 0x3F
37 #define UF11_MANTISSA_SHIFT (23 - UF11_EXPONENT_SHIFT)
38 #define UF11_MAX_EXPONENT (UF11_EXPONENT_BITS << UF11_EXPONENT_SHIFT)
40 #define UF10(e, m) ((e << 5) | (m))
41 #define UF10_EXPONENT_BIAS 15
42 #define UF10_EXPONENT_BITS 0x1F
43 #define UF10_EXPONENT_SHIFT 5
44 #define UF10_MANTISSA_BITS 0x1F
45 #define UF10_MANTISSA_SHIFT (23 - UF10_EXPONENT_SHIFT)
46 #define UF10_MAX_EXPONENT (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT)
48 #define F32_INFINITY 0x7f800000
50 static inline uint32_t f32_to_uf11(float val
)
59 /* Decode little-endian 32-bit floating-point value */
60 int sign
= (f32
.ui
>> 16) & 0x8000;
61 /* Map exponent to the range [-127,128] */
62 int exponent
= ((f32
.ui
>> 23) & 0xff) - 127;
63 int mantissa
= f32
.ui
& 0x007fffff;
65 if (exponent
== 128) { /* Infinity or NaN */
66 /* From the GL_EXT_packed_float spec:
68 * "Additionally: negative infinity is converted to zero; positive
69 * infinity is converted to positive infinity; and both positive and
70 * negative NaN are converted to positive NaN."
72 uf11
= UF11_MAX_EXPONENT
;
81 } else if (val
> 65024.0f
) {
82 /* From the GL_EXT_packed_float spec:
84 * "Likewise, finite positive values greater than 65024 (the maximum
85 * finite representable unsigned 11-bit floating-point value) are
86 * converted to 65024."
90 else if (exponent
> -15) { /* Representable value */
91 exponent
+= UF11_EXPONENT_BIAS
;
92 mantissa
>>= UF11_MANTISSA_SHIFT
;
93 uf11
= exponent
<< UF11_EXPONENT_SHIFT
| mantissa
;
99 static inline float uf11_to_f32(uint16_t val
)
106 int exponent
= (val
& 0x07c0) >> UF11_EXPONENT_SHIFT
;
107 int mantissa
= (val
& 0x003f);
113 const float scale
= 1.0 / (1 << 20);
114 f32
.f
= scale
* mantissa
;
117 else if (exponent
== 31) {
118 f32
.ui
= F32_INFINITY
| mantissa
;
121 float scale
, decimal
;
124 scale
= 1.0f
/ (1 << -exponent
);
127 scale
= (float) (1 << exponent
);
129 decimal
= 1.0f
+ (float) mantissa
/ 64;
130 f32
.f
= scale
* decimal
;
136 static inline uint32_t f32_to_uf10(float val
)
145 /* Decode little-endian 32-bit floating-point value */
146 int sign
= (f32
.ui
>> 16) & 0x8000;
147 /* Map exponent to the range [-127,128] */
148 int exponent
= ((f32
.ui
>> 23) & 0xff) - 127;
149 int mantissa
= f32
.ui
& 0x007fffff;
151 if (exponent
== 128) {
152 /* From the GL_EXT_packed_float spec:
154 * "Additionally: negative infinity is converted to zero; positive
155 * infinity is converted to positive infinity; and both positive and
156 * negative NaN are converted to positive NaN."
158 uf10
= UF10_MAX_EXPONENT
;
167 } else if (val
> 64512.0f
) {
168 /* From the GL_EXT_packed_float spec:
170 * "Likewise, finite positive values greater than 64512 (the maximum
171 * finite representable unsigned 10-bit floating-point value) are
172 * converted to 64512."
176 else if (exponent
> -15) { /* Representable value */
177 exponent
+= UF10_EXPONENT_BIAS
;
178 mantissa
>>= UF10_MANTISSA_SHIFT
;
179 uf10
= exponent
<< UF10_EXPONENT_SHIFT
| mantissa
;
185 static inline float uf10_to_f32(uint16_t val
)
192 int exponent
= (val
& 0x03e0) >> UF10_EXPONENT_SHIFT
;
193 int mantissa
= (val
& 0x001f);
199 const float scale
= 1.0 / (1 << 20);
200 f32
.f
= scale
* mantissa
;
203 else if (exponent
== 31) {
204 f32
.ui
= F32_INFINITY
| mantissa
;
207 float scale
, decimal
;
210 scale
= 1.0f
/ (1 << -exponent
);
213 scale
= (float) (1 << exponent
);
215 decimal
= 1.0f
+ (float) mantissa
/ 32;
216 f32
.f
= scale
* decimal
;
222 static inline uint32_t float3_to_r11g11b10f(const float rgb
[3])
224 return ( f32_to_uf11(rgb
[0]) & 0x7ff) |
225 ((f32_to_uf11(rgb
[1]) & 0x7ff) << 11) |
226 ((f32_to_uf10(rgb
[2]) & 0x3ff) << 22);
229 static inline void r11g11b10f_to_float3(uint32_t rgb
, float retval
[3])
231 retval
[0] = uf11_to_f32( rgb
& 0x7ff);
232 retval
[1] = uf11_to_f32((rgb
>> 11) & 0x7ff);
233 retval
[2] = uf10_to_f32((rgb
>> 22) & 0x3ff);