3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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"
42 /* The linker doesn't like empty files */
46 #ifdef DEBUG_MATH /* This code only used for debugging */
49 static int m_norm_identity[16] = {
55 static int m_norm_general[16] = {
61 static int m_norm_no_rot[16] = {
67 static int *norm_templates[8] = {
77 static int norm_types[8] = {
78 NORM_TRANSFORM_NO_ROT,
79 NORM_TRANSFORM_NO_ROT | NORM_RESCALE,
80 NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE,
82 NORM_TRANSFORM | NORM_RESCALE,
83 NORM_TRANSFORM | NORM_NORMALIZE,
87 static int norm_scale_types[8] = { /* rescale factor */
88 NIL, /* NIL disables rescaling */
97 static int norm_normalize_types[8] = { /* normalizing ?? (no = 0) */
107 static char *norm_strings[8] = {
108 "NORM_TRANSFORM_NO_ROT",
109 "NORM_TRANSFORM_NO_ROT | NORM_RESCALE",
110 "NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE",
112 "NORM_TRANSFORM | NORM_RESCALE",
113 "NORM_TRANSFORM | NORM_NORMALIZE",
119 /* =============================================================
120 * Reference transformations
123 static void ref_norm_transform_rescale( const GLmatrix *mat,
125 const GLvector4f *in,
126 const GLfloat *lengths,
130 const GLfloat *s = in->start;
131 const GLfloat *m = mat->inv;
132 GLfloat (*out)[4] = (GLfloat (*)[4]) dest->start;
136 for ( i = 0 ; i < in->count ; i++ ) {
139 TRANSFORM_NORMAL( t, s, m );
140 SCALE_SCALAR_3V( out[i], scale, t );
142 s = (GLfloat *)((char *)s + in->stride);
146 static void ref_norm_transform_normalize( const GLmatrix *mat,
148 const GLvector4f *in,
149 const GLfloat *lengths,
153 const GLfloat *s = in->start;
154 const GLfloat *m = mat->inv;
155 GLfloat (*out)[4] = (GLfloat (*)[4]) dest->start;
157 for ( i = 0 ; i < in->count ; i++ ) {
160 TRANSFORM_NORMAL( t, s, m );
163 GLfloat len = LEN_SQUARED_3FV( t );
165 /* Hmmm, don't know how we could test the precalculated
168 scale = 1.0 / SQRTF( len );
169 SCALE_SCALAR_3V( out[i], scale, t );
171 out[i][0] = out[i][1] = out[i][2] = 0;
175 SCALE_SCALAR_3V( out[i], scale, t );
178 s = (GLfloat *)((char *)s + in->stride);
183 /* =============================================================
184 * Normal transformation tests
187 static void init_matrix( GLfloat *m )
189 m[0] = 63.0; m[4] = 43.0; m[ 8] = 29.0; m[12] = 43.0;
190 m[1] = 55.0; m[5] = 17.0; m[ 9] = 31.0; m[13] = 7.0;
191 m[2] = 44.0; m[6] = 9.0; m[10] = 7.0; m[14] = 3.0;
192 m[3] = 11.0; m[7] = 23.0; m[11] = 91.0; m[15] = 9.0;
196 static int test_norm_function( normal_func func, int mtype, long *cycles )
198 GLvector4f source[1], dest[1], dest2[1], ref[1], ref2[1];
200 GLfloat s[TEST_COUNT][5], d[TEST_COUNT][4], r[TEST_COUNT][4];
201 GLfloat d2[TEST_COUNT][4], r2[TEST_COUNT][4], length[TEST_COUNT];
205 #ifdef RUN_DEBUG_BENCHMARK
206 int cycle_i; /* the counter for the benchmarks we run */
211 mat->m = (GLfloat *) _mesa_align_malloc( 16 * sizeof(GLfloat), 16 );
212 mat->inv = m = mat->m;
216 scale = 1.0F + rnd () * norm_scale_types[mtype];
218 for ( i = 0 ; i < 4 ; i++ ) {
219 for ( j = 0 ; j < 4 ; j++ ) {
220 switch ( norm_templates[mtype][i * 4 + j] ) {
238 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
239 ASSIGN_3V( d[i], 0.0, 0.0, 0.0 );
240 ASSIGN_3V( s[i], 0.0, 0.0, 0.0 );
241 ASSIGN_3V( d2[i], 0.0, 0.0, 0.0 );
242 for ( j = 0 ; j < 3 ; j++ )
244 length[i] = 1 / SQRTF( LEN_SQUARED_3FV( s[i] ) );
247 source->data = (GLfloat(*)[4]) s;
248 source->start = (GLfloat *) s;
249 source->count = TEST_COUNT;
250 source->stride = sizeof(s[0]);
254 dest->start = (GLfloat *) d;
255 dest->count = TEST_COUNT;
256 dest->stride = sizeof(float[4]);
260 dest2->start = (GLfloat *) d2;
261 dest2->count = TEST_COUNT;
262 dest2->stride = sizeof(float[4]);
266 ref->start = (GLfloat *) r;
267 ref->count = TEST_COUNT;
268 ref->stride = sizeof(float[4]);
272 ref2->start = (GLfloat *) r2;
273 ref2->count = TEST_COUNT;
274 ref2->stride = sizeof(float[4]);
277 if ( norm_normalize_types[mtype] == 0 ) {
278 ref_norm_transform_rescale( mat, scale, source, NULL, ref );
280 ref_norm_transform_normalize( mat, scale, source, NULL, ref );
281 ref_norm_transform_normalize( mat, scale, source, length, ref2 );
284 if ( mesa_profile ) {
285 BEGIN_RACE( *cycles );
286 func( mat, scale, source, NULL, dest );
288 func( mat, scale, source, length, dest2 );
290 func( mat, scale, source, NULL, dest );
291 func( mat, scale, source, length, dest2 );
294 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
295 for ( j = 0 ; j < 3 ; j++ ) {
296 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
297 printf( "-----------------------------\n" );
298 printf( "(i = %i, j = %i)\n", i, j );
299 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
300 d[i][0], r[i][0], r[i][0]/d[i][0],
301 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
302 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
303 d[i][1], r[i][1], r[i][1]/d[i][1],
304 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
305 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
306 d[i][2], r[i][2], r[i][2]/d[i][2],
307 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
311 if ( norm_normalize_types[mtype] != 0 ) {
312 if ( significand_match( d2[i][j], r2[i][j] ) < REQUIRED_PRECISION ) {
313 printf( "------------------- precalculated length case ------\n" );
314 printf( "(i = %i, j = %i)\n", i, j );
315 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
316 d2[i][0], r2[i][0], r2[i][0]/d2[i][0],
317 MAX_PRECISION - significand_match( d2[i][0], r2[i][0] ) );
318 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
319 d2[i][1], r2[i][1], r2[i][1]/d2[i][1],
320 MAX_PRECISION - significand_match( d2[i][1], r2[i][1] ) );
321 printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
322 d2[i][2], r2[i][2], r2[i][2]/d2[i][2],
323 MAX_PRECISION - significand_match( d2[i][2], r2[i][2] ) );
330 _mesa_align_free( mat->m );
334 void _math_test_all_normal_transform_functions( char *description )
337 long benchmark_tab[0xf];
338 static int first_time = 1;
342 mesa_profile = _mesa_getenv( "MESA_PROFILE" );
345 #ifdef RUN_DEBUG_BENCHMARK
346 if ( mesa_profile ) {
347 if ( !counter_overhead ) {
349 printf( "counter overhead: %ld cycles\n\n", counter_overhead );
351 printf( "normal transform results after hooking in %s functions:\n",
353 printf( "\n-------------------------------------------------------\n" );
357 for ( mtype = 0 ; mtype < 8 ; mtype++ ) {
358 normal_func func = _mesa_normal_tab[norm_types[mtype]];
359 long *cycles = &benchmark_tab[mtype];
361 if ( test_norm_function( func, mtype, cycles ) == 0 ) {
363 sprintf( buf, "_mesa_normal_tab[0][%s] failed test (%s)",
364 norm_strings[mtype], description );
365 _mesa_problem( NULL, "%s", buf );
368 #ifdef RUN_DEBUG_BENCHMARK
369 if ( mesa_profile ) {
370 printf( " %li\t", benchmark_tab[mtype] );
371 printf( " | [%s]\n", norm_strings[mtype] );
375 #ifdef RUN_DEBUG_BENCHMARK
376 if ( mesa_profile ) {
383 #endif /* DEBUG_MATH */