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