]> git.sesse.net Git - x264/blob - tools/checkasm.c
Add support for SSE4a (Phenom) LZCNT instruction
[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( add16x16_idct, dct4 );
555     report( "add_idct4 :" );
556
557     ok = 1; used_asm = 0;
558     TEST_IDCT( add8x8_idct8, dct8 );
559     TEST_IDCT( add16x16_idct8, dct8 );
560     report( "add_idct8 :" );
561 #undef TEST_IDCT
562
563 #define TEST_DCTDC( name )\
564     ok = 1; used_asm = 0;\
565     if( dct_asm.name != dct_ref.name )\
566     {\
567         set_func_name( #name );\
568         used_asm = 1;\
569         uint16_t *p = (uint16_t*)buf1;\
570         for( i=0; i<16 && ok; i++ )\
571         {\
572             for( j=0; j<16; j++ )\
573                 dct1[0][0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
574                               : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
575                               : ((*p++)&0x1fff)-0x1000; /* general case */\
576             memcpy( dct2, dct1, 32 );\
577             call_c1( dct_c.name, dct1[0] );\
578             call_a1( dct_asm.name, dct2[0] );\
579             if( memcmp( dct1, dct2, 32 ) )\
580                 ok = 0;\
581         }\
582         call_c2( dct_c.name, dct1[0] );\
583         call_a2( dct_asm.name, dct2[0] );\
584     }\
585     report( #name " :" );
586
587     TEST_DCTDC(  dct4x4dc );
588     TEST_DCTDC( idct4x4dc );
589 #undef TEST_DCTDC
590
591     x264_zigzag_function_t zigzag_c;
592     x264_zigzag_function_t zigzag_ref;
593     x264_zigzag_function_t zigzag_asm;
594
595     DECLARE_ALIGNED_16( int16_t level1[64] );
596     DECLARE_ALIGNED_16( int16_t level2[64] );
597
598 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size )   \
599     if( zigzag_asm.name != zigzag_ref.name ) \
600     { \
601         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
602         used_asm = 1; \
603         memcpy(dct, buf1, size*sizeof(int16_t));\
604         call_c( zigzag_c.name, t1, dct ); \
605         call_a( zigzag_asm.name, t2, dct ); \
606         if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
607         { \
608             ok = 0; \
609             fprintf( stderr, #name " [FAILED]\n" ); \
610         } \
611     }
612
613 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
614     if( zigzag_asm.name != zigzag_ref.name ) \
615     { \
616         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
617         used_asm = 1; \
618         memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
619         memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
620         call_c1( zigzag_c.name, t1, buf2, buf3 );  \
621         call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
622         if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) )  \
623         { \
624             ok = 0; \
625             fprintf( stderr, #name " [FAILED]\n" ); \
626         } \
627         call_c2( zigzag_c.name, t1, buf2, buf3 );  \
628         call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
629     }
630
631     interlace = 0;
632     x264_zigzag_init( 0, &zigzag_c, 0 );
633     x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
634     x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
635
636     ok = 1; used_asm = 0;
637     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
638     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
639     TEST_ZIGZAG_SCAN( interleave_8x8_cavlc, level1, level2, (void*)dct1, 64 );
640     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
641     report( "zigzag_frame :" );
642
643     interlace = 1;
644     x264_zigzag_init( 0, &zigzag_c, 1 );
645     x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
646     x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
647
648     ok = 1; used_asm = 0;
649     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
650     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
651     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
652     report( "zigzag_field :" );
653 #undef TEST_ZIGZAG_SCAN
654 #undef TEST_ZIGZAG_SUB
655
656     return ret;
657 }
658
659 static int check_mc( int cpu_ref, int cpu_new )
660 {
661     x264_mc_functions_t mc_c;
662     x264_mc_functions_t mc_ref;
663     x264_mc_functions_t mc_a;
664     x264_pixel_function_t pixel;
665
666     uint8_t *src     = &buf1[2*32+2];
667     uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
668                          &buf1[7*64+2], &buf1[9*64+2] };
669     uint8_t *dst1    = buf3;
670     uint8_t *dst2    = buf4;
671
672     int dx, dy, i, j, k, w;
673     int ret = 0, ok, used_asm;
674
675     x264_mc_init( 0, &mc_c );
676     x264_mc_init( cpu_ref, &mc_ref );
677     x264_mc_init( cpu_new, &mc_a );
678     x264_pixel_init( 0, &pixel );
679
680 #define MC_TEST_LUMA( w, h ) \
681         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
682         { \
683             set_func_name( "mc_luma_%dx%d", w, h );\
684             used_asm = 1; \
685             memset(buf3, 0xCD, 1024); \
686             memset(buf4, 0xCD, 1024); \
687             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
688             call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h ); \
689             if( memcmp( buf3, buf4, 1024 ) ) \
690             { \
691                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
692                 ok = 0; \
693             } \
694         } \
695         if( mc_a.get_ref != mc_ref.get_ref ) \
696         { \
697             uint8_t *ref = dst2; \
698             int ref_stride = 32; \
699             set_func_name( "get_ref_%dx%d", w, h );\
700             used_asm = 1; \
701             memset(buf3, 0xCD, 1024); \
702             memset(buf4, 0xCD, 1024); \
703             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
704             ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h ); \
705             for( i=0; i<h; i++ ) \
706                 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
707                 { \
708                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
709                     ok = 0; \
710                     break; \
711                 } \
712         }
713
714 #define MC_TEST_CHROMA( w, h ) \
715         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
716         { \
717             set_func_name( "mc_chroma_%dx%d", w, h );\
718             used_asm = 1; \
719             memset(buf3, 0xCD, 1024); \
720             memset(buf4, 0xCD, 1024); \
721             call_c( mc_c.mc_chroma, dst1, 16, src, 32, dx, dy, w, h ); \
722             call_a( mc_a.mc_chroma, dst2, 16, src, 32, dx, dy, w, h ); \
723             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
724             for( j=0; j<h; j++ ) \
725                 for( i=w; i<4; i++ ) \
726                     dst2[i+j*16] = dst1[i+j*16]; \
727             if( memcmp( buf3, buf4, 1024 ) ) \
728             { \
729                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
730                 ok = 0; \
731             } \
732         }
733     ok = 1; used_asm = 0;
734     for( dy = -8; dy < 8; dy++ )
735         for( dx = -128; dx < 128; dx++ )
736         {
737             if( rand()&15 ) continue; // running all of them is too slow
738             MC_TEST_LUMA( 20, 18 );
739             MC_TEST_LUMA( 16, 16 );
740             MC_TEST_LUMA( 16, 8 );
741             MC_TEST_LUMA( 12, 10 );
742             MC_TEST_LUMA( 8, 16 );
743             MC_TEST_LUMA( 8, 8 );
744             MC_TEST_LUMA( 8, 4 );
745             MC_TEST_LUMA( 4, 8 );
746             MC_TEST_LUMA( 4, 4 );
747         }
748     report( "mc luma :" );
749
750     ok = 1; used_asm = 0;
751     for( dy = -1; dy < 9; dy++ )
752         for( dx = -1; dx < 9; dx++ )
753         {
754             MC_TEST_CHROMA( 8, 8 );
755             MC_TEST_CHROMA( 8, 4 );
756             MC_TEST_CHROMA( 4, 8 );
757             MC_TEST_CHROMA( 4, 4 );
758             MC_TEST_CHROMA( 4, 2 );
759             MC_TEST_CHROMA( 2, 4 );
760             MC_TEST_CHROMA( 2, 2 );
761         }
762     report( "mc chroma :" );
763 #undef MC_TEST_LUMA
764 #undef MC_TEST_CHROMA
765
766 #define MC_TEST_AVG( name, weight ) \
767     for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
768     { \
769         memcpy( buf3, buf1+320, 320 ); \
770         memcpy( buf4, buf1+320, 320 ); \
771         if( mc_a.name[i] != mc_ref.name[i] ) \
772         { \
773             set_func_name( "%s_%s", #name, pixel_names[i] );\
774             used_asm = 1; \
775             call_c1( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
776             call_a1( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
777             if( memcmp( buf3, buf4, 320 ) ) \
778             { \
779                 ok = 0; \
780                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
781             } \
782             call_c2( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
783             call_a2( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
784         } \
785     }
786     ok = 1; used_asm = 0;
787     for( w = -63; w <= 127 && ok; w++ )
788         MC_TEST_AVG( avg, w );
789     report( "mc wpredb :" );
790
791     if( mc_a.hpel_filter != mc_ref.hpel_filter )
792     {
793         uint8_t *src = buf1+8+2*64;
794         uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
795         uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
796         void *tmp = buf3+49*64;
797         set_func_name( "hpel_filter" );
798         ok = 1; used_asm = 1;
799         memset( buf3, 0, 4096 );
800         memset( buf4, 0, 4096 );
801         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10, tmp );
802         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10, tmp );
803         for( i=0; i<3; i++ )
804             for( j=0; j<10; j++ )
805                 //FIXME ideally the first pixels would match too, but they aren't actually used
806                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
807                 {
808                     ok = 0;
809                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
810                     for( k=0; k<48; k++ )
811                         printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
812                     printf("\n");
813                     for( k=0; k<48; k++ )
814                         printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
815                     printf("\n");
816                     break;
817                 }
818         report( "hpel filter :" );
819     }
820
821     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
822     {
823         uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
824         uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf3+3072 };
825         set_func_name( "lowres_init" );
826         ok = 1; used_asm = 1;
827         for( w=40; w<=48; w+=8 )
828         {
829             int stride = (w+8)&~15;
830             call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
831             call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
832             for( i=0; i<16; i++)
833             {
834                 for( j=0; j<4; j++)
835                     if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
836                     {
837                         ok = 0;
838                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
839                         for( k=0; k<w; k++ )
840                             printf( "%d ", dstc[j][k+i*stride] );
841                         printf("\n");
842                         for( k=0; k<w; k++ )
843                             printf( "%d ", dsta[j][k+i*stride] );
844                         printf("\n");
845                         break;
846                     }
847             }
848         }
849         report( "lowres init :" );
850     }
851
852 #define INTEGRAL_INIT( name, size, ... )\
853     if( mc_a.name != mc_ref.name )\
854     {\
855         int stride = 80;\
856         set_func_name( #name );\
857         used_asm = 1;\
858         memcpy( buf3, buf1, size*2*stride );\
859         memcpy( buf4, buf1, size*2*stride );\
860         uint16_t *sum = (uint16_t*)buf3;\
861         call_c1( mc_c.name, __VA_ARGS__ );\
862         sum = (uint16_t*)buf4;\
863         call_a1( mc_a.name, __VA_ARGS__ );\
864         if( memcmp( buf3, buf4, (stride-8)*2 )\
865             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
866             ok = 0;\
867         call_c2( mc_c.name, __VA_ARGS__ );\
868         call_a2( mc_a.name, __VA_ARGS__ );\
869     }
870     ok = 1; used_asm = 0;
871     INTEGRAL_INIT( integral_init4h, 2, sum+stride, buf2, stride );
872     INTEGRAL_INIT( integral_init8h, 2, sum+stride, buf2, stride );
873     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
874     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
875     report( "integral init :" );
876
877     return ret;
878 }
879
880 static int check_deblock( int cpu_ref, int cpu_new )
881 {
882     x264_deblock_function_t db_c;
883     x264_deblock_function_t db_ref;
884     x264_deblock_function_t db_a;
885     int ret = 0, ok = 1, used_asm = 0;
886     int alphas[36], betas[36];
887     int8_t tcs[36][4];
888     int a, c, i, j;
889
890     x264_deblock_init( 0, &db_c );
891     x264_deblock_init( cpu_ref, &db_ref );
892     x264_deblock_init( cpu_new, &db_a );
893
894     /* not exactly the real values of a,b,tc but close enough */
895     a = 255; c = 250;
896     for( i = 35; i >= 0; i-- )
897     {
898         alphas[i] = a;
899         betas[i] = (i+1)/2;
900         tcs[i][0] = tcs[i][2] = (c+6)/10;
901         tcs[i][1] = tcs[i][3] = (c+9)/20;
902         a = a*9/10;
903         c = c*9/10;
904     }
905
906 #define TEST_DEBLOCK( name, align, ... ) \
907     for( i = 0; i < 36; i++ ) \
908     { \
909         int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
910         for( j = 0; j < 1024; j++ ) \
911             /* two distributions of random to excersize different failure modes */\
912             buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
913         memcpy( buf4, buf3, 1024 ); \
914         if( db_a.name != db_ref.name ) \
915         { \
916             set_func_name( #name );\
917             used_asm = 1; \
918             call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
919             call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
920             if( memcmp( buf3, buf4, 1024 ) ) \
921             { \
922                 ok = 0; \
923                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
924                 break; \
925             } \
926             call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
927             call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
928         } \
929     }
930
931     TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
932     TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
933     TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
934     TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
935     TEST_DEBLOCK( deblock_h_luma_intra, 0 );
936     TEST_DEBLOCK( deblock_v_luma_intra, 1 );
937     TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
938     TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
939
940     report( "deblock :" );
941
942     return ret;
943 }
944
945 static int check_quant( int cpu_ref, int cpu_new )
946 {
947     x264_quant_function_t qf_c;
948     x264_quant_function_t qf_ref;
949     x264_quant_function_t qf_a;
950     DECLARE_ALIGNED_16( int16_t dct1[64] );
951     DECLARE_ALIGNED_16( int16_t dct2[64] );
952     DECLARE_ALIGNED_16( uint8_t cqm_buf[64] );
953     int ret = 0, ok, used_asm;
954     int oks[2] = {1,1}, used_asms[2] = {0,0};
955     int i, i_cqm, qp;
956     x264_t h_buf;
957     x264_t *h = &h_buf;
958     memset( h, 0, sizeof(*h) );
959     h->pps = h->pps_array;
960     x264_param_default( &h->param );
961     h->param.rc.i_qp_min = 26;
962     h->param.analyse.b_transform_8x8 = 1;
963
964     for( i_cqm = 0; i_cqm < 4; i_cqm++ )
965     {
966         if( i_cqm == 0 )
967         {
968             for( i = 0; i < 6; i++ )
969                 h->pps->scaling_list[i] = x264_cqm_flat16;
970             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
971         }
972         else if( i_cqm == 1 )
973         {
974             for( i = 0; i < 6; i++ )
975                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
976             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
977         }
978         else
979         {
980             if( i_cqm == 2 )
981                 for( i = 0; i < 64; i++ )
982                     cqm_buf[i] = 10 + rand() % 246;
983             else
984                 for( i = 0; i < 64; i++ )
985                     cqm_buf[i] = 1;
986             for( i = 0; i < 6; i++ )
987                 h->pps->scaling_list[i] = cqm_buf;
988             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
989         }
990
991         x264_cqm_init( h );
992         x264_quant_init( h, 0, &qf_c );
993         x264_quant_init( h, cpu_ref, &qf_ref );
994         x264_quant_init( h, cpu_new, &qf_a );
995
996 #define INIT_QUANT8() \
997         { \
998             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
999             int x, y; \
1000             for( y = 0; y < 8; y++ ) \
1001                 for( x = 0; x < 8; x++ ) \
1002                 { \
1003                     unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
1004                     dct1[y*8+x] = dct2[y*8+x] = (rand()%(2*scale+1))-scale; \
1005                 } \
1006         }
1007
1008 #define INIT_QUANT4() \
1009         { \
1010             static const int scale1d[4] = {4,6,4,6}; \
1011             int x, y; \
1012             for( y = 0; y < 4; y++ ) \
1013                 for( x = 0; x < 4; x++ ) \
1014                 { \
1015                     unsigned int scale = 255*scale1d[y]*scale1d[x]; \
1016                     dct1[y*4+x] = dct2[y*4+x] = (rand()%(2*scale+1))-scale; \
1017                 } \
1018         }
1019
1020 #define TEST_QUANT_DC( name, cqm ) \
1021         if( qf_a.name != qf_ref.name ) \
1022         { \
1023             set_func_name( #name ); \
1024             used_asms[0] = 1; \
1025             for( qp = 51; qp > 0; qp-- ) \
1026             { \
1027                 for( i = 0; i < 16; i++ ) \
1028                     dct1[i] = dct2[i] = (rand() & 0x1fff) - 0xfff; \
1029                 call_c1( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1030                 call_a1( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1031                 if( memcmp( dct1, dct2, 16*2 ) )       \
1032                 { \
1033                     oks[0] = 0; \
1034                     fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1035                     break; \
1036                 } \
1037                 call_c2( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1038                 call_a2( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1039             } \
1040         }
1041
1042 #define TEST_QUANT( qname, block, w ) \
1043         if( qf_a.qname != qf_ref.qname ) \
1044         { \
1045             set_func_name( #qname ); \
1046             used_asms[0] = 1; \
1047             for( qp = 51; qp > 0; qp-- ) \
1048             { \
1049                 INIT_QUANT##w() \
1050                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1051                 call_a1( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1052                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1053                 { \
1054                     oks[0] = 0; \
1055                     fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1056                     break; \
1057                 } \
1058                 call_c2( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1059                 call_a2( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1060             } \
1061         }
1062
1063         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1064         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1065         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1066         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1067         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1068         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1069
1070 #define TEST_DEQUANT( qname, dqname, block, w ) \
1071         if( qf_a.dqname != qf_ref.dqname ) \
1072         { \
1073             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1074             used_asms[1] = 1; \
1075             for( qp = 51; qp > 0; qp-- ) \
1076             { \
1077                 INIT_QUANT##w() \
1078                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1079                 memcpy( dct2, dct1, w*w*2 ); \
1080                 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1081                 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1082                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1083                 { \
1084                     oks[1] = 0; \
1085                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1086                     break; \
1087                 } \
1088                 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1089                 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1090             } \
1091         }
1092
1093         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1094         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1095         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1096         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1097
1098 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1099         if( qf_a.dqname != qf_ref.dqname ) \
1100         { \
1101             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1102             used_asms[1] = 1; \
1103             for( qp = 51; qp > 0; qp-- ) \
1104             { \
1105                 for( i = 0; i < 16; i++ ) \
1106                     dct1[i] = rand(); \
1107                 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1108                 memcpy( dct2, dct1, w*w*2 ); \
1109                 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1110                 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1111                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1112                 { \
1113                     oks[1] = 0; \
1114                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1115                 } \
1116                 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1117                 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1118             } \
1119         }
1120
1121         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1122
1123         x264_cqm_delete( h );
1124     }
1125
1126     ok = oks[0]; used_asm = used_asms[0];
1127     report( "quant :" );
1128
1129     ok = oks[1]; used_asm = used_asms[1];
1130     report( "dequant :" );
1131
1132     ok = 1; used_asm = 0;
1133     if( qf_a.denoise_dct != qf_ref.denoise_dct )
1134     {
1135         int size;
1136         used_asm = 1;
1137         for( size = 16; size <= 64; size += 48 )
1138         {
1139             set_func_name( "denoise_dct" );
1140             memcpy(dct1, buf1, size*2);
1141             memcpy(dct2, buf1, size*2);
1142             memcpy(buf3+256, buf3, 256);
1143             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1144             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1145             if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1146                 ok = 0;
1147             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1148             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1149         }
1150     }
1151     report( "denoise dct :" );
1152
1153 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1154     if( qf_a.decname != qf_ref.decname ) \
1155     { \
1156         set_func_name( #decname ); \
1157         used_asm = 1; \
1158         for( i = 0; i < 100; i++ ) \
1159         { \
1160             int result_c, result_a, idx; \
1161             for( idx = 0; idx < w*w; idx++ ) \
1162                 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1163             if( ac ) \
1164                 dct1[0] = 0; \
1165             result_c = call_c( qf_c.decname, (void*)dct1 ); \
1166             result_a = call_a( qf_a.decname, (void*)dct1 ); \
1167             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1168             { \
1169                 ok = 0; \
1170                 fprintf( stderr, #decname ": [FAILED]\n" ); \
1171                 break; \
1172             } \
1173         } \
1174     }
1175
1176     ok = 1; used_asm = 0;
1177     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1178     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1179     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1180     report( "decimate_score :" );
1181
1182 #define TEST_LAST( last, lastname, w, ac ) \
1183     if( qf_a.last != qf_ref.last ) \
1184     { \
1185         set_func_name( #lastname ); \
1186         used_asm = 1; \
1187         for( i = 0; i < 100; i++ ) \
1188         { \
1189             int result_c, result_a, idx, nnz=0; \
1190             int max = rand() & (w*w-1); \
1191             memset( dct1, 0, w*w*2 ); \
1192             for( idx = ac; idx < max; idx++ ) \
1193                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1194             if( !nnz ) \
1195                 dct1[ac] = 1; \
1196             result_c = call_c( qf_c.last, (void*)(dct1+ac) ); \
1197             result_a = call_a( qf_a.last, (void*)(dct1+ac) ); \
1198             if( result_c != result_a ) \
1199             { \
1200                 ok = 0; \
1201                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1202                 break; \
1203             } \
1204         } \
1205     }
1206
1207     ok = 1; used_asm = 0;
1208     TEST_LAST( coeff_last[DCT_CHROMA_DC],  coeff_last4, 2, 0 );
1209     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 4, 1 );
1210     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1211     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1212     report( "coeff_last :" );
1213
1214 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1215     if( qf_a.lastname != qf_ref.lastname ) \
1216     { \
1217         set_func_name( #name ); \
1218         used_asm = 1; \
1219         for( i = 0; i < 100; i++ ) \
1220         { \
1221             x264_run_level_t runlevel_c, runlevel_a; \
1222             int result_c, result_a, idx, nnz=0; \
1223             int max = rand() & (w*w-1); \
1224             memset( dct1, 0, w*w*2 ); \
1225             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1226             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1227             for( idx = ac; idx < max; idx++ ) \
1228                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1229             if( !nnz ) \
1230                 dct1[ac] = 1; \
1231             result_c = call_c( qf_c.lastname, (void*)(dct1+ac), &runlevel_c ); \
1232             result_a = call_a( qf_a.lastname, (void*)(dct1+ac), &runlevel_a ); \
1233             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1234                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1235                 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1236             { \
1237                 ok = 0; \
1238                 fprintf( stderr, #name ": [FAILED]\n" ); \
1239                 break; \
1240             } \
1241         } \
1242     }
1243
1244     ok = 1; used_asm = 0;
1245     TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC],  coeff_level_run4, 2, 0 );
1246     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1247     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1248     report( "coeff_level_run :" );
1249
1250     return ret;
1251 }
1252
1253 static int check_intra( int cpu_ref, int cpu_new )
1254 {
1255     int ret = 0, ok = 1, used_asm = 0;
1256     int i;
1257     DECLARE_ALIGNED_16( uint8_t edge[33] );
1258     struct
1259     {
1260         x264_predict_t      predict_16x16[4+3];
1261         x264_predict_t      predict_8x8c[4+3];
1262         x264_predict8x8_t   predict_8x8[9+3];
1263         x264_predict_t      predict_4x4[9+3];
1264     } ip_c, ip_ref, ip_a;
1265
1266     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1267     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1268     x264_predict_8x8_init( 0, ip_c.predict_8x8 );
1269     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1270
1271     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1272     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1273     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8 );
1274     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1275
1276     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1277     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1278     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8 );
1279     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1280
1281     x264_predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1282
1283 #define INTRA_TEST( name, dir, w, ... ) \
1284     if( ip_a.name[dir] != ip_ref.name[dir] )\
1285     { \
1286         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1287         used_asm = 1; \
1288         memcpy( buf3, buf1, 32*20 );\
1289         memcpy( buf4, buf1, 32*20 );\
1290         call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1291         call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1292         if( memcmp( buf3, buf4, 32*20 ) )\
1293         {\
1294             fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
1295             ok = 0;\
1296             int j,k;\
1297             for(k=-1; k<16; k++)\
1298                 printf("%2x ", edge[16+k]);\
1299             printf("\n");\
1300             for(j=0; j<w; j++){\
1301                 printf("%2x ", edge[14-j]);\
1302                 for(k=0; k<w; k++)\
1303                     printf("%2x ", buf4[48+k+j*32]);\
1304                 printf("\n");\
1305             }\
1306             printf("\n");\
1307             for(j=0; j<w; j++){\
1308                 printf("   ");\
1309                 for(k=0; k<w; k++)\
1310                     printf("%2x ", buf3[48+k+j*32]);\
1311                 printf("\n");\
1312             }\
1313         }\
1314     }
1315
1316     for( i = 0; i < 12; i++ )
1317         INTRA_TEST( predict_4x4, i, 4 );
1318     for( i = 0; i < 7; i++ )
1319         INTRA_TEST( predict_8x8c, i, 8 );
1320     for( i = 0; i < 7; i++ )
1321         INTRA_TEST( predict_16x16, i, 16 );
1322     for( i = 0; i < 12; i++ )
1323         INTRA_TEST( predict_8x8, i, 8, edge );
1324
1325     report( "intra pred :" );
1326     return ret;
1327 }
1328
1329 #define DECL_CABAC(cpu) \
1330 static void run_cabac_##cpu( uint8_t *dst )\
1331 {\
1332     int i;\
1333     x264_cabac_t cb;\
1334     x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1335     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1336     for( i=0; i<0x1000; i++ )\
1337         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1338 }
1339 DECL_CABAC(c)
1340 #ifdef HAVE_MMX
1341 DECL_CABAC(asm)
1342 #else
1343 #define run_cabac_asm run_cabac_c
1344 #endif
1345
1346 static int check_cabac( int cpu_ref, int cpu_new )
1347 {
1348     int ret = 0, ok, used_asm = 1;
1349     if( cpu_ref || run_cabac_c == run_cabac_asm)
1350         return 0;
1351     set_func_name( "cabac_encode_decision" );
1352     memcpy( buf4, buf3, 0x1000 );
1353     call_c( run_cabac_c, buf3 );
1354     call_a( run_cabac_asm, buf4 );
1355     ok = !memcmp( buf3, buf4, 0x1000 );
1356     report( "cabac :" );
1357     return ret;
1358 }
1359
1360 static int check_all_funcs( int cpu_ref, int cpu_new )
1361 {
1362     return check_pixel( cpu_ref, cpu_new )
1363          + check_dct( cpu_ref, cpu_new )
1364          + check_mc( cpu_ref, cpu_new )
1365          + check_intra( cpu_ref, cpu_new )
1366          + check_deblock( cpu_ref, cpu_new )
1367          + check_quant( cpu_ref, cpu_new )
1368          + check_cabac( cpu_ref, cpu_new );
1369 }
1370
1371 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1372 {
1373     *cpu_ref = *cpu_new;
1374     *cpu_new |= flags;
1375     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1376         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1377     if( !quiet )
1378         fprintf( stderr, "x264: %s\n", name );
1379     return check_all_funcs( *cpu_ref, *cpu_new );
1380 }
1381
1382 static int check_all_flags( void )
1383 {
1384     int ret = 0;
1385     int cpu0 = 0, cpu1 = 0;
1386 #ifdef HAVE_MMX
1387     if( x264_cpu_detect() & X264_CPU_MMXEXT )
1388     {
1389         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1390         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1391         cpu1 &= ~X264_CPU_CACHELINE_64;
1392 #ifdef ARCH_X86
1393         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1394         cpu1 &= ~X264_CPU_CACHELINE_32;
1395 #endif
1396         if( x264_cpu_detect() & X264_CPU_LZCNT )
1397         {
1398             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1399             cpu1 &= ~X264_CPU_LZCNT;
1400         }
1401     }
1402     if( x264_cpu_detect() & X264_CPU_SSE2 )
1403     {
1404         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1405         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1406         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1407     }
1408     if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1409     {
1410         cpu1 &= ~X264_CPU_CACHELINE_64;
1411         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1412         cpu1 &= ~X264_CPU_SSE_MISALIGN;
1413     }
1414     if( x264_cpu_detect() & X264_CPU_LZCNT )
1415     {
1416         cpu1 &= ~X264_CPU_CACHELINE_64;
1417         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1418         cpu1 &= ~X264_CPU_LZCNT;
1419     }
1420     if( x264_cpu_detect() & X264_CPU_SSE3 )
1421         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1422     if( x264_cpu_detect() & X264_CPU_SSSE3 )
1423     {
1424         cpu1 &= ~X264_CPU_CACHELINE_64;
1425         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1426         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1427         ret |= add_flags( &cpu0, &cpu1, X264_CPU_PHADD_IS_FAST, "PHADD" );
1428     }
1429     if( x264_cpu_detect() & X264_CPU_SSE4 )
1430     {
1431         cpu1 &= ~X264_CPU_CACHELINE_64;
1432         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1433     }
1434 #elif ARCH_PPC
1435     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1436     {
1437         fprintf( stderr, "x264: ALTIVEC against C\n" );
1438         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1439     }
1440 #endif
1441     return ret;
1442 }
1443
1444 int main(int argc, char *argv[])
1445 {
1446     int ret = 0;
1447     int i;
1448
1449     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1450     {
1451 #if !defined(ARCH_X86) && !defined(ARCH_X86_64)
1452         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1453         return 1;
1454 #endif
1455         do_bench = 1;
1456         if( argv[1][7] == '=' )
1457         {
1458             bench_pattern = argv[1]+8;
1459             bench_pattern_len = strlen(bench_pattern);
1460         }
1461         argc--;
1462         argv++;
1463     }
1464
1465     i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1466     fprintf( stderr, "x264: using random seed %u\n", i );
1467     srand( i );
1468
1469     buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1470     buf2 = buf1 + 0xf00;
1471     buf3 = buf2 + 0xf00;
1472     buf4 = buf3 + 0x1000;
1473     for( i=0; i<0x1e00; i++ )
1474         buf1[i] = rand() & 0xFF;
1475     memset( buf1+0x1e00, 0, 0x2000 );
1476
1477     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1478     if( do_bench )
1479         for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1480         {
1481             buf2 = buf1 + 0xf00;
1482             buf3 = buf2 + 0xf00;
1483             buf4 = buf3 + 0x1000;
1484             ret |= x264_stack_pagealign( check_all_flags, i*16 );
1485             buf1 += 16;
1486             quiet = 1;
1487             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1488         }
1489     else
1490         ret = check_all_flags();
1491
1492     if( ret )
1493     {
1494         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1495         return -1;
1496     }
1497     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
1498     if( do_bench )
1499         print_bench();
1500     return 0;
1501 }
1502