1 /* $Id: m_debug_util.h,v 1.5 2002/06/29 19:48:17 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Gareth Hughes <gareth@valinux.com>
30 #ifndef __M_DEBUG_UTIL_H__
31 #define __M_DEBUG_UTIL_H__
34 #ifdef DEBUG /* This code only used for debugging */
37 /* Comment this out to deactivate the cycle counter.
38 * NOTE: it works only on CPUs which know the 'rdtsc' command (586 or higher)
39 * (hope, you don't try to debug Mesa on a 386 ;)
41 #if defined(__GNUC__) && \
42 ((defined(__i386__) && defined(USE_X86_ASM)) || \
43 (defined(__sparc__) && defined(USE_SPARC_ASM)))
44 #define RUN_DEBUG_BENCHMARK
47 #define TEST_COUNT 128 /* size of the tested vector array */
49 #define REQUIRED_PRECISION 10 /* allow 4 bits to miss */
50 #define MAX_PRECISION 24 /* max. precision possible */
53 #ifdef RUN_DEBUG_BENCHMARK
54 /* Overhead of profiling counter in cycles. Automatically adjusted to
55 * your machine at run time - counter initialization should give very
58 extern long counter_overhead
;
60 /* This is the value of the environment variable MESA_PROFILE, and is
61 * used to determine if we should benchmark the functions as well as
62 * verify their correctness.
64 extern char *mesa_profile
;
66 /* Modify the the number of tests if you like.
67 * We take the minimum of all results, because every error should be
68 * positive (time used by other processes, task switches etc).
69 * It is assumed that all calculations are done in the cache.
74 #if 1 /* PPro, PII, PIII version */
76 /* Profiling on the P6 architecture requires a little more work, due to
77 * the internal out-of-order execution. We must perform a serializing
78 * 'cpuid' instruction before and after the 'rdtsc' instructions to make
79 * sure no other uops are executed when we sample the timestamp counter.
81 #define INIT_COUNTER() \
84 counter_overhead = LONG_MAX; \
85 for ( cycle_i = 0 ; cycle_i < 8 ; cycle_i++ ) { \
86 long cycle_tmp1 = 0, cycle_tmp2 = 0; \
87 __asm__ __volatile__ ( "push %%ebx \n" \
88 "xor %%eax, %%eax \n" \
92 "xor %%eax, %%eax \n" \
96 "xor %%eax, %%eax \n" \
100 "xor %%eax, %%eax \n" \
103 : "=m" (cycle_tmp1), "=m" (cycle_tmp2) \
104 : : "eax", "ecx", "edx" ); \
105 if ( counter_overhead > (cycle_tmp2 - cycle_tmp1) ) { \
106 counter_overhead = cycle_tmp2 - cycle_tmp1; \
111 #define BEGIN_RACE(x) \
113 for ( cycle_i = 0 ; cycle_i < 10 ; cycle_i++ ) { \
114 long cycle_tmp1 = 0, cycle_tmp2 = 0; \
115 __asm__ __volatile__ ( "push %%ebx \n" \
116 "xor %%eax, %%eax \n" \
120 "xor %%eax, %%eax \n" \
123 : "=m" (cycle_tmp1) \
124 : : "eax", "ecx", "edx" );
126 #define END_RACE(x) \
127 __asm__ __volatile__ ( "push %%ebx \n" \
128 "xor %%eax, %%eax \n" \
132 "xor %%eax, %%eax \n" \
135 : "=m" (cycle_tmp2) \
136 : : "eax", "ecx", "edx" ); \
137 if ( x > (cycle_tmp2 - cycle_tmp1) ) { \
138 x = cycle_tmp2 - cycle_tmp1; \
141 x -= counter_overhead;
143 #else /* PPlain, PMMX version */
145 /* To ensure accurate results, we stall the pipelines with the
146 * non-pairable 'cdq' instruction. This ensures all the code being
147 * profiled is complete when the 'rdtsc' instruction executes.
149 #define INIT_COUNTER(x) \
153 for ( cycle_i = 0 ; cycle_i < 32 ; cycle_i++ ) { \
154 long cycle_tmp1, cycle_tmp2, dummy; \
155 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) ); \
156 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) ); \
159 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) ); \
162 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) ); \
163 if ( x > (cycle_tmp2 - cycle_tmp1) ) \
164 x = cycle_tmp2 - cycle_tmp1; \
168 #define BEGIN_RACE(x) \
170 for ( cycle_i = 0 ; cycle_i < 16 ; cycle_i++ ) { \
171 long cycle_tmp1, cycle_tmp2, dummy; \
172 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) ); \
173 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) ); \
176 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) );
179 #define END_RACE(x) \
182 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) ); \
183 if ( x > (cycle_tmp2 - cycle_tmp1) ) \
184 x = cycle_tmp2 - cycle_tmp1; \
186 x -= counter_overhead;
190 #elif defined(__sparc__)
192 #define INIT_COUNTER() \
193 do { counter_overhead = 5; } while(0)
195 #define BEGIN_RACE(x) \
197 for (cycle_i = 0; cycle_i <10; cycle_i++) { \
198 register long cycle_tmp1 asm("l0"); \
199 register long cycle_tmp2 asm("l1"); \
200 /* rd %tick, %l0 */ \
201 __asm__ __volatile__ (".word 0xa1410000" : "=r" (cycle_tmp1)); /* save timestamp */
203 #define END_RACE(x) \
204 /* rd %tick, %l1 */ \
205 __asm__ __volatile__ (".word 0xa3410000" : "=r" (cycle_tmp2)); \
206 if (x > (cycle_tmp2-cycle_tmp1)) x = cycle_tmp2 - cycle_tmp1; \
208 x -= counter_overhead;
211 #error Your processor is not supported for RUN_XFORM_BENCHMARK
216 #define BEGIN_RACE(x)
222 /* =============================================================
226 static GLfloat
rnd( void )
228 GLfloat f
= (GLfloat
)rand() / (GLfloat
)RAND_MAX
;
229 GLfloat gran
= (GLfloat
)(1 << 13);
231 f
= (GLfloat
)(GLint
)(f
* gran
) / gran
;
233 return f
* 2.0 - 1.0;
236 static int significand_match( GLfloat a
, GLfloat b
)
239 int a_ex
, b_ex
, d_ex
;
242 return MAX_PRECISION
; /* Exact match */
245 if ( a
== 0.0F
|| b
== 0.0F
) {
246 /* It would probably be better to check if the
247 * non-zero number is denormalized and return
248 * the index of the highest set bit here.
264 enum { NIL
= 0, ONE
= 1, NEG
= -1, VAR
= 2 };
266 /* Ensure our arrays are correctly aligned.
268 #if defined(__GNUC__)
269 # define ALIGN16 __attribute__ ((aligned (16)))
270 #elif defined(__MSC__)
271 # define ALIGN16 __declspec(align(16)) /* GH: Does this work? */
273 # warning "ALIGN16 will not 16-byte align!\n"
280 #endif /* __M_DEBUG_UTIL_H__ */