Removed all RCS / CVS tags (Id, Header, Date, etc.) from everything.
[mesa.git] / src / mesa / math / m_debug_norm.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 *
25 * Authors:
26 * Gareth Hughes
27 */
28
29 #include "glheader.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "imports.h"
33
34 #include "m_matrix.h"
35 #include "m_xform.h"
36
37 #include "m_debug.h"
38 #include "m_debug_util.h"
39
40
41 #ifdef DEBUG /* This code only used for debugging */
42
43
44 static int m_norm_identity[16] = {
45 ONE, NIL, NIL, NIL,
46 NIL, ONE, NIL, NIL,
47 NIL, NIL, ONE, NIL,
48 NIL, NIL, NIL, NIL
49 };
50 static int m_norm_general[16] = {
51 VAR, VAR, VAR, NIL,
52 VAR, VAR, VAR, NIL,
53 VAR, VAR, VAR, NIL,
54 NIL, NIL, NIL, NIL
55 };
56 static int m_norm_no_rot[16] = {
57 VAR, NIL, NIL, NIL,
58 NIL, VAR, NIL, NIL,
59 NIL, NIL, VAR, NIL,
60 NIL, NIL, NIL, NIL
61 };
62 static int *norm_templates[8] = {
63 m_norm_no_rot,
64 m_norm_no_rot,
65 m_norm_no_rot,
66 m_norm_general,
67 m_norm_general,
68 m_norm_general,
69 m_norm_identity,
70 m_norm_identity
71 };
72 static int norm_types[8] = {
73 NORM_TRANSFORM_NO_ROT,
74 NORM_TRANSFORM_NO_ROT | NORM_RESCALE,
75 NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE,
76 NORM_TRANSFORM,
77 NORM_TRANSFORM | NORM_RESCALE,
78 NORM_TRANSFORM | NORM_NORMALIZE,
79 NORM_RESCALE,
80 NORM_NORMALIZE
81 };
82 static int norm_scale_types[8] = { /* rescale factor */
83 NIL, /* NIL disables rescaling */
84 VAR,
85 NIL,
86 NIL,
87 VAR,
88 NIL,
89 VAR,
90 NIL
91 };
92 static int norm_normalize_types[8] = { /* normalizing ?? (no = 0) */
93 0,
94 0,
95 1,
96 0,
97 0,
98 1,
99 0,
100 1
101 };
102 static char *norm_strings[8] = {
103 "NORM_TRANSFORM_NO_ROT",
104 "NORM_TRANSFORM_NO_ROT | NORM_RESCALE",
105 "NORM_TRANSFORM_NO_ROT | NORM_NORMALIZE",
106 "NORM_TRANSFORM",
107 "NORM_TRANSFORM | NORM_RESCALE",
108 "NORM_TRANSFORM | NORM_NORMALIZE",
109 "NORM_RESCALE",
110 "NORM_NORMALIZE"
111 };
112
113
114 /* =============================================================
115 * Reference transformations
116 */
117
118 static void ref_norm_transform_rescale( const GLmatrix *mat,
119 GLfloat scale,
120 const GLvector4f *in,
121 const GLfloat *lengths,
122 GLvector4f *dest )
123 {
124 GLuint i;
125 const GLfloat *s = in->start;
126 const GLfloat *m = mat->inv;
127 GLfloat (*out)[4] = (GLfloat (*)[4]) dest->start;
128
129 (void) lengths;
130
131 for ( i = 0 ; i < in->count ; i++ ) {
132 GLfloat t[3];
133
134 TRANSFORM_NORMAL( t, s, m );
135 SCALE_SCALAR_3V( out[i], scale, t );
136
137 s = (GLfloat *)((char *)s + in->stride);
138 }
139 }
140
141 static void ref_norm_transform_normalize( const GLmatrix *mat,
142 GLfloat scale,
143 const GLvector4f *in,
144 const GLfloat *lengths,
145 GLvector4f *dest )
146 {
147 GLuint i;
148 const GLfloat *s = in->start;
149 const GLfloat *m = mat->inv;
150 GLfloat (*out)[4] = (GLfloat (*)[4]) dest->start;
151
152 for ( i = 0 ; i < in->count ; i++ ) {
153 GLfloat t[3];
154
155 TRANSFORM_NORMAL( t, s, m );
156
157 if ( !lengths ) {
158 GLfloat len = LEN_SQUARED_3FV( t );
159 if ( len > 1e-20 ) {
160 /* Hmmm, don't know how we could test the precalculated
161 * length case...
162 */
163 scale = 1.0 / sqrt( len );
164 SCALE_SCALAR_3V( out[i], scale, t );
165 } else {
166 out[i][0] = out[i][1] = out[i][2] = 0;
167 }
168 } else {
169 scale = lengths[i];;
170 SCALE_SCALAR_3V( out[i], scale, t );
171 }
172
173 s = (GLfloat *)((char *)s + in->stride);
174 }
175 }
176
177
178 /* =============================================================
179 * Normal transformation tests
180 */
181
182 static void init_matrix( GLfloat *m )
183 {
184 m[0] = 63.0; m[4] = 43.0; m[ 8] = 29.0; m[12] = 43.0;
185 m[1] = 55.0; m[5] = 17.0; m[ 9] = 31.0; m[13] = 7.0;
186 m[2] = 44.0; m[6] = 9.0; m[10] = 7.0; m[14] = 3.0;
187 m[3] = 11.0; m[7] = 23.0; m[11] = 91.0; m[15] = 9.0;
188 }
189
190
191 static int test_norm_function( normal_func func, int mtype, long *cycles )
192 {
193 GLvector4f source[1], dest[1], dest2[1], ref[1], ref2[1];
194 GLmatrix mat[1];
195 GLfloat s[TEST_COUNT][5], d[TEST_COUNT][4], r[TEST_COUNT][4];
196 GLfloat d2[TEST_COUNT][4], r2[TEST_COUNT][4], length[TEST_COUNT];
197 GLfloat scale;
198 GLfloat *m;
199 int i, j;
200 #ifdef RUN_DEBUG_BENCHMARK
201 int cycle_i; /* the counter for the benchmarks we run */
202 #endif
203
204 (void) cycles;
205
206 mat->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
207 mat->inv = m = mat->m;
208
209 init_matrix( m );
210
211 scale = 1.0F + rnd () * norm_scale_types[mtype];
212
213 for ( i = 0 ; i < 4 ; i++ ) {
214 for ( j = 0 ; j < 4 ; j++ ) {
215 switch ( norm_templates[mtype][i * 4 + j] ) {
216 case NIL:
217 m[j * 4 + i] = 0.0;
218 break;
219 case ONE:
220 m[j * 4 + i] = 1.0;
221 break;
222 case NEG:
223 m[j * 4 + i] = -1.0;
224 break;
225 case VAR:
226 break;
227 default:
228 abort();
229 }
230 }
231 }
232
233 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
234 ASSIGN_3V( d[i], 0.0, 0.0, 0.0 );
235 ASSIGN_3V( s[i], 0.0, 0.0, 0.0 );
236 ASSIGN_3V( d2[i], 0.0, 0.0, 0.0 );
237 for ( j = 0 ; j < 3 ; j++ )
238 s[i][j] = rnd();
239 length[i] = 1 / sqrt( LEN_SQUARED_3FV( s[i] ) );
240 }
241
242 source->data = (GLfloat(*)[4]) s;
243 source->start = (GLfloat *) s;
244 source->count = TEST_COUNT;
245 source->stride = sizeof(s[0]);
246 source->flags = 0;
247
248 dest->data = d;
249 dest->start = (GLfloat *) d;
250 dest->count = TEST_COUNT;
251 dest->stride = sizeof(float[4]);
252 dest->flags = 0;
253
254 dest2->data = d2;
255 dest2->start = (GLfloat *) d2;
256 dest2->count = TEST_COUNT;
257 dest2->stride = sizeof(float[4]);
258 dest2->flags = 0;
259
260 ref->data = r;
261 ref->start = (GLfloat *) r;
262 ref->count = TEST_COUNT;
263 ref->stride = sizeof(float[4]);
264 ref->flags = 0;
265
266 ref2->data = r2;
267 ref2->start = (GLfloat *) r2;
268 ref2->count = TEST_COUNT;
269 ref2->stride = sizeof(float[4]);
270 ref2->flags = 0;
271
272 if ( norm_normalize_types[mtype] == 0 ) {
273 ref_norm_transform_rescale( mat, scale, source, NULL, ref );
274 } else {
275 ref_norm_transform_normalize( mat, scale, source, NULL, ref );
276 ref_norm_transform_normalize( mat, scale, source, length, ref2 );
277 }
278
279 if ( mesa_profile ) {
280 BEGIN_RACE( *cycles );
281 func( mat, scale, source, NULL, dest );
282 END_RACE( *cycles );
283 func( mat, scale, source, length, dest2 );
284 } else {
285 func( mat, scale, source, NULL, dest );
286 func( mat, scale, source, length, dest2 );
287 }
288
289 for ( i = 0 ; i < TEST_COUNT ; i++ ) {
290 for ( j = 0 ; j < 3 ; j++ ) {
291 if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
292 _mesa_printf( "-----------------------------\n" );
293 _mesa_printf( "(i = %i, j = %i)\n", i, j );
294 _mesa_printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
295 d[i][0], r[i][0], r[i][0]/d[i][0],
296 MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
297 _mesa_printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
298 d[i][1], r[i][1], r[i][1]/d[i][1],
299 MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
300 _mesa_printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
301 d[i][2], r[i][2], r[i][2]/d[i][2],
302 MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
303 return 0;
304 }
305
306 if ( norm_normalize_types[mtype] != 0 ) {
307 if ( significand_match( d2[i][j], r2[i][j] ) < REQUIRED_PRECISION ) {
308 _mesa_printf( "------------------- precalculated length case ------\n" );
309 _mesa_printf( "(i = %i, j = %i)\n", i, j );
310 _mesa_printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
311 d2[i][0], r2[i][0], r2[i][0]/d2[i][0],
312 MAX_PRECISION - significand_match( d2[i][0], r2[i][0] ) );
313 _mesa_printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
314 d2[i][1], r2[i][1], r2[i][1]/d2[i][1],
315 MAX_PRECISION - significand_match( d2[i][1], r2[i][1] ) );
316 _mesa_printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
317 d2[i][2], r2[i][2], r2[i][2]/d2[i][2],
318 MAX_PRECISION - significand_match( d2[i][2], r2[i][2] ) );
319 return 0;
320 }
321 }
322 }
323 }
324
325 ALIGN_FREE( mat->m );
326 return 1;
327 }
328
329 void _math_test_all_normal_transform_functions( char *description )
330 {
331 int mtype;
332 long benchmark_tab[0xf];
333 static int first_time = 1;
334
335 if ( first_time ) {
336 first_time = 0;
337 mesa_profile = getenv( "MESA_PROFILE" );
338 }
339
340 #ifdef RUN_DEBUG_BENCHMARK
341 if ( mesa_profile ) {
342 if ( !counter_overhead ) {
343 INIT_COUNTER();
344 _mesa_printf( "counter overhead: %ld cycles\n\n", counter_overhead );
345 }
346 _mesa_printf( "normal transform results after hooking in %s functions:\n",
347 description );
348 _mesa_printf( "\n-------------------------------------------------------\n" );
349 }
350 #endif
351
352 for ( mtype = 0 ; mtype < 8 ; mtype++ ) {
353 normal_func func = _mesa_normal_tab[norm_types[mtype]];
354 long *cycles = &benchmark_tab[mtype];
355
356 if ( test_norm_function( func, mtype, cycles ) == 0 ) {
357 char buf[100];
358 _mesa_sprintf( buf, "_mesa_normal_tab[0][%s] failed test (%s)",
359 norm_strings[mtype], description );
360 _mesa_problem( NULL, buf );
361 }
362
363 #ifdef RUN_DEBUG_BENCHMARK
364 if ( mesa_profile ) {
365 _mesa_printf( " %li\t", benchmark_tab[mtype] );
366 _mesa_printf( " | [%s]\n", norm_strings[mtype] );
367 }
368 #endif
369 }
370 #ifdef RUN_DEBUG_BENCHMARK
371 if ( mesa_profile ) {
372 _mesa_printf( "\n" );
373 fflush( stdout );
374 }
375 #endif
376 }
377
378
379 #endif /* DEBUG */