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
30 #ifndef FORMAT_R11G11B10F_H
31 #define FORMAT_R11G11B10F_H
35 #define UF11(e, m) ((e << 6) | (m))
36 #define UF11_EXPONENT_BIAS 15
37 #define UF11_EXPONENT_BITS 0x1F
38 #define UF11_EXPONENT_SHIFT 6
39 #define UF11_MANTISSA_BITS 0x3F
40 #define UF11_MANTISSA_SHIFT (23 - UF11_EXPONENT_SHIFT)
41 #define UF11_MAX_EXPONENT (UF11_EXPONENT_BITS << UF11_EXPONENT_SHIFT)
43 #define UF10(e, m) ((e << 5) | (m))
44 #define UF10_EXPONENT_BIAS 15
45 #define UF10_EXPONENT_BITS 0x1F
46 #define UF10_EXPONENT_SHIFT 5
47 #define UF10_MANTISSA_BITS 0x1F
48 #define UF10_MANTISSA_SHIFT (23 - UF10_EXPONENT_SHIFT)
49 #define UF10_MAX_EXPONENT (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT)
51 #define F32_INFINITY 0x7f800000
53 static inline uint32_t f32_to_uf11(float val
)
62 /* Decode little-endian 32-bit floating-point value */
63 int sign
= (f32
.ui
>> 16) & 0x8000;
64 /* Map exponent to the range [-127,128] */
65 int exponent
= ((f32
.ui
>> 23) & 0xff) - 127;
66 int mantissa
= f32
.ui
& 0x007fffff;
68 if (exponent
== 128) { /* Infinity or NaN */
69 /* From the GL_EXT_packed_float spec:
71 * "Additionally: negative infinity is converted to zero; positive
72 * infinity is converted to positive infinity; and both positive and
73 * negative NaN are converted to positive NaN."
75 uf11
= UF11_MAX_EXPONENT
;
84 } else if (val
> 65024.0f
) {
85 /* From the GL_EXT_packed_float spec:
87 * "Likewise, finite positive values greater than 65024 (the maximum
88 * finite representable unsigned 11-bit floating-point value) are
89 * converted to 65024."
92 } else if (exponent
> -15) { /* Representable value */
93 exponent
+= UF11_EXPONENT_BIAS
;
94 mantissa
>>= UF11_MANTISSA_SHIFT
;
95 uf11
= exponent
<< UF11_EXPONENT_SHIFT
| mantissa
;
101 static inline float uf11_to_f32(uint16_t val
)
108 int exponent
= (val
& 0x07c0) >> UF11_EXPONENT_SHIFT
;
109 int mantissa
= (val
& 0x003f);
115 const float scale
= 1.0 / (1 << 20);
116 f32
.f
= scale
* mantissa
;
118 } else if (exponent
== 31) {
119 f32
.ui
= F32_INFINITY
| mantissa
;
121 float scale
, decimal
;
124 scale
= 1.0f
/ (1 << -exponent
);
126 scale
= (float) (1 << exponent
);
128 decimal
= 1.0f
+ (float) mantissa
/ 64;
129 f32
.f
= scale
* decimal
;
135 static inline uint32_t f32_to_uf10(float val
)
144 /* Decode little-endian 32-bit floating-point value */
145 int sign
= (f32
.ui
>> 16) & 0x8000;
146 /* Map exponent to the range [-127,128] */
147 int exponent
= ((f32
.ui
>> 23) & 0xff) - 127;
148 int mantissa
= f32
.ui
& 0x007fffff;
150 if (exponent
== 128) {
151 /* From the GL_EXT_packed_float spec:
153 * "Additionally: negative infinity is converted to zero; positive
154 * infinity is converted to positive infinity; and both positive and
155 * negative NaN are converted to positive NaN."
157 uf10
= UF10_MAX_EXPONENT
;
166 } else if (val
> 64512.0f
) {
167 /* From the GL_EXT_packed_float spec:
169 * "Likewise, finite positive values greater than 64512 (the maximum
170 * finite representable unsigned 10-bit floating-point value) are
171 * converted to 64512."
174 } else if (exponent
> -15) { /* Representable value */
175 exponent
+= UF10_EXPONENT_BIAS
;
176 mantissa
>>= UF10_MANTISSA_SHIFT
;
177 uf10
= exponent
<< UF10_EXPONENT_SHIFT
| mantissa
;
183 static inline float uf10_to_f32(uint16_t val
)
190 int exponent
= (val
& 0x03e0) >> UF10_EXPONENT_SHIFT
;
191 int mantissa
= (val
& 0x001f);
197 const float scale
= 1.0 / (1 << 19);
198 f32
.f
= scale
* mantissa
;
200 } else if (exponent
== 31) {
201 f32
.ui
= F32_INFINITY
| mantissa
;
203 float scale
, decimal
;
206 scale
= 1.0f
/ (1 << -exponent
);
209 scale
= (float) (1 << exponent
);
211 decimal
= 1.0f
+ (float) mantissa
/ 32;
212 f32
.f
= scale
* decimal
;
218 static inline uint32_t float3_to_r11g11b10f(const float rgb
[3])
220 return ( f32_to_uf11(rgb
[0]) & 0x7ff) |
221 ((f32_to_uf11(rgb
[1]) & 0x7ff) << 11) |
222 ((f32_to_uf10(rgb
[2]) & 0x3ff) << 22);
225 static inline void r11g11b10f_to_float3(uint32_t rgb
, float retval
[3])
227 retval
[0] = uf11_to_f32( rgb
& 0x7ff);
228 retval
[1] = uf11_to_f32((rgb
>> 11) & 0x7ff);
229 retval
[2] = uf10_to_f32((rgb
>> 22) & 0x3ff);
232 #endif /* FORMAT_R11G11B10F_H */