2 * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
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:
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
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.
24 /* Copied from EXT_texture_shared_exponent and edited, getting rid of
25 * expensive float math bits too. */
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
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))
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;
53 unsigned int mantissa
:23;
54 unsigned int biasedexponent
:8;
55 unsigned int negative
:1;
60 static inline int rgb9e5_ClampRange(float x
)
65 max
.value
= MAX_RGB9E5
;
67 if (f
.raw
> 0x7f800000)
68 /* catches neg, NaNs */
70 else if (f
.raw
>= max
.raw
)
76 static inline unsigned int float3_to_rgb9e5(const float rgb
[3])
78 int rm
, gm
, bm
, exp_shared
;
79 float754 revdenom
= {0};
80 float754 rc
, bc
, gc
, maxrgb
;
82 rc
.raw
= rgb9e5_ClampRange(rgb
[0]);
83 gc
.raw
= rgb9e5_ClampRange(rgb
[1]);
84 bc
.raw
= rgb9e5_ClampRange(rgb
[2]);
85 maxrgb
.raw
= MAX3(rc
.raw
, gc
.raw
, bc
.raw
);
88 * Compared to what the spec suggests, instead of conditionally adjusting
89 * the exponent after the fact do it here by doing the equivalent of +0.5 -
90 * the int add will spill over into the exponent in this case.
92 maxrgb
.raw
+= maxrgb
.raw
& (1 << (23-9));
93 exp_shared
= MAX2((maxrgb
.raw
>> 23), -RGB9E5_EXP_BIAS
- 1 + 127) +
94 1 + RGB9E5_EXP_BIAS
- 127;
95 revdenom
.field
.biasedexponent
= 127 - (exp_shared
- RGB9E5_EXP_BIAS
-
96 RGB9E5_MANTISSA_BITS
) + 1;
97 assert(exp_shared
<= RGB9E5_MAX_VALID_BIASED_EXP
);
100 * The spec uses strict round-up behavior (d3d10 disagrees, but in any case
101 * must match what is done above for figuring out exponent).
102 * We avoid the doubles ((int) rc * revdenom + 0.5) by doing the rounding
103 * ourselves (revdenom was adjusted by +1, above).
105 rm
= (int) (rc
.value
* revdenom
.value
);
106 gm
= (int) (gc
.value
* revdenom
.value
);
107 bm
= (int) (bc
.value
* revdenom
.value
);
108 rm
= (rm
& 1) + (rm
>> 1);
109 gm
= (gm
& 1) + (gm
>> 1);
110 bm
= (bm
& 1) + (bm
>> 1);
112 assert(rm
<= MAX_RGB9E5_MANTISSA
);
113 assert(gm
<= MAX_RGB9E5_MANTISSA
);
114 assert(bm
<= MAX_RGB9E5_MANTISSA
);
119 return (exp_shared
<< 27) | (bm
<< 18) | (gm
<< 9) | rm
;
122 static inline void rgb9e5_to_float3(unsigned rgb
, float retval
[3])
125 float754 scale
= {0};
127 exponent
= (rgb
>> 27) - RGB9E5_EXP_BIAS
- RGB9E5_MANTISSA_BITS
;
128 scale
.field
.biasedexponent
= exponent
+ 127;
130 retval
[0] = ( rgb
& 0x1ff) * scale
.value
;
131 retval
[1] = ((rgb
>> 9) & 0x1ff) * scale
.value
;
132 retval
[2] = ((rgb
>> 18) & 0x1ff) * scale
.value
;