Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / savage / savagespan.h
1 /*
2  * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3  * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sub license,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifndef _SAVAGE_SPAN_H
26 #define _SAVAGE_SPAN_H
27
28 #include "drirenderbuffer.h"
29
30
31 extern void savageDDInitSpanFuncs( struct gl_context *ctx );
32
33 extern void
34 savageSetSpanFunctions(driRenderbuffer *rb, const struct gl_config *vis,
35                        GLboolean float_depth);
36
37
38 /*
39  * Savage 16-bit float depth format with zExpOffset=16:
40  *   4 bit unsigned exponent, 12 bit mantissa
41  *
42  * The meaning of the mantissa is different from IEEE floatint point
43  * formats. The same number can't be encoded with different exponents.
44  * So no bits are wasted.
45  *
46  * exponent | range encoded by mantissa | accuracy or mantissa
47  * ---------+---------------------------+---------------------
48  *       15 | 2^-1 .. 1                 | 2^-13
49  *       14 | 2^-2 .. 2^-1              | 2^-14
50  *       13 | 2^-3 .. 2^-2              | 2^-15
51  *      ... | ...                       |
52  *        2 | 2^-14 .. 2^-13            | 2^-27
53  *        1 | 2^-15 .. 2^-14            | 2^-27
54  *        0 | 2^-16 .. 2^-15            | 2^-28
55  *
56  * Note that there is no encoding for numbers < 2^-16.
57  */
58 static INLINE GLuint savageEncodeFloat16( GLdouble x )
59 {
60     GLint r = (GLint)(x * 0x10000000);
61     GLint exp = 0;
62     if (r < 0x1000)
63         return 0;
64     while (r - 0x1000 > 0x0fff) {
65         r >>= 1;
66         exp++;
67     }
68     return exp > 0xf ? 0xffff : (r - 0x1000) | (exp << 12);
69 }
70 static INLINE GLdouble savageDecodeFloat16( GLuint x )
71 {
72     static const GLdouble pow2[16] = {
73         1.0/(1<<28), 1.0/(1<<27), 1.0/(1<<26), 1.0/(1<<25),
74         1.0/(1<<24), 1.0/(1<<23), 1.0/(1<<22), 1.0/(1<<21),
75         1.0/(1<<20), 1.0/(1<<19), 1.0/(1<<18), 1.0/(1<<17),
76         1.0/(1<<16), 1.0/(1<<15), 1.0/(1<<14), 1.0/(1<<13)
77     };
78     static const GLdouble bias[16] = {
79         1.0/(1<<16), 1.0/(1<<15), 1.0/(1<<14), 1.0/(1<<13),
80         1.0/(1<<12), 1.0/(1<<11), 1.0/(1<<10), 1.0/(1<< 9),
81         1.0/(1<< 8), 1.0/(1<< 7), 1.0/(1<< 6), 1.0/(1<< 5),
82         1.0/(1<< 4), 1.0/(1<< 3), 1.0/(1<< 2), 1.0/(1<< 1)
83     };
84     GLuint mant = x & 0x0fff;
85     GLuint exp = (x >> 12) & 0xf;
86     return bias[exp] + pow2[exp]*mant;
87 }
88
89 /*
90  * Savage 24-bit float depth format with zExpOffset=32:
91  *   5 bit unsigned exponent, 19 bit mantissa
92  *
93  * Details analogous to the 16-bit format.
94  */
95 static INLINE GLuint savageEncodeFloat24( GLdouble x )
96 {
97     int64_t r = (int64_t)(x * ((int64_t)1 << (19+32)));
98     GLint exp = 0;
99     if (r < 0x80000)
100         return 0;
101     while (r - 0x80000 > 0x7ffff) {
102         r >>= 1;
103         exp++;
104     }
105     return exp > 0x1f ? 0xffffff : (r - 0x80000) | (exp << 19);
106 }
107 #define _1 (int64_t)1
108 static INLINE GLdouble savageDecodeFloat24( GLuint x )
109 {
110     static const GLdouble pow2[32] = {
111         1.0/(_1<<51), 1.0/(_1<<50), 1.0/(_1<<49), 1.0/(_1<<48),
112         1.0/(_1<<47), 1.0/(_1<<46), 1.0/(_1<<45), 1.0/(_1<<44),
113         1.0/(_1<<43), 1.0/(_1<<42), 1.0/(_1<<41), 1.0/(_1<<40),
114         1.0/(_1<<39), 1.0/(_1<<38), 1.0/(_1<<37), 1.0/(_1<<36),
115         1.0/(_1<<35), 1.0/(_1<<34), 1.0/(_1<<33), 1.0/(_1<<32),
116         1.0/(_1<<31), 1.0/(_1<<30), 1.0/(_1<<29), 1.0/(_1<<28),
117         1.0/(_1<<27), 1.0/(_1<<26), 1.0/(_1<<25), 1.0/(_1<<24),
118         1.0/(_1<<23), 1.0/(_1<<22), 1.0/(_1<<21), 1.0/(_1<<20)
119     };
120     static const GLdouble bias[32] = {
121         1.0/(_1<<32), 1.0/(_1<<31), 1.0/(_1<<30), 1.0/(_1<<29),
122         1.0/(_1<<28), 1.0/(_1<<27), 1.0/(_1<<26), 1.0/(_1<<25),
123         1.0/(_1<<24), 1.0/(_1<<23), 1.0/(_1<<22), 1.0/(_1<<21),
124         1.0/(_1<<20), 1.0/(_1<<19), 1.0/(_1<<18), 1.0/(_1<<17),
125         1.0/(_1<<16), 1.0/(_1<<15), 1.0/(_1<<14), 1.0/(_1<<13),
126         1.0/(_1<<12), 1.0/(_1<<11), 1.0/(_1<<10), 1.0/(_1<< 9),
127         1.0/(_1<< 8), 1.0/(_1<< 7), 1.0/(_1<< 6), 1.0/(_1<< 5),
128         1.0/(_1<< 4), 1.0/(_1<< 3), 1.0/(_1<< 2), 1.0/(_1<< 1)
129     };
130     GLuint mant = x & 0x7ffff;
131     GLuint exp = (x >> 19) & 0x1f;
132     return bias[exp] + pow2[exp]*mant;
133 }
134 #undef _1
135
136
137 #endif