]> git.sesse.net Git - x264/blob - tools/checkasm.c
8e77fbb76ca40233d297fefc17ed3bf0746b4588
[x264] / tools / checkasm.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include "common/common.h"
7 #include "common/cpu.h"
8 #ifdef HAVE_MMX
9 #include "common/i386/pixel.h"
10 #include "common/i386/dct.h"
11 #include "common/i386/mc.h"
12 #endif
13 #ifdef ARCH_PPC
14 #include "common/ppc/pixel.h"
15 #include "common/ppc/mc.h"
16 #endif
17
18 /* buf1, buf2: initialised to random data and shouldn't write into them */
19 uint8_t * buf1, * buf2;
20 /* buf3, buf4: used to store output */
21 uint8_t * buf3, * buf4;
22 /* buf5: temp */
23 uint8_t * buf5;
24
25 #define report( name ) { \
26     if( used_asm ) \
27         fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
28     if( !ok ) ret = -1; \
29 }
30
31 static int check_pixel( int cpu_ref, int cpu_new )
32 {
33     x264_pixel_function_t pixel_c;
34     x264_pixel_function_t pixel_ref;
35     x264_pixel_function_t pixel_asm;
36     x264_predict_t predict_16x16[4+3];
37     x264_predict_t predict_8x8c[4+3];
38     x264_predict_t predict_4x4[9+3];
39     x264_predict8x8_t predict_8x8[9+3];
40     DECLARE_ALIGNED( uint8_t, edge[33], 8 );
41     int ret = 0, ok, used_asm;
42     int i, j;
43
44     x264_pixel_init( 0, &pixel_c );
45     x264_pixel_init( cpu_ref, &pixel_ref );
46     x264_pixel_init( cpu_new, &pixel_asm );
47     x264_predict_16x16_init( 0, predict_16x16 );
48     x264_predict_8x8c_init( 0, predict_8x8c );
49     x264_predict_8x8_init( 0, predict_8x8 );
50     x264_predict_4x4_init( 0, predict_4x4 );
51     x264_predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
52
53 #define TEST_PIXEL( name ) \
54     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
55     { \
56         int res_c, res_asm; \
57         if( pixel_asm.name[i] != pixel_ref.name[i] ) \
58         { \
59             used_asm = 1; \
60             res_c   = pixel_c.name[i]( buf1, 32, buf2, 16 ); \
61             res_asm = pixel_asm.name[i]( buf1, 32, buf2, 16 ); \
62             if( res_c != res_asm ) \
63             { \
64                 ok = 0; \
65                 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
66             } \
67         } \
68     } \
69     report( "pixel " #name " :" );
70
71     TEST_PIXEL( sad );
72     TEST_PIXEL( ssd );
73     TEST_PIXEL( satd );
74     TEST_PIXEL( sa8d );
75
76 #define TEST_PIXEL_X( N ) \
77     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
78     { \
79         int res_c[4]={0}, res_asm[4]={0}; \
80         if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
81         { \
82             used_asm = 1; \
83             res_c[0] = pixel_c.sad[i]( buf1, 16, buf2, 32 ); \
84             res_c[1] = pixel_c.sad[i]( buf1, 16, buf2+30, 32 ); \
85             res_c[2] = pixel_c.sad[i]( buf1, 16, buf2+1, 32 ); \
86             if(N==4) \
87             { \
88                 res_c[3] = pixel_c.sad[i]( buf1, 16, buf2+99, 32 ); \
89                 pixel_asm.sad_x4[i]( buf1, buf2, buf2+30, buf2+1, buf2+99, 32, res_asm ); \
90             } \
91             else \
92                 pixel_asm.sad_x3[i]( buf1, buf2, buf2+30, buf2+1, 32, res_asm ); \
93             if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
94             { \
95                 ok = 0; \
96                 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
97                          i, res_c[0], res_c[1], res_c[2], res_c[3], \
98                          res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
99             } \
100         } \
101     } \
102     report( "pixel sad_x"#N" :" );
103
104     TEST_PIXEL_X(3);
105     TEST_PIXEL_X(4);
106
107 #define TEST_INTRA_SATD( name, pred, satd, i8x8, ... ) \
108     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
109     { \
110         int res_c[3], res_asm[3]; \
111         used_asm = 1; \
112         memcpy( buf3, buf2, 1024 ); \
113         for( i=0; i<3; i++ ) \
114         { \
115             pred[i]( buf3+40, ##__VA_ARGS__ ); \
116             res_c[i] = pixel_c.satd( buf1+40, 16, buf3+40, 32 ); \
117         } \
118         pixel_asm.name( buf1+40, i8x8 ? edge : buf3+40, res_asm ); \
119         if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
120         { \
121             ok = 0; \
122             fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
123                      res_c[0], res_c[1], res_c[2], \
124                      res_asm[0], res_asm[1], res_asm[2] ); \
125         } \
126     }
127
128     ok = 1; used_asm = 0;
129     TEST_INTRA_SATD( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
130     TEST_INTRA_SATD( intra_satd_x3_8x8c, predict_8x8c, satd[PIXEL_8x8], 0 );
131     TEST_INTRA_SATD( intra_satd_x3_4x4, predict_4x4, satd[PIXEL_4x4], 0 );
132     TEST_INTRA_SATD( intra_sa8d_x3_8x8, predict_8x8, sa8d[PIXEL_8x8], 1, edge );
133     report( "intra satd_x3 :" );
134
135     if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
136         pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
137     {
138         float res_c, res_a;
139         ok = 1;
140         x264_cpu_restore( cpu_new );
141         res_c = x264_pixel_ssim_wxh( &pixel_c,   buf1+2, 32, buf2+2, 32, 32, 28 );
142         res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28 );
143         if( fabs(res_c - res_a) > 1e-8 )
144         {
145             ok = 0;
146             fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
147         }
148         report( "ssim :" );
149     }
150
151     ok = 1; used_asm = 0;
152     for( i=0; i<4; i++ )
153         if( pixel_asm.ads[i] != pixel_ref.ads[i] )
154         {
155             uint16_t res_a[32], res_c[32];
156             uint16_t sums[72];
157             int dc[4];
158             for( j=0; j<72; j++ )
159                 sums[j] = rand() & 0x3fff;
160             for( j=0; j<4; j++ )
161                 dc[j] = rand() & 0x3fff;
162             used_asm = 1;
163             pixel_c.ads[i]( dc, sums, 32, res_c, 32 );
164             pixel_asm.ads[i]( dc, sums, 32, res_a, 32 );
165             if( memcmp(res_a, res_c, sizeof(res_c)) )
166                 ok = 0;
167         }
168     report( "esa ads:" );
169
170     return ret;
171 }
172
173 static int check_dct( int cpu_ref, int cpu_new )
174 {
175     x264_dct_function_t dct_c;
176     x264_dct_function_t dct_ref;
177     x264_dct_function_t dct_asm;
178     int ret = 0, ok, used_asm;
179     int16_t dct1[16][4][4] __attribute__((aligned(16)));
180     int16_t dct2[16][4][4] __attribute__((aligned(16)));
181
182     x264_dct_init( 0, &dct_c );
183     x264_dct_init( cpu_ref, &dct_ref);
184     x264_dct_init( cpu_new, &dct_asm );
185 #define TEST_DCT( name, t1, t2, size ) \
186     if( dct_asm.name != dct_ref.name ) \
187     { \
188         used_asm = 1; \
189         dct_c.name( t1, buf1, buf2 ); \
190         dct_asm.name( t2, buf1, buf2 ); \
191         if( memcmp( t1, t2, size ) ) \
192         { \
193             ok = 0; \
194             fprintf( stderr, #name " [FAILED]\n" ); \
195         } \
196     }
197     ok = 1; used_asm = 0;
198     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
199     TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
200     TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
201     report( "sub_dct4 :" );
202
203     ok = 1; used_asm = 0;
204     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
205     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
206     report( "sub_dct8 :" );
207 #undef TEST_DCT
208
209     /* copy coefs because idct8 modifies them in place */
210     memcpy( buf5, dct1, 512 );
211
212 #define TEST_IDCT( name ) \
213     if( dct_asm.name != dct_ref.name ) \
214     { \
215         used_asm = 1; \
216         memcpy( buf3, buf1, 32*32 ); \
217         memcpy( buf4, buf1, 32*32 ); \
218         memcpy( dct1, buf5, 512 ); \
219         memcpy( dct2, buf5, 512 ); \
220         dct_c.name( buf3, (void*)dct1 ); \
221         dct_asm.name( buf4, (void*)dct2 ); \
222         if( memcmp( buf3, buf4, 32*32 ) ) \
223         { \
224             ok = 0; \
225             fprintf( stderr, #name " [FAILED]\n" ); \
226         } \
227     }
228     ok = 1; used_asm = 0;
229     TEST_IDCT( add4x4_idct );
230     TEST_IDCT( add8x8_idct );
231     TEST_IDCT( add16x16_idct );
232     report( "add_idct4 :" );
233
234     ok = 1; used_asm = 0;
235     TEST_IDCT( add8x8_idct8 );
236     TEST_IDCT( add16x16_idct8 );
237     report( "add_idct8 :" );
238 #undef TEST_IDCT
239
240     ok = 1; used_asm = 0;
241     if( dct_asm.dct4x4dc != dct_ref.dct4x4dc )
242     {
243         int16_t dct1[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
244         int16_t dct2[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
245         used_asm = 1;
246         dct_c.dct4x4dc( dct1 );
247         dct_asm.dct4x4dc( dct2 );
248         if( memcmp( dct1, dct2, 32 ) )
249         {
250             ok = 0;
251             fprintf( stderr, " - dct4x4dc :        [FAILED]\n" );
252         }
253     }
254     if( dct_asm.dct4x4dc != dct_ref.dct4x4dc )
255     {
256         int16_t dct1[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
257         int16_t dct2[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
258         used_asm = 1;
259         dct_c.idct4x4dc( dct1 );
260         dct_asm.idct4x4dc( dct2 );
261         if( memcmp( dct1, dct2, 32 ) )
262         {
263             ok = 0;
264             fprintf( stderr, " - idct4x4dc :        [FAILED]\n" );
265         }
266     }
267     report( "(i)dct4x4dc :" );
268
269     ok = 1; used_asm = 0;
270     if( dct_asm.dct2x2dc != dct_ref.dct2x2dc )
271     {
272         int16_t dct1[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
273         int16_t dct2[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
274         used_asm = 1;
275         dct_c.dct2x2dc( dct1 );
276         dct_asm.dct2x2dc( dct2 );
277         if( memcmp( dct1, dct2, 4*2 ) )
278         {
279             ok = 0;
280             fprintf( stderr, " - dct2x2dc :        [FAILED]\n" );
281         }
282     }
283     if( dct_asm.idct2x2dc != dct_ref.idct2x2dc )
284     {
285         int16_t dct1[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
286         int16_t dct2[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
287         used_asm = 1;
288         dct_c.idct2x2dc( dct1 );
289         dct_asm.idct2x2dc( dct2 );
290         if( memcmp( dct1, dct2, 4*2 ) )
291         {
292             ok = 0;
293             fprintf( stderr, " - idct2x2dc :       [FAILED]\n" );
294         }
295     }
296     report( "(i)dct2x2dc :" );
297
298     return ret;
299 }
300
301 static int check_mc( int cpu_ref, int cpu_new )
302 {
303     x264_mc_functions_t mc_c;
304     x264_mc_functions_t mc_ref;
305     x264_mc_functions_t mc_a;
306
307     uint8_t *src     = &buf1[2*32+2];
308     uint8_t *src2[4] = { &buf1[2*32+2],  &buf1[7*32+2],
309                          &buf1[12*32+2], &buf1[17*32+2] };
310     uint8_t *dst1    = &buf3[2*32+2];
311     uint8_t *dst2    = &buf4[2*32+2];
312
313     int dx, dy, i, j, w;
314     int ret = 0, ok, used_asm;
315
316     x264_mc_init( 0, &mc_c );
317     x264_mc_init( cpu_ref, &mc_ref );
318     x264_mc_init( cpu_new, &mc_a );
319
320 #define MC_TEST_LUMA( w, h ) \
321         if( mc_a.mc_luma != mc_ref.mc_luma ) \
322         { \
323             used_asm = 1; \
324             memset(buf3, 0xCD, 1024); \
325             memset(buf4, 0xCD, 1024); \
326             mc_c.mc_luma( src2, 32, dst1, 16, dx, dy, w, h );     \
327             mc_a.mc_luma( src2, 32, dst2, 16, dx, dy, w, h );   \
328             if( memcmp( buf3, buf4, 1024 ) )               \
329             { \
330                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h );   \
331                 ok = 0; \
332             } \
333         }
334
335 #define MC_TEST_CHROMA( w, h ) \
336         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
337         { \
338             used_asm = 1; \
339             memset(buf3, 0xCD, 1024); \
340             memset(buf4, 0xCD, 1024); \
341             mc_c.mc_chroma( src, 32, dst1, 16, dx, dy, w, h );     \
342             mc_a.mc_chroma( src, 32, dst2, 16, dx, dy, w, h );   \
343             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
344             for( j=0; j<h; j++ ) \
345                 for( i=w; i<4; i++ ) \
346                     dst2[i+j*16] = dst1[i+j*16]; \
347             if( memcmp( buf3, buf4, 1024 ) )               \
348             { \
349                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h );   \
350                 ok = 0; \
351             } \
352         }
353     ok = 1; used_asm = 0;
354     for( dy = 0; dy < 4; dy++ )
355         for( dx = 0; dx < 4; dx++ )
356         {
357             MC_TEST_LUMA( 16, 16 );
358             MC_TEST_LUMA( 16, 8 );
359             MC_TEST_LUMA( 8, 16 );
360             MC_TEST_LUMA( 8, 8 );
361             MC_TEST_LUMA( 8, 4 );
362             MC_TEST_LUMA( 4, 8 );
363             MC_TEST_LUMA( 4, 4 );
364         }
365     report( "mc luma :" );
366
367     ok = 1; used_asm = 0;
368     for( dy = -1; dy < 9; dy++ )
369         for( dx = -1; dx < 9; dx++ )
370         {
371             MC_TEST_CHROMA( 8, 8 );
372             MC_TEST_CHROMA( 8, 4 );
373             MC_TEST_CHROMA( 4, 8 );
374             MC_TEST_CHROMA( 4, 4 );
375             MC_TEST_CHROMA( 4, 2 );
376             MC_TEST_CHROMA( 2, 4 );
377             MC_TEST_CHROMA( 2, 2 );
378         }
379     report( "mc chroma :" );
380 #undef MC_TEST_LUMA
381 #undef MC_TEST_CHROMA
382
383 #define MC_TEST_AVG( name, ... ) \
384     for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
385     { \
386         memcpy( buf3, buf1, 1024 ); \
387         memcpy( buf4, buf1, 1024 ); \
388         if( mc_a.name[i] != mc_ref.name[i] ) \
389         { \
390             used_asm = 1; \
391             mc_c.name[i]( buf3, 32, buf2, 16, ##__VA_ARGS__ ); \
392             mc_a.name[i]( buf4, 32, buf2, 16, ##__VA_ARGS__ ); \
393             if( memcmp( buf3, buf4, 1024 ) )               \
394             { \
395                 ok = 0; \
396                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
397             } \
398         } \
399     }
400     MC_TEST_AVG( avg );
401     report( "mc avg :" );
402     for( w = -64; w <= 128 && ok; w++ )
403         MC_TEST_AVG( avg_weight, w );
404     report( "mc wpredb :" );
405
406     return ret;
407 }
408
409 static int check_deblock( int cpu_ref, int cpu_new )
410 {
411     x264_deblock_function_t db_c;
412     x264_deblock_function_t db_ref;
413     x264_deblock_function_t db_a;
414     int ret = 0, ok = 1, used_asm = 0;
415     int alphas[36], betas[36];
416     int8_t tcs[36][4];
417     int a, c, i, j;
418
419     x264_deblock_init( 0, &db_c );
420     x264_deblock_init( cpu_ref, &db_ref );
421     x264_deblock_init( cpu_new, &db_a );
422
423     /* not exactly the real values of a,b,tc but close enough */
424     a = 255; c = 250;
425     for( i = 35; i >= 0; i-- )
426     {
427         alphas[i] = a;
428         betas[i] = (i+1)/2;
429         tcs[i][0] = tcs[i][2] = (c+6)/10;
430         tcs[i][1] = tcs[i][3] = (c+9)/20;
431         a = a*9/10;
432         c = c*9/10;
433     }
434
435 #define TEST_DEBLOCK( name, ... ) \
436     for( i = 0; i < 36; i++ ) \
437     { \
438         for( j = 0; j < 1024; j++ ) \
439             /* two distributions of random to excersize different failure modes */\
440             buf1[j] = rand() & (i&1 ? 0xf : 0xff ); \
441         memcpy( buf3, buf1, 1024 ); \
442         memcpy( buf4, buf1, 1024 ); \
443         if( db_a.name != db_ref.name ) \
444         { \
445             used_asm = 1; \
446             db_c.name( &buf3[8*32], 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
447             db_a.name( &buf4[8*32], 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
448             if( memcmp( buf3, buf4, 1024 ) )               \
449             { \
450                 ok = 0; \
451                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
452                 break; \
453             } \
454         } \
455     }
456
457     TEST_DEBLOCK( deblock_h_luma, tcs[i] );
458     TEST_DEBLOCK( deblock_v_luma, tcs[i] );
459     TEST_DEBLOCK( deblock_h_chroma, tcs[i] );
460     TEST_DEBLOCK( deblock_v_chroma, tcs[i] );
461     TEST_DEBLOCK( deblock_h_luma_intra );
462     TEST_DEBLOCK( deblock_v_luma_intra );
463     TEST_DEBLOCK( deblock_h_chroma_intra );
464     TEST_DEBLOCK( deblock_v_chroma_intra );
465
466     report( "deblock :" );
467
468     return ret;
469 }
470
471 static int check_quant( int cpu_ref, int cpu_new )
472 {
473     x264_quant_function_t qf_c;
474     x264_quant_function_t qf_ref;
475     x264_quant_function_t qf_a;
476     int16_t dct1[64]    __attribute__((__aligned__(16)));
477     int16_t dct2[64]    __attribute__((__aligned__(16)));
478     uint8_t cqm_buf[64] __attribute__((__aligned__(16)));
479     int ret = 0, ok, used_asm;
480     int oks[2] = {1,1}, used_asms[2] = {0,0};
481     int i, i_cqm, qp;
482     x264_t h_buf;
483     x264_t *h = &h_buf;
484     h->pps = h->pps_array;
485     x264_param_default( &h->param );
486     h->param.rc.i_qp_min = 26;
487
488     for( i_cqm = 0; i_cqm < 4; i_cqm++ )
489     {
490         if( i_cqm == 0 )
491             for( i = 0; i < 6; i++ )
492                 h->pps->scaling_list[i] = x264_cqm_flat16;
493         else if( i_cqm == 1 )
494             for( i = 0; i < 6; i++ )
495                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
496         else
497         {
498             if( i_cqm == 2 )
499                 for( i = 0; i < 64; i++ )
500                     cqm_buf[i] = 10 + rand() % 246;
501             else
502                 for( i = 0; i < 64; i++ )
503                     cqm_buf[i] = 1;
504             for( i = 0; i < 6; i++ )
505                 h->pps->scaling_list[i] = cqm_buf;
506         }
507
508         x264_cqm_init( h );
509         x264_quant_init( h, 0, &qf_c );
510         x264_quant_init( h, cpu_ref, &qf_ref );
511         x264_quant_init( h, cpu_new, &qf_a );
512
513 #define INIT_QUANT8() \
514         { \
515             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
516             int x, y; \
517             for( y = 0; y < 8; y++ ) \
518                 for( x = 0; x < 8; x++ ) \
519                 { \
520                     unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
521                     dct1[y*8+x] = dct2[y*8+x] = (rand()%(2*scale+1))-scale; \
522                 } \
523         }
524
525 #define INIT_QUANT4() \
526         { \
527             static const int scale1d[4] = {4,6,4,6}; \
528             int x, y; \
529             for( y = 0; y < 4; y++ ) \
530                 for( x = 0; x < 4; x++ ) \
531                 { \
532                     unsigned int scale = 255*scale1d[y]*scale1d[x]; \
533                     dct1[y*4+x] = dct2[y*4+x] = (rand()%(2*scale+1))-scale; \
534                 } \
535         }
536
537 #define TEST_QUANT_DC( name, cqm ) \
538         if( qf_a.name != qf_ref.name ) \
539         { \
540             used_asms[0] = 1; \
541             for( qp = 51; qp > 0; qp-- ) \
542             { \
543                 for( i = 0; i < 16; i++ ) \
544                     dct1[i] = dct2[i] = (rand() & 0x1fff) - 0xfff; \
545                 qf_c.name( (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
546                 qf_a.name( (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
547                 if( memcmp( dct1, dct2, 16*2 ) )       \
548                 { \
549                     oks[0] = 0; \
550                     fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
551                     break; \
552                 } \
553             } \
554         }
555
556 #define TEST_QUANT( qname, block, w ) \
557         if( qf_a.qname != qf_ref.qname ) \
558         { \
559             used_asms[0] = 1; \
560             for( qp = 51; qp > 0; qp-- ) \
561             { \
562                 INIT_QUANT##w() \
563                 qf_c.qname( (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
564                 qf_a.qname( (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
565                 if( memcmp( dct1, dct2, w*w*2 ) ) \
566                 { \
567                     oks[0] = 0; \
568                     fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
569                     break; \
570                 } \
571             } \
572         }
573
574         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
575         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
576         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
577         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
578         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
579         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
580
581 #define TEST_DEQUANT( qname, dqname, block, w ) \
582         if( qf_a.dqname != qf_ref.dqname ) \
583         { \
584             used_asms[1] = 1; \
585             for( qp = 51; qp > 0; qp-- ) \
586             { \
587                 INIT_QUANT##w() \
588                 qf_c.qname( (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
589                 memcpy( dct2, dct1, w*w*2 ); \
590                 qf_c.dqname( (void*)dct1, h->dequant##w##_mf[block], qp ); \
591                 qf_a.dqname( (void*)dct2, h->dequant##w##_mf[block], qp ); \
592                 if( memcmp( dct1, dct2, w*w*2 ) ) \
593                 { \
594                     oks[1] = 0; \
595                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
596                     break; \
597                 } \
598             } \
599         }
600
601         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
602         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
603         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
604         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
605     }
606
607     ok = oks[0]; used_asm = used_asms[0];
608     report( "quant :" );
609
610     ok = oks[1]; used_asm = used_asms[1];
611     report( "dequant :" );
612
613     return ret;
614 }
615
616 static int check_intra( int cpu_ref, int cpu_new )
617 {
618     int ret = 0, ok = 1, used_asm = 0;
619     int i;
620     DECLARE_ALIGNED( uint8_t, edge[33], 8 );
621     struct
622     {
623         x264_predict_t      predict_16x16[4+3];
624         x264_predict_t      predict_8x8c[4+3];
625         x264_predict8x8_t   predict_8x8[9+3];
626         x264_predict_t      predict_4x4[9+3];
627     } ip_c, ip_ref, ip_a;
628
629     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
630     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
631     x264_predict_8x8_init( 0, ip_c.predict_8x8 );
632     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
633
634     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
635     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
636     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8 );
637     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
638
639     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
640     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
641     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8 );
642     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
643
644     x264_predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
645
646 #define INTRA_TEST( name, dir, ... ) \
647     if( ip_a.name[dir] != ip_ref.name[dir] )\
648     { \
649         used_asm = 1; \
650         memcpy( buf3, buf1, 32*20 );\
651         memcpy( buf4, buf1, 32*20 );\
652         ip_c.name[dir]( buf3+48, ##__VA_ARGS__ );\
653         ip_a.name[dir]( buf4+48, ##__VA_ARGS__ );\
654         if( memcmp( buf3, buf4, 32*20 ) )\
655         {\
656             fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
657             ok = 0;\
658             int j,k;\
659             for(k=-1; k<16; k++)\
660                 printf("%2x ", edge[16+k]);\
661             printf("\n");\
662             for(j=0; j<8; j++){\
663                 printf("%2x ", edge[j]);\
664                 for(k=0; k<8; k++)\
665                     printf("%2x ", buf4[48+k+j*32]);\
666                 printf("\n");\
667             }\
668             printf("\n");\
669             for(j=0; j<8; j++){\
670                 printf("   ");\
671                 for(k=0; k<8; k++)\
672                     printf("%2x ", buf3[48+k+j*32]);\
673                 printf("\n");\
674             }\
675         }\
676     }
677
678     for( i = 0; i < 12; i++ )
679         INTRA_TEST( predict_4x4, i );
680     for( i = 0; i < 7; i++ )
681         INTRA_TEST( predict_8x8c, i );
682     for( i = 0; i < 7; i++ )
683         INTRA_TEST( predict_16x16, i );
684     for( i = 0; i < 12; i++ )
685         INTRA_TEST( predict_8x8, i, edge );
686
687     report( "intra pred :" );
688     return ret;
689 }
690
691 int check_all( int cpu_ref, int cpu_new )
692 {
693     return check_pixel( cpu_ref, cpu_new )
694          + check_dct( cpu_ref, cpu_new )
695          + check_mc( cpu_ref, cpu_new )
696          + check_intra( cpu_ref, cpu_new )
697          + check_deblock( cpu_ref, cpu_new )
698          + check_quant( cpu_ref, cpu_new );
699 }
700
701 int main(int argc, char *argv[])
702 {
703     int ret = 0;
704     int cpu0 = 0, cpu1 = 0;
705     int i;
706
707     buf1 = x264_malloc( 1024 ); /* 32 x 32 */
708     buf2 = x264_malloc( 1024 );
709     buf3 = x264_malloc( 1024 );
710     buf4 = x264_malloc( 1024 );
711     buf5 = x264_malloc( 1024 );
712
713     i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
714     fprintf( stderr, "x264: using random seed %u\n", i );
715     srand( i );
716
717     for( i = 0; i < 1024; i++ )
718     {
719         buf1[i] = rand() & 0xFF;
720         buf2[i] = rand() & 0xFF;
721         buf3[i] = buf4[i] = 0;
722     }
723
724 #ifdef HAVE_MMX
725     fprintf( stderr, "x264: MMXEXT against C\n" );
726     cpu1 = X264_CPU_MMX | X264_CPU_MMXEXT;
727     ret = check_all( 0, cpu1 );
728
729     if( x264_cpu_detect() & X264_CPU_SSE2 )
730     {
731         fprintf( stderr, "\nx264: SSE2 against C\n" );
732         cpu0 = cpu1;
733         cpu1 |= X264_CPU_SSE | X264_CPU_SSE2;
734         ret |= check_all( cpu0, cpu1 );
735
736         if( x264_cpu_detect() & X264_CPU_SSSE3 )
737         {
738             fprintf( stderr, "\nx264: SSSE3 against C\n" );
739             cpu0 = cpu1;
740             cpu1 |= X264_CPU_SSE3 | X264_CPU_SSSE3;
741             ret |= check_all( cpu0, cpu1 );
742         }
743     }
744 #elif ARCH_PPC
745     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
746     {
747         fprintf( stderr, "x264: ALTIVEC against C\n" );
748         ret = check_all( 0, X264_CPU_ALTIVEC );
749     }
750 #endif
751
752     if( ret == 0 )
753     {
754         fprintf( stderr, "x264: All tests passed Yeah :)\n" );
755         return 0;
756     }
757     fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
758     return -1;
759 }
760