1b8e80941Smrg/*
2b8e80941Smrg * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg/* Copied from EXT_texture_shared_exponent and edited, getting rid of
25b8e80941Smrg * expensive float math bits too. */
26b8e80941Smrg
27b8e80941Smrg#ifndef RGB9E5_H
28b8e80941Smrg#define RGB9E5_H
29b8e80941Smrg
30b8e80941Smrg#include <assert.h>
31b8e80941Smrg#include <stdint.h>
32b8e80941Smrg
33b8e80941Smrg#include "c99_math.h"
34b8e80941Smrg
35b8e80941Smrg#define RGB9E5_EXPONENT_BITS          5
36b8e80941Smrg#define RGB9E5_MANTISSA_BITS          9
37b8e80941Smrg#define RGB9E5_EXP_BIAS               15
38b8e80941Smrg#define RGB9E5_MAX_VALID_BIASED_EXP   31
39b8e80941Smrg
40b8e80941Smrg#define MAX_RGB9E5_EXP               (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
41b8e80941Smrg#define RGB9E5_MANTISSA_VALUES       (1<<RGB9E5_MANTISSA_BITS)
42b8e80941Smrg#define MAX_RGB9E5_MANTISSA          (RGB9E5_MANTISSA_VALUES-1)
43b8e80941Smrg#define MAX_RGB9E5                   (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))
44b8e80941Smrg
45b8e80941Smrgstatic inline int rgb9e5_ClampRange(float x)
46b8e80941Smrg{
47b8e80941Smrg   union { float f; uint32_t u; } f, max;
48b8e80941Smrg   f.f = x;
49b8e80941Smrg   max.f = MAX_RGB9E5;
50b8e80941Smrg
51b8e80941Smrg   if (f.u > 0x7f800000)
52b8e80941Smrg  /* catches neg, NaNs */
53b8e80941Smrg      return 0;
54b8e80941Smrg   else if (f.u >= max.u)
55b8e80941Smrg      return max.u;
56b8e80941Smrg   else
57b8e80941Smrg      return f.u;
58b8e80941Smrg}
59b8e80941Smrg
60b8e80941Smrgstatic inline uint32_t float3_to_rgb9e5(const float rgb[3])
61b8e80941Smrg{
62b8e80941Smrg   int rm, gm, bm, exp_shared;
63b8e80941Smrg   uint32_t revdenom_biasedexp;
64b8e80941Smrg   union { float f; uint32_t u; } rc, bc, gc, maxrgb, revdenom;
65b8e80941Smrg
66b8e80941Smrg   rc.u = rgb9e5_ClampRange(rgb[0]);
67b8e80941Smrg   gc.u = rgb9e5_ClampRange(rgb[1]);
68b8e80941Smrg   bc.u = rgb9e5_ClampRange(rgb[2]);
69b8e80941Smrg   maxrgb.u = MAX3(rc.u, gc.u, bc.u);
70b8e80941Smrg
71b8e80941Smrg   /*
72b8e80941Smrg    * Compared to what the spec suggests, instead of conditionally adjusting
73b8e80941Smrg    * the exponent after the fact do it here by doing the equivalent of +0.5 -
74b8e80941Smrg    * the int add will spill over into the exponent in this case.
75b8e80941Smrg    */
76b8e80941Smrg   maxrgb.u += maxrgb.u & (1 << (23-9));
77b8e80941Smrg   exp_shared = MAX2((maxrgb.u >> 23), -RGB9E5_EXP_BIAS - 1 + 127) +
78b8e80941Smrg                1 + RGB9E5_EXP_BIAS - 127;
79b8e80941Smrg   revdenom_biasedexp = 127 - (exp_shared - RGB9E5_EXP_BIAS -
80b8e80941Smrg                               RGB9E5_MANTISSA_BITS) + 1;
81b8e80941Smrg   revdenom.u = revdenom_biasedexp << 23;
82b8e80941Smrg   assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
83b8e80941Smrg
84b8e80941Smrg   /*
85b8e80941Smrg    * The spec uses strict round-up behavior (d3d10 disagrees, but in any case
86b8e80941Smrg    * must match what is done above for figuring out exponent).
87b8e80941Smrg    * We avoid the doubles ((int) rc * revdenom + 0.5) by doing the rounding
88b8e80941Smrg    * ourselves (revdenom was adjusted by +1, above).
89b8e80941Smrg    */
90b8e80941Smrg   rm = (int) (rc.f * revdenom.f);
91b8e80941Smrg   gm = (int) (gc.f * revdenom.f);
92b8e80941Smrg   bm = (int) (bc.f * revdenom.f);
93b8e80941Smrg   rm = (rm & 1) + (rm >> 1);
94b8e80941Smrg   gm = (gm & 1) + (gm >> 1);
95b8e80941Smrg   bm = (bm & 1) + (bm >> 1);
96b8e80941Smrg
97b8e80941Smrg   assert(rm <= MAX_RGB9E5_MANTISSA);
98b8e80941Smrg   assert(gm <= MAX_RGB9E5_MANTISSA);
99b8e80941Smrg   assert(bm <= MAX_RGB9E5_MANTISSA);
100b8e80941Smrg   assert(rm >= 0);
101b8e80941Smrg   assert(gm >= 0);
102b8e80941Smrg   assert(bm >= 0);
103b8e80941Smrg
104b8e80941Smrg   return (exp_shared << 27) | (bm << 18) | (gm << 9) | rm;
105b8e80941Smrg}
106b8e80941Smrg
107b8e80941Smrgstatic inline void rgb9e5_to_float3(uint32_t rgb, float retval[3])
108b8e80941Smrg{
109b8e80941Smrg   int exponent;
110b8e80941Smrg   union { float f; uint32_t u; } scale;
111b8e80941Smrg
112b8e80941Smrg   exponent = (rgb >> 27) - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
113b8e80941Smrg   scale.u = (exponent + 127) << 23;
114b8e80941Smrg
115b8e80941Smrg   retval[0] = ( rgb        & 0x1ff) * scale.f;
116b8e80941Smrg   retval[1] = ((rgb >> 9)  & 0x1ff) * scale.f;
117b8e80941Smrg   retval[2] = ((rgb >> 18) & 0x1ff) * scale.f;
118b8e80941Smrg}
119b8e80941Smrg
120b8e80941Smrg#endif
121