u_format_r11g11b10f.h revision b48359184e36ecd11510e9c87e3db535935c99e2
101e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor/* 201e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com> 301e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * 401e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * Permission is hereby granted, free of charge, to any person obtaining a 501e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * copy of this software and associated documentation files (the "Software"), 601e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * to deal in the Software without restriction, including without limitation 701e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * and/or sell copies of the Software, and to permit persons to whom the 901e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * Software is furnished to do so, subject to the following conditions: 1001e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * 1101e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * The above copyright notice and this permission notice (including the next 1201e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * paragraph) shall be included in all copies or substantial portions of the 1301e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * Software. 1401e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * 1501e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2101e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * DEALINGS IN THE SOFTWARE. 2201e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor */ 2301e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor 2401e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor/* Based on code from The OpenGL Programming Guide / 7th Edition, Appendix J. 25cf6b0a20c697ba8daf2dff3a4cce2a028b33cb48Richard Smith * Available here: http://www.opengl-redbook.com/appendices/ 2601e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor * The algorithm in the book contains a bug though, which is fixed in the code 27cf6b0a20c697ba8daf2dff3a4cce2a028b33cb48Richard Smith * below. 28cf6b0a20c697ba8daf2dff3a4cce2a028b33cb48Richard Smith */ 2901e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor 3001e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor#define UF11_EXPONENT_BIAS 15 31cf6b0a20c697ba8daf2dff3a4cce2a028b33cb48Richard Smith#define UF11_EXPONENT_BITS 0x1F 3201e56aecb77a96dcd93fa0e901b919f2e441981dDouglas Gregor#define UF11_EXPONENT_SHIFT 6 33#define UF11_MANTISSA_BITS 0x3F 34#define UF11_MANTISSA_SHIFT (23 - UF11_EXPONENT_SHIFT) 35#define UF11_MAX_EXPONENT (UF11_EXPONENT_BITS << UF11_EXPONENT_SHIFT) 36 37#define UF10_EXPONENT_BIAS 15 38#define UF10_EXPONENT_BITS 0x1F 39#define UF10_EXPONENT_SHIFT 5 40#define UF10_MANTISSA_BITS 0x3F 41#define UF10_MANTISSA_SHIFT (23 - UF10_EXPONENT_SHIFT) 42#define UF10_MAX_EXPONENT (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT) 43 44#define F32_INFINITY 0x7f800000 45 46static INLINE unsigned f32_to_uf11(float val) 47{ 48 uint32_t f32 = (*(uint32_t *) &val); 49 uint16_t uf11 = 0; 50 51 /* Decode little-endian 32-bit floating-point value */ 52 int sign = (f32 >> 16) & 0x8000; 53 /* Map exponent to the range [-127,128] */ 54 int exponent = ((f32 >> 23) & 0xff) - 127; 55 int mantissa = f32 & 0x007fffff; 56 57 if (sign) return 0; 58 59 if (exponent == 128) { /* Infinity or NaN */ 60 uf11 = UF11_MAX_EXPONENT; 61 if (mantissa) uf11 |= (mantissa & UF11_MANTISSA_BITS); 62 } 63 else if (exponent > 15) { /* Overflow - flush to Infinity */ 64 uf11 = UF11_MAX_EXPONENT; 65 } 66 else if (exponent > -15) { /* Representable value */ 67 exponent += UF11_EXPONENT_BIAS; 68 mantissa >>= UF11_MANTISSA_SHIFT; 69 uf11 = exponent << UF11_EXPONENT_SHIFT | mantissa; 70 } 71 72 return uf11; 73} 74 75static INLINE float uf11_to_f32(uint16_t val) 76{ 77 union { 78 float f; 79 uint32_t ui; 80 } f32; 81 82 int exponent = (val & 0x07c0) >> UF11_EXPONENT_SHIFT; 83 int mantissa = (val & 0x003f); 84 85 f32.f = 0.0; 86 87 if (exponent == 0) { 88 if (mantissa != 0) { 89 const float scale = 1.0 / (1 << 20); 90 f32.f = scale * mantissa; 91 } 92 } 93 else if (exponent == 31) { 94 f32.ui = F32_INFINITY | mantissa; 95 } 96 else { 97 float scale, decimal; 98 exponent -= 15; 99 if (exponent < 0) { 100 scale = 1.0 / (1 << -exponent); 101 } 102 else { 103 scale = 1 << exponent; 104 } 105 decimal = 1.0 + (float) mantissa / 64; 106 f32.f = scale * decimal; 107 } 108 109 return f32.f; 110} 111 112static INLINE unsigned f32_to_uf10(float val) 113{ 114 uint32_t f32 = (*(uint32_t *) &val); 115 uint16_t uf10 = 0; 116 117 /* Decode little-endian 32-bit floating-point value */ 118 int sign = (f32 >> 16) & 0x8000; 119 /* Map exponent to the range [-127,128] */ 120 int exponent = ((f32 >> 23) & 0xff) - 127; 121 int mantissa = f32 & 0x007fffff; 122 123 if (sign) return 0; 124 125 if (exponent == 128) { /* Infinity or NaN */ 126 uf10 = UF10_MAX_EXPONENT; 127 if (mantissa) uf10 |= (mantissa & UF10_MANTISSA_BITS); 128 } 129 else if (exponent > 15) { /* Overflow - flush to Infinity */ 130 uf10 = UF10_MAX_EXPONENT; 131 } 132 else if (exponent > -15) { /* Representable value */ 133 exponent += UF10_EXPONENT_BIAS; 134 mantissa >>= UF10_MANTISSA_SHIFT; 135 uf10 = exponent << UF10_EXPONENT_SHIFT | mantissa; 136 } 137 138 return uf10; 139} 140 141static INLINE float uf10_to_f32(uint16_t val) 142{ 143 union { 144 float f; 145 uint32_t ui; 146 } f32; 147 148 int exponent = (val & 0x07c0) >> UF10_EXPONENT_SHIFT; 149 int mantissa = (val & 0x003f); 150 151 f32.f = 0.0; 152 153 if (exponent == 0) { 154 if (mantissa != 0) { 155 const float scale = 1.0 / (1 << 20); 156 f32.f = scale * mantissa; 157 } 158 } 159 else if (exponent == 31) { 160 f32.ui = F32_INFINITY | mantissa; 161 } 162 else { 163 float scale, decimal; 164 exponent -= 15; 165 if (exponent < 0) { 166 scale = 1.0 / (1 << -exponent); 167 } 168 else { 169 scale = 1 << exponent; 170 } 171 decimal = 1.0 + (float) mantissa / 32; 172 f32.f = scale * decimal; 173 } 174 175 return f32.f; 176} 177 178static INLINE unsigned float3_to_r11g11b10f(const float rgb[3]) 179{ 180 return ( f32_to_uf11(rgb[0]) & 0x7ff) | 181 ((f32_to_uf11(rgb[1]) & 0x7ff) << 11) | 182 ((f32_to_uf10(rgb[2]) & 0x3ff) << 22); 183} 184 185static INLINE void r11g11b10f_to_float3(unsigned rgb, float retval[3]) 186{ 187 retval[0] = uf11_to_f32( rgb & 0x7ff); 188 retval[1] = uf11_to_f32((rgb >> 11) & 0x7ff); 189 retval[2] = uf10_to_f32((rgb >> 22) & 0x3ff); 190} 191