]> git.sesse.net Git - x264/blob - tools/checkasm.c
Add dedicated variance function instead of using SAD+SSD
[x264] / tools / checkasm.c
1 /*****************************************************************************
2  * checkasm.c: assembly check tool
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <math.h>
29
30 #include "common/common.h"
31 #include "common/cpu.h"
32
33 /* buf1, buf2: initialised to random data and shouldn't write into them */
34 uint8_t * buf1, * buf2;
35 /* buf3, buf4: used to store output */
36 uint8_t * buf3, * buf4;
37
38 int quiet = 0;
39
40 #define report( name ) { \
41     if( used_asm && !quiet ) \
42         fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
43     if( !ok ) ret = -1; \
44 }
45
46 #define BENCH_RUNS 100  // tradeoff between accuracy and speed
47 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
48 #define MAX_FUNCS 1000  // just has to be big enough to hold all the existing functions
49 #define MAX_CPUS 10     // number of different combinations of cpu flags
50
51 typedef struct {
52     void *pointer; // just for detecting duplicates
53     uint32_t cpu;
54     uint32_t cycles;
55     uint32_t den;
56 } bench_t;
57
58 typedef struct {
59     char *name;
60     bench_t vers[MAX_CPUS];
61 } bench_func_t;
62
63 int do_bench = 0;
64 int bench_pattern_len = 0;
65 const char *bench_pattern = "";
66 char func_name[100];
67 static bench_func_t benchs[MAX_FUNCS];
68
69 static const char *pixel_names[10] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x2", "2x4", "2x2" };
70 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
71 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
72 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
73 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
74
75 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
76
77 static inline uint32_t read_time(void)
78 {
79 #if defined(__GNUC__) && (defined(ARCH_X86) || defined(ARCH_X86_64))
80     uint32_t a;
81     asm volatile( "rdtsc" :"=a"(a) ::"edx" );
82     return a;
83 #else
84     return 0;
85 #endif
86 }
87
88 static bench_t* get_bench( const char *name, int cpu )
89 {
90     int i, j;
91     for( i=0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
92         assert( i < MAX_FUNCS );
93     if( !benchs[i].name )
94         benchs[i].name = strdup( name );
95     if( !cpu )
96         return &benchs[i].vers[0];
97     for( j=1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
98         assert( j < MAX_CPUS );
99     benchs[i].vers[j].cpu = cpu;
100     return &benchs[i].vers[j];
101 }
102
103 int cmp_nop( const void *a, const void *b )
104 {
105     return *(uint16_t*)a - *(uint16_t*)b;
106 }
107
108 int cmp_bench( const void *a, const void *b )
109 {
110     // asciibetical sort except preserving numbers
111     const char *sa = ((bench_func_t*)a)->name;
112     const char *sb = ((bench_func_t*)b)->name;
113     for(;; sa++, sb++)
114     {
115         if( !*sa && !*sb ) return 0;
116         if( isdigit(*sa) && isdigit(*sb) && isdigit(sa[1]) != isdigit(sb[1]) )
117             return isdigit(sa[1]) - isdigit(sb[1]);
118         if( *sa != *sb ) return *sa - *sb;
119     }
120 }
121
122 static void print_bench(void)
123 {
124     uint16_t nops[10000] = {0};
125     int i, j, k, nfuncs, nop_time=0;
126
127     for( i=0; i<10000; i++ )
128     {
129         int t = read_time();
130         nops[i] = read_time() - t;
131     }
132     qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
133     for( i=500; i<9500; i++ )
134         nop_time += nops[i];
135     nop_time /= 900;
136     printf( "nop: %d\n", nop_time );
137
138     for( i=0; i<MAX_FUNCS && benchs[i].name; i++ );
139     nfuncs=i;
140     qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
141     for( i=0; i<nfuncs; i++ )
142         for( j=0; j<MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
143         {
144             bench_t *b = &benchs[i].vers[j];
145             if( !b->den ) continue;
146             for( k=0; k<j && benchs[i].vers[k].pointer != b->pointer; k++ );
147             if( k<j ) continue;
148             printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
149                     b->cpu&X264_CPU_PHADD_IS_FAST ? "phadd" :
150                     b->cpu&X264_CPU_SSSE3 ? "ssse3" :
151                     b->cpu&X264_CPU_SSE3 ? "sse3" :
152                     /* print sse2slow only if there's also a sse2fast version of the same func */
153                     b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
154                     b->cpu&X264_CPU_SSE2 ? "sse2" :
155                     b->cpu&X264_CPU_MMX ? "mmx" : "c",
156                     b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
157                     b->cpu&X264_CPU_CACHELINE_64 ? "_c64" : "",
158                     ((int64_t)10*b->cycles/b->den - nop_time)/4 );
159         }
160 }
161
162 #if defined(ARCH_X86) || defined(ARCH_X86_64)
163 int x264_stack_pagealign( int (*func)(), int align );
164 #else
165 #define x264_stack_pagealign( func, align ) func()
166 #endif
167
168 #define call_c1(func,...) func(__VA_ARGS__)
169
170 #ifdef ARCH_X86
171 /* detect when callee-saved regs aren't saved.
172  * needs an explicit asm check because it only sometimes crashes in normal use. */
173 long x264_checkasm_call( long (*func)(), int *ok, ... );
174 #define call_a1(func,...) x264_checkasm_call((long(*)())func, &ok, __VA_ARGS__)
175 #else
176 #define call_a1 call_c1
177 #endif
178
179 #define call_bench(func,cpu,...)\
180     if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
181     {\
182         uint32_t tsum = 0;\
183         int tcount = 0;\
184         int ti;\
185         call_a1(func, __VA_ARGS__);\
186         for( ti=0; ti<(cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
187         {\
188             uint32_t t = read_time();\
189             func(__VA_ARGS__);\
190             func(__VA_ARGS__);\
191             func(__VA_ARGS__);\
192             func(__VA_ARGS__);\
193             t = read_time() - t;\
194             if( t*tcount <= tsum*4 && ti > 0 )\
195             {\
196                 tsum += t;\
197                 tcount++;\
198             }\
199         }\
200         bench_t *b = get_bench( func_name, cpu );\
201         b->cycles += tsum;\
202         b->den += tcount;\
203         b->pointer = func;\
204     }
205
206 /* for most functions, run benchmark and correctness test at the same time.
207  * for those that modify their inputs, run the above macros separately */
208 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
209 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
210 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
211 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
212
213
214 static int check_pixel( int cpu_ref, int cpu_new )
215 {
216     x264_pixel_function_t pixel_c;
217     x264_pixel_function_t pixel_ref;
218     x264_pixel_function_t pixel_asm;
219     x264_predict_t predict_16x16[4+3];
220     x264_predict_t predict_8x8c[4+3];
221     x264_predict_t predict_4x4[9+3];
222     x264_predict8x8_t predict_8x8[9+3];
223     DECLARE_ALIGNED_16( uint8_t edge[33] );
224     uint16_t cost_mv[32];
225     int ret = 0, ok, used_asm;
226     int i, j;
227
228     x264_pixel_init( 0, &pixel_c );
229     x264_pixel_init( cpu_ref, &pixel_ref );
230     x264_pixel_init( cpu_new, &pixel_asm );
231     x264_predict_16x16_init( 0, predict_16x16 );
232     x264_predict_8x8c_init( 0, predict_8x8c );
233     x264_predict_8x8_init( 0, predict_8x8 );
234     x264_predict_4x4_init( 0, predict_4x4 );
235     x264_predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
236
237 #define TEST_PIXEL( name, align ) \
238     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
239     { \
240         int res_c, res_asm; \
241         if( pixel_asm.name[i] != pixel_ref.name[i] ) \
242         { \
243             set_func_name( "%s_%s", #name, pixel_names[i] ); \
244             for( j=0; j<64; j++ ) \
245             { \
246                 used_asm = 1; \
247                 res_c   = call_c( pixel_c.name[i], buf1, 16, buf2+j*!align, 64 ); \
248                 res_asm = call_a( pixel_asm.name[i], buf1, 16, buf2+j*!align, 64 ); \
249                 if( res_c != res_asm ) \
250                 { \
251                     ok = 0; \
252                     fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
253                     break; \
254                 } \
255             } \
256         } \
257     } \
258     report( "pixel " #name " :" );
259
260     TEST_PIXEL( sad, 0 );
261     TEST_PIXEL( ssd, 1 );
262     TEST_PIXEL( satd, 0 );
263     TEST_PIXEL( sa8d, 0 );
264
265 #define TEST_PIXEL_X( N ) \
266     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
267     { \
268         int res_c[4]={0}, res_asm[4]={0}; \
269         if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
270         { \
271             set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
272             for( j=0; j<64; j++) \
273             { \
274                 uint8_t *pix2 = buf2+j; \
275                 used_asm = 1; \
276                 res_c[0] = pixel_c.sad[i]( buf1, 16, pix2, 64 ); \
277                 res_c[1] = pixel_c.sad[i]( buf1, 16, pix2+6, 64 ); \
278                 res_c[2] = pixel_c.sad[i]( buf1, 16, pix2+1, 64 ); \
279                 if(N==4) \
280                 { \
281                     res_c[3] = pixel_c.sad[i]( buf1, 16, pix2+10, 64 ); \
282                     call_a( pixel_asm.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
283                 } \
284                 else \
285                     call_a( pixel_asm.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
286                 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
287                 { \
288                     ok = 0; \
289                     fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
290                              i, res_c[0], res_c[1], res_c[2], res_c[3], \
291                              res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
292                 } \
293                 if(N==4) \
294                     call_c2( pixel_c.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
295                 else \
296                     call_c2( pixel_c.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
297             } \
298         } \
299     } \
300     report( "pixel sad_x"#N" :" );
301
302     TEST_PIXEL_X(3);
303     TEST_PIXEL_X(4);
304
305 #define TEST_PIXEL_VAR( i ) \
306     if( pixel_asm.var[i] != pixel_ref.var[i] ) \
307     { \
308         uint32_t res_c, res_asm; \
309         uint32_t sad_c, sad_asm; \
310         set_func_name( "%s_%s", "var", pixel_names[i] ); \
311         used_asm = 1; \
312         res_c   = call_c( pixel_c.var[i], buf1, 16, &sad_c ); \
313         res_asm = call_a( pixel_asm.var[i], buf1, 16, &sad_asm ); \
314         if( (res_c != res_asm) || (sad_c != sad_asm) ) \
315         { \
316             ok = 0; \
317             fprintf( stderr, "var[%d]: %d,%d != %d,%d [FAILED]\n", i, res_c, sad_c, res_asm, sad_asm ); \
318         } \
319     }
320
321     ok = 1; used_asm = 0;
322     TEST_PIXEL_VAR( PIXEL_16x16 );
323     TEST_PIXEL_VAR( PIXEL_8x8 );
324     report( "pixel var :" );
325
326 #define TEST_INTRA_SATD( name, pred, satd, i8x8, ... ) \
327     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
328     { \
329         int res_c[3], res_asm[3]; \
330         set_func_name( #name );\
331         used_asm = 1; \
332         memcpy( buf3, buf2, 1024 ); \
333         for( i=0; i<3; i++ ) \
334         { \
335             pred[i]( buf3+40, ##__VA_ARGS__ ); \
336             res_c[i] = pixel_c.satd( buf1+40, 16, buf3+40, 32 ); \
337         } \
338         call_a( pixel_asm.name, buf1+40, i8x8 ? edge : buf3+40, res_asm ); \
339         if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
340         { \
341             ok = 0; \
342             fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
343                      res_c[0], res_c[1], res_c[2], \
344                      res_asm[0], res_asm[1], res_asm[2] ); \
345         } \
346     }
347
348     ok = 1; used_asm = 0;
349     TEST_INTRA_SATD( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
350     TEST_INTRA_SATD( intra_satd_x3_8x8c, predict_8x8c, satd[PIXEL_8x8], 0 );
351     TEST_INTRA_SATD( intra_satd_x3_4x4, predict_4x4, satd[PIXEL_4x4], 0 );
352     TEST_INTRA_SATD( intra_sa8d_x3_8x8, predict_8x8, sa8d[PIXEL_8x8], 1, edge );
353     report( "intra satd_x3 :" );
354
355     if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
356         pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
357     {
358         float res_c, res_a;
359         int sums[5][4] = {{0}};
360         used_asm = ok = 1;
361         x264_emms();
362         res_c = x264_pixel_ssim_wxh( &pixel_c,   buf1+2, 32, buf2+2, 32, 32, 28 );
363         res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28 );
364         if( fabs(res_c - res_a) > 1e-6 )
365         {
366             ok = 0;
367             fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
368         }
369         set_func_name( "ssim_core" );
370         call_c2( pixel_c.ssim_4x4x2_core,   buf1+2, 32, buf2+2, 32, sums );
371         call_a2( pixel_asm.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
372         set_func_name( "ssim_end" );
373         call_c2( pixel_c.ssim_end4,   sums, sums, 4 );
374         call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
375         report( "ssim :" );
376     }
377
378     ok = 1; used_asm = 0;
379     for( i=0; i<32; i++ )
380         cost_mv[i] = i*10;
381     for( i=0; i<100 && ok; i++ )
382         if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
383         {
384             DECLARE_ALIGNED_16( uint16_t sums[72] );
385             DECLARE_ALIGNED_16( int dc[4] );
386             int16_t mvs_a[32], mvs_c[32];
387             int mvn_a, mvn_c;
388             int thresh = rand() & 0x3fff;
389             set_func_name( "esa_ads" );
390             for( j=0; j<72; j++ )
391                 sums[j] = rand() & 0x3fff;
392             for( j=0; j<4; j++ )
393                 dc[j] = rand() & 0x3fff;
394             used_asm = 1;
395             mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
396             mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
397             if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
398             {
399                 ok = 0;
400                 printf("c%d: ", i&3);
401                 for(j=0; j<mvn_c; j++)
402                     printf("%d ", mvs_c[j]);
403                 printf("\na%d: ", i&3);
404                 for(j=0; j<mvn_a; j++)
405                     printf("%d ", mvs_a[j]);
406                 printf("\n\n");
407             }
408         }
409     report( "esa ads:" );
410
411     return ret;
412 }
413
414 static int check_dct( int cpu_ref, int cpu_new )
415 {
416     x264_dct_function_t dct_c;
417     x264_dct_function_t dct_ref;
418     x264_dct_function_t dct_asm;
419     x264_quant_function_t qf;
420     int ret = 0, ok, used_asm, i, interlace;
421     DECLARE_ALIGNED_16( int16_t dct1[16][4][4] );
422     DECLARE_ALIGNED_16( int16_t dct2[16][4][4] );
423     DECLARE_ALIGNED_16( int16_t dct4[16][4][4] );
424     DECLARE_ALIGNED_16( int16_t dct8[4][8][8] );
425     x264_t h_buf;
426     x264_t *h = &h_buf;
427
428     x264_dct_init( 0, &dct_c );
429     x264_dct_init( cpu_ref, &dct_ref);
430     x264_dct_init( cpu_new, &dct_asm );
431
432     memset( h, 0, sizeof(*h) );
433     h->pps = h->pps_array;
434     x264_param_default( &h->param );
435     h->param.analyse.i_luma_deadzone[0] = 0;
436     h->param.analyse.i_luma_deadzone[1] = 0;
437     h->param.analyse.b_transform_8x8 = 1;
438     for( i=0; i<6; i++ )
439         h->pps->scaling_list[i] = x264_cqm_flat16;
440     x264_cqm_init( h );
441     x264_quant_init( h, 0, &qf );
442
443 #define TEST_DCT( name, t1, t2, size ) \
444     if( dct_asm.name != dct_ref.name ) \
445     { \
446         set_func_name( #name );\
447         used_asm = 1; \
448         call_c( dct_c.name, t1, buf1, buf2 ); \
449         call_a( dct_asm.name, t2, buf1, buf2 ); \
450         if( memcmp( t1, t2, size ) ) \
451         { \
452             ok = 0; \
453             fprintf( stderr, #name " [FAILED]\n" ); \
454         } \
455     }
456     ok = 1; used_asm = 0;
457     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
458     TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
459     TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
460     report( "sub_dct4 :" );
461
462     ok = 1; used_asm = 0;
463     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
464     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
465     report( "sub_dct8 :" );
466 #undef TEST_DCT
467
468     // fdct and idct are denormalized by different factors, so quant/dequant
469     // is needed to force the coefs into the right range.
470     dct_c.sub16x16_dct( dct4, buf1, buf2 );
471     dct_c.sub16x16_dct8( dct8, buf1, buf2 );
472     for( i=0; i<16; i++ )
473     {
474         qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
475         qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
476     }
477     for( i=0; i<4; i++ )
478     {
479         qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
480         qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
481     }
482
483 #define TEST_IDCT( name, src ) \
484     if( dct_asm.name != dct_ref.name ) \
485     { \
486         set_func_name( #name );\
487         used_asm = 1; \
488         memcpy( buf3, buf1, 32*32 ); \
489         memcpy( buf4, buf1, 32*32 ); \
490         memcpy( dct1, src, 512 ); \
491         memcpy( dct2, src, 512 ); \
492         call_c1( dct_c.name, buf3, (void*)dct1 ); \
493         call_a1( dct_asm.name, buf4, (void*)dct2 ); \
494         if( memcmp( buf3, buf4, 32*32 ) ) \
495         { \
496             ok = 0; \
497             fprintf( stderr, #name " [FAILED]\n" ); \
498         } \
499         call_c2( dct_c.name, buf3, (void*)dct1 ); \
500         call_a2( dct_asm.name, buf4, (void*)dct2 ); \
501     }
502     ok = 1; used_asm = 0;
503     TEST_IDCT( add4x4_idct, dct4 );
504     TEST_IDCT( add8x8_idct, dct4 );
505     TEST_IDCT( add16x16_idct, dct4 );
506     report( "add_idct4 :" );
507
508     ok = 1; used_asm = 0;
509     TEST_IDCT( add8x8_idct8, dct8 );
510     TEST_IDCT( add16x16_idct8, dct8 );
511     report( "add_idct8 :" );
512 #undef TEST_IDCT
513
514     ok = 1; used_asm = 0;
515     if( dct_asm.dct4x4dc != dct_ref.dct4x4dc )
516     {
517         DECLARE_ALIGNED_16( int16_t dct1[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
518         DECLARE_ALIGNED_16( int16_t dct2[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
519         set_func_name( "dct4x4dc" );
520         used_asm = 1;
521         call_c1( dct_c.dct4x4dc, dct1 );
522         call_a1( dct_asm.dct4x4dc, dct2 );
523         if( memcmp( dct1, dct2, 32 ) )
524         {
525             ok = 0;
526             fprintf( stderr, " - dct4x4dc :        [FAILED]\n" );
527         }
528         call_c2( dct_c.dct4x4dc, dct1 );
529         call_a2( dct_asm.dct4x4dc, dct2 );
530     }
531     if( dct_asm.idct4x4dc != dct_ref.idct4x4dc )
532     {
533         DECLARE_ALIGNED_16( int16_t dct1[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
534         DECLARE_ALIGNED_16( int16_t dct2[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
535         set_func_name( "idct4x4dc" );
536         used_asm = 1;
537         call_c1( dct_c.idct4x4dc, dct1 );
538         call_a1( dct_asm.idct4x4dc, dct2 );
539         if( memcmp( dct1, dct2, 32 ) )
540         {
541             ok = 0;
542             fprintf( stderr, " - idct4x4dc :        [FAILED]\n" );
543         }
544         call_c2( dct_c.idct4x4dc, dct1 );
545         call_a2( dct_asm.idct4x4dc, dct2 );
546     }
547     report( "(i)dct4x4dc :" );
548
549     ok = 1; used_asm = 0;
550     if( dct_asm.dct2x2dc != dct_ref.dct2x2dc )
551     {
552         DECLARE_ALIGNED_16( int16_t dct1[2][2] ) = {{-12, 42},{2, 90}};
553         DECLARE_ALIGNED_16( int16_t dct2[2][2] ) = {{-12, 42},{2, 90}};
554         set_func_name( "dct2x2dc" );
555         used_asm = 1;
556         call_c( dct_c.dct2x2dc, dct1 );
557         call_a( dct_asm.dct2x2dc, dct2 );
558         if( memcmp( dct1, dct2, 4*2 ) )
559         {
560             ok = 0;
561             fprintf( stderr, " - dct2x2dc :        [FAILED]\n" );
562         }
563     }
564     if( dct_asm.idct2x2dc != dct_ref.idct2x2dc )
565     {
566         DECLARE_ALIGNED_16( int16_t dct1[2][2] ) = {{-12, 42},{2, 90}};
567         DECLARE_ALIGNED_16( int16_t dct2[2][2] ) = {{-12, 42},{2, 90}};
568         set_func_name( "idct2x2dc" );
569         used_asm = 1;
570         call_c( dct_c.idct2x2dc, dct1 );
571         call_a( dct_asm.idct2x2dc, dct2 );
572         if( memcmp( dct1, dct2, 4*2 ) )
573         {
574             ok = 0;
575             fprintf( stderr, " - idct2x2dc :       [FAILED]\n" );
576         }
577     }
578     report( "(i)dct2x2dc :" );
579
580     x264_zigzag_function_t zigzag_c;
581     x264_zigzag_function_t zigzag_ref;
582     x264_zigzag_function_t zigzag_asm;
583
584     DECLARE_ALIGNED_16( int16_t level1[64] );
585     DECLARE_ALIGNED_16( int16_t level2[64] );
586
587 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size )   \
588     if( zigzag_asm.name != zigzag_ref.name ) \
589     { \
590         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
591         used_asm = 1; \
592         call_c( zigzag_c.name, t1, dct ); \
593         call_a( zigzag_asm.name, t2, dct ); \
594         if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
595         { \
596             ok = 0; \
597             fprintf( stderr, #name " [FAILED]\n" ); \
598         } \
599     }
600
601 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
602     if( zigzag_asm.name != zigzag_ref.name ) \
603     { \
604         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
605         used_asm = 1; \
606         memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
607         memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
608         call_c1( zigzag_c.name, t1, buf2, buf3 );  \
609         call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
610         if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) )  \
611         { \
612             ok = 0; \
613             fprintf( stderr, #name " [FAILED]\n" ); \
614         } \
615         call_c2( zigzag_c.name, t1, buf2, buf3 );  \
616         call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
617     }
618
619     interlace = 0;
620     x264_zigzag_init( 0, &zigzag_c, 0 );
621     x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
622     x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
623
624     ok = 1; used_asm = 0;
625     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
626     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
627     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
628     report( "zigzag_frame :" );
629
630     interlace = 1;
631     x264_zigzag_init( 0, &zigzag_c, 1 );
632     x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
633     x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
634
635     ok = 1; used_asm = 0;
636     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
637     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
638     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
639     report( "zigzag_field :" );
640 #undef TEST_ZIGZAG_SCAN
641 #undef TEST_ZIGZAG_SUB
642
643     return ret;
644 }
645
646 static int check_mc( int cpu_ref, int cpu_new )
647 {
648     x264_mc_functions_t mc_c;
649     x264_mc_functions_t mc_ref;
650     x264_mc_functions_t mc_a;
651     x264_pixel_function_t pixel;
652
653     uint8_t *src     = &buf1[2*32+2];
654     uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
655                          &buf1[7*64+2], &buf1[9*64+2] };
656     uint8_t *dst1    = buf3;
657     uint8_t *dst2    = buf4;
658
659     int dx, dy, i, j, k, w;
660     int ret = 0, ok, used_asm;
661
662     x264_mc_init( 0, &mc_c );
663     x264_mc_init( cpu_ref, &mc_ref );
664     x264_mc_init( cpu_new, &mc_a );
665     x264_pixel_init( 0, &pixel );
666
667 #define MC_TEST_LUMA( w, h ) \
668         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
669         { \
670             set_func_name( "mc_luma_%dx%d", w, h );\
671             used_asm = 1; \
672             memset(buf3, 0xCD, 1024); \
673             memset(buf4, 0xCD, 1024); \
674             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
675             call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h ); \
676             if( memcmp( buf3, buf4, 1024 ) ) \
677             { \
678                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
679                 ok = 0; \
680             } \
681         } \
682         if( mc_a.get_ref != mc_ref.get_ref ) \
683         { \
684             uint8_t *ref = dst2; \
685             int ref_stride = 32; \
686             set_func_name( "get_ref_%dx%d", w, h );\
687             used_asm = 1; \
688             memset(buf3, 0xCD, 1024); \
689             memset(buf4, 0xCD, 1024); \
690             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
691             ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h ); \
692             for( i=0; i<h; i++ ) \
693                 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
694                 { \
695                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
696                     ok = 0; \
697                     break; \
698                 } \
699         }
700
701 #define MC_TEST_CHROMA( w, h ) \
702         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
703         { \
704             set_func_name( "mc_chroma_%dx%d", w, h );\
705             used_asm = 1; \
706             memset(buf3, 0xCD, 1024); \
707             memset(buf4, 0xCD, 1024); \
708             call_c( mc_c.mc_chroma, dst1, 16, src, 32, dx, dy, w, h ); \
709             call_a( mc_a.mc_chroma, dst2, 16, src, 32, dx, dy, w, h ); \
710             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
711             for( j=0; j<h; j++ ) \
712                 for( i=w; i<4; i++ ) \
713                     dst2[i+j*16] = dst1[i+j*16]; \
714             if( memcmp( buf3, buf4, 1024 ) ) \
715             { \
716                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
717                 ok = 0; \
718             } \
719         }
720     ok = 1; used_asm = 0;
721     for( dy = -8; dy < 8; dy++ )
722         for( dx = -128; dx < 128; dx++ )
723         {
724             if( rand()&15 ) continue; // running all of them is too slow
725             MC_TEST_LUMA( 20, 18 );
726             MC_TEST_LUMA( 16, 16 );
727             MC_TEST_LUMA( 16, 8 );
728             MC_TEST_LUMA( 12, 10 );
729             MC_TEST_LUMA( 8, 16 );
730             MC_TEST_LUMA( 8, 8 );
731             MC_TEST_LUMA( 8, 4 );
732             MC_TEST_LUMA( 4, 8 );
733             MC_TEST_LUMA( 4, 4 );
734         }
735     report( "mc luma :" );
736
737     ok = 1; used_asm = 0;
738     for( dy = -1; dy < 9; dy++ )
739         for( dx = -1; dx < 9; dx++ )
740         {
741             MC_TEST_CHROMA( 8, 8 );
742             MC_TEST_CHROMA( 8, 4 );
743             MC_TEST_CHROMA( 4, 8 );
744             MC_TEST_CHROMA( 4, 4 );
745             MC_TEST_CHROMA( 4, 2 );
746             MC_TEST_CHROMA( 2, 4 );
747             MC_TEST_CHROMA( 2, 2 );
748         }
749     report( "mc chroma :" );
750 #undef MC_TEST_LUMA
751 #undef MC_TEST_CHROMA
752
753 #define MC_TEST_AVG( name, ... ) \
754     for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
755     { \
756         memcpy( buf3, buf1, 1024 ); \
757         memcpy( buf4, buf1, 1024 ); \
758         if( mc_a.name[i] != mc_ref.name[i] ) \
759         { \
760             set_func_name( "%s_%s", #name, pixel_names[i] );\
761             used_asm = 1; \
762             call_c1( mc_c.name[i], buf3, 32, buf2, 16, ##__VA_ARGS__ ); \
763             call_a1( mc_a.name[i], buf4, 32, buf2, 16, ##__VA_ARGS__ ); \
764             if( memcmp( buf3, buf4, 1024 ) )               \
765             { \
766                 ok = 0; \
767                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
768             } \
769             call_c2( mc_c.name[i], buf3, 32, buf2, 16, ##__VA_ARGS__ ); \
770             call_a2( mc_a.name[i], buf4, 32, buf2, 16, ##__VA_ARGS__ ); \
771         } \
772     }
773     MC_TEST_AVG( avg );
774     report( "mc avg :" );
775     ok = 1; used_asm = 0;
776     for( w = -64; w <= 128 && ok; w++ )
777         MC_TEST_AVG( avg_weight, w );
778     report( "mc wpredb :" );
779
780     if( mc_a.hpel_filter != mc_ref.hpel_filter )
781     {
782         uint8_t *src = buf1+8+2*64;
783         uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
784         uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
785         set_func_name( "hpel_filter" );
786         ok = 1; used_asm = 1;
787         memset( buf3, 0, 4096 );
788         memset( buf4, 0, 4096 );
789         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10 );
790         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10 );
791         for( i=0; i<3; i++ )
792             for( j=0; j<10; j++ )
793                 //FIXME ideally the first pixels would match too, but they aren't actually used
794                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
795                 {
796                     ok = 0;
797                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
798                     for( k=0; k<48; k++ )
799                         printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
800                     printf("\n");
801                     for( k=0; k<48; k++ )
802                         printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
803                     printf("\n");
804                     break;
805                 }
806         report( "hpel filter :" );
807     }
808
809     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
810     {
811         uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
812         uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf3+3072 };
813         set_func_name( "lowres_init" );
814         for( w=40; w<=48; w+=8 )
815             if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
816             {
817                 int stride = (w+8)&~15;
818                 used_asm = 1;
819                 call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
820                 call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
821                 for( i=0; i<16; i++)
822                 {
823                     for( j=0; j<4; j++)
824                         if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
825                         {
826                             ok = 0;
827                             fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
828                             for( k=0; k<w; k++ )
829                                 printf( "%d ", dstc[j][k+i*stride] );
830                             printf("\n");
831                             for( k=0; k<w; k++ )
832                                 printf( "%d ", dsta[j][k+i*stride] );
833                             printf("\n");
834                             break;
835                         }
836                 }
837             }
838         report( "lowres init :" );
839     }
840
841     return ret;
842 }
843
844 static int check_deblock( int cpu_ref, int cpu_new )
845 {
846     x264_deblock_function_t db_c;
847     x264_deblock_function_t db_ref;
848     x264_deblock_function_t db_a;
849     int ret = 0, ok = 1, used_asm = 0;
850     int alphas[36], betas[36];
851     int8_t tcs[36][4];
852     int a, c, i, j;
853
854     x264_deblock_init( 0, &db_c );
855     x264_deblock_init( cpu_ref, &db_ref );
856     x264_deblock_init( cpu_new, &db_a );
857
858     /* not exactly the real values of a,b,tc but close enough */
859     a = 255; c = 250;
860     for( i = 35; i >= 0; i-- )
861     {
862         alphas[i] = a;
863         betas[i] = (i+1)/2;
864         tcs[i][0] = tcs[i][2] = (c+6)/10;
865         tcs[i][1] = tcs[i][3] = (c+9)/20;
866         a = a*9/10;
867         c = c*9/10;
868     }
869
870 #define TEST_DEBLOCK( name, align, ... ) \
871     for( i = 0; i < 36; i++ ) \
872     { \
873         int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
874         for( j = 0; j < 1024; j++ ) \
875             /* two distributions of random to excersize different failure modes */\
876             buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
877         memcpy( buf4, buf3, 1024 ); \
878         if( db_a.name != db_ref.name ) \
879         { \
880             set_func_name( #name );\
881             used_asm = 1; \
882             call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
883             call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
884             if( memcmp( buf3, buf4, 1024 ) ) \
885             { \
886                 ok = 0; \
887                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
888                 break; \
889             } \
890             call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
891             call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
892         } \
893     }
894
895     TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
896     TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
897     TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
898     TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
899     TEST_DEBLOCK( deblock_h_luma_intra, 0 );
900     TEST_DEBLOCK( deblock_v_luma_intra, 1 );
901     TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
902     TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
903
904     report( "deblock :" );
905
906     return ret;
907 }
908
909 static int check_quant( int cpu_ref, int cpu_new )
910 {
911     x264_quant_function_t qf_c;
912     x264_quant_function_t qf_ref;
913     x264_quant_function_t qf_a;
914     DECLARE_ALIGNED_16( int16_t dct1[64] );
915     DECLARE_ALIGNED_16( int16_t dct2[64] );
916     DECLARE_ALIGNED_16( uint8_t cqm_buf[64] );
917     int ret = 0, ok, used_asm;
918     int oks[2] = {1,1}, used_asms[2] = {0,0};
919     int i, i_cqm, qp;
920     x264_t h_buf;
921     x264_t *h = &h_buf;
922     memset( h, 0, sizeof(*h) );
923     h->pps = h->pps_array;
924     x264_param_default( &h->param );
925     h->param.rc.i_qp_min = 26;
926     h->param.analyse.b_transform_8x8 = 1;
927
928     for( i_cqm = 0; i_cqm < 4; i_cqm++ )
929     {
930         if( i_cqm == 0 )
931         {
932             for( i = 0; i < 6; i++ )
933                 h->pps->scaling_list[i] = x264_cqm_flat16;
934             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
935         }
936         else if( i_cqm == 1 )
937         {
938             for( i = 0; i < 6; i++ )
939                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
940             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
941         }
942         else
943         {
944             if( i_cqm == 2 )
945                 for( i = 0; i < 64; i++ )
946                     cqm_buf[i] = 10 + rand() % 246;
947             else
948                 for( i = 0; i < 64; i++ )
949                     cqm_buf[i] = 1;
950             for( i = 0; i < 6; i++ )
951                 h->pps->scaling_list[i] = cqm_buf;
952             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
953         }
954
955         x264_cqm_init( h );
956         x264_quant_init( h, 0, &qf_c );
957         x264_quant_init( h, cpu_ref, &qf_ref );
958         x264_quant_init( h, cpu_new, &qf_a );
959
960 #define INIT_QUANT8() \
961         { \
962             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
963             int x, y; \
964             for( y = 0; y < 8; y++ ) \
965                 for( x = 0; x < 8; x++ ) \
966                 { \
967                     unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
968                     dct1[y*8+x] = dct2[y*8+x] = (rand()%(2*scale+1))-scale; \
969                 } \
970         }
971
972 #define INIT_QUANT4() \
973         { \
974             static const int scale1d[4] = {4,6,4,6}; \
975             int x, y; \
976             for( y = 0; y < 4; y++ ) \
977                 for( x = 0; x < 4; x++ ) \
978                 { \
979                     unsigned int scale = 255*scale1d[y]*scale1d[x]; \
980                     dct1[y*4+x] = dct2[y*4+x] = (rand()%(2*scale+1))-scale; \
981                 } \
982         }
983
984 #define TEST_QUANT_DC( name, cqm ) \
985         if( qf_a.name != qf_ref.name ) \
986         { \
987             set_func_name( #name ); \
988             used_asms[0] = 1; \
989             for( qp = 51; qp > 0; qp-- ) \
990             { \
991                 for( i = 0; i < 16; i++ ) \
992                     dct1[i] = dct2[i] = (rand() & 0x1fff) - 0xfff; \
993                 call_c1( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
994                 call_a1( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
995                 if( memcmp( dct1, dct2, 16*2 ) )       \
996                 { \
997                     oks[0] = 0; \
998                     fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
999                     break; \
1000                 } \
1001                 call_c2( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1002                 call_a2( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1003             } \
1004         }
1005
1006 #define TEST_QUANT( qname, block, w ) \
1007         if( qf_a.qname != qf_ref.qname ) \
1008         { \
1009             set_func_name( #qname ); \
1010             used_asms[0] = 1; \
1011             for( qp = 51; qp > 0; qp-- ) \
1012             { \
1013                 INIT_QUANT##w() \
1014                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1015                 call_a1( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1016                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1017                 { \
1018                     oks[0] = 0; \
1019                     fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1020                     break; \
1021                 } \
1022                 call_c2( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1023                 call_a2( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1024             } \
1025         }
1026
1027         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1028         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1029         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1030         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1031         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1032         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1033
1034 #define TEST_DEQUANT( qname, dqname, block, w ) \
1035         if( qf_a.dqname != qf_ref.dqname ) \
1036         { \
1037             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1038             used_asms[1] = 1; \
1039             for( qp = 51; qp > 0; qp-- ) \
1040             { \
1041                 INIT_QUANT##w() \
1042                 call_c( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1043                 memcpy( dct2, dct1, w*w*2 ); \
1044                 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1045                 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1046                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1047                 { \
1048                     oks[1] = 0; \
1049                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1050                     break; \
1051                 } \
1052                 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1053                 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1054             } \
1055         }
1056
1057         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1058         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1059         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1060         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1061
1062         x264_cqm_delete( h );
1063     }
1064
1065     ok = oks[0]; used_asm = used_asms[0];
1066     report( "quant :" );
1067
1068     ok = oks[1]; used_asm = used_asms[1];
1069     report( "dequant :" );
1070
1071
1072     if( qf_a.denoise_dct_core != qf_ref.denoise_dct_core )
1073     {
1074         int size;
1075         for( size = 16; size <= 64; size += 48 )
1076         {
1077             set_func_name( "denoise_dct" );
1078             used_asm = 1;
1079             memcpy(dct1, buf1, size*2);
1080             memcpy(dct2, buf1, size*2);
1081             memcpy(buf3+256, buf3, 256);
1082             call_c1( qf_c.denoise_dct_core, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1083             call_a1( qf_a.denoise_dct_core, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1084             if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1085                 ok = 0;
1086             call_c2( qf_c.denoise_dct_core, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1087             call_a2( qf_a.denoise_dct_core, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1088         }
1089     }
1090     report( "denoise dct :" );
1091
1092     return ret;
1093 }
1094
1095 static int check_intra( int cpu_ref, int cpu_new )
1096 {
1097     int ret = 0, ok = 1, used_asm = 0;
1098     int i;
1099     DECLARE_ALIGNED_16( uint8_t edge[33] );
1100     struct
1101     {
1102         x264_predict_t      predict_16x16[4+3];
1103         x264_predict_t      predict_8x8c[4+3];
1104         x264_predict8x8_t   predict_8x8[9+3];
1105         x264_predict_t      predict_4x4[9+3];
1106     } ip_c, ip_ref, ip_a;
1107
1108     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1109     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1110     x264_predict_8x8_init( 0, ip_c.predict_8x8 );
1111     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1112
1113     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1114     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1115     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8 );
1116     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1117
1118     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1119     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1120     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8 );
1121     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1122
1123     x264_predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1124
1125 #define INTRA_TEST( name, dir, w, ... ) \
1126     if( ip_a.name[dir] != ip_ref.name[dir] )\
1127     { \
1128         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1129         used_asm = 1; \
1130         memcpy( buf3, buf1, 32*20 );\
1131         memcpy( buf4, buf1, 32*20 );\
1132         call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1133         call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1134         if( memcmp( buf3, buf4, 32*20 ) )\
1135         {\
1136             fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
1137             ok = 0;\
1138             int j,k;\
1139             for(k=-1; k<16; k++)\
1140                 printf("%2x ", edge[16+k]);\
1141             printf("\n");\
1142             for(j=0; j<w; j++){\
1143                 printf("%2x ", edge[14-j]);\
1144                 for(k=0; k<w; k++)\
1145                     printf("%2x ", buf4[48+k+j*32]);\
1146                 printf("\n");\
1147             }\
1148             printf("\n");\
1149             for(j=0; j<w; j++){\
1150                 printf("   ");\
1151                 for(k=0; k<w; k++)\
1152                     printf("%2x ", buf3[48+k+j*32]);\
1153                 printf("\n");\
1154             }\
1155         }\
1156     }
1157
1158     for( i = 0; i < 12; i++ )
1159         INTRA_TEST( predict_4x4, i, 4 );
1160     for( i = 0; i < 7; i++ )
1161         INTRA_TEST( predict_8x8c, i, 8 );
1162     for( i = 0; i < 7; i++ )
1163         INTRA_TEST( predict_16x16, i, 16 );
1164     for( i = 0; i < 12; i++ )
1165         INTRA_TEST( predict_8x8, i, 8, edge );
1166
1167     report( "intra pred :" );
1168     return ret;
1169 }
1170
1171 #define DECL_CABAC(cpu) \
1172 static void run_cabac_##cpu( uint8_t *dst )\
1173 {\
1174     int i;\
1175     x264_cabac_t cb;\
1176     x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1177     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1178     for( i=0; i<0x1000; i++ )\
1179         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1180 }
1181 DECL_CABAC(c)
1182 #ifdef HAVE_MMX
1183 DECL_CABAC(asm)
1184 #else
1185 #define run_cabac_asm run_cabac_c
1186 #endif
1187
1188 static int check_cabac( int cpu_ref, int cpu_new )
1189 {
1190     int ret = 0, ok, used_asm = 1;
1191     if( cpu_ref || run_cabac_c == run_cabac_asm)
1192         return 0;
1193     set_func_name( "cabac_encode_decision" );
1194     memcpy( buf4, buf3, 0x1000 );
1195     call_c( run_cabac_c, buf3 );
1196     call_a( run_cabac_asm, buf4 );
1197     ok = !memcmp( buf3, buf4, 0x1000 );
1198     report( "cabac :" );
1199     return ret;
1200 }
1201
1202 int check_all_funcs( int cpu_ref, int cpu_new )
1203 {
1204     return check_pixel( cpu_ref, cpu_new )
1205          + check_dct( cpu_ref, cpu_new )
1206          + check_mc( cpu_ref, cpu_new )
1207          + check_intra( cpu_ref, cpu_new )
1208          + check_deblock( cpu_ref, cpu_new )
1209          + check_quant( cpu_ref, cpu_new )
1210          + check_cabac( cpu_ref, cpu_new );
1211 }
1212
1213 int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1214 {
1215     *cpu_ref = *cpu_new;
1216     *cpu_new |= flags;
1217     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1218         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1219     if( !quiet )
1220         fprintf( stderr, "x264: %s\n", name );
1221     return check_all_funcs( *cpu_ref, *cpu_new );
1222 }
1223
1224 int check_all_flags( void )
1225 {
1226     int ret = 0;
1227     int cpu0 = 0, cpu1 = 0;
1228 #ifdef HAVE_MMX
1229     if( x264_cpu_detect() & X264_CPU_MMXEXT )
1230     {
1231         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1232         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1233         cpu1 &= ~X264_CPU_CACHELINE_64;
1234 #ifdef ARCH_X86
1235         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1236         cpu1 &= ~X264_CPU_CACHELINE_32;
1237 #endif
1238     }
1239     if( x264_cpu_detect() & X264_CPU_SSE2 )
1240     {
1241         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1242         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1243         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1244     }
1245     if( x264_cpu_detect() & X264_CPU_SSE3 )
1246         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1247     if( x264_cpu_detect() & X264_CPU_SSSE3 )
1248     {
1249         cpu1 &= ~X264_CPU_CACHELINE_64;
1250         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1251         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1252         ret |= add_flags( &cpu0, &cpu1, X264_CPU_PHADD_IS_FAST, "PHADD" );
1253     }
1254 #elif ARCH_PPC
1255     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1256     {
1257         fprintf( stderr, "x264: ALTIVEC against C\n" );
1258         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1259     }
1260 #endif
1261     return ret;
1262 }
1263
1264 int main(int argc, char *argv[])
1265 {
1266     int ret = 0;
1267     int i;
1268
1269     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1270     {
1271 #if !defined(ARCH_X86) && !defined(ARCH_X86_64)
1272         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1273         return 1;
1274 #endif
1275         do_bench = 1;
1276         if( argv[1][7] == '=' )
1277         {
1278             bench_pattern = argv[1]+8;
1279             bench_pattern_len = strlen(bench_pattern);
1280         }
1281         argc--;
1282         argv++;
1283     }
1284
1285     i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1286     fprintf( stderr, "x264: using random seed %u\n", i );
1287     srand( i );
1288
1289     buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1290     buf2 = buf1 + 0xf00;
1291     buf3 = buf2 + 0xf00;
1292     buf4 = buf3 + 0x1000;
1293     for( i=0; i<0x1e00; i++ )
1294         buf1[i] = rand() & 0xFF;
1295     memset( buf1+0x1e00, 0, 0x2000 );
1296
1297     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1298     if( do_bench )
1299         for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1300         {
1301             buf2 = buf1 + 0xf00;
1302             buf3 = buf2 + 0xf00;
1303             buf4 = buf3 + 0x1000;
1304             ret |= x264_stack_pagealign( check_all_flags, i*16 );
1305             buf1 += 16;
1306             quiet = 1;
1307             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1308         }
1309     else
1310         ret = check_all_flags();
1311
1312     if( ret )
1313     {
1314         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1315         return -1;
1316     }
1317     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
1318     if( do_bench )
1319         print_bench();
1320     return 0;
1321 }
1322