summaryrefslogtreecommitdiffstats
path: root/src/mesa/math/m_debug_util.h
blob: e169034453042b37eeb9acfdf3db5bbfcea7b688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

/*
 * Mesa 3-D graphics library
 * Version:  3.5
 *
 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Gareth Hughes
 */

#ifndef __M_DEBUG_UTIL_H__
#define __M_DEBUG_UTIL_H__


#ifdef DEBUG  /* This code only used for debugging */


/* Comment this out to deactivate the cycle counter.
 * NOTE: it works only on CPUs which know the 'rdtsc' command (586 or higher)
 * (hope, you don't try to debug Mesa on a 386 ;)
 */
#if defined(__GNUC__) && \
    ((defined(__i386__) && defined(USE_X86_ASM)) || \
     (defined(__sparc__) && defined(USE_SPARC_ASM)))
#define  RUN_DEBUG_BENCHMARK
#endif

#define TEST_COUNT		128	/* size of the tested vector array   */

#define REQUIRED_PRECISION	10	/* allow 4 bits to miss              */
#define MAX_PRECISION		24	/* max. precision possible           */


#ifdef  RUN_DEBUG_BENCHMARK
/* Overhead of profiling counter in cycles.  Automatically adjusted to
 * your machine at run time - counter initialization should give very
 * consistent results.
 */
extern long counter_overhead;

/* This is the value of the environment variable MESA_PROFILE, and is
 * used to determine if we should benchmark the functions as well as
 * verify their correctness.
 */
extern char *mesa_profile;

/* Modify the the number of tests if you like.
 * We take the minimum of all results, because every error should be
 * positive (time used by other processes, task switches etc).
 * It is assumed that all calculations are done in the cache.
 */

#if defined(__i386__)

#if 1 /* PPro, PII, PIII version */

/* Profiling on the P6 architecture requires a little more work, due to
 * the internal out-of-order execution.  We must perform a serializing
 * 'cpuid' instruction before and after the 'rdtsc' instructions to make
 * sure no other uops are executed when we sample the timestamp counter.
 */
#define  INIT_COUNTER()							\
   do {									\
      int cycle_i;							\
      counter_overhead = LONG_MAX;					\
      for ( cycle_i = 0 ; cycle_i < 8 ; cycle_i++ ) {			\
	 long cycle_tmp1 = 0, cycle_tmp2 = 0;				\
	 __asm__ __volatile__ ( "push %%ebx       \n"			\
				"xor %%eax, %%eax \n"			\
				"cpuid            \n"			\
				"rdtsc            \n"			\
				"mov %%eax, %0    \n"			\
				"xor %%eax, %%eax \n"			\
				"cpuid            \n"			\
				"pop %%ebx        \n"			\
				"push %%ebx       \n"			\
				"xor %%eax, %%eax \n"			\
				"cpuid            \n"			\
				"rdtsc            \n"			\
				"mov %%eax, %1    \n"			\
				"xor %%eax, %%eax \n"			\
				"cpuid            \n"			\
				"pop %%ebx        \n"			\
				: "=m" (cycle_tmp1), "=m" (cycle_tmp2)	\
				: : "eax", "ecx", "edx" );		\
	 if ( counter_overhead > (cycle_tmp2 - cycle_tmp1) ) {		\
	    counter_overhead = cycle_tmp2 - cycle_tmp1;			\
	 }								\
      }									\
   } while (0)

#define  BEGIN_RACE(x)							\
   x = LONG_MAX;							\
   for ( cycle_i = 0 ; cycle_i < 10 ; cycle_i++ ) {			\
      long cycle_tmp1 = 0, cycle_tmp2 = 0;				\
      __asm__ __volatile__ ( "push %%ebx       \n"			\
			     "xor %%eax, %%eax \n"			\
			     "cpuid            \n"			\
			     "rdtsc            \n"			\
			     "mov %%eax, %0    \n"			\
			     "xor %%eax, %%eax \n"			\
			     "cpuid            \n"			\
			     "pop %%ebx        \n"			\
			     : "=m" (cycle_tmp1)			\
			     : : "eax", "ecx", "edx" );

#define END_RACE(x)							\
      __asm__ __volatile__ ( "push %%ebx       \n"			\
			     "xor %%eax, %%eax \n"			\
			     "cpuid            \n"			\
			     "rdtsc            \n"			\
			     "mov %%eax, %0    \n"			\
			     "xor %%eax, %%eax \n"			\
			     "cpuid            \n"			\
			     "pop %%ebx        \n"			\
			     : "=m" (cycle_tmp2)			\
			     : : "eax", "ecx", "edx" );			\
      if ( x > (cycle_tmp2 - cycle_tmp1) ) {				\
	 x = cycle_tmp2 - cycle_tmp1;					\
      }									\
   }									\
   x -= counter_overhead;

#else /* PPlain, PMMX version */

/* To ensure accurate results, we stall the pipelines with the
 * non-pairable 'cdq' instruction.  This ensures all the code being
 * profiled is complete when the 'rdtsc' instruction executes.
 */
#define  INIT_COUNTER(x)						\
   do {									\
      int cycle_i;							\
      x = LONG_MAX;							\
      for ( cycle_i = 0 ; cycle_i < 32 ; cycle_i++ ) {			\
	 long cycle_tmp1, cycle_tmp2, dummy;				\
	 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) );		\
	 __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) );		\
	 __asm__ ( "cdq" );						\
	 __asm__ ( "cdq" );						\
	 __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) );		\
	 __asm__ ( "cdq" );						\
	 __asm__ ( "cdq" );						\
	 __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) );		\
	 if ( x > (cycle_tmp2 - cycle_tmp1) )				\
	    x = cycle_tmp2 - cycle_tmp1;				\
      }									\
   } while (0)

#define  BEGIN_RACE(x)							\
   x = LONG_MAX;							\
   for ( cycle_i = 0 ; cycle_i < 16 ; cycle_i++ ) {			\
      long cycle_tmp1, cycle_tmp2, dummy;				\
      __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp1) );			\
      __asm__ ( "mov %%eax, %0" : "=a" (cycle_tmp2) );			\
      __asm__ ( "cdq" );						\
      __asm__ ( "cdq" );						\
      __asm__ ( "rdtsc" : "=a" (cycle_tmp1), "=d" (dummy) );


#define END_RACE(x)							\
      __asm__ ( "cdq" );						\
      __asm__ ( "cdq" );						\
      __asm__ ( "rdtsc" : "=a" (cycle_tmp2), "=d" (dummy) );		\
      if ( x > (cycle_tmp2 - cycle_tmp1) )				\
	 x = cycle_tmp2 - cycle_tmp1;					\
   }									\
   x -= counter_overhead;

#endif

#elif defined(__sparc__)

#define  INIT_COUNTER()	\
	 do { counter_overhead = 5; } while(0)

#define  BEGIN_RACE(x)                                                        \
x = LONG_MAX;                                                                 \
for (cycle_i = 0; cycle_i <10; cycle_i++) {                                   \
   register long cycle_tmp1 asm("l0");					      \
   register long cycle_tmp2 asm("l1");					      \
   /* rd %tick, %l0 */							      \
   __asm__ __volatile__ (".word 0xa1410000" : "=r" (cycle_tmp1));  /*  save timestamp   */

#define END_RACE(x)                                                           \
   /* rd %tick, %l1 */							      \
   __asm__ __volatile__ (".word 0xa3410000" : "=r" (cycle_tmp2));	      \
   if (x > (cycle_tmp2-cycle_tmp1)) x = cycle_tmp2 - cycle_tmp1;              \
}                                                                             \
x -= counter_overhead;

#else
#error Your processor is not supported for RUN_XFORM_BENCHMARK
#endif

#else

#define BEGIN_RACE(x)
#define END_RACE(x)

#endif


/* =============================================================
 * Helper functions
 */

static GLfloat rnd( void )
{
   GLfloat f = (GLfloat)rand() / (GLfloat)RAND_MAX;
   GLfloat gran = (GLfloat)(1 << 13);

   f = (GLfloat)(GLint)(f * gran) / gran;

   return f * 2.0 - 1.0;
}

static int significand_match( GLfloat a, GLfloat b )
{
   GLfloat d = a - b;
   int a_ex, b_ex, d_ex;

   if ( d == 0.0F ) {
      return MAX_PRECISION;   /* Exact match */
   }

   if ( a == 0.0F || b == 0.0F ) {
      /* It would probably be better to check if the
       * non-zero number is denormalized and return
       * the index of the highest set bit here.
       */
      return 0;
   }

   frexp( a, &a_ex );
   frexp( b, &b_ex );
   frexp( d, &d_ex );

   if ( a_ex < b_ex ) {
      return a_ex - d_ex;
   } else {
      return b_ex - d_ex;
   }
}

enum { NIL = 0, ONE = 1, NEG = -1, VAR = 2 };

/* Ensure our arrays are correctly aligned.
 */
#if defined(__GNUC__)
#  define ALIGN16	type array __attribute__ ((aligned (16)))
#elif defined(__MSC__)
#  define ALIGN16	type array __declspec(align(16)) /* GH: Does this work? */
#elif defined(__WATCOMC__)
#  define ALIGN16	                    /* Watcom does not support this */ 
#elif defined(__xlC__)
#  define ALIGN16(type, array)       type __align (16) array 
#else
#  warning "ALIGN16 will not 16-byte align!\n"
#  define ALIGN16
#endif


#endif /* DEBUG */

#endif /* __M_DEBUG_UTIL_H__ */