2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS 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
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Updated for P6 architecture by Gareth Hughes.
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
38 #include "m_debug_util.h"
41 /* The linker doesn't like empty files */
45 #ifdef DEBUG_MATH /* This code only used for debugging */
48 /* Overhead of profiling counter in cycles. Automatically adjusted to
49 * your machine at run time - counter initialization should give very
52 long counter_overhead
= 0;
54 /* This is the value of the environment variable MESA_PROFILE, and is
55 * used to determine if we should benchmark the functions as well as
56 * verify their correctness.
58 char *mesa_profile
= NULL
;
61 static int m_general
[16] = {
67 static int m_identity
[16] = {
73 static int m_2d
[16] = {
79 static int m_2d_no_rot
[16] = {
85 static int m_3d
[16] = {
91 static int m_3d_no_rot
[16] = {
97 static int m_perspective
[16] = {
103 static int *templates
[7] = {
112 static enum GLmatrixtype mtypes
[7] = {
121 static char *mstrings
[7] = {
125 "MATRIX_PERSPECTIVE",
132 /* =============================================================
133 * Reference transformations
136 static void ref_transform( GLvector4f
*dst
,
138 const GLvector4f
*src
)
141 GLfloat
*s
= (GLfloat
*)src
->start
;
142 GLfloat (*d
)[4] = (GLfloat (*)[4])dst
->start
;
143 const GLfloat
*m
= mat
->m
;
145 for ( i
= 0 ; i
< src
->count
; i
++ ) {
146 TRANSFORM_POINT( d
[i
], m
, s
);
147 s
= (GLfloat
*)((char *)s
+ src
->stride
);
152 /* =============================================================
153 * Vertex transformation tests
156 static void init_matrix( GLfloat
*m
)
158 m
[0] = 63.0; m
[4] = 43.0; m
[ 8] = 29.0; m
[12] = 43.0;
159 m
[1] = 55.0; m
[5] = 17.0; m
[ 9] = 31.0; m
[13] = 7.0;
160 m
[2] = 44.0; m
[6] = 9.0; m
[10] = 7.0; m
[14] = 3.0;
161 m
[3] = 11.0; m
[7] = 23.0; m
[11] = 91.0; m
[15] = 9.0;
164 ALIGN16(static GLfloat
, s
[TEST_COUNT
][4]);
165 ALIGN16(static GLfloat
, d
[TEST_COUNT
][4]);
166 ALIGN16(static GLfloat
, r
[TEST_COUNT
][4]);
168 static int test_transform_function( transform_func func
, int psize
,
169 int mtype
, unsigned long *cycles
)
171 GLvector4f source
[1], dest
[1], ref
[1];
175 #ifdef RUN_DEBUG_BENCHMARK
176 int cycle_i
; /* the counter for the benchmarks we run */
182 _mesa_problem( NULL
, "test_transform_function called with psize > 4\n" );
186 mat
->m
= _mesa_align_malloc( 16 * sizeof(GLfloat
), 16 );
187 mat
->type
= mtypes
[mtype
];
190 assert( ((long)m
& 15) == 0 );
194 for ( i
= 0 ; i
< 4 ; i
++ ) {
195 for ( j
= 0 ; j
< 4 ; j
++ ) {
196 switch ( templates
[mtype
][i
* 4 + j
] ) {
215 for ( i
= 0 ; i
< TEST_COUNT
; i
++) {
216 ASSIGN_4V( d
[i
], 0.0, 0.0, 0.0, 1.0 );
217 ASSIGN_4V( s
[i
], 0.0, 0.0, 0.0, 1.0 );
218 for ( j
= 0 ; j
< psize
; j
++ )
222 source
->data
= (GLfloat(*)[4])s
;
223 source
->start
= (GLfloat
*)s
;
224 source
->count
= TEST_COUNT
;
225 source
->stride
= sizeof(s
[0]);
229 dest
->data
= (GLfloat(*)[4])d
;
230 dest
->start
= (GLfloat
*)d
;
231 dest
->count
= TEST_COUNT
;
232 dest
->stride
= sizeof(float[4]);
236 ref
->data
= (GLfloat(*)[4])r
;
237 ref
->start
= (GLfloat
*)r
;
238 ref
->count
= TEST_COUNT
;
239 ref
->stride
= sizeof(float[4]);
243 ref_transform( ref
, mat
, source
);
245 if ( mesa_profile
) {
246 BEGIN_RACE( *cycles
);
247 func( dest
, mat
->m
, source
);
251 func( dest
, mat
->m
, source
);
254 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
255 for ( j
= 0 ; j
< 4 ; j
++ ) {
256 if ( significand_match( d
[i
][j
], r
[i
][j
] ) < REQUIRED_PRECISION
) {
257 printf("-----------------------------\n" );
258 printf("(i = %i, j = %i)\n", i
, j
);
259 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
260 d
[i
][0], r
[i
][0], r
[i
][0]-d
[i
][0],
261 MAX_PRECISION
- significand_match( d
[i
][0], r
[i
][0] ) );
262 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
263 d
[i
][1], r
[i
][1], r
[i
][1]-d
[i
][1],
264 MAX_PRECISION
- significand_match( d
[i
][1], r
[i
][1] ) );
265 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
266 d
[i
][2], r
[i
][2], r
[i
][2]-d
[i
][2],
267 MAX_PRECISION
- significand_match( d
[i
][2], r
[i
][2] ) );
268 printf("%f \t %f \t [diff = %e - %i bit missed]\n",
269 d
[i
][3], r
[i
][3], r
[i
][3]-d
[i
][3],
270 MAX_PRECISION
- significand_match( d
[i
][3], r
[i
][3] ) );
276 _mesa_align_free( mat
->m
);
280 void _math_test_all_transform_functions( char *description
)
283 unsigned long benchmark_tab
[4][7];
284 static int first_time
= 1;
288 mesa_profile
= getenv( "MESA_PROFILE" );
291 #ifdef RUN_DEBUG_BENCHMARK
292 if ( mesa_profile
) {
293 if ( !counter_overhead
) {
295 printf("counter overhead: %lu cycles\n\n", counter_overhead
);
297 printf("transform results after hooking in %s functions:\n", description
);
301 #ifdef RUN_DEBUG_BENCHMARK
302 if ( mesa_profile
) {
304 for ( psize
= 1 ; psize
<= 4 ; psize
++ ) {
305 printf(" p%d\t", psize
);
307 printf("\n--------------------------------------------------------\n" );
311 for ( mtype
= 0 ; mtype
< 7 ; mtype
++ ) {
312 for ( psize
= 1 ; psize
<= 4 ; psize
++ ) {
313 transform_func func
= _mesa_transform_tab
[psize
][mtypes
[mtype
]];
314 unsigned long *cycles
= &(benchmark_tab
[psize
-1][mtype
]);
316 if ( test_transform_function( func
, psize
, mtype
, cycles
) == 0 ) {
318 sprintf(buf
, "_mesa_transform_tab[0][%d][%s] failed test (%s)",
319 psize
, mstrings
[mtype
], description
);
320 _mesa_problem( NULL
, "%s", buf
);
322 #ifdef RUN_DEBUG_BENCHMARK
324 printf(" %li\t", benchmark_tab
[psize
-1][mtype
] );
327 #ifdef RUN_DEBUG_BENCHMARK
329 printf(" | [%s]\n", mstrings
[mtype
] );
332 #ifdef RUN_DEBUG_BENCHMARK
339 #endif /* DEBUG_MATH */