1 /*
2  * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23 
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
27  * below.
28  */
29 
30 #ifndef FORMAT_R11G11B10F_H
31 #define FORMAT_R11G11B10F_H
32 
33 #include <stdint.h>
34 
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)
42 
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)
50 
51 #define F32_INFINITY         0x7f800000
52 
f32_to_uf11(float val)53 static inline uint32_t f32_to_uf11(float val)
54 {
55    union {
56       float f;
57       uint32_t ui;
58    } f32 = {val};
59 
60    uint16_t uf11 = 0;
61 
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;
67 
68    if (exponent == 128) { /* Infinity or NaN */
69       /* From the GL_EXT_packed_float spec:
70        *
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."
74        */
75       uf11 = UF11_MAX_EXPONENT;
76       if (mantissa) {
77          uf11 |= 1; /* NaN */
78       } else {
79          if (sign)
80             uf11 = 0; /* 0.0 */
81       }
82    } else if (sign) {
83       return 0;
84    } else if (val > 65024.0f) {
85       /* From the GL_EXT_packed_float spec:
86        *
87        *     "Likewise, finite positive values greater than 65024 (the maximum
88        *      finite representable unsigned 11-bit floating-point value) are
89        *      converted to 65024."
90        */
91       uf11 = UF11(30, 63);
92    } else if (exponent > -15) { /* Representable value */
93       exponent += UF11_EXPONENT_BIAS;
94       mantissa >>= UF11_MANTISSA_SHIFT;
95       uf11 = exponent << UF11_EXPONENT_SHIFT | mantissa;
96    }
97 
98    return uf11;
99 }
100 
uf11_to_f32(uint16_t val)101 static inline float uf11_to_f32(uint16_t val)
102 {
103    union {
104       float f;
105       uint32_t ui;
106    } f32;
107 
108    int exponent = (val & 0x07c0) >> UF11_EXPONENT_SHIFT;
109    int mantissa = (val & 0x003f);
110 
111    f32.f = 0.0;
112 
113    if (exponent == 0) {
114       if (mantissa != 0) {
115          const float scale = 1.0 / (1 << 20);
116          f32.f = scale * mantissa;
117       }
118    } else if (exponent == 31) {
119       f32.ui = F32_INFINITY | mantissa;
120    } else {
121       float scale, decimal;
122       exponent -= 15;
123       if (exponent < 0) {
124          scale = 1.0f / (1 << -exponent);
125       } else {
126          scale = (float) (1 << exponent);
127       }
128       decimal = 1.0f + (float) mantissa / 64;
129       f32.f = scale * decimal;
130    }
131 
132    return f32.f;
133 }
134 
f32_to_uf10(float val)135 static inline uint32_t f32_to_uf10(float val)
136 {
137    union {
138       float f;
139       uint32_t ui;
140    } f32 = {val};
141 
142    uint16_t uf10 = 0;
143 
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;
149 
150    if (exponent == 128) {
151       /* From the GL_EXT_packed_float spec:
152        *
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."
156        */
157       uf10 = UF10_MAX_EXPONENT;
158       if (mantissa) {
159          uf10 |= 1; /* NaN */
160       } else {
161          if (sign)
162             uf10 = 0; /* 0.0 */
163       }
164    } else if (sign) {
165       return 0;
166    } else if (val > 64512.0f) {
167       /* From the GL_EXT_packed_float spec:
168        *
169        *     "Likewise, finite positive values greater than 64512 (the maximum
170        *      finite representable unsigned 10-bit floating-point value) are
171        *      converted to 64512."
172        */
173       uf10 = UF10(30, 31);
174    } else if (exponent > -15) { /* Representable value */
175       exponent += UF10_EXPONENT_BIAS;
176       mantissa >>= UF10_MANTISSA_SHIFT;
177       uf10 = exponent << UF10_EXPONENT_SHIFT | mantissa;
178    }
179 
180    return uf10;
181 }
182 
uf10_to_f32(uint16_t val)183 static inline float uf10_to_f32(uint16_t val)
184 {
185    union {
186       float f;
187       uint32_t ui;
188    } f32;
189 
190    int exponent = (val & 0x03e0) >> UF10_EXPONENT_SHIFT;
191    int mantissa = (val & 0x001f);
192 
193    f32.f = 0.0;
194 
195    if (exponent == 0) {
196       if (mantissa != 0) {
197          const float scale = 1.0 / (1 << 19);
198          f32.f = scale * mantissa;
199       }
200    } else if (exponent == 31) {
201       f32.ui = F32_INFINITY | mantissa;
202    } else {
203       float scale, decimal;
204       exponent -= 15;
205       if (exponent < 0) {
206          scale = 1.0f / (1 << -exponent);
207       }
208       else {
209          scale = (float) (1 << exponent);
210       }
211       decimal = 1.0f + (float) mantissa / 32;
212       f32.f = scale * decimal;
213    }
214 
215    return f32.f;
216 }
217 
float3_to_r11g11b10f(const float rgb[3])218 static inline uint32_t float3_to_r11g11b10f(const float rgb[3])
219 {
220    return ( f32_to_uf11(rgb[0]) & 0x7ff) |
221           ((f32_to_uf11(rgb[1]) & 0x7ff) << 11) |
222           ((f32_to_uf10(rgb[2]) & 0x3ff) << 22);
223 }
224 
r11g11b10f_to_float3(uint32_t rgb,float retval[3])225 static inline void r11g11b10f_to_float3(uint32_t rgb, float retval[3])
226 {
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);
230 }
231 
232 #endif /* FORMAT_R11G11B10F_H */
233