OSDN Git Service

gallium/auxiliary: Ensure c99_math.h is included.
[android-x86/external-mesa.git] / src / gallium / auxiliary / util / u_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. */
25
26 #ifndef RGB9E5_H
27 #define RGB9E5_H
28
29 #include <assert.h>
30
31 #include "c99_math.h"
32
33 #define RGB9E5_EXPONENT_BITS          5
34 #define RGB9E5_MANTISSA_BITS          9
35 #define RGB9E5_EXP_BIAS               15
36 #define RGB9E5_MAX_VALID_BIASED_EXP   31
37
38 #define MAX_RGB9E5_EXP               (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
39 #define RGB9E5_MANTISSA_VALUES       (1<<RGB9E5_MANTISSA_BITS)
40 #define MAX_RGB9E5_MANTISSA          (RGB9E5_MANTISSA_VALUES-1)
41 #define MAX_RGB9E5                   (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))
42 #define EPSILON_RGB9E5               ((1.0/RGB9E5_MANTISSA_VALUES) / (1<<RGB9E5_EXP_BIAS))
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 static inline float rgb9e5_ClampRange(float x)
78 {
79    if (x > 0.0f) {
80       if (x >= MAX_RGB9E5) {
81          return MAX_RGB9E5;
82       } else {
83          return x;
84       }
85    } else {
86       /* NaN gets here too since comparisons with NaN always fail! */
87       return 0.0;
88    }
89 }
90
91 /* Ok, FloorLog2 is not correct for the denorm and zero values, but we
92    are going to do a max of this value with the minimum rgb9e5 exponent
93    that will hide these problem cases. */
94 static inline int rgb9e5_FloorLog2(float x)
95 {
96    float754 f;
97
98    f.value = x;
99    return (f.field.biasedexponent - 127);
100 }
101
102 static inline unsigned float3_to_rgb9e5(const float rgb[3])
103 {
104    rgb9e5 retval;
105    float maxrgb;
106    int rm, gm, bm;
107    float rc, gc, bc;
108    int exp_shared, maxm;
109    double denom;
110
111    rc = rgb9e5_ClampRange(rgb[0]);
112    gc = rgb9e5_ClampRange(rgb[1]);
113    bc = rgb9e5_ClampRange(rgb[2]);
114
115    maxrgb = MAX3(rc, gc, bc);
116    exp_shared = MAX2(-RGB9E5_EXP_BIAS-1, rgb9e5_FloorLog2(maxrgb)) + 1 + RGB9E5_EXP_BIAS;
117    assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
118    assert(exp_shared >= 0);
119    /* This exp2 function could be replaced by a table. */
120    denom = exp2(exp_shared - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS);
121
122    maxm = (int) floor(maxrgb / denom + 0.5);
123    if (maxm == MAX_RGB9E5_MANTISSA+1) {
124       denom *= 2;
125       exp_shared += 1;
126       assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
127    } else {
128       assert(maxm <= MAX_RGB9E5_MANTISSA);
129    }
130
131    rm = (int) floor(rc / denom + 0.5);
132    gm = (int) floor(gc / denom + 0.5);
133    bm = (int) floor(bc / denom + 0.5);
134
135    assert(rm <= MAX_RGB9E5_MANTISSA);
136    assert(gm <= MAX_RGB9E5_MANTISSA);
137    assert(bm <= MAX_RGB9E5_MANTISSA);
138    assert(rm >= 0);
139    assert(gm >= 0);
140    assert(bm >= 0);
141
142    retval.field.r = rm;
143    retval.field.g = gm;
144    retval.field.b = bm;
145    retval.field.biasedexponent = exp_shared;
146
147    return retval.raw;
148 }
149
150 static inline void rgb9e5_to_float3(unsigned rgb, float retval[3])
151 {
152    rgb9e5 v;
153    int exponent;
154    float scale;
155
156    v.raw = rgb;
157    exponent = v.field.biasedexponent - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
158    scale = exp2f(exponent);
159
160    retval[0] = v.field.r * scale;
161    retval[1] = v.field.g * scale;
162    retval[2] = v.field.b * scale;
163 }
164
165 #endif