101e04c3fSmrg/*
201e04c3fSmrg * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/* Based on code from The OpenGL Programming Guide / 7th Edition, Appendix J.
2501e04c3fSmrg * Available here: http://www.opengl-redbook.com/appendices/
2601e04c3fSmrg * The algorithm in the book contains a bug though, which is fixed in the code
2701e04c3fSmrg * below.
2801e04c3fSmrg */
2901e04c3fSmrg
3001e04c3fSmrg#ifndef FORMAT_R11G11B10F_H
3101e04c3fSmrg#define FORMAT_R11G11B10F_H
3201e04c3fSmrg
3301e04c3fSmrg#include <stdint.h>
3401e04c3fSmrg
3501e04c3fSmrg#define UF11(e, m)           ((e << 6) | (m))
3601e04c3fSmrg#define UF11_EXPONENT_BIAS   15
3701e04c3fSmrg#define UF11_EXPONENT_BITS   0x1F
3801e04c3fSmrg#define UF11_EXPONENT_SHIFT  6
3901e04c3fSmrg#define UF11_MANTISSA_BITS   0x3F
4001e04c3fSmrg#define UF11_MANTISSA_SHIFT  (23 - UF11_EXPONENT_SHIFT)
4101e04c3fSmrg#define UF11_MAX_EXPONENT    (UF11_EXPONENT_BITS << UF11_EXPONENT_SHIFT)
4201e04c3fSmrg
4301e04c3fSmrg#define UF10(e, m)           ((e << 5) | (m))
4401e04c3fSmrg#define UF10_EXPONENT_BIAS   15
4501e04c3fSmrg#define UF10_EXPONENT_BITS   0x1F
4601e04c3fSmrg#define UF10_EXPONENT_SHIFT  5
4701e04c3fSmrg#define UF10_MANTISSA_BITS   0x1F
4801e04c3fSmrg#define UF10_MANTISSA_SHIFT  (23 - UF10_EXPONENT_SHIFT)
4901e04c3fSmrg#define UF10_MAX_EXPONENT    (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT)
5001e04c3fSmrg
5101e04c3fSmrg#define F32_INFINITY         0x7f800000
5201e04c3fSmrg
5301e04c3fSmrgstatic inline uint32_t f32_to_uf11(float val)
5401e04c3fSmrg{
5501e04c3fSmrg   union {
5601e04c3fSmrg      float f;
5701e04c3fSmrg      uint32_t ui;
5801e04c3fSmrg   } f32 = {val};
5901e04c3fSmrg
6001e04c3fSmrg   uint16_t uf11 = 0;
6101e04c3fSmrg
6201e04c3fSmrg   /* Decode little-endian 32-bit floating-point value */
6301e04c3fSmrg   int sign = (f32.ui >> 16) & 0x8000;
6401e04c3fSmrg   /* Map exponent to the range [-127,128] */
6501e04c3fSmrg   int exponent = ((f32.ui >> 23) & 0xff) - 127;
6601e04c3fSmrg   int mantissa = f32.ui & 0x007fffff;
6701e04c3fSmrg
6801e04c3fSmrg   if (exponent == 128) { /* Infinity or NaN */
6901e04c3fSmrg      /* From the GL_EXT_packed_float spec:
7001e04c3fSmrg       *
7101e04c3fSmrg       *     "Additionally: negative infinity is converted to zero; positive
7201e04c3fSmrg       *      infinity is converted to positive infinity; and both positive and
7301e04c3fSmrg       *      negative NaN are converted to positive NaN."
7401e04c3fSmrg       */
7501e04c3fSmrg      uf11 = UF11_MAX_EXPONENT;
7601e04c3fSmrg      if (mantissa) {
7701e04c3fSmrg         uf11 |= 1; /* NaN */
7801e04c3fSmrg      } else {
7901e04c3fSmrg         if (sign)
8001e04c3fSmrg            uf11 = 0; /* 0.0 */
8101e04c3fSmrg      }
8201e04c3fSmrg   } else if (sign) {
8301e04c3fSmrg      return 0;
8401e04c3fSmrg   } else if (val > 65024.0f) {
8501e04c3fSmrg      /* From the GL_EXT_packed_float spec:
8601e04c3fSmrg       *
8701e04c3fSmrg       *     "Likewise, finite positive values greater than 65024 (the maximum
8801e04c3fSmrg       *      finite representable unsigned 11-bit floating-point value) are
8901e04c3fSmrg       *      converted to 65024."
9001e04c3fSmrg       */
9101e04c3fSmrg      uf11 = UF11(30, 63);
9201e04c3fSmrg   } else if (exponent > -15) { /* Representable value */
9301e04c3fSmrg      exponent += UF11_EXPONENT_BIAS;
9401e04c3fSmrg      mantissa >>= UF11_MANTISSA_SHIFT;
9501e04c3fSmrg      uf11 = exponent << UF11_EXPONENT_SHIFT | mantissa;
9601e04c3fSmrg   }
9701e04c3fSmrg
9801e04c3fSmrg   return uf11;
9901e04c3fSmrg}
10001e04c3fSmrg
10101e04c3fSmrgstatic inline float uf11_to_f32(uint16_t val)
10201e04c3fSmrg{
10301e04c3fSmrg   union {
10401e04c3fSmrg      float f;
10501e04c3fSmrg      uint32_t ui;
10601e04c3fSmrg   } f32;
10701e04c3fSmrg
10801e04c3fSmrg   int exponent = (val & 0x07c0) >> UF11_EXPONENT_SHIFT;
10901e04c3fSmrg   int mantissa = (val & 0x003f);
11001e04c3fSmrg
11101e04c3fSmrg   f32.f = 0.0;
11201e04c3fSmrg
11301e04c3fSmrg   if (exponent == 0) {
11401e04c3fSmrg      if (mantissa != 0) {
11501e04c3fSmrg         const float scale = 1.0 / (1 << 20);
11601e04c3fSmrg         f32.f = scale * mantissa;
11701e04c3fSmrg      }
11801e04c3fSmrg   } else if (exponent == 31) {
11901e04c3fSmrg      f32.ui = F32_INFINITY | mantissa;
12001e04c3fSmrg   } else {
12101e04c3fSmrg      float scale, decimal;
12201e04c3fSmrg      exponent -= 15;
12301e04c3fSmrg      if (exponent < 0) {
12401e04c3fSmrg         scale = 1.0f / (1 << -exponent);
12501e04c3fSmrg      } else {
12601e04c3fSmrg         scale = (float) (1 << exponent);
12701e04c3fSmrg      }
12801e04c3fSmrg      decimal = 1.0f + (float) mantissa / 64;
12901e04c3fSmrg      f32.f = scale * decimal;
13001e04c3fSmrg   }
13101e04c3fSmrg
13201e04c3fSmrg   return f32.f;
13301e04c3fSmrg}
13401e04c3fSmrg
13501e04c3fSmrgstatic inline uint32_t f32_to_uf10(float val)
13601e04c3fSmrg{
13701e04c3fSmrg   union {
13801e04c3fSmrg      float f;
13901e04c3fSmrg      uint32_t ui;
14001e04c3fSmrg   } f32 = {val};
14101e04c3fSmrg
14201e04c3fSmrg   uint16_t uf10 = 0;
14301e04c3fSmrg
14401e04c3fSmrg   /* Decode little-endian 32-bit floating-point value */
14501e04c3fSmrg   int sign = (f32.ui >> 16) & 0x8000;
14601e04c3fSmrg   /* Map exponent to the range [-127,128] */
14701e04c3fSmrg   int exponent = ((f32.ui >> 23) & 0xff) - 127;
14801e04c3fSmrg   int mantissa = f32.ui & 0x007fffff;
14901e04c3fSmrg
15001e04c3fSmrg   if (exponent == 128) {
15101e04c3fSmrg      /* From the GL_EXT_packed_float spec:
15201e04c3fSmrg       *
15301e04c3fSmrg       *     "Additionally: negative infinity is converted to zero; positive
15401e04c3fSmrg       *      infinity is converted to positive infinity; and both positive and
15501e04c3fSmrg       *      negative NaN are converted to positive NaN."
15601e04c3fSmrg       */
15701e04c3fSmrg      uf10 = UF10_MAX_EXPONENT;
15801e04c3fSmrg      if (mantissa) {
15901e04c3fSmrg         uf10 |= 1; /* NaN */
16001e04c3fSmrg      } else {
16101e04c3fSmrg         if (sign)
16201e04c3fSmrg            uf10 = 0; /* 0.0 */
16301e04c3fSmrg      }
16401e04c3fSmrg   } else if (sign) {
16501e04c3fSmrg      return 0;
16601e04c3fSmrg   } else if (val > 64512.0f) {
16701e04c3fSmrg      /* From the GL_EXT_packed_float spec:
16801e04c3fSmrg       *
16901e04c3fSmrg       *     "Likewise, finite positive values greater than 64512 (the maximum
17001e04c3fSmrg       *      finite representable unsigned 10-bit floating-point value) are
17101e04c3fSmrg       *      converted to 64512."
17201e04c3fSmrg       */
17301e04c3fSmrg      uf10 = UF10(30, 31);
17401e04c3fSmrg   } else if (exponent > -15) { /* Representable value */
17501e04c3fSmrg      exponent += UF10_EXPONENT_BIAS;
17601e04c3fSmrg      mantissa >>= UF10_MANTISSA_SHIFT;
17701e04c3fSmrg      uf10 = exponent << UF10_EXPONENT_SHIFT | mantissa;
17801e04c3fSmrg   }
17901e04c3fSmrg
18001e04c3fSmrg   return uf10;
18101e04c3fSmrg}
18201e04c3fSmrg
18301e04c3fSmrgstatic inline float uf10_to_f32(uint16_t val)
18401e04c3fSmrg{
18501e04c3fSmrg   union {
18601e04c3fSmrg      float f;
18701e04c3fSmrg      uint32_t ui;
18801e04c3fSmrg   } f32;
18901e04c3fSmrg
19001e04c3fSmrg   int exponent = (val & 0x03e0) >> UF10_EXPONENT_SHIFT;
19101e04c3fSmrg   int mantissa = (val & 0x001f);
19201e04c3fSmrg
19301e04c3fSmrg   f32.f = 0.0;
19401e04c3fSmrg
19501e04c3fSmrg   if (exponent == 0) {
19601e04c3fSmrg      if (mantissa != 0) {
19701e04c3fSmrg         const float scale = 1.0 / (1 << 19);
19801e04c3fSmrg         f32.f = scale * mantissa;
19901e04c3fSmrg      }
20001e04c3fSmrg   } else if (exponent == 31) {
20101e04c3fSmrg      f32.ui = F32_INFINITY | mantissa;
20201e04c3fSmrg   } else {
20301e04c3fSmrg      float scale, decimal;
20401e04c3fSmrg      exponent -= 15;
20501e04c3fSmrg      if (exponent < 0) {
20601e04c3fSmrg         scale = 1.0f / (1 << -exponent);
20701e04c3fSmrg      }
20801e04c3fSmrg      else {
20901e04c3fSmrg         scale = (float) (1 << exponent);
21001e04c3fSmrg      }
21101e04c3fSmrg      decimal = 1.0f + (float) mantissa / 32;
21201e04c3fSmrg      f32.f = scale * decimal;
21301e04c3fSmrg   }
21401e04c3fSmrg
21501e04c3fSmrg   return f32.f;
21601e04c3fSmrg}
21701e04c3fSmrg
21801e04c3fSmrgstatic inline uint32_t float3_to_r11g11b10f(const float rgb[3])
21901e04c3fSmrg{
22001e04c3fSmrg   return ( f32_to_uf11(rgb[0]) & 0x7ff) |
22101e04c3fSmrg          ((f32_to_uf11(rgb[1]) & 0x7ff) << 11) |
22201e04c3fSmrg          ((f32_to_uf10(rgb[2]) & 0x3ff) << 22);
22301e04c3fSmrg}
22401e04c3fSmrg
22501e04c3fSmrgstatic inline void r11g11b10f_to_float3(uint32_t rgb, float retval[3])
22601e04c3fSmrg{
22701e04c3fSmrg   retval[0] = uf11_to_f32( rgb        & 0x7ff);
22801e04c3fSmrg   retval[1] = uf11_to_f32((rgb >> 11) & 0x7ff);
22901e04c3fSmrg   retval[2] = uf10_to_f32((rgb >> 22) & 0x3ff);
23001e04c3fSmrg}
23101e04c3fSmrg
23201e04c3fSmrg#endif /* FORMAT_R11G11B10F_H */
233