2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 * Updated for P6 architecture by Gareth Hughes.
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/macros.h"
33 #include "main/imports.h"
39 #include "m_debug_util.h"
42 /* The linker doesn't like empty files */
46 #ifdef DEBUG_MATH /* This code only used for debugging */
49 /* Overhead of profiling counter in cycles. Automatically adjusted to
50 * your machine at run time - counter initialization should give very
53 long counter_overhead
= 0;
55 /* This is the value of the environment variable MESA_PROFILE, and is
56 * used to determine if we should benchmark the functions as well as
57 * verify their correctness.
59 char *mesa_profile
= NULL
;
62 static int m_general
[16] = {
68 static int m_identity
[16] = {
74 static int m_2d
[16] = {
80 static int m_2d_no_rot
[16] = {
86 static int m_3d
[16] = {
92 static int m_3d_no_rot
[16] = {
98 static int m_perspective
[16] = {
104 static int *templates
[7] = {
113 static enum GLmatrixtype mtypes
[7] = {
122 static char *mstrings
[7] = {
126 "MATRIX_PERSPECTIVE",
133 /* =============================================================
134 * Reference transformations
137 static void ref_transform( GLvector4f
*dst
,
139 const GLvector4f
*src
)
142 GLfloat
*s
= (GLfloat
*)src
->start
;
143 GLfloat (*d
)[4] = (GLfloat (*)[4])dst
->start
;
144 const GLfloat
*m
= mat
->m
;
146 for ( i
= 0 ; i
< src
->count
; i
++ ) {
147 TRANSFORM_POINT( d
[i
], m
, s
);
148 s
= (GLfloat
*)((char *)s
+ src
->stride
);
153 /* =============================================================
154 * Vertex transformation tests
157 static void init_matrix( GLfloat
*m
)
159 m
[0] = 63.0; m
[4] = 43.0; m
[ 8] = 29.0; m
[12] = 43.0;
160 m
[1] = 55.0; m
[5] = 17.0; m
[ 9] = 31.0; m
[13] = 7.0;
161 m
[2] = 44.0; m
[6] = 9.0; m
[10] = 7.0; m
[14] = 3.0;
162 m
[3] = 11.0; m
[7] = 23.0; m
[11] = 91.0; m
[15] = 9.0;
165 ALIGN16(static GLfloat
, s
[TEST_COUNT
][4]);
166 ALIGN16(static GLfloat
, d
[TEST_COUNT
][4]);
167 ALIGN16(static GLfloat
, r
[TEST_COUNT
][4]);
169 static int test_transform_function( transform_func func
, int psize
,
170 int mtype
, unsigned long *cycles
)
172 GLvector4f source
[1], dest
[1], ref
[1];
176 #ifdef RUN_DEBUG_BENCHMARK
177 int cycle_i
; /* the counter for the benchmarks we run */
183 _mesa_problem( NULL
, "test_transform_function called with psize > 4\n" );
187 mat
->m
= _mesa_align_malloc( 16 * sizeof(GLfloat
), 16 );
188 mat
->type
= mtypes
[mtype
];
191 ASSERT( ((long)m
& 15) == 0 );
195 for ( i
= 0 ; i
< 4 ; i
++ ) {
196 for ( j
= 0 ; j
< 4 ; j
++ ) {
197 switch ( templates
[mtype
][i
* 4 + j
] ) {
216 for ( i
= 0 ; i
< TEST_COUNT
; i
++) {
217 ASSIGN_4V( d
[i
], 0.0, 0.0, 0.0, 1.0 );
218 ASSIGN_4V( s
[i
], 0.0, 0.0, 0.0, 1.0 );
219 for ( j
= 0 ; j
< psize
; j
++ )
223 source
->data
= (GLfloat(*)[4])s
;
224 source
->start
= (GLfloat
*)s
;
225 source
->count
= TEST_COUNT
;
226 source
->stride
= sizeof(s
[0]);
230 dest
->data
= (GLfloat(*)[4])d
;
231 dest
->start
= (GLfloat
*)d
;
232 dest
->count
= TEST_COUNT
;
233 dest
->stride
= sizeof(float[4]);
237 ref
->data
= (GLfloat(*)[4])r
;
238 ref
->start
= (GLfloat
*)r
;
239 ref
->count
= TEST_COUNT
;
240 ref
->stride
= sizeof(float[4]);
244 ref_transform( ref
, mat
, source
);
246 if ( mesa_profile
) {
247 BEGIN_RACE( *cycles
);
248 func( dest
, mat
->m
, source
);
252 func( dest
, mat
->m
, source
);
255 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
256 for ( j
= 0 ; j
< 4 ; j
++ ) {
257 if ( significand_match( d
[i
][j
], r
[i
][j
] ) < REQUIRED_PRECISION
) {
258 printf("-----------------------------\n" );
259 printf("(i = %i, j = %i)\n", i
, j
);
260 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
261 d
[i
][0], r
[i
][0], r
[i
][0]-d
[i
][0],
262 MAX_PRECISION
- significand_match( d
[i
][0], r
[i
][0] ) );
263 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
264 d
[i
][1], r
[i
][1], r
[i
][1]-d
[i
][1],
265 MAX_PRECISION
- significand_match( d
[i
][1], r
[i
][1] ) );
266 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
267 d
[i
][2], r
[i
][2], r
[i
][2]-d
[i
][2],
268 MAX_PRECISION
- significand_match( d
[i
][2], r
[i
][2] ) );
269 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
270 d
[i
][3], r
[i
][3], r
[i
][3]-d
[i
][3],
271 MAX_PRECISION
- significand_match( d
[i
][3], r
[i
][3] ) );
277 _mesa_align_free( mat
->m
);
281 void _math_test_all_transform_functions( char *description
)
284 unsigned long benchmark_tab
[4][7];
285 static int first_time
= 1;
289 mesa_profile
= _mesa_getenv( "MESA_PROFILE" );
292 #ifdef RUN_DEBUG_BENCHMARK
293 if ( mesa_profile
) {
294 if ( !counter_overhead
) {
296 printf("counter overhead: %lu cycles\n\n", counter_overhead
);
298 printf("transform results after hooking in %s functions:\n", description
);
302 #ifdef RUN_DEBUG_BENCHMARK
303 if ( mesa_profile
) {
305 for ( psize
= 1 ; psize
<= 4 ; psize
++ ) {
306 printf(" p%d\t", psize
);
308 printf("\n--------------------------------------------------------\n" );
312 for ( mtype
= 0 ; mtype
< 7 ; mtype
++ ) {
313 for ( psize
= 1 ; psize
<= 4 ; psize
++ ) {
314 transform_func func
= _mesa_transform_tab
[psize
][mtypes
[mtype
]];
315 unsigned long *cycles
= &(benchmark_tab
[psize
-1][mtype
]);
317 if ( test_transform_function( func
, psize
, mtype
, cycles
) == 0 ) {
319 sprintf(buf
, "_mesa_transform_tab[0][%d][%s] failed test (%s)",
320 psize
, mstrings
[mtype
], description
);
321 _mesa_problem( NULL
, "%s", buf
);
323 #ifdef RUN_DEBUG_BENCHMARK
325 printf(" %li\t", benchmark_tab
[psize
-1][mtype
] );
328 #ifdef RUN_DEBUG_BENCHMARK
330 printf(" | [%s]\n", mstrings
[mtype
] );
333 #ifdef RUN_DEBUG_BENCHMARK
340 #endif /* DEBUG_MATH */