1 /* $Id: m_debug_norm.c,v 1.6 2001/03/29 06:46:27 gareth 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>
40 #include "m_debug_util.h"
43 #ifdef DEBUG /* This code only used for debugging */
46 static int m_norm_identity
[16] = {
52 static int m_norm_general
[16] = {
58 static int m_norm_no_rot
[16] = {
64 static int *norm_templates
[8] = {
74 static int norm_types
[8] = {
75 NORM_TRANSFORM_NO_ROT
,
76 NORM_TRANSFORM_NO_ROT
| NORM_RESCALE
,
77 NORM_TRANSFORM_NO_ROT
| NORM_NORMALIZE
,
79 NORM_TRANSFORM
| NORM_RESCALE
,
80 NORM_TRANSFORM
| NORM_NORMALIZE
,
84 static int norm_scale_types
[8] = { /* rescale factor */
85 NIL
, /* NIL disables rescaling */
94 static int norm_normalize_types
[8] = { /* normalizing ?? (no = 0) */
104 static char *norm_strings
[8] = {
105 "NORM_TRANSFORM_NO_ROT",
106 "NORM_TRANSFORM_NO_ROT | NORM_RESCALE",
107 "NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE",
109 "NORM_TRANSFORM | NORM_RESCALE",
110 "NORM_TRANSFORM | NORM_NORMALIZE",
116 /* ================================================================
117 * Reference transformations
120 static void ref_norm_transform_rescale( const GLmatrix
*mat
,
122 const GLvector3f
*in
,
123 const GLfloat
*lengths
,
124 const GLubyte mask
[],
128 const GLfloat
*s
= in
->start
;
129 const GLfloat
*m
= mat
->inv
;
130 GLfloat (*out
)[3] = (GLfloat (*)[3])dest
->start
;
135 for ( i
= 0 ; i
< in
->count
; i
++ ) {
138 TRANSFORM_NORMAL( t
, s
, m
);
139 SCALE_SCALAR_3V( out
[i
], scale
, t
);
141 s
= (GLfloat
*)((char *)s
+ in
->stride
);
145 static void ref_norm_transform_normalize( const GLmatrix
*mat
,
147 const GLvector3f
*in
,
148 const GLfloat
*lengths
,
149 const GLubyte mask
[],
153 const GLfloat
*s
= in
->start
;
154 const GLfloat
*m
= mat
->inv
;
155 GLfloat (*out
)[3] = (GLfloat (*)[3])dest
->start
;
159 for ( i
= 0 ; i
< in
->count
; i
++ ) {
162 TRANSFORM_NORMAL( t
, s
, m
);
165 GLfloat len
= LEN_SQUARED_3FV( t
);
167 /* Hmmm, don't know how we could test the precalculated
170 scale
= 1.0 / sqrt( len
);
171 SCALE_SCALAR_3V( out
[i
], scale
, t
);
173 out
[i
][0] = out
[i
][1] = out
[i
][2] = 0;
177 SCALE_SCALAR_3V( out
[i
], scale
, t
);
180 s
= (GLfloat
*)((char *)s
+ in
->stride
);
185 /* ================================================================
186 * Normal transformation tests
189 static int test_norm_function( normal_func func
, int mtype
, long *cycles
)
191 GLvector3f source
[1], dest
[1], dest2
[1], ref
[1], ref2
[1];
193 GLfloat s
[TEST_COUNT
][5], d
[TEST_COUNT
][3], r
[TEST_COUNT
][3];
194 GLfloat d2
[TEST_COUNT
][3], r2
[TEST_COUNT
][3], length
[TEST_COUNT
];
198 #ifdef RUN_DEBUG_BENCHMARK
199 int cycle_i
; /* the counter for the benchmarks we run */
204 mat
->m
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
205 mat
->inv
= m
= mat
->m
;
209 scale
= 1.0F
+ rnd () * norm_scale_types
[mtype
];
211 for ( i
= 0 ; i
< 4 ; i
++ ) {
212 for ( j
= 0 ; j
< 4 ; j
++ ) {
213 switch ( norm_templates
[mtype
][i
* 4 + j
] ) {
231 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
232 ASSIGN_3V( d
[i
], 0.0, 0.0, 0.0 );
233 ASSIGN_3V( s
[i
], 0.0, 0.0, 0.0 );
234 ASSIGN_3V( d2
[i
], 0.0, 0.0, 0.0 );
235 for ( j
= 0 ; j
< 3 ; j
++ )
237 length
[i
] = 1 / sqrt( LEN_SQUARED_3FV( s
[i
] ) );
240 source
->data
= (GLfloat(*)[3])s
;
241 source
->start
= (GLfloat
*)s
;
242 source
->count
= TEST_COUNT
;
243 source
->stride
= sizeof(s
[0]);
246 dest
->data
= (GLfloat(*)[3])d
;
247 dest
->start
= (GLfloat
*)d
;
248 dest
->count
= TEST_COUNT
;
249 dest
->stride
= sizeof(float[3]);
252 dest2
->data
= (GLfloat(*)[3])d2
;
253 dest2
->start
= (GLfloat
*)d2
;
254 dest2
->count
= TEST_COUNT
;
255 dest2
->stride
= sizeof(float[3]);
258 ref
->data
= (GLfloat(*)[3])r
;
259 ref
->start
= (GLfloat
*)r
;
260 ref
->count
= TEST_COUNT
;
261 ref
->stride
= sizeof(float[3]);
264 ref2
->data
= (GLfloat(*)[3])r2
;
265 ref2
->start
= (GLfloat
*)r2
;
266 ref2
->count
= TEST_COUNT
;
267 ref2
->stride
= sizeof(float[3]);
270 if ( norm_normalize_types
[mtype
] == 0 ) {
271 ref_norm_transform_rescale( mat
, scale
, source
, NULL
, NULL
, ref
);
273 ref_norm_transform_normalize( mat
, scale
, source
, NULL
, NULL
, ref
);
274 ref_norm_transform_normalize( mat
, scale
, source
, length
, NULL
, ref2
);
277 if ( mesa_profile
) {
278 BEGIN_RACE( *cycles
);
279 func( mat
, scale
, source
, NULL
, NULL
, dest
);
281 func( mat
, scale
, source
, length
, NULL
, dest2
);
283 func( mat
, scale
, source
, NULL
, NULL
, dest
);
284 func( mat
, scale
, source
, length
, NULL
, dest2
);
287 for ( i
= 0 ; i
< TEST_COUNT
; i
++ ) {
288 for ( j
= 0 ; j
< 3 ; j
++ ) {
289 if ( significand_match( d
[i
][j
], r
[i
][j
] ) < REQUIRED_PRECISION
) {
290 printf( "-----------------------------\n" );
291 printf( "(i = %i, j = %i)\n", i
, j
);
292 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
293 d
[i
][0], r
[i
][0], r
[i
][0]/d
[i
][0],
294 MAX_PRECISION
- significand_match( d
[i
][0], r
[i
][0] ) );
295 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
296 d
[i
][1], r
[i
][1], r
[i
][1]/d
[i
][1],
297 MAX_PRECISION
- significand_match( d
[i
][1], r
[i
][1] ) );
298 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
299 d
[i
][2], r
[i
][2], r
[i
][2]/d
[i
][2],
300 MAX_PRECISION
- significand_match( d
[i
][2], r
[i
][2] ) );
304 if ( norm_normalize_types
[mtype
] != 0 ) {
305 if ( significand_match( d2
[i
][j
], r2
[i
][j
] ) < REQUIRED_PRECISION
) {
306 printf( "------------------- precalculated length case ------\n" );
307 printf( "(i = %i, j = %i)\n", i
, j
);
308 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
309 d2
[i
][0], r2
[i
][0], r2
[i
][0]/d2
[i
][0],
310 MAX_PRECISION
- significand_match( d2
[i
][0], r2
[i
][0] ) );
311 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
312 d2
[i
][1], r2
[i
][1], r2
[i
][1]/d2
[i
][1],
313 MAX_PRECISION
- significand_match( d2
[i
][1], r2
[i
][1] ) );
314 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
315 d2
[i
][2], r2
[i
][2], r2
[i
][2]/d2
[i
][2],
316 MAX_PRECISION
- significand_match( d2
[i
][2], r2
[i
][2] ) );
323 ALIGN_FREE( mat
->m
);
327 void _math_test_all_normal_transform_functions( char *description
)
330 long benchmark_tab
[0xf][0x4];
331 static int first_time
= 1;
335 mesa_profile
= getenv( "MESA_PROFILE" );
338 #ifdef RUN_DEBUG_BENCHMARK
339 if ( mesa_profile
) {
340 if ( !counter_overhead
) {
342 printf( "counter overhead: %ld cycles\n\n", counter_overhead
);
344 printf( "normal transform results after hooking in %s functions:\n",
346 printf( "\n-------------------------------------------------------\n" );
350 for ( mtype
= 0 ; mtype
< 8 ; mtype
++ ) {
351 normal_func func
= _mesa_normal_tab
[norm_types
[mtype
]][0];
352 long *cycles
= &(benchmark_tab
[mtype
][0]);
354 if ( test_norm_function( func
, mtype
, cycles
) == 0 ) {
356 sprintf( buf
, "_mesa_normal_tab[0][%s] failed test (%s)",
357 norm_strings
[mtype
], description
);
358 _mesa_problem( NULL
, buf
);
361 #ifdef RUN_DEBUG_BENCHMARK
362 if ( mesa_profile
) {
363 printf( " %li\t", benchmark_tab
[mtype
][0] );
364 printf( " | [%s]\n", norm_strings
[mtype
] );
368 #ifdef RUN_DEBUG_BENCHMARK
369 if ( mesa_profile
) {