ALIGN16 macro repairs
[mesa.git] / src / mesa / math / m_debug_util.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Gareth Hughes
26 */
27
28 #ifndef __M_DEBUG_UTIL_H__
29 #define __M_DEBUG_UTIL_H__
30
31
32 #ifdef DEBUG /* This code only used for debugging */
33
34
35 /* Comment this out to deactivate the cycle counter.
36 * NOTE: it works only on CPUs which know the 'rdtsc' command (586 or higher)
37 * (hope, you don't try to debug Mesa on a 386 ;)
38 */
39 #if defined(__GNUC__) && \
40 ((defined(__i386__) && defined(USE_X86_ASM)) || \
41 (defined(__sparc__) && defined(USE_SPARC_ASM)))
42 #define RUN_DEBUG_BENCHMARK
43 #endif
44
45 #define TEST_COUNT 128 /* size of the tested vector array */
46
47 #define REQUIRED_PRECISION 10 /* allow 4 bits to miss */
48 #define MAX_PRECISION 24 /* max. precision possible */
49
50
51 #ifdef RUN_DEBUG_BENCHMARK
52 /* Overhead of profiling counter in cycles. Automatically adjusted to
53 * your machine at run time - counter initialization should give very
54 * consistent results.
55 */
56 extern long counter_overhead;
57
58 /* This is the value of the environment variable MESA_PROFILE, and is
59 * used to determine if we should benchmark the functions as well as
60 * verify their correctness.
61 */
62 extern char *mesa_profile;
63
64 /* Modify the the number of tests if you like.
65 * We take the minimum of all results, because every error should be
66 * positive (time used by other processes, task switches etc).
67 * It is assumed that all calculations are done in the cache.
68 */
69
70 #if defined(__i386__)
71
72 #if 1 /* PPro, PII, PIII version */
73
74 /* Profiling on the P6 architecture requires a little more work, due to
75 * the internal out-of-order execution. We must perform a serializing
76 * 'cpuid' instruction before and after the 'rdtsc' instructions to make
77 * sure no other uops are executed when we sample the timestamp counter.
78 */
79 #define INIT_COUNTER() \
80 do { \
81 int cycle_i; \
82 counter_overhead = LONG_MAX; \
83 for ( cycle_i = 0 ; cycle_i < 8 ; cycle_i++ ) { \
84 long cycle_tmp1 = 0, cycle_tmp2 = 0; \
85 __asm__ __volatile__ ( "push %%ebx \n" \
86 "xor %%eax, %%eax \n" \
87 "cpuid \n" \
88 "rdtsc \n" \
89 "mov %%eax, %0 \n" \
90 "xor %%eax, %%eax \n" \
91 "cpuid \n" \
92 "pop %%ebx \n" \
93 "push %%ebx \n" \
94 "xor %%eax, %%eax \n" \
95 "cpuid \n" \
96 "rdtsc \n" \
97 "mov %%eax, %1 \n" \
98 "xor %%eax, %%eax \n" \
99 "cpuid \n" \
100 "pop %%ebx \n" \
101 : "=m" (cycle_tmp1), "=m" (cycle_tmp2) \
102 : : "eax", "ecx", "edx" ); \
103 if ( counter_overhead > (cycle_tmp2 - cycle_tmp1) ) { \
104 counter_overhead = cycle_tmp2 - cycle_tmp1; \
105 } \
106 } \
107 } while (0)
108
109 #define BEGIN_RACE(x) \
110 x = LONG_MAX; \
111 for ( cycle_i = 0 ; cycle_i < 10 ; cycle_i++ ) { \
112 long cycle_tmp1 = 0, cycle_tmp2 = 0; \
113 __asm__ __volatile__ ( "push %%ebx \n" \
114 "xor %%eax, %%eax \n" \
115 "cpuid \n" \
116 "rdtsc \n" \
117 "mov %%eax, %0 \n" \
118 "xor %%eax, %%eax \n" \
119 "cpuid \n" \
120 "pop %%ebx \n" \
121 : "=m" (cycle_tmp1) \
122 : : "eax", "ecx", "edx" );
123
124 #define END_RACE(x) \
125 __asm__ __volatile__ ( "push %%ebx \n" \
126 "xor %%eax, %%eax \n" \
127 "cpuid \n" \
128 "rdtsc \n" \
129 "mov %%eax, %0 \n" \
130 "xor %%eax, %%eax \n" \
131 "cpuid \n" \
132 "pop %%ebx \n" \
133 : "=m" (cycle_tmp2) \
134 : : "eax", "ecx", "edx" ); \
135 if ( x > (cycle_tmp2 - cycle_tmp1) ) { \
136 x = cycle_tmp2 - cycle_tmp1; \
137 } \
138 } \
139 x -= counter_overhead;
140
141 #else /* PPlain, PMMX version */
142
143 /* To ensure accurate results, we stall the pipelines with the
144 * non-pairable 'cdq' instruction. This ensures all the code being
145 * profiled is complete when the 'rdtsc' instruction executes.
146 */
147 #define INIT_COUNTER(x) \
148 do { \
149 int cycle_i; \
150 x = LONG_MAX; \
151 for ( cycle_i = 0 ; cycle_i < 32 ; cycle_i++ ) { \
152 long cycle_tmp1, cycle_tmp2, dummy; \
153 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) ); \
154 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) ); \
155 __asm__ ( "cdq" ); \
156 __asm__ ( "cdq" ); \
157 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) ); \
158 __asm__ ( "cdq" ); \
159 __asm__ ( "cdq" ); \
160 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) ); \
161 if ( x > (cycle_tmp2 - cycle_tmp1) ) \
162 x = cycle_tmp2 - cycle_tmp1; \
163 } \
164 } while (0)
165
166 #define BEGIN_RACE(x) \
167 x = LONG_MAX; \
168 for ( cycle_i = 0 ; cycle_i < 16 ; cycle_i++ ) { \
169 long cycle_tmp1, cycle_tmp2, dummy; \
170 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) ); \
171 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) ); \
172 __asm__ ( "cdq" ); \
173 __asm__ ( "cdq" ); \
174 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) );
175
176
177 #define END_RACE(x) \
178 __asm__ ( "cdq" ); \
179 __asm__ ( "cdq" ); \
180 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) ); \
181 if ( x > (cycle_tmp2 - cycle_tmp1) ) \
182 x = cycle_tmp2 - cycle_tmp1; \
183 } \
184 x -= counter_overhead;
185
186 #endif
187
188 #elif defined(__sparc__)
189
190 #define INIT_COUNTER() \
191 do { counter_overhead = 5; } while(0)
192
193 #define BEGIN_RACE(x) \
194 x = LONG_MAX; \
195 for (cycle_i = 0; cycle_i <10; cycle_i++) { \
196 register long cycle_tmp1 asm("l0"); \
197 register long cycle_tmp2 asm("l1"); \
198 /* rd %tick, %l0 */ \
199 __asm__ __volatile__ (".word 0xa1410000" : "=r" (cycle_tmp1)); /* save timestamp */
200
201 #define END_RACE(x) \
202 /* rd %tick, %l1 */ \
203 __asm__ __volatile__ (".word 0xa3410000" : "=r" (cycle_tmp2)); \
204 if (x > (cycle_tmp2-cycle_tmp1)) x = cycle_tmp2 - cycle_tmp1; \
205 } \
206 x -= counter_overhead;
207
208 #else
209 #error Your processor is not supported for RUN_XFORM_BENCHMARK
210 #endif
211
212 #else
213
214 #define BEGIN_RACE(x)
215 #define END_RACE(x)
216
217 #endif
218
219
220 /* =============================================================
221 * Helper functions
222 */
223
224 static GLfloat rnd( void )
225 {
226 GLfloat f = (GLfloat)rand() / (GLfloat)RAND_MAX;
227 GLfloat gran = (GLfloat)(1 << 13);
228
229 f = (GLfloat)(GLint)(f * gran) / gran;
230
231 return f * 2.0 - 1.0;
232 }
233
234 static int significand_match( GLfloat a, GLfloat b )
235 {
236 GLfloat d = a - b;
237 int a_ex, b_ex, d_ex;
238
239 if ( d == 0.0F ) {
240 return MAX_PRECISION; /* Exact match */
241 }
242
243 if ( a == 0.0F || b == 0.0F ) {
244 /* It would probably be better to check if the
245 * non-zero number is denormalized and return
246 * the index of the highest set bit here.
247 */
248 return 0;
249 }
250
251 frexp( a, &a_ex );
252 frexp( b, &b_ex );
253 frexp( d, &d_ex );
254
255 if ( a_ex < b_ex ) {
256 return a_ex - d_ex;
257 } else {
258 return b_ex - d_ex;
259 }
260 }
261
262 enum { NIL = 0, ONE = 1, NEG = -1, VAR = 2 };
263
264 /* Ensure our arrays are correctly aligned.
265 */
266 #if defined(__GNUC__)
267 # define ALIGN16(type, array) type array __attribute__ ((aligned (16)))
268 #elif defined(__MSC__)
269 # define ALIGN16(type, array) type array __declspec(align(16)) /* GH: Does this work? */
270 #elif defined(__WATCOMC__)
271 # define ALIGN16(type, array) /* Watcom does not support this */
272 #elif defined(__xlC__)
273 # define ALIGN16(type, array) type __align (16) array
274 #else
275 # warning "ALIGN16 will not 16-byte align!\n"
276 # define ALIGN16
277 #endif
278
279
280 #endif /* DEBUG */
281
282 #endif /* __M_DEBUG_UTIL_H__ */