Fixed some suspicious pointer casts that caused lots of
[mesa.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 extern void savageDDInitSpanFuncs( GLcontext *ctx );
29
30 /*
31 * Savage 16-bit float depth format with zExpOffset=16:
32 * 4 bit unsigned exponent, 12 bit mantissa
33 *
34 * The meaning of the mantissa is different from IEEE floatint point
35 * formats. The same number can't be encoded with different exponents.
36 * So no bits are wasted.
37 *
38 * exponent | range encoded by mantissa | accuracy or mantissa
39 * ---------+---------------------------+---------------------
40 * 15 | 2^-1 .. 1 | 2^-13
41 * 14 | 2^-2 .. 2^-1 | 2^-14
42 * 13 | 2^-3 .. 2^-2 | 2^-15
43 * ... | ... |
44 * 2 | 2^-14 .. 2^-13 | 2^-27
45 * 1 | 2^-15 .. 2^-14 | 2^-27
46 * 0 | 2^-16 .. 2^-15 | 2^-28
47 *
48 * Note that there is no encoding for numbers < 2^-16.
49 */
50 static __inline GLuint savageEncodeFloat16( GLdouble x )
51 {
52 GLint r = (GLint)(x * 0x10000000);
53 GLint exp = 0;
54 if (r < 0x1000)
55 return 0;
56 while (r - 0x1000 > 0x0fff) {
57 r >>= 1;
58 exp++;
59 }
60 return exp > 0xf ? 0xffff : (r - 0x1000) | (exp << 12);
61 }
62 static __inline GLdouble savageDecodeFloat16( GLuint x )
63 {
64 static const GLdouble pow2[16] = {
65 1.0/(1<<28), 1.0/(1<<27), 1.0/(1<<26), 1.0/(1<<25),
66 1.0/(1<<24), 1.0/(1<<23), 1.0/(1<<22), 1.0/(1<<21),
67 1.0/(1<<20), 1.0/(1<<19), 1.0/(1<<18), 1.0/(1<<17),
68 1.0/(1<<16), 1.0/(1<<15), 1.0/(1<<14), 1.0/(1<<13)
69 };
70 static const GLdouble bias[16] = {
71 1.0/(1<<16), 1.0/(1<<15), 1.0/(1<<14), 1.0/(1<<13),
72 1.0/(1<<12), 1.0/(1<<11), 1.0/(1<<10), 1.0/(1<< 9),
73 1.0/(1<< 8), 1.0/(1<< 7), 1.0/(1<< 6), 1.0/(1<< 5),
74 1.0/(1<< 4), 1.0/(1<< 3), 1.0/(1<< 2), 1.0/(1<< 1)
75 };
76 GLuint mant = x & 0x0fff;
77 GLuint exp = (x >> 12) & 0xf;
78 return bias[exp] + pow2[exp]*mant;
79 }
80
81 /*
82 * Savage 24-bit float depth format with zExpOffset=32:
83 * 5 bit unsigned exponent, 19 bit mantissa
84 *
85 * Details analogous to the 16-bit format.
86 */
87 static __inline GLuint savageEncodeFloat24( GLdouble x )
88 {
89 int64_t r = (int64_t)(x * ((int64_t)1 << (19+32)));
90 GLint exp = 0;
91 if (r < 0x80000)
92 return 0;
93 while (r - 0x80000 > 0x7ffff) {
94 r >>= 1;
95 exp++;
96 }
97 return exp > 0x1f ? 0xffffff : (r - 0x80000) | (exp << 19);
98 }
99 #define _1 (int64_t)1
100 static __inline GLdouble savageDecodeFloat24( GLuint x )
101 {
102 static const GLdouble pow2[32] = {
103 1.0/(_1<<51), 1.0/(_1<<50), 1.0/(_1<<49), 1.0/(_1<<48),
104 1.0/(_1<<47), 1.0/(_1<<46), 1.0/(_1<<45), 1.0/(_1<<44),
105 1.0/(_1<<43), 1.0/(_1<<42), 1.0/(_1<<41), 1.0/(_1<<40),
106 1.0/(_1<<39), 1.0/(_1<<38), 1.0/(_1<<37), 1.0/(_1<<36),
107 1.0/(_1<<35), 1.0/(_1<<34), 1.0/(_1<<33), 1.0/(_1<<32),
108 1.0/(_1<<31), 1.0/(_1<<30), 1.0/(_1<<29), 1.0/(_1<<28),
109 1.0/(_1<<27), 1.0/(_1<<26), 1.0/(_1<<25), 1.0/(_1<<24),
110 1.0/(_1<<23), 1.0/(_1<<22), 1.0/(_1<<21), 1.0/(_1<<20)
111 };
112 static const GLdouble bias[32] = {
113 1.0/(_1<<32), 1.0/(_1<<31), 1.0/(_1<<30), 1.0/(_1<<29),
114 1.0/(_1<<28), 1.0/(_1<<27), 1.0/(_1<<26), 1.0/(_1<<25),
115 1.0/(_1<<24), 1.0/(_1<<23), 1.0/(_1<<22), 1.0/(_1<<21),
116 1.0/(_1<<20), 1.0/(_1<<19), 1.0/(_1<<18), 1.0/(_1<<17),
117 1.0/(_1<<16), 1.0/(_1<<15), 1.0/(_1<<14), 1.0/(_1<<13),
118 1.0/(_1<<12), 1.0/(_1<<11), 1.0/(_1<<10), 1.0/(_1<< 9),
119 1.0/(_1<< 8), 1.0/(_1<< 7), 1.0/(_1<< 6), 1.0/(_1<< 5),
120 1.0/(_1<< 4), 1.0/(_1<< 3), 1.0/(_1<< 2), 1.0/(_1<< 1)
121 };
122 GLuint mant = x & 0x7ffff;
123 GLuint exp = (x >> 19) & 0x1f;
124 return bias[exp] + pow2[exp]*mant;
125 }
126 #undef _1
127
128 #endif