]> git.sesse.net Git - x264/blob - tools/checkasm.c
More --me tesa optimizations
[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 // GCC doesn't align stack variables on ARM, so use .bss
34 #ifdef ARCH_ARM
35 #undef ALIGNED_16
36 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
37 #endif
38
39 /* buf1, buf2: initialised to random data and shouldn't write into them */
40 uint8_t * buf1, * buf2;
41 /* buf3, buf4: used to store output */
42 uint8_t * buf3, * buf4;
43
44 int quiet = 0;
45
46 #define report( name ) { \
47     if( used_asm && !quiet ) \
48         fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
49     if( !ok ) ret = -1; \
50 }
51
52 #define BENCH_RUNS 100  // tradeoff between accuracy and speed
53 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
54 #define MAX_FUNCS 1000  // just has to be big enough to hold all the existing functions
55 #define MAX_CPUS 10     // number of different combinations of cpu flags
56
57 typedef struct {
58     void *pointer; // just for detecting duplicates
59     uint32_t cpu;
60     uint32_t cycles;
61     uint32_t den;
62 } bench_t;
63
64 typedef struct {
65     char *name;
66     bench_t vers[MAX_CPUS];
67 } bench_func_t;
68
69 int do_bench = 0;
70 int bench_pattern_len = 0;
71 const char *bench_pattern = "";
72 char func_name[100];
73 static bench_func_t benchs[MAX_FUNCS];
74
75 static const char *pixel_names[10] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x2", "2x4", "2x2" };
76 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
77 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
78 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
79 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
80
81 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
82
83 static inline uint32_t read_time(void)
84 {
85     uint32_t a = 0;
86 #if defined(__GNUC__) && (defined(ARCH_X86) || defined(ARCH_X86_64))
87     asm volatile( "rdtsc" :"=a"(a) ::"edx" );
88 #elif defined(ARCH_PPC)
89     asm volatile( "mftb %0" : "=r" (a) );
90 #elif defined(ARCH_ARM)     // ARMv7 only
91     asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) );
92 #endif
93     return a;
94 }
95
96 static bench_t* get_bench( const char *name, int cpu )
97 {
98     int i, j;
99     for( i=0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
100         assert( i < MAX_FUNCS );
101     if( !benchs[i].name )
102         benchs[i].name = strdup( name );
103     if( !cpu )
104         return &benchs[i].vers[0];
105     for( j=1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
106         assert( j < MAX_CPUS );
107     benchs[i].vers[j].cpu = cpu;
108     return &benchs[i].vers[j];
109 }
110
111 static int cmp_nop( const void *a, const void *b )
112 {
113     return *(uint16_t*)a - *(uint16_t*)b;
114 }
115
116 static int cmp_bench( const void *a, const void *b )
117 {
118     // asciibetical sort except preserving numbers
119     const char *sa = ((bench_func_t*)a)->name;
120     const char *sb = ((bench_func_t*)b)->name;
121     for(;; sa++, sb++)
122     {
123         if( !*sa && !*sb ) return 0;
124         if( isdigit(*sa) && isdigit(*sb) && isdigit(sa[1]) != isdigit(sb[1]) )
125             return isdigit(sa[1]) - isdigit(sb[1]);
126         if( *sa != *sb ) return *sa - *sb;
127     }
128 }
129
130 static void print_bench(void)
131 {
132     uint16_t nops[10000] = {0};
133     int i, j, k, nfuncs, nop_time=0;
134
135     for( i=0; i<10000; i++ )
136     {
137         int t = read_time();
138         nops[i] = read_time() - t;
139     }
140     qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
141     for( i=500; i<9500; i++ )
142         nop_time += nops[i];
143     nop_time /= 900;
144     printf( "nop: %d\n", nop_time );
145
146     for( i=0; i<MAX_FUNCS && benchs[i].name; i++ );
147     nfuncs=i;
148     qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
149     for( i=0; i<nfuncs; i++ )
150         for( j=0; j<MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
151         {
152             bench_t *b = &benchs[i].vers[j];
153             if( !b->den ) continue;
154             for( k=0; k<j && benchs[i].vers[k].pointer != b->pointer; k++ );
155             if( k<j ) continue;
156             printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
157                     b->cpu&X264_CPU_SSE4 ? "sse4" :
158                     b->cpu&X264_CPU_SHUFFLE_IS_FAST ? "fastshuffle" :
159                     b->cpu&X264_CPU_SSSE3 ? "ssse3" :
160                     b->cpu&X264_CPU_SSE3 ? "sse3" :
161                     /* print sse2slow only if there's also a sse2fast version of the same func */
162                     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" :
163                     b->cpu&X264_CPU_SSE2 ? "sse2" :
164                     b->cpu&X264_CPU_MMX ? "mmx" :
165                     b->cpu&X264_CPU_ALTIVEC ? "altivec" :
166                     b->cpu&X264_CPU_NEON ? "neon" :
167                     b->cpu&X264_CPU_ARMV6 ? "armv6" : "c",
168                     b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
169                     b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
170                     b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
171                     b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
172                     b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" : "",
173                     ((int64_t)10*b->cycles/b->den - nop_time)/4 );
174         }
175 }
176
177 #if defined(ARCH_X86) || defined(ARCH_X86_64)
178 int x264_stack_pagealign( int (*func)(), int align );
179 #else
180 #define x264_stack_pagealign( func, align ) func()
181 #endif
182
183 #define call_c1(func,...) func(__VA_ARGS__)
184
185 #if defined(ARCH_X86) || defined(_WIN64)
186 /* detect when callee-saved regs aren't saved.
187  * needs an explicit asm check because it only sometimes crashes in normal use. */
188 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
189 #define call_a1(func,...) x264_checkasm_call((intptr_t(*)())func, &ok, __VA_ARGS__)
190 #else
191 #define call_a1 call_c1
192 #endif
193
194 #define call_bench(func,cpu,...)\
195     if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
196     {\
197         uint32_t tsum = 0;\
198         int tcount = 0;\
199         int ti;\
200         call_a1(func, __VA_ARGS__);\
201         for( ti=0; ti<(cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
202         {\
203             uint32_t t = read_time();\
204             func(__VA_ARGS__);\
205             func(__VA_ARGS__);\
206             func(__VA_ARGS__);\
207             func(__VA_ARGS__);\
208             t = read_time() - t;\
209             if( t*tcount <= tsum*4 && ti > 0 )\
210             {\
211                 tsum += t;\
212                 tcount++;\
213             }\
214         }\
215         bench_t *b = get_bench( func_name, cpu );\
216         b->cycles += tsum;\
217         b->den += tcount;\
218         b->pointer = func;\
219     }
220
221 /* for most functions, run benchmark and correctness test at the same time.
222  * for those that modify their inputs, run the above macros separately */
223 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
224 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
225 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
226 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
227
228
229 static int check_pixel( int cpu_ref, int cpu_new )
230 {
231     x264_pixel_function_t pixel_c;
232     x264_pixel_function_t pixel_ref;
233     x264_pixel_function_t pixel_asm;
234     x264_predict_t predict_16x16[4+3];
235     x264_predict_t predict_8x8c[4+3];
236     x264_predict_t predict_4x4[9+3];
237     x264_predict8x8_t predict_8x8[9+3];
238     x264_predict_8x8_filter_t predict_8x8_filter;
239     ALIGNED_16( uint8_t edge[33] );
240     uint16_t cost_mv[32];
241     int ret = 0, ok, used_asm;
242     int i, j;
243
244     x264_pixel_init( 0, &pixel_c );
245     x264_pixel_init( cpu_ref, &pixel_ref );
246     x264_pixel_init( cpu_new, &pixel_asm );
247     x264_predict_16x16_init( 0, predict_16x16 );
248     x264_predict_8x8c_init( 0, predict_8x8c );
249     x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
250     x264_predict_4x4_init( 0, predict_4x4 );
251     predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
252
253     // maximize sum
254     for( i=0; i<256; i++ )
255     {
256         int z = i|(i>>4);
257         z ^= z>>2;
258         z ^= z>>1;
259         buf3[i] = ~(buf4[i] = -(z&1));
260     }
261     // random pattern made of maxed pixel differences, in case an intermediate value overflows
262     for( ; i<0x1000; i++ )
263         buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
264
265 #define TEST_PIXEL( name, align ) \
266     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
267     { \
268         int res_c, res_asm; \
269         if( pixel_asm.name[i] != pixel_ref.name[i] ) \
270         { \
271             set_func_name( "%s_%s", #name, pixel_names[i] ); \
272             used_asm = 1; \
273             for( j=0; j<64; j++ ) \
274             { \
275                 res_c   = call_c( pixel_c.name[i], buf1, 16, buf2+j*!align, 64 ); \
276                 res_asm = call_a( pixel_asm.name[i], buf1, 16, buf2+j*!align, 64 ); \
277                 if( res_c != res_asm ) \
278                 { \
279                     ok = 0; \
280                     fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
281                     break; \
282                 } \
283             } \
284             for( j=0; j<0x1000 && ok; j+=256 ) \
285             { \
286                 res_c   = pixel_c  .name[i]( buf3+j, 16, buf4+j, 16 ); \
287                 res_asm = pixel_asm.name[i]( buf3+j, 16, buf4+j, 16 ); \
288                 if( res_c != res_asm ) \
289                 { \
290                     ok = 0; \
291                     fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
292                 } \
293             } \
294         } \
295     } \
296     report( "pixel " #name " :" );
297
298     TEST_PIXEL( sad, 0 );
299     TEST_PIXEL( sad_aligned, 1 );
300     TEST_PIXEL( ssd, 1 );
301     TEST_PIXEL( satd, 0 );
302     TEST_PIXEL( sa8d, 1 );
303
304 #define TEST_PIXEL_X( N ) \
305     for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
306     { \
307         int res_c[4]={0}, res_asm[4]={0}; \
308         if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
309         { \
310             set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
311             used_asm = 1; \
312             for( j=0; j<64; j++) \
313             { \
314                 uint8_t *pix2 = buf2+j; \
315                 res_c[0] = pixel_c.sad[i]( buf1, 16, pix2, 64 ); \
316                 res_c[1] = pixel_c.sad[i]( buf1, 16, pix2+6, 64 ); \
317                 res_c[2] = pixel_c.sad[i]( buf1, 16, pix2+1, 64 ); \
318                 if(N==4) \
319                 { \
320                     res_c[3] = pixel_c.sad[i]( buf1, 16, pix2+10, 64 ); \
321                     call_a( pixel_asm.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
322                 } \
323                 else \
324                     call_a( pixel_asm.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
325                 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
326                 { \
327                     ok = 0; \
328                     fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
329                              i, res_c[0], res_c[1], res_c[2], res_c[3], \
330                              res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
331                 } \
332                 if(N==4) \
333                     call_c2( pixel_c.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
334                 else \
335                     call_c2( pixel_c.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
336             } \
337         } \
338     } \
339     report( "pixel sad_x"#N" :" );
340
341     TEST_PIXEL_X(3);
342     TEST_PIXEL_X(4);
343
344 #define TEST_PIXEL_VAR( i ) \
345     if( pixel_asm.var[i] != pixel_ref.var[i] ) \
346     { \
347         set_func_name( "%s_%s", "var", pixel_names[i] ); \
348         used_asm = 1; \
349         /* abi-check wrapper can't return uint64_t, so separate it from return value check */\
350         call_c1( pixel_c.var[i], buf1, 16 ); \
351         call_a1( pixel_asm.var[i], buf1, 16 ); \
352         uint64_t res_c   = pixel_c.var[i]( buf1, 16 ); \
353         uint64_t res_asm = pixel_asm.var[i]( buf1, 16 ); \
354         if( res_c != res_asm ) \
355         { \
356             ok = 0; \
357             fprintf( stderr, "var[%d]: %d %d != %d %d [FAILED]\n", i, (int)res_c, (int)(res_c>>32), (int)res_asm, (int)(res_asm>>32) ); \
358         } \
359         call_c2( pixel_c.var[i], buf1, 16 ); \
360         call_a2( pixel_asm.var[i], buf1, 16 ); \
361     }
362
363     ok = 1; used_asm = 0;
364     TEST_PIXEL_VAR( PIXEL_16x16 );
365     TEST_PIXEL_VAR( PIXEL_8x8 );
366     report( "pixel var :" );
367
368     ok = 1; used_asm = 0;
369     if( pixel_asm.var2_8x8 != pixel_ref.var2_8x8 )
370     {
371         int res_c, res_asm, ssd_c, ssd_asm;
372         set_func_name( "var2_8x8" );
373         used_asm = 1;
374         res_c   = call_c( pixel_c.var2_8x8, buf1, 16, buf2, 16, &ssd_c );
375         res_asm = call_a( pixel_asm.var2_8x8, buf1, 16, buf2, 16, &ssd_asm );
376         if( res_c != res_asm || ssd_c != ssd_asm )
377         {
378             ok = 0;
379             fprintf( stderr, "var[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm );
380         }
381     }
382
383     report( "pixel var2 :" );
384
385     for( i=0, ok=1, used_asm=0; i<4; i++ )
386         if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
387         {
388             set_func_name( "hadamard_ac_%s", pixel_names[i] );
389             used_asm = 1;
390             for( j=0; j<32; j++ )
391             {
392                 uint8_t *pix = (j&16 ? buf1 : buf3) + (j&15)*256;
393                 call_c1( pixel_c.hadamard_ac[i], buf1, 16 );
394                 call_a1( pixel_asm.hadamard_ac[i], buf1, 16 );
395                 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
396                 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
397                 if( rc != ra )
398                 {
399                     ok = 0;
400                     fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
401                     break;
402                 }
403             }
404             call_c2( pixel_c.hadamard_ac[i], buf1, 16 );
405             call_a2( pixel_asm.hadamard_ac[i], buf1, 16 );
406         }
407     report( "pixel hadamard_ac :" );
408
409 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
410     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
411     { \
412         int res_c[3], res_asm[3]; \
413         set_func_name( #name );\
414         used_asm = 1; \
415         memcpy( buf3, buf2, 1024 ); \
416         for( i=0; i<3; i++ ) \
417         { \
418             pred[i]( buf3+48, ##__VA_ARGS__ ); \
419             res_c[i] = pixel_c.satd( buf1+48, 16, buf3+48, 32 ); \
420         } \
421         call_a( pixel_asm.name, buf1+48, i8x8 ? edge : buf3+48, res_asm ); \
422         if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
423         { \
424             ok = 0; \
425             fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
426                      res_c[0], res_c[1], res_c[2], \
427                      res_asm[0], res_asm[1], res_asm[2] ); \
428         } \
429     }
430
431     ok = 1; used_asm = 0;
432     TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
433     TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8]  , 0 );
434     TEST_INTRA_MBCMP( intra_satd_x3_4x4  , predict_4x4  , satd[PIXEL_4x4]  , 0 );
435     TEST_INTRA_MBCMP( intra_sa8d_x3_8x8  , predict_8x8  , sa8d[PIXEL_8x8]  , 1, edge );
436     report( "intra satd_x3 :" );
437     TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
438     TEST_INTRA_MBCMP( intra_sad_x3_8x8c  , predict_8x8c , sad [PIXEL_8x8]  , 0 );
439     TEST_INTRA_MBCMP( intra_sad_x3_8x8   , predict_8x8  , sad [PIXEL_8x8]  , 1, edge );
440     TEST_INTRA_MBCMP( intra_sad_x3_4x4   , predict_4x4  , sad [PIXEL_4x4]  , 0 );
441     report( "intra sad_x3 :" );
442
443     if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
444         pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
445     {
446         float res_c, res_a;
447         ALIGNED_16( int sums[5][4] ) = {{0}};
448         used_asm = ok = 1;
449         x264_emms();
450         res_c = x264_pixel_ssim_wxh( &pixel_c,   buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
451         res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
452         if( fabs(res_c - res_a) > 1e-6 )
453         {
454             ok = 0;
455             fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
456         }
457         set_func_name( "ssim_core" );
458         call_c2( pixel_c.ssim_4x4x2_core,   buf1+2, 32, buf2+2, 32, sums );
459         call_a2( pixel_asm.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
460         set_func_name( "ssim_end" );
461         call_c2( pixel_c.ssim_end4,   sums, sums, 4 );
462         call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
463         report( "ssim :" );
464     }
465
466     ok = 1; used_asm = 0;
467     for( i=0; i<32; i++ )
468         cost_mv[i] = i*10;
469     for( i=0; i<100 && ok; i++ )
470         if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
471         {
472             ALIGNED_16( uint16_t sums[72] );
473             ALIGNED_16( int dc[4] );
474             int16_t mvs_a[32], mvs_c[32];
475             int mvn_a, mvn_c;
476             int thresh = rand() & 0x3fff;
477             set_func_name( "esa_ads" );
478             for( j=0; j<72; j++ )
479                 sums[j] = rand() & 0x3fff;
480             for( j=0; j<4; j++ )
481                 dc[j] = rand() & 0x3fff;
482             used_asm = 1;
483             mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
484             mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
485             if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
486             {
487                 ok = 0;
488                 printf("c%d: ", i&3);
489                 for(j=0; j<mvn_c; j++)
490                     printf("%d ", mvs_c[j]);
491                 printf("\na%d: ", i&3);
492                 for(j=0; j<mvn_a; j++)
493                     printf("%d ", mvs_a[j]);
494                 printf("\n\n");
495             }
496         }
497     report( "esa ads:" );
498
499     return ret;
500 }
501
502 static int check_dct( int cpu_ref, int cpu_new )
503 {
504     x264_dct_function_t dct_c;
505     x264_dct_function_t dct_ref;
506     x264_dct_function_t dct_asm;
507     x264_quant_function_t qf;
508     int ret = 0, ok, used_asm, i, j, interlace;
509     ALIGNED_16( int16_t dct1[16][16] );
510     ALIGNED_16( int16_t dct2[16][16] );
511     ALIGNED_16( int16_t dct4[16][16] );
512     ALIGNED_16( int16_t dct8[4][64] );
513     ALIGNED_8( int16_t dctdc[2][4] );
514     x264_t h_buf;
515     x264_t *h = &h_buf;
516
517     x264_dct_init( 0, &dct_c );
518     x264_dct_init( cpu_ref, &dct_ref);
519     x264_dct_init( cpu_new, &dct_asm );
520
521     memset( h, 0, sizeof(*h) );
522     h->pps = h->pps_array;
523     x264_param_default( &h->param );
524     h->chroma_qp_table = i_chroma_qp_table + 12;
525     h->param.analyse.i_luma_deadzone[0] = 0;
526     h->param.analyse.i_luma_deadzone[1] = 0;
527     h->param.analyse.b_transform_8x8 = 1;
528     for( i=0; i<6; i++ )
529         h->pps->scaling_list[i] = x264_cqm_flat16;
530     x264_cqm_init( h );
531     x264_quant_init( h, 0, &qf );
532
533 #define TEST_DCT( name, t1, t2, size ) \
534     if( dct_asm.name != dct_ref.name ) \
535     { \
536         set_func_name( #name );\
537         used_asm = 1; \
538         call_c( dct_c.name, t1, buf1, buf2 ); \
539         call_a( dct_asm.name, t2, buf1, buf2 ); \
540         if( memcmp( t1, t2, size ) ) \
541         { \
542             ok = 0; \
543             fprintf( stderr, #name " [FAILED]\n" ); \
544         } \
545     }
546     ok = 1; used_asm = 0;
547     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
548     TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
549     TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4*2 );
550     TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
551     report( "sub_dct4 :" );
552
553     ok = 1; used_asm = 0;
554     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
555     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
556     report( "sub_dct8 :" );
557 #undef TEST_DCT
558
559     // fdct and idct are denormalized by different factors, so quant/dequant
560     // is needed to force the coefs into the right range.
561     dct_c.sub16x16_dct( dct4, buf1, buf2 );
562     dct_c.sub16x16_dct8( dct8, buf1, buf2 );
563     for( i=0; i<16; i++ )
564     {
565         qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
566         qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
567     }
568     for( i=0; i<4; i++ )
569     {
570         qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
571         qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
572     }
573
574 #define TEST_IDCT( name, src ) \
575     if( dct_asm.name != dct_ref.name ) \
576     { \
577         set_func_name( #name );\
578         used_asm = 1; \
579         memcpy( buf3, buf1, 32*32 ); \
580         memcpy( buf4, buf1, 32*32 ); \
581         memcpy( dct1, src, 512 ); \
582         memcpy( dct2, src, 512 ); \
583         call_c1( dct_c.name, buf3, (void*)dct1 ); \
584         call_a1( dct_asm.name, buf4, (void*)dct2 ); \
585         if( memcmp( buf3, buf4, 32*32 ) ) \
586         { \
587             ok = 0; \
588             fprintf( stderr, #name " [FAILED]\n" ); \
589         } \
590         call_c2( dct_c.name, buf3, (void*)dct1 ); \
591         call_a2( dct_asm.name, buf4, (void*)dct2 ); \
592     }
593     ok = 1; used_asm = 0;
594     TEST_IDCT( add4x4_idct, dct4 );
595     TEST_IDCT( add8x8_idct, dct4 );
596     TEST_IDCT( add8x8_idct_dc, dct4 );
597     TEST_IDCT( add16x16_idct, dct4 );
598     TEST_IDCT( add16x16_idct_dc, dct4 );
599     report( "add_idct4 :" );
600
601     ok = 1; used_asm = 0;
602     TEST_IDCT( add8x8_idct8, dct8 );
603     TEST_IDCT( add16x16_idct8, dct8 );
604     report( "add_idct8 :" );
605 #undef TEST_IDCT
606
607 #define TEST_DCTDC( name )\
608     ok = 1; used_asm = 0;\
609     if( dct_asm.name != dct_ref.name )\
610     {\
611         set_func_name( #name );\
612         used_asm = 1;\
613         uint16_t *p = (uint16_t*)buf1;\
614         for( i=0; i<16 && ok; i++ )\
615         {\
616             for( j=0; j<16; j++ )\
617                 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
618                            : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
619                            : ((*p++)&0x1fff)-0x1000; /* general case */\
620             memcpy( dct2, dct1, 32 );\
621             call_c1( dct_c.name, dct1[0] );\
622             call_a1( dct_asm.name, dct2[0] );\
623             if( memcmp( dct1, dct2, 32 ) )\
624                 ok = 0;\
625         }\
626         call_c2( dct_c.name, dct1[0] );\
627         call_a2( dct_asm.name, dct2[0] );\
628     }\
629     report( #name " :" );
630
631     TEST_DCTDC(  dct4x4dc );
632     TEST_DCTDC( idct4x4dc );
633 #undef TEST_DCTDC
634
635     x264_zigzag_function_t zigzag_c;
636     x264_zigzag_function_t zigzag_ref;
637     x264_zigzag_function_t zigzag_asm;
638
639     ALIGNED_16( int16_t level1[64] );
640     ALIGNED_16( int16_t level2[64] );
641
642 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size )   \
643     if( zigzag_asm.name != zigzag_ref.name ) \
644     { \
645         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
646         used_asm = 1; \
647         memcpy(dct, buf1, size*sizeof(int16_t));\
648         call_c( zigzag_c.name, t1, dct ); \
649         call_a( zigzag_asm.name, t2, dct ); \
650         if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
651         { \
652             ok = 0; \
653             fprintf( stderr, #name " [FAILED]\n" ); \
654         } \
655     }
656
657 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
658     if( zigzag_asm.name != zigzag_ref.name ) \
659     { \
660         int nz_a, nz_c; \
661         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
662         used_asm = 1; \
663         memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
664         memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
665         nz_c = call_c1( zigzag_c.name, t1, buf2, buf3 );  \
666         nz_a = call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
667         if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a )  \
668         { \
669             ok = 0; \
670             fprintf( stderr, #name " [FAILED]\n" ); \
671         } \
672         call_c2( zigzag_c.name, t1, buf2, buf3 );  \
673         call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
674     }
675
676 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
677     if( zigzag_asm.name != zigzag_ref.name ) \
678     { \
679         int nz_a, nz_c; \
680         int16_t dc_a, dc_c; \
681         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
682         used_asm = 1; \
683         for( i = 0; i < 2; i++ ) \
684         { \
685             memcpy( buf3, buf2, 16*FDEC_STRIDE ); \
686             memcpy( buf4, buf2, 16*FDEC_STRIDE ); \
687             for( j = 0; j < 4; j++ ) \
688             { \
689                 memcpy( buf3 + j*FDEC_STRIDE, (i?buf1:buf2) + j*FENC_STRIDE, 4 ); \
690                 memcpy( buf4 + j*FDEC_STRIDE, (i?buf1:buf2) + j*FENC_STRIDE, 4 ); \
691             } \
692             nz_c = call_c1( zigzag_c.name, t1, buf2, buf3, &dc_c );  \
693             nz_a = call_a1( zigzag_asm.name, t2, buf2, buf4, &dc_a ); \
694             if( memcmp( t1+1, t2+1, 15*sizeof(int16_t) ) || memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a || dc_c != dc_a )  \
695             { \
696                 ok = 0; \
697                 fprintf( stderr, #name " [FAILED]\n" ); \
698                 break; \
699             } \
700         } \
701         call_c2( zigzag_c.name, t1, buf2, buf3, &dc_c );  \
702         call_a2( zigzag_asm.name, t2, buf2, buf4, &dc_a ); \
703     }
704
705 #define TEST_INTERLEAVE( name, t1, t2, dct, size )   \
706     if( zigzag_asm.name != zigzag_ref.name ) \
707     { \
708         for( j=0; j<100; j++ ) \
709         { \
710             set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
711             used_asm = 1; \
712             memcpy(dct, buf1, size*sizeof(int16_t));\
713             for( i=0; i<size; i++ ) \
714                 dct[i] = rand()&0x1F ? 0 : dct[i]; \
715             memcpy(buf3, buf4, 10*sizeof(uint8_t)); \
716             call_c( zigzag_c.name, t1, dct, buf3 ); \
717             call_a( zigzag_asm.name, t2, dct, buf4 ); \
718             if( memcmp( t1, t2, size*sizeof(int16_t) ) || memcmp( buf3, buf4, 10*sizeof(uint8_t) ) ) \
719             { \
720                 ok = 0; \
721             } \
722         } \
723     }
724
725     interlace = 0;
726     x264_zigzag_init( 0, &zigzag_c, 0 );
727     x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
728     x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
729
730     ok = 1; used_asm = 0;
731     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
732     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
733     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
734     TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
735     report( "zigzag_frame :" );
736
737     interlace = 1;
738     x264_zigzag_init( 0, &zigzag_c, 1 );
739     x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
740     x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
741
742     ok = 1; used_asm = 0;
743     TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
744     TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
745     TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
746     TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
747     report( "zigzag_field :" );
748
749     ok = 1; used_asm = 0;
750     TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
751     report( "zigzag_interleave :" );
752 #undef TEST_ZIGZAG_SCAN
753 #undef TEST_ZIGZAG_SUB
754
755     return ret;
756 }
757
758 static int check_mc( int cpu_ref, int cpu_new )
759 {
760     x264_mc_functions_t mc_c;
761     x264_mc_functions_t mc_ref;
762     x264_mc_functions_t mc_a;
763     x264_pixel_function_t pixel;
764
765     uint8_t *src     = &buf1[2*64+2];
766     uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
767                          &buf1[7*64+2], &buf1[9*64+2] };
768     uint8_t *dst1    = buf3;
769     uint8_t *dst2    = buf4;
770
771     int dx, dy, i, j, k, w;
772     int ret = 0, ok, used_asm;
773
774     x264_mc_init( 0, &mc_c );
775     x264_mc_init( cpu_ref, &mc_ref );
776     x264_mc_init( cpu_new, &mc_a );
777     x264_pixel_init( 0, &pixel );
778
779 #define MC_TEST_LUMA( w, h ) \
780         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
781         { \
782             const x264_weight_t *weight = weight_none; \
783             set_func_name( "mc_luma_%dx%d", w, h );\
784             used_asm = 1; \
785             memset(buf3, 0xCD, 1024); \
786             memset(buf4, 0xCD, 1024); \
787             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
788             call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
789             if( memcmp( buf3, buf4, 1024 ) ) \
790             { \
791                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
792                 ok = 0; \
793             } \
794         } \
795         if( mc_a.get_ref != mc_ref.get_ref ) \
796         { \
797             uint8_t *ref = dst2; \
798             int ref_stride = 32; \
799             const x264_weight_t *weight = weight_none; \
800             set_func_name( "get_ref_%dx%d", w, h );\
801             used_asm = 1; \
802             memset(buf3, 0xCD, 1024); \
803             memset(buf4, 0xCD, 1024); \
804             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
805             ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
806             for( i=0; i<h; i++ ) \
807                 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
808                 { \
809                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
810                     ok = 0; \
811                     break; \
812                 } \
813         }
814
815 #define MC_TEST_CHROMA( w, h ) \
816         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
817         { \
818             set_func_name( "mc_chroma_%dx%d", w, h );\
819             used_asm = 1; \
820             memset(buf3, 0xCD, 1024); \
821             memset(buf4, 0xCD, 1024); \
822             call_c( mc_c.mc_chroma, dst1, 16, src, 64, dx, dy, w, h ); \
823             call_a( mc_a.mc_chroma, dst2, 16, src, 64, dx, dy, w, h ); \
824             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
825             for( j=0; j<h; j++ ) \
826                 for( i=w; i<4; i++ ) \
827                     dst2[i+j*16] = dst1[i+j*16]; \
828             if( memcmp( buf3, buf4, 1024 ) ) \
829             { \
830                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
831                 ok = 0; \
832             } \
833         }
834     ok = 1; used_asm = 0;
835     for( dy = -8; dy < 8; dy++ )
836         for( dx = -128; dx < 128; dx++ )
837         {
838             if( rand()&15 ) continue; // running all of them is too slow
839             MC_TEST_LUMA( 20, 18 );
840             MC_TEST_LUMA( 16, 16 );
841             MC_TEST_LUMA( 16, 8 );
842             MC_TEST_LUMA( 12, 10 );
843             MC_TEST_LUMA( 8, 16 );
844             MC_TEST_LUMA( 8, 8 );
845             MC_TEST_LUMA( 8, 4 );
846             MC_TEST_LUMA( 4, 8 );
847             MC_TEST_LUMA( 4, 4 );
848         }
849     report( "mc luma :" );
850
851     ok = 1; used_asm = 0;
852     for( dy = -1; dy < 9; dy++ )
853         for( dx = -128; dx < 128; dx++ )
854         {
855             if( rand()&15 ) continue;
856             MC_TEST_CHROMA( 8, 8 );
857             MC_TEST_CHROMA( 8, 4 );
858             MC_TEST_CHROMA( 4, 8 );
859             MC_TEST_CHROMA( 4, 4 );
860             MC_TEST_CHROMA( 4, 2 );
861             MC_TEST_CHROMA( 2, 4 );
862             MC_TEST_CHROMA( 2, 2 );
863         }
864     report( "mc chroma :" );
865 #undef MC_TEST_LUMA
866 #undef MC_TEST_CHROMA
867
868 #define MC_TEST_AVG( name, weight ) \
869     for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
870     { \
871         memcpy( buf3, buf1+320, 320 ); \
872         memcpy( buf4, buf1+320, 320 ); \
873         if( mc_a.name[i] != mc_ref.name[i] ) \
874         { \
875             set_func_name( "%s_%s", #name, pixel_names[i] );\
876             used_asm = 1; \
877             call_c1( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
878             call_a1( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
879             if( memcmp( buf3, buf4, 320 ) ) \
880             { \
881                 ok = 0; \
882                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
883             } \
884             call_c2( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
885             call_a2( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
886         } \
887     }
888     ok = 1; used_asm = 0;
889     for( w = -63; w <= 127 && ok; w++ )
890         MC_TEST_AVG( avg, w );
891     report( "mc wpredb :" );
892
893 #define MC_TEST_WEIGHT( name, weight, aligned ) \
894     int align_off = (aligned ? 0 : rand()%16); \
895     for( i = 1, ok = 1, used_asm = 0; i <= 5; i++ ) \
896     { \
897         ALIGNED_16( uint8_t buffC[640] ); \
898         ALIGNED_16( uint8_t buffA[640] ); \
899         j = X264_MAX( i*4, 2 ); \
900         memset( buffC, 0, 640 ); \
901         memset( buffA, 0, 640 ); \
902         x264_t ha; \
903         ha.mc = mc_a; \
904         /* w12 is the same as w16 in some cases */ \
905         if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
906             continue; \
907         if( mc_a.name[i] != mc_ref.name[i] ) \
908         { \
909             int k; \
910             set_func_name( "%s_w%d", #name, j ); \
911             used_asm = 1; \
912             call_c1( mc_c.weight[i], buffC, 32, buf2+align_off, 32, &weight, 16 ); \
913             mc_a.weight_cache(&ha, &weight); \
914             call_a1( weight.weightfn[i], buffA, 32, buf2+align_off, 32, &weight, 16 ); \
915             for( k = 0; k < 16; k++ ) \
916                 if( memcmp( &buffC[k*32], &buffA[k*32], j ) ) \
917                 { \
918                     ok = 0; \
919                     fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
920                     break; \
921                 } \
922             call_c2( mc_c.weight[i], buffC, 32, buf2+align_off, 32, &weight, 16 ); \
923             call_a2( weight.weightfn[i], buffA, 32, buf2+align_off, 32, &weight, 16 ); \
924         } \
925     }
926
927     ok = 1; used_asm = 0;
928
929     int s,o,d;
930     int align_cnt = 0;
931     for( s = 0; s <= 127 && ok; s++ )
932     {
933         for( o = -128; o <= 127 && ok; o++ )
934         {
935             if( rand() & 2047 ) continue;
936             for( d = 0; d <= 7 && ok; d++ )
937             {
938                 if( s == 1<<d )
939                     continue;
940                 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
941                 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
942             }
943         }
944
945     }
946     report( "mc weight :" );
947
948     ok = 1; used_asm = 0;
949     s = 1; d = 0;
950     for( o = 0; o <= 127 && ok; o++ )
951     {
952         if( rand() & 15 ) continue;
953         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
954         MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
955     }
956     report( "mc offsetadd :" );
957     ok = 1; used_asm = 0;
958     for( o = -128; o < 0 && ok; o++ )
959     {
960         if( rand() & 15 ) continue;
961         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
962         MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
963     }
964     report( "mc offsetsub :" );
965
966     if( mc_a.hpel_filter != mc_ref.hpel_filter )
967     {
968         uint8_t *src = buf1+8+2*64;
969         uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
970         uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
971         void *tmp = buf3+49*64;
972         set_func_name( "hpel_filter" );
973         ok = 1; used_asm = 1;
974         memset( buf3, 0, 4096 );
975         memset( buf4, 0, 4096 );
976         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10, tmp );
977         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10, tmp );
978         for( i=0; i<3; i++ )
979             for( j=0; j<10; j++ )
980                 //FIXME ideally the first pixels would match too, but they aren't actually used
981                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
982                 {
983                     ok = 0;
984                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
985                     for( k=0; k<48; k++ )
986                         printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
987                     printf("\n");
988                     for( k=0; k<48; k++ )
989                         printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
990                     printf("\n");
991                     break;
992                 }
993         report( "hpel filter :" );
994     }
995
996     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
997     {
998         uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
999         uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf4+3072 };
1000         set_func_name( "lowres_init" );
1001         ok = 1; used_asm = 1;
1002         for( w=40; w<=48; w+=8 )
1003         {
1004             int stride = (w+8)&~15;
1005             call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1006             call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1007             for( i=0; i<16; i++)
1008             {
1009                 for( j=0; j<4; j++)
1010                     if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
1011                     {
1012                         ok = 0;
1013                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1014                         for( k=0; k<w; k++ )
1015                             printf( "%d ", dstc[j][k+i*stride] );
1016                         printf("\n");
1017                         for( k=0; k<w; k++ )
1018                             printf( "%d ", dsta[j][k+i*stride] );
1019                         printf("\n");
1020                         break;
1021                     }
1022             }
1023         }
1024         report( "lowres init :" );
1025     }
1026
1027 #define INTEGRAL_INIT( name, size, ... )\
1028     if( mc_a.name != mc_ref.name )\
1029     {\
1030         int stride = 80;\
1031         set_func_name( #name );\
1032         used_asm = 1;\
1033         memcpy( buf3, buf1, size*2*stride );\
1034         memcpy( buf4, buf1, size*2*stride );\
1035         uint16_t *sum = (uint16_t*)buf3;\
1036         call_c1( mc_c.name, __VA_ARGS__ );\
1037         sum = (uint16_t*)buf4;\
1038         call_a1( mc_a.name, __VA_ARGS__ );\
1039         if( memcmp( buf3, buf4, (stride-8)*2 )\
1040             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1041             ok = 0;\
1042         call_c2( mc_c.name, __VA_ARGS__ );\
1043         call_a2( mc_a.name, __VA_ARGS__ );\
1044     }
1045     ok = 1; used_asm = 0;
1046     INTEGRAL_INIT( integral_init4h, 2, sum+stride, buf2, stride );
1047     INTEGRAL_INIT( integral_init8h, 2, sum+stride, buf2, stride );
1048     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1049     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1050     report( "integral init :" );
1051
1052     if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1053     {
1054         ok = 1; used_asm = 1;
1055         set_func_name( "mbtree_propagate" );
1056         int *dsta = (int*)buf3;
1057         int *dstc = dsta+400;
1058         uint16_t *prop = (uint16_t*)buf1;
1059         uint16_t *intra = (uint16_t*)buf4;
1060         uint16_t *inter = intra+400;
1061         uint16_t *qscale = inter+400;
1062         uint16_t *rand = (uint16_t*)buf2;
1063         x264_emms();
1064         for( i=0; i<400; i++ )
1065         {
1066             intra[i]  = *rand++ & 0x7fff;
1067             intra[i] += !intra[i];
1068             inter[i]  = *rand++ & 0x7fff;
1069             qscale[i] = *rand++ & 0x7fff;
1070         }
1071         call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, 400 );
1072         call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, 400 );
1073         // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1074         x264_emms();
1075         for( i=0; i<400; i++ )
1076             ok &= abs(dstc[i]-dsta[i]) <= (abs(dstc[i])>512) || fabs((double)dstc[i]/dsta[i]-1) < 1e-6;
1077         report( "mbtree propagate :" );
1078     }
1079
1080     return ret;
1081 }
1082
1083 static int check_deblock( int cpu_ref, int cpu_new )
1084 {
1085     x264_deblock_function_t db_c;
1086     x264_deblock_function_t db_ref;
1087     x264_deblock_function_t db_a;
1088     int ret = 0, ok = 1, used_asm = 0;
1089     int alphas[36], betas[36];
1090     int8_t tcs[36][4];
1091     int a, c, i, j;
1092
1093     x264_deblock_init( 0, &db_c );
1094     x264_deblock_init( cpu_ref, &db_ref );
1095     x264_deblock_init( cpu_new, &db_a );
1096
1097     /* not exactly the real values of a,b,tc but close enough */
1098     a = 255; c = 250;
1099     for( i = 35; i >= 0; i-- )
1100     {
1101         alphas[i] = a;
1102         betas[i] = (i+1)/2;
1103         tcs[i][0] = tcs[i][2] = (c+6)/10;
1104         tcs[i][1] = tcs[i][3] = (c+9)/20;
1105         a = a*9/10;
1106         c = c*9/10;
1107     }
1108
1109 #define TEST_DEBLOCK( name, align, ... ) \
1110     for( i = 0; i < 36; i++ ) \
1111     { \
1112         int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
1113         for( j = 0; j < 1024; j++ ) \
1114             /* two distributions of random to excersize different failure modes */\
1115             buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
1116         memcpy( buf4, buf3, 1024 ); \
1117         if( db_a.name != db_ref.name ) \
1118         { \
1119             set_func_name( #name );\
1120             used_asm = 1; \
1121             call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1122             call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1123             if( memcmp( buf3, buf4, 1024 ) ) \
1124             { \
1125                 ok = 0; \
1126                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1127                 break; \
1128             } \
1129             call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1130             call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1131         } \
1132     }
1133
1134     TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
1135     TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
1136     TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
1137     TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
1138     TEST_DEBLOCK( deblock_h_luma_intra, 0 );
1139     TEST_DEBLOCK( deblock_v_luma_intra, 1 );
1140     TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
1141     TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
1142
1143     report( "deblock :" );
1144
1145     return ret;
1146 }
1147
1148 static int check_quant( int cpu_ref, int cpu_new )
1149 {
1150     x264_quant_function_t qf_c;
1151     x264_quant_function_t qf_ref;
1152     x264_quant_function_t qf_a;
1153     ALIGNED_16( int16_t dct1[64] );
1154     ALIGNED_16( int16_t dct2[64] );
1155     ALIGNED_16( uint8_t cqm_buf[64] );
1156     int ret = 0, ok, used_asm;
1157     int oks[2] = {1,1}, used_asms[2] = {0,0};
1158     int i, j, i_cqm, qp;
1159     x264_t h_buf;
1160     x264_t *h = &h_buf;
1161     memset( h, 0, sizeof(*h) );
1162     h->pps = h->pps_array;
1163     x264_param_default( &h->param );
1164     h->chroma_qp_table = i_chroma_qp_table + 12;
1165     h->param.rc.i_qp_min = 26;
1166     h->param.analyse.b_transform_8x8 = 1;
1167
1168     for( i_cqm = 0; i_cqm < 4; i_cqm++ )
1169     {
1170         if( i_cqm == 0 )
1171         {
1172             for( i = 0; i < 6; i++ )
1173                 h->pps->scaling_list[i] = x264_cqm_flat16;
1174             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1175         }
1176         else if( i_cqm == 1 )
1177         {
1178             for( i = 0; i < 6; i++ )
1179                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1180             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1181         }
1182         else
1183         {
1184             if( i_cqm == 2 )
1185                 for( i = 0; i < 64; i++ )
1186                     cqm_buf[i] = 10 + rand() % 246;
1187             else
1188                 for( i = 0; i < 64; i++ )
1189                     cqm_buf[i] = 1;
1190             for( i = 0; i < 6; i++ )
1191                 h->pps->scaling_list[i] = cqm_buf;
1192             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1193         }
1194
1195         x264_cqm_init( h );
1196         x264_quant_init( h, 0, &qf_c );
1197         x264_quant_init( h, cpu_ref, &qf_ref );
1198         x264_quant_init( h, cpu_new, &qf_a );
1199
1200 #define INIT_QUANT8() \
1201         { \
1202             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1203             for( i = 0; i < 64; i++ ) \
1204             { \
1205                 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1206                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1207             } \
1208         }
1209
1210 #define INIT_QUANT4() \
1211         { \
1212             static const int scale1d[4] = {4,6,4,6}; \
1213             for( i = 0; i < 16; i++ ) \
1214             { \
1215                 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1216                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1217             } \
1218         }
1219
1220 #define TEST_QUANT_DC( name, cqm ) \
1221         if( qf_a.name != qf_ref.name ) \
1222         { \
1223             set_func_name( #name ); \
1224             used_asms[0] = 1; \
1225             for( qp = 51; qp > 0; qp-- ) \
1226             { \
1227                 for( j = 0; j < 2; j++ ) \
1228                 { \
1229                     int result_c, result_a; \
1230                     for( i = 0; i < 16; i++ ) \
1231                         dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1232                     result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1233                     result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1234                     if( memcmp( dct1, dct2, 16*2 ) || result_c != result_a )       \
1235                     { \
1236                         oks[0] = 0; \
1237                         fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1238                         break; \
1239                     } \
1240                     call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1241                     call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1242                 } \
1243             } \
1244         }
1245
1246 #define TEST_QUANT( qname, block, w ) \
1247         if( qf_a.qname != qf_ref.qname ) \
1248         { \
1249             set_func_name( #qname ); \
1250             used_asms[0] = 1; \
1251             for( qp = 51; qp > 0; qp-- ) \
1252             { \
1253                 for( j = 0; j < 2; j++ ) \
1254                 { \
1255                     int result_c, result_a; \
1256                     INIT_QUANT##w() \
1257                     result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1258                     result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1259                     if( memcmp( dct1, dct2, w*w*2 ) || result_c != result_a ) \
1260                     { \
1261                         oks[0] = 0; \
1262                         fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1263                         break; \
1264                     } \
1265                     call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1266                     call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1267                 } \
1268             } \
1269         }
1270
1271         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1272         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1273         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1274         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1275         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1276         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1277
1278 #define TEST_DEQUANT( qname, dqname, block, w ) \
1279         if( qf_a.dqname != qf_ref.dqname ) \
1280         { \
1281             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1282             used_asms[1] = 1; \
1283             j = 1; \
1284             for( qp = 51; qp > 0; qp-- ) \
1285             { \
1286                 INIT_QUANT##w() \
1287                 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1288                 memcpy( dct2, dct1, w*w*2 ); \
1289                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1290                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1291                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1292                 { \
1293                     oks[1] = 0; \
1294                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1295                     break; \
1296                 } \
1297                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1298                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1299             } \
1300         }
1301
1302         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1303         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1304         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1305         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1306
1307 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1308         if( qf_a.dqname != qf_ref.dqname ) \
1309         { \
1310             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1311             used_asms[1] = 1; \
1312             for( qp = 51; qp > 0; qp-- ) \
1313             { \
1314                 for( i = 0; i < 16; i++ ) \
1315                     dct1[i] = rand(); \
1316                 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1317                 memcpy( dct2, dct1, w*w*2 ); \
1318                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1319                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1320                 if( memcmp( dct1, dct2, w*w*2 ) ) \
1321                 { \
1322                     oks[1] = 0; \
1323                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1324                 } \
1325                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1326                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1327             } \
1328         }
1329
1330         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1331
1332         x264_cqm_delete( h );
1333     }
1334
1335     ok = oks[0]; used_asm = used_asms[0];
1336     report( "quant :" );
1337
1338     ok = oks[1]; used_asm = used_asms[1];
1339     report( "dequant :" );
1340
1341     ok = 1; used_asm = 0;
1342     if( qf_a.denoise_dct != qf_ref.denoise_dct )
1343     {
1344         int size;
1345         used_asm = 1;
1346         for( size = 16; size <= 64; size += 48 )
1347         {
1348             set_func_name( "denoise_dct" );
1349             memcpy(dct1, buf1, size*2);
1350             memcpy(dct2, buf1, size*2);
1351             memcpy(buf3+256, buf3, 256);
1352             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1353             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1354             if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1355                 ok = 0;
1356             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1357             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1358         }
1359     }
1360     report( "denoise dct :" );
1361
1362 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1363     if( qf_a.decname != qf_ref.decname ) \
1364     { \
1365         set_func_name( #decname ); \
1366         used_asm = 1; \
1367         for( i = 0; i < 100; i++ ) \
1368         { \
1369             int result_c, result_a, idx; \
1370             for( idx = 0; idx < w*w; idx++ ) \
1371                 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1372             if( ac ) \
1373                 dct1[0] = 0; \
1374             result_c = call_c( qf_c.decname, dct1 ); \
1375             result_a = call_a( qf_a.decname, dct1 ); \
1376             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1377             { \
1378                 ok = 0; \
1379                 fprintf( stderr, #decname ": [FAILED]\n" ); \
1380                 break; \
1381             } \
1382         } \
1383     }
1384
1385     ok = 1; used_asm = 0;
1386     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1387     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1388     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1389     report( "decimate_score :" );
1390
1391 #define TEST_LAST( last, lastname, w, ac ) \
1392     if( qf_a.last != qf_ref.last ) \
1393     { \
1394         set_func_name( #lastname ); \
1395         used_asm = 1; \
1396         for( i = 0; i < 100; i++ ) \
1397         { \
1398             int result_c, result_a, idx, nnz=0; \
1399             int max = rand() & (w*w-1); \
1400             memset( dct1, 0, w*w*2 ); \
1401             for( idx = ac; idx < max; idx++ ) \
1402                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1403             if( !nnz ) \
1404                 dct1[ac] = 1; \
1405             result_c = call_c( qf_c.last, dct1+ac ); \
1406             result_a = call_a( qf_a.last, dct1+ac ); \
1407             if( result_c != result_a ) \
1408             { \
1409                 ok = 0; \
1410                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1411                 break; \
1412             } \
1413         } \
1414     }
1415
1416     ok = 1; used_asm = 0;
1417     TEST_LAST( coeff_last[DCT_CHROMA_DC],  coeff_last4, 2, 0 );
1418     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 4, 1 );
1419     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1420     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1421     report( "coeff_last :" );
1422
1423 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1424     if( qf_a.lastname != qf_ref.lastname ) \
1425     { \
1426         set_func_name( #name ); \
1427         used_asm = 1; \
1428         for( i = 0; i < 100; i++ ) \
1429         { \
1430             x264_run_level_t runlevel_c, runlevel_a; \
1431             int result_c, result_a, idx, nnz=0; \
1432             int max = rand() & (w*w-1); \
1433             memset( dct1, 0, w*w*2 ); \
1434             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1435             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1436             for( idx = ac; idx < max; idx++ ) \
1437                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1438             if( !nnz ) \
1439                 dct1[ac] = 1; \
1440             result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
1441             result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
1442             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1443                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1444                 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1445             { \
1446                 ok = 0; \
1447                 fprintf( stderr, #name ": [FAILED]\n" ); \
1448                 break; \
1449             } \
1450         } \
1451     }
1452
1453     ok = 1; used_asm = 0;
1454     TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC],  coeff_level_run4, 2, 0 );
1455     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1456     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1457     report( "coeff_level_run :" );
1458
1459     return ret;
1460 }
1461
1462 static int check_intra( int cpu_ref, int cpu_new )
1463 {
1464     int ret = 0, ok = 1, used_asm = 0;
1465     int i;
1466     ALIGNED_16( uint8_t edge[33] );
1467     ALIGNED_16( uint8_t edge2[33] );
1468     struct
1469     {
1470         x264_predict_t      predict_16x16[4+3];
1471         x264_predict_t      predict_8x8c[4+3];
1472         x264_predict8x8_t   predict_8x8[9+3];
1473         x264_predict_t      predict_4x4[9+3];
1474         x264_predict_8x8_filter_t predict_8x8_filter;
1475     } ip_c, ip_ref, ip_a;
1476
1477     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1478     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1479     x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
1480     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1481
1482     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1483     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1484     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
1485     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1486
1487     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1488     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1489     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
1490     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1491
1492     ip_c.predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1493
1494 #define INTRA_TEST( name, dir, w, ... ) \
1495     if( ip_a.name[dir] != ip_ref.name[dir] )\
1496     { \
1497         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1498         used_asm = 1; \
1499         memcpy( buf3, buf1, 32*20 );\
1500         memcpy( buf4, buf1, 32*20 );\
1501         call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1502         call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1503         if( memcmp( buf3, buf4, 32*20 ) )\
1504         {\
1505             fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
1506             ok = 0;\
1507             int j,k;\
1508             for(k=-1; k<16; k++)\
1509                 printf("%2x ", edge[16+k]);\
1510             printf("\n");\
1511             for(j=0; j<w; j++){\
1512                 printf("%2x ", edge[14-j]);\
1513                 for(k=0; k<w; k++)\
1514                     printf("%2x ", buf4[48+k+j*32]);\
1515                 printf("\n");\
1516             }\
1517             printf("\n");\
1518             for(j=0; j<w; j++){\
1519                 printf("   ");\
1520                 for(k=0; k<w; k++)\
1521                     printf("%2x ", buf3[48+k+j*32]);\
1522                 printf("\n");\
1523             }\
1524         }\
1525     }
1526
1527     for( i = 0; i < 12; i++ )
1528         INTRA_TEST( predict_4x4, i, 4 );
1529     for( i = 0; i < 7; i++ )
1530         INTRA_TEST( predict_8x8c, i, 8 );
1531     for( i = 0; i < 7; i++ )
1532         INTRA_TEST( predict_16x16, i, 16 );
1533     for( i = 0; i < 12; i++ )
1534         INTRA_TEST( predict_8x8, i, 8, edge );
1535
1536     set_func_name("intra_predict_8x8_filter");
1537     if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
1538     {
1539         used_asm = 1;
1540         for( i = 0; i < 32; i++ )
1541         {
1542             memcpy( edge2, edge, 33 );
1543             call_c(ip_c.predict_8x8_filter, buf1+48, edge, (i&24)>>1, i&7);
1544             call_a(ip_a.predict_8x8_filter, buf1+48, edge2, (i&24)>>1, i&7);
1545             if( memcmp( edge, edge2, 33 ) )
1546             {
1547                 fprintf( stderr, "predict_8x8_filter :  [FAILED] %d %d\n", (i&24)>>1, i&7);
1548                 ok = 0;
1549             }
1550         }
1551     }
1552
1553     report( "intra pred :" );
1554     return ret;
1555 }
1556
1557 #define DECL_CABAC(cpu) \
1558 static void run_cabac_##cpu( uint8_t *dst )\
1559 {\
1560     int i;\
1561     x264_cabac_t cb;\
1562     x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1563     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1564     for( i=0; i<0x1000; i++ )\
1565         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1566 }
1567 DECL_CABAC(c)
1568 #ifdef HAVE_MMX
1569 DECL_CABAC(asm)
1570 #else
1571 #define run_cabac_asm run_cabac_c
1572 #endif
1573
1574 static int check_cabac( int cpu_ref, int cpu_new )
1575 {
1576     int ret = 0, ok, used_asm = 1;
1577     if( cpu_ref || run_cabac_c == run_cabac_asm)
1578         return 0;
1579     set_func_name( "cabac_encode_decision" );
1580     memcpy( buf4, buf3, 0x1000 );
1581     call_c( run_cabac_c, buf3 );
1582     call_a( run_cabac_asm, buf4 );
1583     ok = !memcmp( buf3, buf4, 0x1000 );
1584     report( "cabac :" );
1585     return ret;
1586 }
1587
1588 static int check_all_funcs( int cpu_ref, int cpu_new )
1589 {
1590     return check_pixel( cpu_ref, cpu_new )
1591          + check_dct( cpu_ref, cpu_new )
1592          + check_mc( cpu_ref, cpu_new )
1593          + check_intra( cpu_ref, cpu_new )
1594          + check_deblock( cpu_ref, cpu_new )
1595          + check_quant( cpu_ref, cpu_new )
1596          + check_cabac( cpu_ref, cpu_new );
1597 }
1598
1599 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1600 {
1601     *cpu_ref = *cpu_new;
1602     *cpu_new |= flags;
1603     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1604         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1605     if( !quiet )
1606         fprintf( stderr, "x264: %s\n", name );
1607     return check_all_funcs( *cpu_ref, *cpu_new );
1608 }
1609
1610 static int check_all_flags( void )
1611 {
1612     int ret = 0;
1613     int cpu0 = 0, cpu1 = 0;
1614 #ifdef HAVE_MMX
1615     if( x264_cpu_detect() & X264_CPU_MMXEXT )
1616     {
1617         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1618         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1619         cpu1 &= ~X264_CPU_CACHELINE_64;
1620 #ifdef ARCH_X86
1621         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1622         cpu1 &= ~X264_CPU_CACHELINE_32;
1623 #endif
1624         if( x264_cpu_detect() & X264_CPU_LZCNT )
1625         {
1626             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1627             cpu1 &= ~X264_CPU_LZCNT;
1628         }
1629     }
1630     if( x264_cpu_detect() & X264_CPU_SSE2 )
1631     {
1632         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1633         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1634         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1635         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
1636         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1637     }
1638     if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1639     {
1640         cpu1 &= ~X264_CPU_CACHELINE_64;
1641         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1642         cpu1 &= ~X264_CPU_SSE_MISALIGN;
1643     }
1644     if( x264_cpu_detect() & X264_CPU_LZCNT )
1645     {
1646         cpu1 &= ~X264_CPU_CACHELINE_64;
1647         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1648         cpu1 &= ~X264_CPU_LZCNT;
1649     }
1650     if( x264_cpu_detect() & X264_CPU_SSE3 )
1651         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1652     if( x264_cpu_detect() & X264_CPU_SSSE3 )
1653     {
1654         cpu1 &= ~X264_CPU_CACHELINE_64;
1655         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1656         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1657         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
1658         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1659     }
1660     if( x264_cpu_detect() & X264_CPU_SSE4 )
1661     {
1662         cpu1 &= ~X264_CPU_CACHELINE_64;
1663         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1664     }
1665 #elif ARCH_PPC
1666     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1667     {
1668         fprintf( stderr, "x264: ALTIVEC against C\n" );
1669         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1670     }
1671 #elif ARCH_ARM
1672     if( x264_cpu_detect() & X264_CPU_ARMV6 )
1673         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
1674     if( x264_cpu_detect() & X264_CPU_NEON )
1675         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
1676     if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
1677         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
1678 #endif
1679     return ret;
1680 }
1681
1682 int main(int argc, char *argv[])
1683 {
1684     int ret = 0;
1685     int i;
1686
1687     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1688     {
1689 #if !defined(ARCH_X86) && !defined(ARCH_X86_64) && !defined(ARCH_PPC) && !defined(ARCH_ARM)
1690         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1691         return 1;
1692 #endif
1693         do_bench = 1;
1694         if( argv[1][7] == '=' )
1695         {
1696             bench_pattern = argv[1]+8;
1697             bench_pattern_len = strlen(bench_pattern);
1698         }
1699         argc--;
1700         argv++;
1701     }
1702
1703     i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1704     fprintf( stderr, "x264: using random seed %u\n", i );
1705     srand( i );
1706
1707     buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1708     if( !buf1 )
1709     {
1710         fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
1711         return -1;
1712     }
1713     buf2 = buf1 + 0xf00;
1714     buf3 = buf2 + 0xf00;
1715     buf4 = buf3 + 0x1000;
1716     for( i=0; i<0x1e00; i++ )
1717         buf1[i] = rand() & 0xFF;
1718     memset( buf1+0x1e00, 0, 0x2000 );
1719
1720     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1721     if( do_bench )
1722         for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1723         {
1724             buf2 = buf1 + 0xf00;
1725             buf3 = buf2 + 0xf00;
1726             buf4 = buf3 + 0x1000;
1727             ret |= x264_stack_pagealign( check_all_flags, i*16 );
1728             buf1 += 16;
1729             quiet = 1;
1730             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1731         }
1732     else
1733         ret = check_all_flags();
1734
1735     if( ret )
1736     {
1737         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1738         return -1;
1739     }
1740     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
1741     if( do_bench )
1742         print_bench();
1743     return 0;
1744 }
1745