]> git.sesse.net Git - x264/blob - tools/checkasm.c
Much faster chroma encoding and other opts
[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 static int cmp_nop( const void *a, const void *b )
104 {
105     return *(uint16_t*)a - *(uint16_t*)b;
106 }
107
108 static 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_SSE4 ? "sse4" :
150                     b->cpu&X264_CPU_PHADD_IS_FAST ? "phadd" :
151                     b->cpu&X264_CPU_SSSE3 ? "ssse3" :
152                     b->cpu&X264_CPU_SSE3 ? "sse3" :
153                     /* print sse2slow only if there's also a sse2fast version of the same func */
154                     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" :
155                     b->cpu&X264_CPU_SSE2 ? "sse2" :
156                     b->cpu&X264_CPU_MMX ? "mmx" : "c",
157                     b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
158                     b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
159                     b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
160                     b->cpu&X264_CPU_LZCNT ? "_lzcnt" : "",
161                     ((int64_t)10*b->cycles/b->den - nop_time)/4 );
162         }
163 }
164
165 #if defined(ARCH_X86) || defined(ARCH_X86_64)
166 int x264_stack_pagealign( int (*func)(), int align );
167 #else
168 #define x264_stack_pagealign( func, align ) func()
169 #endif
170
171 #define call_c1(func,...) func(__VA_ARGS__)
172
173 #ifdef ARCH_X86
174 /* detect when callee-saved regs aren't saved.
175  * needs an explicit asm check because it only sometimes crashes in normal use. */
176 long x264_checkasm_call( long (*func)(), int *ok, ... );
177 #define call_a1(func,...) x264_checkasm_call((long(*)())func, &ok, __VA_ARGS__)
178 #else
179 #define call_a1 call_c1
180 #endif
181
182 #define call_bench(func,cpu,...)\
183     if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
184     {\
185         uint32_t tsum = 0;\
186         int tcount = 0;\
187         int ti;\
188         call_a1(func, __VA_ARGS__);\
189         for( ti=0; ti<(cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
190         {\
191             uint32_t t = read_time();\
192             func(__VA_ARGS__);\
193             func(__VA_ARGS__);\
194             func(__VA_ARGS__);\
195             func(__VA_ARGS__);\
196             t = read_time() - t;\
197             if( t*tcount <= tsum*4 && ti > 0 )\
198             {\
199                 tsum += t;\
200                 tcount++;\
201             }\
202         }\
203         bench_t *b = get_bench( func_name, cpu );\
204         b->cycles += tsum;\
205         b->den += tcount;\
206         b->pointer = func;\
207     }
208
209 /* for most functions, run benchmark and correctness test at the same time.
210  * for those that modify their inputs, run the above macros separately */
211 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
212 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
213 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
214 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
215
216
217 static int check_pixel( int cpu_ref, int cpu_new )
218 {
219     x264_pixel_function_t pixel_c;
220     x264_pixel_function_t pixel_ref;
221     x264_pixel_function_t pixel_asm;
222     x264_predict_t predict_16x16[4+3];
223     x264_predict_t predict_8x8c[4+3];
224     x264_predict_t predict_4x4[9+3];
225     x264_predict8x8_t predict_8x8[9+3];
226     DECLARE_ALIGNED_16( uint8_t edge[33] );
227     uint16_t cost_mv[32];
228     int ret = 0, ok, used_asm;
229     int i, j;
230
231     x264_pixel_init( 0, &pixel_c );
232     x264_pixel_init( cpu_ref, &pixel_ref );
233     x264_pixel_init( cpu_new, &pixel_asm );
234     x264_predict_16x16_init( 0, predict_16x16 );
235     x264_predict_8x8c_init( 0, predict_8x8c );
236     x264_predict_8x8_init( 0, predict_8x8 );
237     x264_predict_4x4_init( 0, predict_4x4 );
238     x264_predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
239
240     // maximize sum
241     for( i=0; i<256; i++ )
242     {
243         int z = i|(i>>4);
244         z ^= z>>2;
245         z ^= z>>1;
246         buf3[i] = ~(buf4[i] = -(z&1));
247     }
248     // random pattern made of maxed pixel differences, in case an intermediate value overflows
249     for( ; i<0x1000; i++ )
250         buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
251
252 #define TEST_PIXEL( name, align ) \
253     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
254     { \
255         int res_c, res_asm; \
256         if( pixel_asm.name[i] != pixel_ref.name[i] ) \
257         { \
258             set_func_name( "%s_%s", #name, pixel_names[i] ); \
259             used_asm = 1; \
260             for( j=0; j<64; j++ ) \
261             { \
262                 res_c   = call_c( pixel_c.name[i], buf1, 16, buf2+j*!align, 64 ); \
263                 res_asm = call_a( pixel_asm.name[i], buf1, 16, buf2+j*!align, 64 ); \
264                 if( res_c != res_asm ) \
265                 { \
266                     ok = 0; \
267                     fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
268                     break; \
269                 } \
270             } \
271             for( j=0; j<0x1000 && ok; j+=256 ) \
272             { \
273                 res_c   = pixel_c  .name[i]( buf3+j, 16, buf4+j, 16 ); \
274                 res_asm = pixel_asm.name[i]( buf3+j, 16, buf4+j, 16 ); \
275                 if( res_c != res_asm ) \
276                 { \
277                     ok = 0; \
278                     fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
279                 } \
280             } \
281         } \
282     } \
283     report( "pixel " #name " :" );
284
285     TEST_PIXEL( sad, 0 );
286     TEST_PIXEL( sad_aligned, 1 );
287     TEST_PIXEL( ssd, 1 );
288     TEST_PIXEL( satd, 0 );
289     TEST_PIXEL( sa8d, 0 );
290
291 #define TEST_PIXEL_X( N ) \
292     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
293     { \
294         int res_c[4]={0}, res_asm[4]={0}; \
295         if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
296         { \
297             set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
298             used_asm = 1; \
299             for( j=0; j<64; j++) \
300             { \
301                 uint8_t *pix2 = buf2+j; \
302                 res_c[0] = pixel_c.sad[i]( buf1, 16, pix2, 64 ); \
303                 res_c[1] = pixel_c.sad[i]( buf1, 16, pix2+6, 64 ); \
304                 res_c[2] = pixel_c.sad[i]( buf1, 16, pix2+1, 64 ); \
305                 if(N==4) \
306                 { \
307                     res_c[3] = pixel_c.sad[i]( buf1, 16, pix2+10, 64 ); \
308                     call_a( pixel_asm.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
309                 } \
310                 else \
311                     call_a( pixel_asm.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
312                 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
313                 { \
314                     ok = 0; \
315                     fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
316                              i, res_c[0], res_c[1], res_c[2], res_c[3], \
317                              res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
318                 } \
319                 if(N==4) \
320                     call_c2( pixel_c.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
321                 else \
322                     call_c2( pixel_c.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
323             } \
324         } \
325     } \
326     report( "pixel sad_x"#N" :" );
327
328     TEST_PIXEL_X(3);
329     TEST_PIXEL_X(4);
330
331 #define TEST_PIXEL_VAR( i ) \
332     if( pixel_asm.var[i] != pixel_ref.var[i] ) \
333     { \
334         int res_c, res_asm; \
335         set_func_name( "%s_%s", "var", pixel_names[i] ); \
336         used_asm = 1; \
337         res_c   = call_c( pixel_c.var[i], buf1, 16 ); \
338         res_asm = call_a( pixel_asm.var[i], buf1, 16 ); \
339         if( res_c != res_asm ) \
340         { \
341             ok = 0; \
342             fprintf( stderr, "var[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
343         } \
344     }
345
346     ok = 1; used_asm = 0;
347     TEST_PIXEL_VAR( PIXEL_16x16 );
348     TEST_PIXEL_VAR( PIXEL_8x8 );
349     report( "pixel var :" );
350
351     for( i=0, ok=1, used_asm=0; i<4; i++ )
352         if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
353         {
354             set_func_name( "hadamard_ac_%s", pixel_names[i] );
355             used_asm = 1;
356             for( j=0; j<32; j++ )
357             {
358                 uint8_t *pix = (j&16 ? buf1 : buf3) + (j&15)*256;
359                 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
360                 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
361                 if( rc != ra )
362                 {
363                     ok = 0;
364                     fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
365                     break;
366                 }
367             }
368             call_c2( pixel_c.hadamard_ac[i], buf1, 16 );
369             call_a2( pixel_asm.hadamard_ac[i], buf1, 16 );
370         }
371     report( "pixel hadamard_ac :" );
372
373 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
374     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
375     { \
376         int res_c[3], res_asm[3]; \
377         set_func_name( #name );\
378         used_asm = 1; \
379         memcpy( buf3, buf2, 1024 ); \
380         for( i=0; i<3; i++ ) \
381         { \
382             pred[i]( buf3+48, ##__VA_ARGS__ ); \
383             res_c[i] = pixel_c.satd( buf1+48, 16, buf3+48, 32 ); \
384         } \
385         call_a( pixel_asm.name, buf1+48, i8x8 ? edge : buf3+48, res_asm ); \
386         if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
387         { \
388             ok = 0; \
389             fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
390                      res_c[0], res_c[1], res_c[2], \
391                      res_asm[0], res_asm[1], res_asm[2] ); \
392         } \
393     }
394
395     ok = 1; used_asm = 0;
396     TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
397     TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8]  , 0 );
398     TEST_INTRA_MBCMP( intra_satd_x3_4x4  , predict_4x4  , satd[PIXEL_4x4]  , 0 );
399     TEST_INTRA_MBCMP( intra_sa8d_x3_8x8  , predict_8x8  , sa8d[PIXEL_8x8]  , 1, edge );
400     report( "intra satd_x3 :" );
401     TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
402     report( "intra sad_x3 :" );
403
404     if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
405         pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
406     {
407         float res_c, res_a;
408         int sums[5][4] = {{0}};
409         used_asm = ok = 1;
410         x264_emms();
411         res_c = x264_pixel_ssim_wxh( &pixel_c,   buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
412         res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
413         if( fabs(res_c - res_a) > 1e-6 )
414         {
415             ok = 0;
416             fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
417         }
418         set_func_name( "ssim_core" );
419         call_c2( pixel_c.ssim_4x4x2_core,   buf1+2, 32, buf2+2, 32, sums );
420         call_a2( pixel_asm.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
421         set_func_name( "ssim_end" );
422         call_c2( pixel_c.ssim_end4,   sums, sums, 4 );
423         call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
424         report( "ssim :" );
425     }
426
427     ok = 1; used_asm = 0;
428     for( i=0; i<32; i++ )
429         cost_mv[i] = i*10;
430     for( i=0; i<100 && ok; i++ )
431         if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
432         {
433             DECLARE_ALIGNED_16( uint16_t sums[72] );
434             DECLARE_ALIGNED_16( int dc[4] );
435             int16_t mvs_a[32], mvs_c[32];
436             int mvn_a, mvn_c;
437             int thresh = rand() & 0x3fff;
438             set_func_name( "esa_ads" );
439             for( j=0; j<72; j++ )
440                 sums[j] = rand() & 0x3fff;
441             for( j=0; j<4; j++ )
442                 dc[j] = rand() & 0x3fff;
443             used_asm = 1;
444             mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
445             mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
446             if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
447             {
448                 ok = 0;
449                 printf("c%d: ", i&3);
450                 for(j=0; j<mvn_c; j++)
451                     printf("%d ", mvs_c[j]);
452                 printf("\na%d: ", i&3);
453                 for(j=0; j<mvn_a; j++)
454                     printf("%d ", mvs_a[j]);
455                 printf("\n\n");
456             }
457         }
458     report( "esa ads:" );
459
460     return ret;
461 }
462
463 static int check_dct( int cpu_ref, int cpu_new )
464 {
465     x264_dct_function_t dct_c;
466     x264_dct_function_t dct_ref;
467     x264_dct_function_t dct_asm;
468     x264_quant_function_t qf;
469     int ret = 0, ok, used_asm, i, j, interlace;
470     DECLARE_ALIGNED_16( int16_t dct1[16][4][4] );
471     DECLARE_ALIGNED_16( int16_t dct2[16][4][4] );
472     DECLARE_ALIGNED_16( int16_t dct4[16][4][4] );
473     DECLARE_ALIGNED_16( int16_t dct8[4][8][8] );
474     x264_t h_buf;
475     x264_t *h = &h_buf;
476
477     x264_dct_init( 0, &dct_c );
478     x264_dct_init( cpu_ref, &dct_ref);
479     x264_dct_init( cpu_new, &dct_asm );
480
481     memset( h, 0, sizeof(*h) );
482     h->pps = h->pps_array;
483     x264_param_default( &h->param );
484     h->param.analyse.i_luma_deadzone[0] = 0;
485     h->param.analyse.i_luma_deadzone[1] = 0;
486     h->param.analyse.b_transform_8x8 = 1;
487     for( i=0; i<6; i++ )
488         h->pps->scaling_list[i] = x264_cqm_flat16;
489     x264_cqm_init( h );
490     x264_quant_init( h, 0, &qf );
491
492 #define TEST_DCT( name, t1, t2, size ) \
493     if( dct_asm.name != dct_ref.name ) \
494     { \
495         set_func_name( #name );\
496         used_asm = 1; \
497         call_c( dct_c.name, t1, buf1, buf2 ); \
498         call_a( dct_asm.name, t2, buf1, buf2 ); \
499         if( memcmp( t1, t2, size ) ) \
500         { \
501             ok = 0; \
502             fprintf( stderr, #name " [FAILED]\n" ); \
503         } \
504     }
505     ok = 1; used_asm = 0;
506     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
507     TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
508     TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
509     report( "sub_dct4 :" );
510
511     ok = 1; used_asm = 0;
512     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
513     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
514     report( "sub_dct8 :" );
515 #undef TEST_DCT
516
517     // fdct and idct are denormalized by different factors, so quant/dequant
518     // is needed to force the coefs into the right range.
519     dct_c.sub16x16_dct( dct4, buf1, buf2 );
520     dct_c.sub16x16_dct8( dct8, buf1, buf2 );
521     for( i=0; i<16; i++ )
522     {
523         qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
524         qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
525     }
526     for( i=0; i<4; i++ )
527     {
528         qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
529         qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
530     }
531
532 #define TEST_IDCT( name, src ) \
533     if( dct_asm.name != dct_ref.name ) \
534     { \
535         set_func_name( #name );\
536         used_asm = 1; \
537         memcpy( buf3, buf1, 32*32 ); \
538         memcpy( buf4, buf1, 32*32 ); \
539         memcpy( dct1, src, 512 ); \
540         memcpy( dct2, src, 512 ); \
541         call_c1( dct_c.name, buf3, (void*)dct1 ); \
542         call_a1( dct_asm.name, buf4, (void*)dct2 ); \
543         if( memcmp( buf3, buf4, 32*32 ) ) \
544         { \
545             ok = 0; \
546             fprintf( stderr, #name " [FAILED]\n" ); \
547         } \
548         call_c2( dct_c.name, buf3, (void*)dct1 ); \
549         call_a2( dct_asm.name, buf4, (void*)dct2 ); \
550     }
551     ok = 1; used_asm = 0;
552     TEST_IDCT( add4x4_idct, dct4 );
553     TEST_IDCT( add8x8_idct, dct4 );
554     TEST_IDCT( add8x8_idct_dc, dct4 );
555     TEST_IDCT( add16x16_idct, dct4 );
556     report( "add_idct4 :" );
557
558     ok = 1; used_asm = 0;
559     TEST_IDCT( add8x8_idct8, dct8 );
560     TEST_IDCT( add16x16_idct8, dct8 );
561     report( "add_idct8 :" );
562 #undef TEST_IDCT
563
564 #define TEST_DCTDC( name )\
565     ok = 1; used_asm = 0;\
566     if( dct_asm.name != dct_ref.name )\
567     {\
568         set_func_name( #name );\
569         used_asm = 1;\
570         uint16_t *p = (uint16_t*)buf1;\
571         for( i=0; i<16 && ok; i++ )\
572         {\
573             for( j=0; j<16; j++ )\
574                 dct1[0][0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
575                               : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
576                               : ((*p++)&0x1fff)-0x1000; /* general case */\
577             memcpy( dct2, dct1, 32 );\
578             call_c1( dct_c.name, dct1[0] );\
579             call_a1( dct_asm.name, dct2[0] );\
580             if( memcmp( dct1, dct2, 32 ) )\
581                 ok = 0;\
582         }\
583         call_c2( dct_c.name, dct1[0] );\
584         call_a2( dct_asm.name, dct2[0] );\
585     }\
586     report( #name " :" );
587
588     TEST_DCTDC(  dct4x4dc );
589     TEST_DCTDC( idct4x4dc );
590 #undef TEST_DCTDC
591
592     x264_zigzag_function_t zigzag_c;
593     x264_zigzag_function_t zigzag_ref;
594     x264_zigzag_function_t zigzag_asm;
595
596     DECLARE_ALIGNED_16( int16_t level1[64] );
597     DECLARE_ALIGNED_16( int16_t level2[64] );
598
599 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size )   \
600     if( zigzag_asm.name != zigzag_ref.name ) \
601     { \
602         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
603         used_asm = 1; \
604         memcpy(dct, buf1, size*sizeof(int16_t));\
605         call_c( zigzag_c.name, t1, dct ); \
606         call_a( zigzag_asm.name, t2, dct ); \
607         if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
608         { \
609             ok = 0; \
610             fprintf( stderr, #name " [FAILED]\n" ); \
611         } \
612     }
613
614 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
615     if( zigzag_asm.name != zigzag_ref.name ) \
616     { \
617         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
618         used_asm = 1; \
619         memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
620         memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
621         call_c1( zigzag_c.name, t1, buf2, buf3 );  \
622         call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
623         if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) )  \
624         { \
625             ok = 0; \
626             fprintf( stderr, #name " [FAILED]\n" ); \
627         } \
628         call_c2( zigzag_c.name, t1, buf2, buf3 );  \
629         call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
630     }
631
632     interlace = 0;
633     x264_zigzag_init( 0, &zigzag_c, 0 );
634     x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
635     x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
636
637     ok = 1; used_asm = 0;
638     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
639     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
640     TEST_ZIGZAG_SCAN( interleave_8x8_cavlc, level1, level2, (void*)dct1, 64 );
641     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
642     report( "zigzag_frame :" );
643
644     interlace = 1;
645     x264_zigzag_init( 0, &zigzag_c, 1 );
646     x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
647     x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
648
649     ok = 1; used_asm = 0;
650     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
651     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
652     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
653     report( "zigzag_field :" );
654 #undef TEST_ZIGZAG_SCAN
655 #undef TEST_ZIGZAG_SUB
656
657     return ret;
658 }
659
660 static int check_mc( int cpu_ref, int cpu_new )
661 {
662     x264_mc_functions_t mc_c;
663     x264_mc_functions_t mc_ref;
664     x264_mc_functions_t mc_a;
665     x264_pixel_function_t pixel;
666
667     uint8_t *src     = &buf1[2*32+2];
668     uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
669                          &buf1[7*64+2], &buf1[9*64+2] };
670     uint8_t *dst1    = buf3;
671     uint8_t *dst2    = buf4;
672
673     int dx, dy, i, j, k, w;
674     int ret = 0, ok, used_asm;
675
676     x264_mc_init( 0, &mc_c );
677     x264_mc_init( cpu_ref, &mc_ref );
678     x264_mc_init( cpu_new, &mc_a );
679     x264_pixel_init( 0, &pixel );
680
681 #define MC_TEST_LUMA( w, h ) \
682         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
683         { \
684             set_func_name( "mc_luma_%dx%d", w, h );\
685             used_asm = 1; \
686             memset(buf3, 0xCD, 1024); \
687             memset(buf4, 0xCD, 1024); \
688             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
689             call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h ); \
690             if( memcmp( buf3, buf4, 1024 ) ) \
691             { \
692                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
693                 ok = 0; \
694             } \
695         } \
696         if( mc_a.get_ref != mc_ref.get_ref ) \
697         { \
698             uint8_t *ref = dst2; \
699             int ref_stride = 32; \
700             set_func_name( "get_ref_%dx%d", w, h );\
701             used_asm = 1; \
702             memset(buf3, 0xCD, 1024); \
703             memset(buf4, 0xCD, 1024); \
704             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
705             ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h ); \
706             for( i=0; i<h; i++ ) \
707                 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
708                 { \
709                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
710                     ok = 0; \
711                     break; \
712                 } \
713         }
714
715 #define MC_TEST_CHROMA( w, h ) \
716         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
717         { \
718             set_func_name( "mc_chroma_%dx%d", w, h );\
719             used_asm = 1; \
720             memset(buf3, 0xCD, 1024); \
721             memset(buf4, 0xCD, 1024); \
722             call_c( mc_c.mc_chroma, dst1, 16, src, 32, dx, dy, w, h ); \
723             call_a( mc_a.mc_chroma, dst2, 16, src, 32, dx, dy, w, h ); \
724             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
725             for( j=0; j<h; j++ ) \
726                 for( i=w; i<4; i++ ) \
727                     dst2[i+j*16] = dst1[i+j*16]; \
728             if( memcmp( buf3, buf4, 1024 ) ) \
729             { \
730                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
731                 ok = 0; \
732             } \
733         }
734     ok = 1; used_asm = 0;
735     for( dy = -8; dy < 8; dy++ )
736         for( dx = -128; dx < 128; dx++ )
737         {
738             if( rand()&15 ) continue; // running all of them is too slow
739             MC_TEST_LUMA( 20, 18 );
740             MC_TEST_LUMA( 16, 16 );
741             MC_TEST_LUMA( 16, 8 );
742             MC_TEST_LUMA( 12, 10 );
743             MC_TEST_LUMA( 8, 16 );
744             MC_TEST_LUMA( 8, 8 );
745             MC_TEST_LUMA( 8, 4 );
746             MC_TEST_LUMA( 4, 8 );
747             MC_TEST_LUMA( 4, 4 );
748         }
749     report( "mc luma :" );
750
751     ok = 1; used_asm = 0;
752     for( dy = -1; dy < 9; dy++ )
753         for( dx = -1; dx < 9; dx++ )
754         {
755             MC_TEST_CHROMA( 8, 8 );
756             MC_TEST_CHROMA( 8, 4 );
757             MC_TEST_CHROMA( 4, 8 );
758             MC_TEST_CHROMA( 4, 4 );
759             MC_TEST_CHROMA( 4, 2 );
760             MC_TEST_CHROMA( 2, 4 );
761             MC_TEST_CHROMA( 2, 2 );
762         }
763     report( "mc chroma :" );
764 #undef MC_TEST_LUMA
765 #undef MC_TEST_CHROMA
766
767 #define MC_TEST_AVG( name, weight ) \
768     for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
769     { \
770         memcpy( buf3, buf1+320, 320 ); \
771         memcpy( buf4, buf1+320, 320 ); \
772         if( mc_a.name[i] != mc_ref.name[i] ) \
773         { \
774             set_func_name( "%s_%s", #name, pixel_names[i] );\
775             used_asm = 1; \
776             call_c1( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
777             call_a1( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
778             if( memcmp( buf3, buf4, 320 ) ) \
779             { \
780                 ok = 0; \
781                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
782             } \
783             call_c2( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
784             call_a2( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
785         } \
786     }
787     ok = 1; used_asm = 0;
788     for( w = -63; w <= 127 && ok; w++ )
789         MC_TEST_AVG( avg, w );
790     report( "mc wpredb :" );
791
792     if( mc_a.hpel_filter != mc_ref.hpel_filter )
793     {
794         uint8_t *src = buf1+8+2*64;
795         uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
796         uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
797         void *tmp = buf3+49*64;
798         set_func_name( "hpel_filter" );
799         ok = 1; used_asm = 1;
800         memset( buf3, 0, 4096 );
801         memset( buf4, 0, 4096 );
802         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10, tmp );
803         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10, tmp );
804         for( i=0; i<3; i++ )
805             for( j=0; j<10; j++ )
806                 //FIXME ideally the first pixels would match too, but they aren't actually used
807                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
808                 {
809                     ok = 0;
810                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
811                     for( k=0; k<48; k++ )
812                         printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
813                     printf("\n");
814                     for( k=0; k<48; k++ )
815                         printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
816                     printf("\n");
817                     break;
818                 }
819         report( "hpel filter :" );
820     }
821
822     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
823     {
824         uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
825         uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf3+3072 };
826         set_func_name( "lowres_init" );
827         ok = 1; used_asm = 1;
828         for( w=40; w<=48; w+=8 )
829         {
830             int stride = (w+8)&~15;
831             call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
832             call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
833             for( i=0; i<16; i++)
834             {
835                 for( j=0; j<4; j++)
836                     if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
837                     {
838                         ok = 0;
839                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
840                         for( k=0; k<w; k++ )
841                             printf( "%d ", dstc[j][k+i*stride] );
842                         printf("\n");
843                         for( k=0; k<w; k++ )
844                             printf( "%d ", dsta[j][k+i*stride] );
845                         printf("\n");
846                         break;
847                     }
848             }
849         }
850         report( "lowres init :" );
851     }
852
853 #define INTEGRAL_INIT( name, size, ... )\
854     if( mc_a.name != mc_ref.name )\
855     {\
856         int stride = 80;\
857         set_func_name( #name );\
858         used_asm = 1;\
859         memcpy( buf3, buf1, size*2*stride );\
860         memcpy( buf4, buf1, size*2*stride );\
861         uint16_t *sum = (uint16_t*)buf3;\
862         call_c1( mc_c.name, __VA_ARGS__ );\
863         sum = (uint16_t*)buf4;\
864         call_a1( mc_a.name, __VA_ARGS__ );\
865         if( memcmp( buf3, buf4, (stride-8)*2 )\
866             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
867             ok = 0;\
868         call_c2( mc_c.name, __VA_ARGS__ );\
869         call_a2( mc_a.name, __VA_ARGS__ );\
870     }
871     ok = 1; used_asm = 0;
872     INTEGRAL_INIT( integral_init4h, 2, sum+stride, buf2, stride );
873     INTEGRAL_INIT( integral_init8h, 2, sum+stride, buf2, stride );
874     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
875     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
876     report( "integral init :" );
877
878     return ret;
879 }
880
881 static int check_deblock( int cpu_ref, int cpu_new )
882 {
883     x264_deblock_function_t db_c;
884     x264_deblock_function_t db_ref;
885     x264_deblock_function_t db_a;
886     int ret = 0, ok = 1, used_asm = 0;
887     int alphas[36], betas[36];
888     int8_t tcs[36][4];
889     int a, c, i, j;
890
891     x264_deblock_init( 0, &db_c );
892     x264_deblock_init( cpu_ref, &db_ref );
893     x264_deblock_init( cpu_new, &db_a );
894
895     /* not exactly the real values of a,b,tc but close enough */
896     a = 255; c = 250;
897     for( i = 35; i >= 0; i-- )
898     {
899         alphas[i] = a;
900         betas[i] = (i+1)/2;
901         tcs[i][0] = tcs[i][2] = (c+6)/10;
902         tcs[i][1] = tcs[i][3] = (c+9)/20;
903         a = a*9/10;
904         c = c*9/10;
905     }
906
907 #define TEST_DEBLOCK( name, align, ... ) \
908     for( i = 0; i < 36; i++ ) \
909     { \
910         int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
911         for( j = 0; j < 1024; j++ ) \
912             /* two distributions of random to excersize different failure modes */\
913             buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
914         memcpy( buf4, buf3, 1024 ); \
915         if( db_a.name != db_ref.name ) \
916         { \
917             set_func_name( #name );\
918             used_asm = 1; \
919             call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
920             call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
921             if( memcmp( buf3, buf4, 1024 ) ) \
922             { \
923                 ok = 0; \
924                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
925                 break; \
926             } \
927             call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
928             call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
929         } \
930     }
931
932     TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
933     TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
934     TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
935     TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
936     TEST_DEBLOCK( deblock_h_luma_intra, 0 );
937     TEST_DEBLOCK( deblock_v_luma_intra, 1 );
938     TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
939     TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
940
941     report( "deblock :" );
942
943     return ret;
944 }
945
946 static int check_quant( int cpu_ref, int cpu_new )
947 {
948     x264_quant_function_t qf_c;
949     x264_quant_function_t qf_ref;
950     x264_quant_function_t qf_a;
951     DECLARE_ALIGNED_16( int16_t dct1[64] );
952     DECLARE_ALIGNED_16( int16_t dct2[64] );
953     DECLARE_ALIGNED_16( uint8_t cqm_buf[64] );
954     int ret = 0, ok, used_asm;
955     int oks[2] = {1,1}, used_asms[2] = {0,0};
956     int i, i_cqm, qp;
957     x264_t h_buf;
958     x264_t *h = &h_buf;
959     memset( h, 0, sizeof(*h) );
960     h->pps = h->pps_array;
961     x264_param_default( &h->param );
962     h->param.rc.i_qp_min = 26;
963     h->param.analyse.b_transform_8x8 = 1;
964
965     for( i_cqm = 0; i_cqm < 4; i_cqm++ )
966     {
967         if( i_cqm == 0 )
968         {
969             for( i = 0; i < 6; i++ )
970                 h->pps->scaling_list[i] = x264_cqm_flat16;
971             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
972         }
973         else if( i_cqm == 1 )
974         {
975             for( i = 0; i < 6; i++ )
976                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
977             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
978         }
979         else
980         {
981             if( i_cqm == 2 )
982                 for( i = 0; i < 64; i++ )
983                     cqm_buf[i] = 10 + rand() % 246;
984             else
985                 for( i = 0; i < 64; i++ )
986                     cqm_buf[i] = 1;
987             for( i = 0; i < 6; i++ )
988                 h->pps->scaling_list[i] = cqm_buf;
989             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
990         }
991
992         x264_cqm_init( h );
993         x264_quant_init( h, 0, &qf_c );
994         x264_quant_init( h, cpu_ref, &qf_ref );
995         x264_quant_init( h, cpu_new, &qf_a );
996
997 #define INIT_QUANT8() \
998         { \
999             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1000             int x, y; \
1001             for( y = 0; y < 8; y++ ) \
1002                 for( x = 0; x < 8; x++ ) \
1003                 { \
1004                     unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
1005                     dct1[y*8+x] = dct2[y*8+x] = (rand()%(2*scale+1))-scale; \
1006                 } \
1007         }
1008
1009 #define INIT_QUANT4() \
1010         { \
1011             static const int scale1d[4] = {4,6,4,6}; \
1012             int x, y; \
1013             for( y = 0; y < 4; y++ ) \
1014                 for( x = 0; x < 4; x++ ) \
1015                 { \
1016                     unsigned int scale = 255*scale1d[y]*scale1d[x]; \
1017                     dct1[y*4+x] = dct2[y*4+x] = (rand()%(2*scale+1))-scale; \
1018                 } \
1019         }
1020
1021 #define TEST_QUANT_DC( name, cqm ) \
1022         if( qf_a.name != qf_ref.name ) \
1023         { \
1024             set_func_name( #name ); \
1025             used_asms[0] = 1; \
1026             for( qp = 51; qp > 0; qp-- ) \
1027             { \
1028                 for( i = 0; i < 16; i++ ) \
1029                     dct1[i] = dct2[i] = (rand() & 0x1fff) - 0xfff; \
1030                 call_c1( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1031                 call_a1( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1032                 if( memcmp( dct1, dct2, 16*2 ) )       \
1033                 { \
1034                     oks[0] = 0; \
1035                     fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1036                     break; \
1037                 } \
1038                 call_c2( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1039                 call_a2( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1040             } \
1041         }
1042
1043 #define TEST_QUANT( qname, block, w ) \
1044         if( qf_a.qname != qf_ref.qname ) \
1045         { \
1046             set_func_name( #qname ); \
1047             used_asms[0] = 1; \
1048             for( qp = 51; qp > 0; qp-- ) \
1049             { \
1050                 INIT_QUANT##w() \
1051                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1052                 call_a1( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1053                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1054                 { \
1055                     oks[0] = 0; \
1056                     fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1057                     break; \
1058                 } \
1059                 call_c2( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1060                 call_a2( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1061             } \
1062         }
1063
1064         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1065         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1066         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1067         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1068         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1069         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1070
1071 #define TEST_DEQUANT( qname, dqname, block, w ) \
1072         if( qf_a.dqname != qf_ref.dqname ) \
1073         { \
1074             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1075             used_asms[1] = 1; \
1076             for( qp = 51; qp > 0; qp-- ) \
1077             { \
1078                 INIT_QUANT##w() \
1079                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1080                 memcpy( dct2, dct1, w*w*2 ); \
1081                 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1082                 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1083                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1084                 { \
1085                     oks[1] = 0; \
1086                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1087                     break; \
1088                 } \
1089                 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1090                 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1091             } \
1092         }
1093
1094         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1095         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1096         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1097         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1098
1099 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1100         if( qf_a.dqname != qf_ref.dqname ) \
1101         { \
1102             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1103             used_asms[1] = 1; \
1104             for( qp = 51; qp > 0; qp-- ) \
1105             { \
1106                 for( i = 0; i < 16; i++ ) \
1107                     dct1[i] = rand(); \
1108                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1109                 memcpy( dct2, dct1, w*w*2 ); \
1110                 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1111                 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1112                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1113                 { \
1114                     oks[1] = 0; \
1115                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1116                 } \
1117                 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1118                 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1119             } \
1120         }
1121
1122         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1123
1124         x264_cqm_delete( h );
1125     }
1126
1127     ok = oks[0]; used_asm = used_asms[0];
1128     report( "quant :" );
1129
1130     ok = oks[1]; used_asm = used_asms[1];
1131     report( "dequant :" );
1132
1133     ok = 1; used_asm = 0;
1134     if( qf_a.denoise_dct != qf_ref.denoise_dct )
1135     {
1136         int size;
1137         used_asm = 1;
1138         for( size = 16; size <= 64; size += 48 )
1139         {
1140             set_func_name( "denoise_dct" );
1141             memcpy(dct1, buf1, size*2);
1142             memcpy(dct2, buf1, size*2);
1143             memcpy(buf3+256, buf3, 256);
1144             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1145             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1146             if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1147                 ok = 0;
1148             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1149             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1150         }
1151     }
1152     report( "denoise dct :" );
1153
1154 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1155     if( qf_a.decname != qf_ref.decname ) \
1156     { \
1157         set_func_name( #decname ); \
1158         used_asm = 1; \
1159         for( i = 0; i < 100; i++ ) \
1160         { \
1161             int result_c, result_a, idx; \
1162             for( idx = 0; idx < w*w; idx++ ) \
1163                 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1164             if( ac ) \
1165                 dct1[0] = 0; \
1166             result_c = call_c( qf_c.decname, (void*)dct1 ); \
1167             result_a = call_a( qf_a.decname, (void*)dct1 ); \
1168             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1169             { \
1170                 ok = 0; \
1171                 fprintf( stderr, #decname ": [FAILED]\n" ); \
1172                 break; \
1173             } \
1174         } \
1175     }
1176
1177     ok = 1; used_asm = 0;
1178     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1179     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1180     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1181     report( "decimate_score :" );
1182
1183 #define TEST_LAST( last, lastname, w, ac ) \
1184     if( qf_a.last != qf_ref.last ) \
1185     { \
1186         set_func_name( #lastname ); \
1187         used_asm = 1; \
1188         for( i = 0; i < 100; i++ ) \
1189         { \
1190             int result_c, result_a, idx, nnz=0; \
1191             int max = rand() & (w*w-1); \
1192             memset( dct1, 0, w*w*2 ); \
1193             for( idx = ac; idx < max; idx++ ) \
1194                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1195             if( !nnz ) \
1196                 dct1[ac] = 1; \
1197             result_c = call_c( qf_c.last, (void*)(dct1+ac) ); \
1198             result_a = call_a( qf_a.last, (void*)(dct1+ac) ); \
1199             if( result_c != result_a ) \
1200             { \
1201                 ok = 0; \
1202                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1203                 break; \
1204             } \
1205         } \
1206     }
1207
1208     ok = 1; used_asm = 0;
1209     TEST_LAST( coeff_last[DCT_CHROMA_DC],  coeff_last4, 2, 0 );
1210     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 4, 1 );
1211     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1212     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1213     report( "coeff_last :" );
1214
1215 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1216     if( qf_a.lastname != qf_ref.lastname ) \
1217     { \
1218         set_func_name( #name ); \
1219         used_asm = 1; \
1220         for( i = 0; i < 100; i++ ) \
1221         { \
1222             x264_run_level_t runlevel_c, runlevel_a; \
1223             int result_c, result_a, idx, nnz=0; \
1224             int max = rand() & (w*w-1); \
1225             memset( dct1, 0, w*w*2 ); \
1226             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1227             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1228             for( idx = ac; idx < max; idx++ ) \
1229                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1230             if( !nnz ) \
1231                 dct1[ac] = 1; \
1232             result_c = call_c( qf_c.lastname, (void*)(dct1+ac), &runlevel_c ); \
1233             result_a = call_a( qf_a.lastname, (void*)(dct1+ac), &runlevel_a ); \
1234             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1235                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1236                 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1237             { \
1238                 ok = 0; \
1239                 fprintf( stderr, #name ": [FAILED]\n" ); \
1240                 break; \
1241             } \
1242         } \
1243     }
1244
1245     ok = 1; used_asm = 0;
1246     TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC],  coeff_level_run4, 2, 0 );
1247     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1248     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1249     report( "coeff_level_run :" );
1250
1251     return ret;
1252 }
1253
1254 static int check_intra( int cpu_ref, int cpu_new )
1255 {
1256     int ret = 0, ok = 1, used_asm = 0;
1257     int i;
1258     DECLARE_ALIGNED_16( uint8_t edge[33] );
1259     struct
1260     {
1261         x264_predict_t      predict_16x16[4+3];
1262         x264_predict_t      predict_8x8c[4+3];
1263         x264_predict8x8_t   predict_8x8[9+3];
1264         x264_predict_t      predict_4x4[9+3];
1265     } ip_c, ip_ref, ip_a;
1266
1267     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1268     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1269     x264_predict_8x8_init( 0, ip_c.predict_8x8 );
1270     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1271
1272     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1273     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1274     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8 );
1275     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1276
1277     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1278     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1279     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8 );
1280     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1281
1282     x264_predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1283
1284 #define INTRA_TEST( name, dir, w, ... ) \
1285     if( ip_a.name[dir] != ip_ref.name[dir] )\
1286     { \
1287         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1288         used_asm = 1; \
1289         memcpy( buf3, buf1, 32*20 );\
1290         memcpy( buf4, buf1, 32*20 );\
1291         call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1292         call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1293         if( memcmp( buf3, buf4, 32*20 ) )\
1294         {\
1295             fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
1296             ok = 0;\
1297             int j,k;\
1298             for(k=-1; k<16; k++)\
1299                 printf("%2x ", edge[16+k]);\
1300             printf("\n");\
1301             for(j=0; j<w; j++){\
1302                 printf("%2x ", edge[14-j]);\
1303                 for(k=0; k<w; k++)\
1304                     printf("%2x ", buf4[48+k+j*32]);\
1305                 printf("\n");\
1306             }\
1307             printf("\n");\
1308             for(j=0; j<w; j++){\
1309                 printf("   ");\
1310                 for(k=0; k<w; k++)\
1311                     printf("%2x ", buf3[48+k+j*32]);\
1312                 printf("\n");\
1313             }\
1314         }\
1315     }
1316
1317     for( i = 0; i < 12; i++ )
1318         INTRA_TEST( predict_4x4, i, 4 );
1319     for( i = 0; i < 7; i++ )
1320         INTRA_TEST( predict_8x8c, i, 8 );
1321     for( i = 0; i < 7; i++ )
1322         INTRA_TEST( predict_16x16, i, 16 );
1323     for( i = 0; i < 12; i++ )
1324         INTRA_TEST( predict_8x8, i, 8, edge );
1325
1326     report( "intra pred :" );
1327     return ret;
1328 }
1329
1330 #define DECL_CABAC(cpu) \
1331 static void run_cabac_##cpu( uint8_t *dst )\
1332 {\
1333     int i;\
1334     x264_cabac_t cb;\
1335     x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1336     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1337     for( i=0; i<0x1000; i++ )\
1338         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1339 }
1340 DECL_CABAC(c)
1341 #ifdef HAVE_MMX
1342 DECL_CABAC(asm)
1343 #else
1344 #define run_cabac_asm run_cabac_c
1345 #endif
1346
1347 static int check_cabac( int cpu_ref, int cpu_new )
1348 {
1349     int ret = 0, ok, used_asm = 1;
1350     if( cpu_ref || run_cabac_c == run_cabac_asm)
1351         return 0;
1352     set_func_name( "cabac_encode_decision" );
1353     memcpy( buf4, buf3, 0x1000 );
1354     call_c( run_cabac_c, buf3 );
1355     call_a( run_cabac_asm, buf4 );
1356     ok = !memcmp( buf3, buf4, 0x1000 );
1357     report( "cabac :" );
1358     return ret;
1359 }
1360
1361 static int check_all_funcs( int cpu_ref, int cpu_new )
1362 {
1363     return check_pixel( cpu_ref, cpu_new )
1364          + check_dct( cpu_ref, cpu_new )
1365          + check_mc( cpu_ref, cpu_new )
1366          + check_intra( cpu_ref, cpu_new )
1367          + check_deblock( cpu_ref, cpu_new )
1368          + check_quant( cpu_ref, cpu_new )
1369          + check_cabac( cpu_ref, cpu_new );
1370 }
1371
1372 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1373 {
1374     *cpu_ref = *cpu_new;
1375     *cpu_new |= flags;
1376     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1377         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1378     if( !quiet )
1379         fprintf( stderr, "x264: %s\n", name );
1380     return check_all_funcs( *cpu_ref, *cpu_new );
1381 }
1382
1383 static int check_all_flags( void )
1384 {
1385     int ret = 0;
1386     int cpu0 = 0, cpu1 = 0;
1387 #ifdef HAVE_MMX
1388     if( x264_cpu_detect() & X264_CPU_MMXEXT )
1389     {
1390         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1391         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1392         cpu1 &= ~X264_CPU_CACHELINE_64;
1393 #ifdef ARCH_X86
1394         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1395         cpu1 &= ~X264_CPU_CACHELINE_32;
1396 #endif
1397         if( x264_cpu_detect() & X264_CPU_LZCNT )
1398         {
1399             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1400             cpu1 &= ~X264_CPU_LZCNT;
1401         }
1402     }
1403     if( x264_cpu_detect() & X264_CPU_SSE2 )
1404     {
1405         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1406         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1407         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1408     }
1409     if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1410     {
1411         cpu1 &= ~X264_CPU_CACHELINE_64;
1412         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1413         cpu1 &= ~X264_CPU_SSE_MISALIGN;
1414     }
1415     if( x264_cpu_detect() & X264_CPU_LZCNT )
1416     {
1417         cpu1 &= ~X264_CPU_CACHELINE_64;
1418         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1419         cpu1 &= ~X264_CPU_LZCNT;
1420     }
1421     if( x264_cpu_detect() & X264_CPU_SSE3 )
1422         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1423     if( x264_cpu_detect() & X264_CPU_SSSE3 )
1424     {
1425         cpu1 &= ~X264_CPU_CACHELINE_64;
1426         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1427         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1428         ret |= add_flags( &cpu0, &cpu1, X264_CPU_PHADD_IS_FAST, "PHADD" );
1429     }
1430     if( x264_cpu_detect() & X264_CPU_SSE4 )
1431     {
1432         cpu1 &= ~X264_CPU_CACHELINE_64;
1433         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1434     }
1435 #elif ARCH_PPC
1436     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1437     {
1438         fprintf( stderr, "x264: ALTIVEC against C\n" );
1439         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1440     }
1441 #endif
1442     return ret;
1443 }
1444
1445 int main(int argc, char *argv[])
1446 {
1447     int ret = 0;
1448     int i;
1449
1450     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1451     {
1452 #if !defined(ARCH_X86) && !defined(ARCH_X86_64)
1453         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1454         return 1;
1455 #endif
1456         do_bench = 1;
1457         if( argv[1][7] == '=' )
1458         {
1459             bench_pattern = argv[1]+8;
1460             bench_pattern_len = strlen(bench_pattern);
1461         }
1462         argc--;
1463         argv++;
1464     }
1465
1466     i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1467     fprintf( stderr, "x264: using random seed %u\n", i );
1468     srand( i );
1469
1470     buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1471     buf2 = buf1 + 0xf00;
1472     buf3 = buf2 + 0xf00;
1473     buf4 = buf3 + 0x1000;
1474     for( i=0; i<0x1e00; i++ )
1475         buf1[i] = rand() & 0xFF;
1476     memset( buf1+0x1e00, 0, 0x2000 );
1477
1478     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1479     if( do_bench )
1480         for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1481         {
1482             buf2 = buf1 + 0xf00;
1483             buf3 = buf2 + 0xf00;
1484             buf4 = buf3 + 0x1000;
1485             ret |= x264_stack_pagealign( check_all_flags, i*16 );
1486             buf1 += 16;
1487             quiet = 1;
1488             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1489         }
1490     else
1491         ret = check_all_flags();
1492
1493     if( ret )
1494     {
1495         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1496         return -1;
1497     }
1498     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
1499     if( do_bench )
1500         print_bench();
1501     return 0;
1502 }
1503