util: Move format_rgb9e5.h to src/util
[mesa.git] / src / util / format_rgb9e5.h
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 /* Copied from EXT_texture_shared_exponent and edited, getting rid of
25 * expensive float math bits too. */
26
27 #ifndef RGB9E5_H
28 #define RGB9E5_H
29
30 #include <assert.h>
31
32 #include "c99_math.h"
33
34 #define RGB9E5_EXPONENT_BITS 5
35 #define RGB9E5_MANTISSA_BITS 9
36 #define RGB9E5_EXP_BIAS 15
37 #define RGB9E5_MAX_VALID_BIASED_EXP 31
38
39 #define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
40 #define RGB9E5_MANTISSA_VALUES (1<<RGB9E5_MANTISSA_BITS)
41 #define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES-1)
42 #define MAX_RGB9E5 (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))
43
44 typedef union {
45 unsigned int raw;
46 float value;
47 struct {
48 #if defined(MESA_BIG_ENDIAN) || defined(PIPE_ARCH_BIG_ENDIAN)
49 unsigned int negative:1;
50 unsigned int biasedexponent:8;
51 unsigned int mantissa:23;
52 #else
53 unsigned int mantissa:23;
54 unsigned int biasedexponent:8;
55 unsigned int negative:1;
56 #endif
57 } field;
58 } float754;
59
60 typedef union {
61 unsigned int raw;
62 struct {
63 #if defined(MESA_BIG_ENDIAN) || defined(PIPE_ARCH_BIG_ENDIAN)
64 unsigned int biasedexponent:RGB9E5_EXPONENT_BITS;
65 unsigned int b:RGB9E5_MANTISSA_BITS;
66 unsigned int g:RGB9E5_MANTISSA_BITS;
67 unsigned int r:RGB9E5_MANTISSA_BITS;
68 #else
69 unsigned int r:RGB9E5_MANTISSA_BITS;
70 unsigned int g:RGB9E5_MANTISSA_BITS;
71 unsigned int b:RGB9E5_MANTISSA_BITS;
72 unsigned int biasedexponent:RGB9E5_EXPONENT_BITS;
73 #endif
74 } field;
75 } rgb9e5;
76
77
78 static inline int rgb9e5_ClampRange(float x)
79 {
80 float754 f;
81 float754 max;
82 f.value = x;
83 max.value = MAX_RGB9E5;
84
85 if (f.raw > 0x7f800000)
86 /* catches neg, NaNs */
87 return 0;
88 else if (f.raw >= max.raw)
89 return max.raw;
90 else
91 return f.raw;
92 }
93
94 static inline unsigned float3_to_rgb9e5(const float rgb[3])
95 {
96 rgb9e5 retval;
97 int rm, gm, bm, exp_shared;
98 float754 revdenom = {0};
99 float754 rc, bc, gc, maxrgb;
100
101 rc.raw = rgb9e5_ClampRange(rgb[0]);
102 gc.raw = rgb9e5_ClampRange(rgb[1]);
103 bc.raw = rgb9e5_ClampRange(rgb[2]);
104 maxrgb.raw = MAX3(rc.raw, gc.raw, bc.raw);
105
106 /*
107 * Compared to what the spec suggests, instead of conditionally adjusting
108 * the exponent after the fact do it here by doing the equivalent of +0.5 -
109 * the int add will spill over into the exponent in this case.
110 */
111 maxrgb.raw += maxrgb.raw & (1 << (23-9));
112 exp_shared = MAX2((maxrgb.raw >> 23), -RGB9E5_EXP_BIAS - 1 + 127) +
113 1 + RGB9E5_EXP_BIAS - 127;
114 revdenom.field.biasedexponent = 127 - (exp_shared - RGB9E5_EXP_BIAS -
115 RGB9E5_MANTISSA_BITS) + 1;
116 assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
117
118 /*
119 * The spec uses strict round-up behavior (d3d10 disagrees, but in any case
120 * must match what is done above for figuring out exponent).
121 * We avoid the doubles ((int) rc * revdenom + 0.5) by doing the rounding
122 * ourselves (revdenom was adjusted by +1, above).
123 */
124 rm = (int) (rc.value * revdenom.value);
125 gm = (int) (gc.value * revdenom.value);
126 bm = (int) (bc.value * revdenom.value);
127 rm = (rm & 1) + (rm >> 1);
128 gm = (gm & 1) + (gm >> 1);
129 bm = (bm & 1) + (bm >> 1);
130
131 assert(rm <= MAX_RGB9E5_MANTISSA);
132 assert(gm <= MAX_RGB9E5_MANTISSA);
133 assert(bm <= MAX_RGB9E5_MANTISSA);
134 assert(rm >= 0);
135 assert(gm >= 0);
136 assert(bm >= 0);
137
138 retval.field.r = rm;
139 retval.field.g = gm;
140 retval.field.b = bm;
141 retval.field.biasedexponent = exp_shared;
142
143 return retval.raw;
144 }
145
146 static inline void rgb9e5_to_float3(unsigned rgb, float retval[3])
147 {
148 rgb9e5 v;
149 int exponent;
150 float754 scale = {0};
151
152 v.raw = rgb;
153 exponent = v.field.biasedexponent - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
154 scale.field.biasedexponent = exponent + 127;
155
156 retval[0] = v.field.r * scale.value;
157 retval[1] = v.field.g * scale.value;
158 retval[2] = v.field.b * scale.value;
159 }
160
161 #endif