]> git.sesse.net Git - x264/blob - tools/checkasm.c
183cef5f80d74616a9dfc2dc61c4129ea3a819e1
[x264] / tools / checkasm.c
1 /*****************************************************************************
2  * checkasm.c: assembly check tool
3  *****************************************************************************
4  * Copyright (C) 2003-2015 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  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include <ctype.h>
29 #include "common/common.h"
30 #include "common/cpu.h"
31
32 // GCC doesn't align stack variables on ARM, so use .bss
33 #if ARCH_ARM
34 #undef ALIGNED_16
35 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
36 #endif
37
38 /* buf1, buf2: initialised to random data and shouldn't write into them */
39 uint8_t *buf1, *buf2;
40 /* buf3, buf4: used to store output */
41 uint8_t *buf3, *buf4;
42 /* pbuf1, pbuf2: initialised to random pixel data and shouldn't write into them. */
43 pixel *pbuf1, *pbuf2;
44 /* pbuf3, pbuf4: point to buf3, buf4, just for type convenience */
45 pixel *pbuf3, *pbuf4;
46
47 int quiet = 0;
48
49 #define report( name ) { \
50     if( used_asm && !quiet ) \
51         fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
52     if( !ok ) ret = -1; \
53 }
54
55 #define BENCH_RUNS 100  // tradeoff between accuracy and speed
56 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
57 #define MAX_FUNCS 1000  // just has to be big enough to hold all the existing functions
58 #define MAX_CPUS 30     // number of different combinations of cpu flags
59
60 typedef struct
61 {
62     void *pointer; // just for detecting duplicates
63     uint32_t cpu;
64     uint64_t cycles;
65     uint32_t den;
66 } bench_t;
67
68 typedef struct
69 {
70     char *name;
71     bench_t vers[MAX_CPUS];
72 } bench_func_t;
73
74 int do_bench = 0;
75 int bench_pattern_len = 0;
76 const char *bench_pattern = "";
77 char func_name[100];
78 static bench_func_t benchs[MAX_FUNCS];
79
80 static const char *pixel_names[12] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x16", "4x2", "2x8", "2x4", "2x2" };
81 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
82 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
83 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
84 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
85 static const char **intra_predict_8x16c_names = intra_predict_8x8c_names;
86
87 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
88
89 static inline uint32_t read_time(void)
90 {
91     uint32_t a = 0;
92 #if HAVE_X86_INLINE_ASM
93     asm volatile( "lfence \n"
94                   "rdtsc  \n"
95                   : "=a"(a) :: "edx", "memory" );
96 #elif ARCH_PPC
97     asm volatile( "mftb %0" : "=r"(a) :: "memory" );
98 #elif ARCH_ARM     // ARMv7 only
99     asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) :: "memory" );
100 #elif ARCH_AARCH64
101     uint64_t b = 0;
102     asm volatile( "mrs %0, pmccntr_el0" : "=r"(b) :: "memory" );
103     a = b;
104 #elif ARCH_MIPS
105     asm volatile( "rdhwr %0, $2" : "=r"(a) :: "memory" );
106 #endif
107     return a;
108 }
109
110 static bench_t* get_bench( const char *name, int cpu )
111 {
112     int i, j;
113     for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
114         assert( i < MAX_FUNCS );
115     if( !benchs[i].name )
116         benchs[i].name = strdup( name );
117     if( !cpu )
118         return &benchs[i].vers[0];
119     for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
120         assert( j < MAX_CPUS );
121     benchs[i].vers[j].cpu = cpu;
122     return &benchs[i].vers[j];
123 }
124
125 static int cmp_nop( const void *a, const void *b )
126 {
127     return *(uint16_t*)a - *(uint16_t*)b;
128 }
129
130 static int cmp_bench( const void *a, const void *b )
131 {
132     // asciibetical sort except preserving numbers
133     const char *sa = ((bench_func_t*)a)->name;
134     const char *sb = ((bench_func_t*)b)->name;
135     for( ;; sa++, sb++ )
136     {
137         if( !*sa && !*sb )
138             return 0;
139         if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
140             return isdigit( sa[1] ) - isdigit( sb[1] );
141         if( *sa != *sb )
142             return *sa - *sb;
143     }
144 }
145
146 static void print_bench(void)
147 {
148     uint16_t nops[10000];
149     int nfuncs, nop_time=0;
150
151     for( int i = 0; i < 10000; i++ )
152     {
153         uint32_t t = read_time();
154         nops[i] = read_time() - t;
155     }
156     qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
157     for( int i = 500; i < 9500; i++ )
158         nop_time += nops[i];
159     nop_time /= 900;
160     printf( "nop: %d\n", nop_time );
161
162     for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
163     qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
164     for( int i = 0; i < nfuncs; i++ )
165         for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
166         {
167             int k;
168             bench_t *b = &benchs[i].vers[j];
169             if( !b->den )
170                 continue;
171             for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
172             if( k < j )
173                 continue;
174             printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
175 #if HAVE_MMX
176                     b->cpu&X264_CPU_AVX2 ? "avx2" :
177                     b->cpu&X264_CPU_FMA3 ? "fma3" :
178                     b->cpu&X264_CPU_FMA4 ? "fma4" :
179                     b->cpu&X264_CPU_XOP ? "xop" :
180                     b->cpu&X264_CPU_AVX ? "avx" :
181                     b->cpu&X264_CPU_SSE42 ? "sse42" :
182                     b->cpu&X264_CPU_SSE4 ? "sse4" :
183                     b->cpu&X264_CPU_SSSE3 ? "ssse3" :
184                     b->cpu&X264_CPU_SSE3 ? "sse3" :
185                     /* print sse2slow only if there's also a sse2fast version of the same func */
186                     b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS-1 && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
187                     b->cpu&X264_CPU_SSE2 ? "sse2" :
188                     b->cpu&X264_CPU_SSE ? "sse" :
189                     b->cpu&X264_CPU_MMX ? "mmx" :
190 #elif ARCH_PPC
191                     b->cpu&X264_CPU_ALTIVEC ? "altivec" :
192 #elif ARCH_ARM
193                     b->cpu&X264_CPU_NEON ? "neon" :
194                     b->cpu&X264_CPU_ARMV6 ? "armv6" :
195 #elif ARCH_AARCH64
196                     b->cpu&X264_CPU_NEON ? "neon" :
197                     b->cpu&X264_CPU_ARMV8 ? "armv8" :
198 #elif ARCH_MIPS
199                     b->cpu&X264_CPU_MSA ? "msa" :
200 #endif
201                     "c",
202 #if HAVE_MMX
203                     b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
204                     b->cpu&X264_CPU_SLOW_ATOM && b->cpu&X264_CPU_CACHELINE_64 ? "_c64_atom" :
205                     b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
206                     b->cpu&X264_CPU_SLOW_SHUFFLE ? "_slowshuffle" :
207                     b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
208                     b->cpu&X264_CPU_BMI2 ? "_bmi2" :
209                     b->cpu&X264_CPU_BMI1 ? "_bmi1" :
210                     b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
211                     b->cpu&X264_CPU_SLOW_ATOM ? "_atom" :
212 #elif ARCH_ARM
213                     b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
214 #endif
215                     "",
216                     (int64_t)(10*b->cycles/b->den - nop_time)/4 );
217         }
218 }
219
220 #if ARCH_X86 || ARCH_X86_64
221 int x264_stack_pagealign( int (*func)(), int align );
222
223 /* detect when callee-saved regs aren't saved
224  * needs an explicit asm check because it only sometimes crashes in normal use. */
225 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
226 #else
227 #define x264_stack_pagealign( func, align ) func()
228 #endif
229
230 #if ARCH_AARCH64
231 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
232 #endif
233
234 #define call_c1(func,...) func(__VA_ARGS__)
235
236 #if ARCH_X86_64
237 /* Evil hack: detect incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
238  * This is done by clobbering the stack with junk around the stack pointer and calling the
239  * assembly function through x264_checkasm_call with added dummy arguments which forces all
240  * real arguments to be passed on the stack and not in registers. For 32-bit argument the
241  * upper half of the 64-bit register location on the stack will now contain junk. Note that
242  * this is dependant on compiler behaviour and that interrupts etc. at the wrong time may
243  * overwrite the junk written to the stack so there's no guarantee that it will always
244  * detect all functions that assumes zero-extension.
245  */
246 void x264_checkasm_stack_clobber( uint64_t clobber, ... );
247 #define call_a1(func,...) ({ \
248     uint64_t r = (rand() & 0xffff) * 0x0001000100010001ULL; \
249     x264_checkasm_stack_clobber( r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r ); /* max_args+6 */ \
250     x264_checkasm_call(( intptr_t(*)())func, &ok, 0, 0, 0, 0, __VA_ARGS__ ); })
251 #elif ARCH_X86 || (ARCH_AARCH64 && !defined(__APPLE__))
252 #define call_a1(func,...) x264_checkasm_call( (intptr_t(*)())func, &ok, __VA_ARGS__ )
253 #else
254 #define call_a1 call_c1
255 #endif
256
257 #define call_bench(func,cpu,...)\
258     if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
259     {\
260         uint64_t tsum = 0;\
261         int tcount = 0;\
262         call_a1(func, __VA_ARGS__);\
263         for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
264         {\
265             uint32_t t = read_time();\
266             func(__VA_ARGS__);\
267             func(__VA_ARGS__);\
268             func(__VA_ARGS__);\
269             func(__VA_ARGS__);\
270             t = read_time() - t;\
271             if( (uint64_t)t*tcount <= tsum*4 && ti > 0 )\
272             {\
273                 tsum += t;\
274                 tcount++;\
275             }\
276         }\
277         bench_t *b = get_bench( func_name, cpu );\
278         b->cycles += tsum;\
279         b->den += tcount;\
280         b->pointer = func;\
281     }
282
283 /* for most functions, run benchmark and correctness test at the same time.
284  * for those that modify their inputs, run the above macros separately */
285 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
286 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
287 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
288 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
289
290
291 static int check_pixel( int cpu_ref, int cpu_new )
292 {
293     x264_pixel_function_t pixel_c;
294     x264_pixel_function_t pixel_ref;
295     x264_pixel_function_t pixel_asm;
296     x264_predict_t predict_4x4[12];
297     x264_predict8x8_t predict_8x8[12];
298     x264_predict_8x8_filter_t predict_8x8_filter;
299     ALIGNED_16( pixel edge[36] );
300     uint16_t cost_mv[32];
301     int ret = 0, ok, used_asm;
302
303     x264_pixel_init( 0, &pixel_c );
304     x264_pixel_init( cpu_ref, &pixel_ref );
305     x264_pixel_init( cpu_new, &pixel_asm );
306     x264_predict_4x4_init( 0, predict_4x4 );
307     x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
308     predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
309
310     // maximize sum
311     for( int i = 0; i < 256; i++ )
312     {
313         int z = i|(i>>4);
314         z ^= z>>2;
315         z ^= z>>1;
316         pbuf4[i] = -(z&1) & PIXEL_MAX;
317         pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
318     }
319     // random pattern made of maxed pixel differences, in case an intermediate value overflows
320     for( int i = 256; i < 0x1000; i++ )
321     {
322         pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
323         pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
324     }
325
326 #define TEST_PIXEL( name, align ) \
327     ok = 1, used_asm = 0; \
328     for( int i = 0; i < ARRAY_ELEMS(pixel_c.name); i++ ) \
329     { \
330         int res_c, res_asm; \
331         if( pixel_asm.name[i] != pixel_ref.name[i] ) \
332         { \
333             set_func_name( "%s_%s", #name, pixel_names[i] ); \
334             used_asm = 1; \
335             for( int j = 0; j < 64; j++ ) \
336             { \
337                 res_c   = call_c( pixel_c.name[i],   pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
338                 res_asm = call_a( pixel_asm.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
339                 if( res_c != res_asm ) \
340                 { \
341                     ok = 0; \
342                     fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
343                     break; \
344                 } \
345             } \
346             for( int j = 0; j < 0x1000 && ok; j += 256 ) \
347             { \
348                 res_c   = pixel_c  .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
349                 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
350                 if( res_c != res_asm ) \
351                 { \
352                     ok = 0; \
353                     fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
354                 } \
355             } \
356         } \
357     } \
358     report( "pixel " #name " :" );
359
360     TEST_PIXEL( sad, 0 );
361     TEST_PIXEL( sad_aligned, 1 );
362     TEST_PIXEL( ssd, 1 );
363     TEST_PIXEL( satd, 0 );
364     TEST_PIXEL( sa8d, 1 );
365
366     ok = 1, used_asm = 0;
367     if( pixel_asm.sa8d_satd[PIXEL_16x16] != pixel_ref.sa8d_satd[PIXEL_16x16] )
368     {
369         set_func_name( "sa8d_satd_%s", pixel_names[PIXEL_16x16] );
370         used_asm = 1;
371         for( int j = 0; j < 64; j++ )
372         {
373             uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 );
374             uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 );
375             uint64_t res_a = call_a( pixel_asm.sa8d_satd[PIXEL_16x16], pbuf1, (intptr_t)16, pbuf2, (intptr_t)64 );
376             uint32_t cost8_a = res_a;
377             uint32_t cost4_a = res_a >> 32;
378             if( cost8_a != cost8_c || cost4_a != cost4_c )
379             {
380                 ok = 0;
381                 fprintf( stderr, "sa8d_satd [%d]: (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16,
382                          cost8_c, cost4_c, cost8_a, cost4_a );
383                 break;
384             }
385         }
386         for( int j = 0; j < 0x1000 && ok; j += 256 ) \
387         {
388             uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
389             uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
390             uint64_t res_a = pixel_asm.sa8d_satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
391             uint32_t cost8_a = res_a;
392             uint32_t cost4_a = res_a >> 32;
393             if( cost8_a != cost8_c || cost4_a != cost4_c )
394             {
395                 ok = 0;
396                 fprintf( stderr, "sa8d_satd [%d]: overflow (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16,
397                          cost8_c, cost4_c, cost8_a, cost4_a );
398             }
399         }
400     }
401     report( "pixel sa8d_satd :" );
402
403 #define TEST_PIXEL_X( N ) \
404     ok = 1; used_asm = 0; \
405     for( int i = 0; i < 7; i++ ) \
406     { \
407         ALIGNED_16( int res_c[4] ) = {0}; \
408         ALIGNED_16( int res_asm[4] ) = {0}; \
409         if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
410         { \
411             set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
412             used_asm = 1; \
413             for( int j = 0; j < 64; j++ ) \
414             { \
415                 pixel *pix2 = pbuf2+j; \
416                 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2,   64 ); \
417                 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
418                 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
419                 if( N == 4 ) \
420                 { \
421                     res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
422                     call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
423                 } \
424                 else \
425                     call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
426                 if( memcmp(res_c, res_asm, N*sizeof(int)) ) \
427                 { \
428                     ok = 0; \
429                     fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
430                              i, res_c[0], res_c[1], res_c[2], res_c[3], \
431                              res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
432                 } \
433                 if( N == 4 ) \
434                     call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
435                 else \
436                     call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
437             } \
438         } \
439     } \
440     report( "pixel sad_x"#N" :" );
441
442     TEST_PIXEL_X(3);
443     TEST_PIXEL_X(4);
444
445 #define TEST_PIXEL_VAR( i ) \
446     if( pixel_asm.var[i] != pixel_ref.var[i] ) \
447     { \
448         set_func_name( "%s_%s", "var", pixel_names[i] ); \
449         used_asm = 1; \
450         /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
451         call_c1( pixel_c.var[i],   pbuf1,           16 ); \
452         call_a1( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
453         uint64_t res_c   = pixel_c.var[i]( pbuf1, 16 ); \
454         uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
455         if( res_c != res_asm ) \
456         { \
457             ok = 0; \
458             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) ); \
459         } \
460         call_c2( pixel_c.var[i],   pbuf1, (intptr_t)16 ); \
461         call_a2( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
462     }
463
464     ok = 1; used_asm = 0;
465     TEST_PIXEL_VAR( PIXEL_16x16 );
466     TEST_PIXEL_VAR( PIXEL_8x16 );
467     TEST_PIXEL_VAR( PIXEL_8x8 );
468     report( "pixel var :" );
469
470 #define TEST_PIXEL_VAR2( i ) \
471     if( pixel_asm.var2[i] != pixel_ref.var2[i] ) \
472     { \
473         int res_c, res_asm, ssd_c, ssd_asm; \
474         set_func_name( "%s_%s", "var2", pixel_names[i] ); \
475         used_asm = 1; \
476         res_c   = call_c( pixel_c.var2[i],   pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_c   ); \
477         res_asm = call_a( pixel_asm.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_asm ); \
478         if( res_c != res_asm || ssd_c != ssd_asm ) \
479         { \
480             ok = 0; \
481             fprintf( stderr, "var2[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm ); \
482         } \
483     }
484
485     ok = 1; used_asm = 0;
486     TEST_PIXEL_VAR2( PIXEL_8x16 );
487     TEST_PIXEL_VAR2( PIXEL_8x8 );
488     report( "pixel var2 :" );
489
490     ok = 1; used_asm = 0;
491     for( int i = 0; i < 4; i++ )
492         if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
493         {
494             set_func_name( "hadamard_ac_%s", pixel_names[i] );
495             used_asm = 1;
496             for( int j = 0; j < 32; j++ )
497             {
498                 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
499                 call_c1( pixel_c.hadamard_ac[i],   pbuf1, (intptr_t)16 );
500                 call_a1( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
501                 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
502                 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
503                 if( rc != ra )
504                 {
505                     ok = 0;
506                     fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
507                     break;
508                 }
509             }
510             call_c2( pixel_c.hadamard_ac[i],   pbuf1, (intptr_t)16 );
511             call_a2( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
512         }
513     report( "pixel hadamard_ac :" );
514
515     // maximize sum
516     for( int i = 0; i < 32; i++ )
517         for( int j = 0; j < 16; j++ )
518             pbuf4[16*i+j] = -((i+j)&1) & PIXEL_MAX;
519     ok = 1; used_asm = 0;
520     if( pixel_asm.vsad != pixel_ref.vsad )
521     {
522         for( int h = 2; h <= 32; h += 2 )
523         {
524             int res_c, res_asm;
525             set_func_name( "vsad" );
526             used_asm = 1;
527             for( int j = 0; j < 2 && ok; j++ )
528             {
529                 pixel *p = j ? pbuf4 : pbuf1;
530                 res_c   = call_c( pixel_c.vsad,   p, (intptr_t)16, h );
531                 res_asm = call_a( pixel_asm.vsad, p, (intptr_t)16, h );
532                 if( res_c != res_asm )
533                 {
534                     ok = 0;
535                     fprintf( stderr, "vsad: height=%d, %d != %d\n", h, res_c, res_asm );
536                     break;
537                 }
538             }
539         }
540     }
541     report( "pixel vsad :" );
542
543     ok = 1; used_asm = 0;
544     if( pixel_asm.asd8 != pixel_ref.asd8 )
545     {
546         set_func_name( "asd8" );
547         used_asm = 1;
548         int res_c = call_c( pixel_c.asd8,   pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
549         int res_a = call_a( pixel_asm.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
550         if( res_c != res_a )
551         {
552             ok = 0;
553             fprintf( stderr, "asd: %d != %d\n", res_c, res_a );
554         }
555     }
556     report( "pixel asd :" );
557
558 #define TEST_INTRA_X3( name, i8x8, ... ) \
559     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
560     { \
561         ALIGNED_16( int res_c[3] ); \
562         ALIGNED_16( int res_asm[3] ); \
563         set_func_name( #name ); \
564         used_asm = 1; \
565         call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
566         call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
567         if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
568         { \
569             ok = 0; \
570             fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
571                      res_c[0], res_c[1], res_c[2], \
572                      res_asm[0], res_asm[1], res_asm[2] ); \
573         } \
574     }
575
576 #define TEST_INTRA_X9( name, cmp ) \
577     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
578     { \
579         set_func_name( #name ); \
580         used_asm = 1; \
581         ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
582         for( int i=0; i<17; i++ ) \
583             bitcosts[i] = 9*(i!=8); \
584         memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
585         memcpy( pbuf4, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
586         for( int i=0; i<32; i++ ) \
587         { \
588             pixel *fenc = pbuf1+48+i*12; \
589             pixel *fdec1 = pbuf3+48+i*12; \
590             pixel *fdec2 = pbuf4+48+i*12; \
591             int pred_mode = i%9; \
592             int res_c = INT_MAX; \
593             for( int j=0; j<9; j++ ) \
594             { \
595                 predict_4x4[j]( fdec1 ); \
596                 int cost = pixel_c.cmp[PIXEL_4x4]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
597                 if( cost < (uint16_t)res_c ) \
598                     res_c = cost + (j<<16); \
599             } \
600             predict_4x4[res_c>>16]( fdec1 ); \
601             int res_a = call_a( pixel_asm.name, fenc, fdec2, bitcosts+8-pred_mode ); \
602             if( res_c != res_a ) \
603             { \
604                 ok = 0; \
605                 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
606                 break; \
607             } \
608             if( memcmp(fdec1, fdec2, 4*FDEC_STRIDE*sizeof(pixel)) ) \
609             { \
610                 ok = 0; \
611                 fprintf( stderr, #name" [FAILED]\n" ); \
612                 for( int j=0; j<16; j++ ) \
613                     fprintf( stderr, "%02x ", fdec1[(j&3)+(j>>2)*FDEC_STRIDE] ); \
614                 fprintf( stderr, "\n" ); \
615                 for( int j=0; j<16; j++ ) \
616                     fprintf( stderr, "%02x ", fdec2[(j&3)+(j>>2)*FDEC_STRIDE] ); \
617                 fprintf( stderr, "\n" ); \
618                 break; \
619             } \
620         } \
621     }
622
623 #define TEST_INTRA8_X9( name, cmp ) \
624     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
625     { \
626         set_func_name( #name ); \
627         used_asm = 1; \
628         ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
629         ALIGNED_ARRAY_16( uint16_t, satds_c,[16] ); \
630         ALIGNED_ARRAY_16( uint16_t, satds_a,[16] ); \
631         memset( satds_c, 0, 16 * sizeof(*satds_c) ); \
632         memset( satds_a, 0, 16 * sizeof(*satds_a) ); \
633         for( int i=0; i<17; i++ ) \
634             bitcosts[i] = 9*(i!=8); \
635         for( int i=0; i<32; i++ ) \
636         { \
637             pixel *fenc = pbuf1+48+i*12; \
638             pixel *fdec1 = pbuf3+48+i*12; \
639             pixel *fdec2 = pbuf4+48+i*12; \
640             int pred_mode = i%9; \
641             int res_c = INT_MAX; \
642             predict_8x8_filter( fdec1, edge, ALL_NEIGHBORS, ALL_NEIGHBORS ); \
643             for( int j=0; j<9; j++ ) \
644             { \
645                 predict_8x8[j]( fdec1, edge ); \
646                 satds_c[j] = pixel_c.cmp[PIXEL_8x8]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
647                 if( satds_c[j] < (uint16_t)res_c ) \
648                     res_c = satds_c[j] + (j<<16); \
649             } \
650             predict_8x8[res_c>>16]( fdec1, edge ); \
651             int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \
652             if( res_c != res_a || memcmp(satds_c, satds_a, 16 * sizeof(*satds_c)) ) \
653             { \
654                 ok = 0; \
655                 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
656                 for( int j = 0; j < 9; j++ ) \
657                     fprintf( stderr, "%5d ", satds_c[j]); \
658                 fprintf( stderr, "\n" ); \
659                 for( int j = 0; j < 9; j++ ) \
660                     fprintf( stderr, "%5d ", satds_a[j]); \
661                 fprintf( stderr, "\n" ); \
662                 break; \
663             } \
664             for( int j=0; j<8; j++ ) \
665                 if( memcmp(fdec1+j*FDEC_STRIDE, fdec2+j*FDEC_STRIDE, 8*sizeof(pixel)) ) \
666                     ok = 0; \
667             if( !ok ) \
668             { \
669                 fprintf( stderr, #name" [FAILED]\n" ); \
670                 for( int j=0; j<8; j++ ) \
671                 { \
672                     for( int k=0; k<8; k++ ) \
673                         fprintf( stderr, "%02x ", fdec1[k+j*FDEC_STRIDE] ); \
674                     fprintf( stderr, "\n" ); \
675                 } \
676                 fprintf( stderr, "\n" ); \
677                 for( int j=0; j<8; j++ ) \
678                 { \
679                     for( int k=0; k<8; k++ ) \
680                         fprintf( stderr, "%02x ", fdec2[k+j*FDEC_STRIDE] ); \
681                     fprintf( stderr, "\n" ); \
682                 } \
683                 fprintf( stderr, "\n" ); \
684                 break; \
685             } \
686         } \
687     }
688
689     memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) );
690     ok = 1; used_asm = 0;
691     TEST_INTRA_X3( intra_satd_x3_16x16, 0 );
692     TEST_INTRA_X3( intra_satd_x3_8x16c, 0 );
693     TEST_INTRA_X3( intra_satd_x3_8x8c, 0 );
694     TEST_INTRA_X3( intra_sa8d_x3_8x8, 1, edge );
695     TEST_INTRA_X3( intra_satd_x3_4x4, 0 );
696     report( "intra satd_x3 :" );
697     ok = 1; used_asm = 0;
698     TEST_INTRA_X3( intra_sad_x3_16x16, 0 );
699     TEST_INTRA_X3( intra_sad_x3_8x16c, 0 );
700     TEST_INTRA_X3( intra_sad_x3_8x8c, 0 );
701     TEST_INTRA_X3( intra_sad_x3_8x8, 1, edge );
702     TEST_INTRA_X3( intra_sad_x3_4x4, 0 );
703     report( "intra sad_x3 :" );
704     ok = 1; used_asm = 0;
705     TEST_INTRA_X9( intra_satd_x9_4x4, satd );
706     TEST_INTRA8_X9( intra_sa8d_x9_8x8, sa8d );
707     report( "intra satd_x9 :" );
708     ok = 1; used_asm = 0;
709     TEST_INTRA_X9( intra_sad_x9_4x4, sad );
710     TEST_INTRA8_X9( intra_sad_x9_8x8, sad );
711     report( "intra sad_x9 :" );
712
713     ok = 1; used_asm = 0;
714     if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
715     {
716         used_asm = 1;
717         set_func_name( "ssd_nv12" );
718         uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
719         pixel_c.ssd_nv12_core(   pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
720         pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
721         if( res_u_c != res_u_a || res_v_c != res_v_a )
722         {
723             ok = 0;
724             fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
725                      res_u_c, res_v_c, res_u_a, res_v_a );
726         }
727         call_c( pixel_c.ssd_nv12_core,   pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_c, &res_v_c );
728         call_a( pixel_asm.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_a, &res_v_a );
729     }
730     report( "ssd_nv12 :" );
731
732     if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
733         pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
734     {
735         int cnt;
736         float res_c, res_a;
737         ALIGNED_16( int sums[5][4] ) = {{0}};
738         used_asm = ok = 1;
739         x264_emms();
740         res_c = x264_pixel_ssim_wxh( &pixel_c,   pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
741         res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
742         if( fabs( res_c - res_a ) > 1e-6 )
743         {
744             ok = 0;
745             fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
746         }
747         set_func_name( "ssim_core" );
748         call_c( pixel_c.ssim_4x4x2_core,   pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
749         call_a( pixel_asm.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
750         set_func_name( "ssim_end" );
751         call_c2( pixel_c.ssim_end4,   sums, sums, 4 );
752         call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
753         /* check incorrect assumptions that 32-bit ints are zero-extended to 64-bit */
754         call_c1( pixel_c.ssim_end4,   sums, sums, 3 );
755         call_a1( pixel_asm.ssim_end4, sums, sums, 3 );
756         report( "ssim :" );
757     }
758
759     ok = 1; used_asm = 0;
760     for( int i = 0; i < 32; i++ )
761         cost_mv[i] = i*10;
762     for( int i = 0; i < 100 && ok; i++ )
763         if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
764         {
765             ALIGNED_16( uint16_t sums[72] );
766             ALIGNED_16( int dc[4] );
767             ALIGNED_16( int16_t mvs_a[48] );
768             ALIGNED_16( int16_t mvs_c[48] );
769             int mvn_a, mvn_c;
770             int thresh = rand() & 0x3fff;
771             set_func_name( "esa_ads" );
772             for( int j = 0; j < 72; j++ )
773                 sums[j] = rand() & 0x3fff;
774             for( int j = 0; j < 4; j++ )
775                 dc[j] = rand() & 0x3fff;
776             used_asm = 1;
777             mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
778             mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
779             if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
780             {
781                 ok = 0;
782                 printf( "c%d: ", i&3 );
783                 for( int j = 0; j < mvn_c; j++ )
784                     printf( "%d ", mvs_c[j] );
785                 printf( "\na%d: ", i&3 );
786                 for( int j = 0; j < mvn_a; j++ )
787                     printf( "%d ", mvs_a[j] );
788                 printf( "\n\n" );
789             }
790         }
791     report( "esa ads:" );
792
793     return ret;
794 }
795
796 static int check_dct( int cpu_ref, int cpu_new )
797 {
798     x264_dct_function_t dct_c;
799     x264_dct_function_t dct_ref;
800     x264_dct_function_t dct_asm;
801     x264_quant_function_t qf;
802     int ret = 0, ok, used_asm, interlace = 0;
803     ALIGNED_ARRAY_N( dctcoef, dct1, [16],[16] );
804     ALIGNED_ARRAY_N( dctcoef, dct2, [16],[16] );
805     ALIGNED_ARRAY_N( dctcoef, dct4, [16],[16] );
806     ALIGNED_ARRAY_N( dctcoef, dct8, [4],[64] );
807     ALIGNED_16( dctcoef dctdc[2][8] );
808     x264_t h_buf;
809     x264_t *h = &h_buf;
810
811     x264_dct_init( 0, &dct_c );
812     x264_dct_init( cpu_ref, &dct_ref);
813     x264_dct_init( cpu_new, &dct_asm );
814
815     memset( h, 0, sizeof(*h) );
816     x264_param_default( &h->param );
817     h->sps->i_chroma_format_idc = 1;
818     h->chroma_qp_table = i_chroma_qp_table + 12;
819     h->param.analyse.i_luma_deadzone[0] = 0;
820     h->param.analyse.i_luma_deadzone[1] = 0;
821     h->param.analyse.b_transform_8x8 = 1;
822     for( int i = 0; i < 6; i++ )
823         h->pps->scaling_list[i] = x264_cqm_flat16;
824     x264_cqm_init( h );
825     x264_quant_init( h, 0, &qf );
826
827     /* overflow test cases */
828     for( int i = 0; i < 5; i++ )
829     {
830         pixel *enc = &pbuf3[16*i*FENC_STRIDE];
831         pixel *dec = &pbuf4[16*i*FDEC_STRIDE];
832
833         for( int j = 0; j < 16; j++ )
834         {
835             int cond_a = (i < 2) ? 1 : ((j&3) == 0 || (j&3) == (i-1));
836             int cond_b = (i == 0) ? 1 : !cond_a;
837             enc[0] = enc[1] = enc[4] = enc[5] = enc[8] = enc[9] = enc[12] = enc[13] = cond_a ? PIXEL_MAX : 0;
838             enc[2] = enc[3] = enc[6] = enc[7] = enc[10] = enc[11] = enc[14] = enc[15] = cond_b ? PIXEL_MAX : 0;
839
840             for( int k = 0; k < 4; k++ )
841                 dec[k] = PIXEL_MAX - enc[k];
842
843             enc += FENC_STRIDE;
844             dec += FDEC_STRIDE;
845         }
846     }
847
848 #define TEST_DCT( name, t1, t2, size ) \
849     if( dct_asm.name != dct_ref.name ) \
850     { \
851         set_func_name( #name ); \
852         used_asm = 1; \
853         pixel *enc = pbuf3; \
854         pixel *dec = pbuf4; \
855         for( int j = 0; j < 5; j++) \
856         { \
857             call_c( dct_c.name, t1, &pbuf1[j*64], &pbuf2[j*64] ); \
858             call_a( dct_asm.name, t2, &pbuf1[j*64], &pbuf2[j*64] ); \
859             if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
860             { \
861                 ok = 0; \
862                 fprintf( stderr, #name " [FAILED]\n" ); \
863                 for( int k = 0; k < size; k++ )\
864                     printf( "%d ", ((dctcoef*)t1)[k] );\
865                 printf("\n");\
866                 for( int k = 0; k < size; k++ )\
867                     printf( "%d ", ((dctcoef*)t2)[k] );\
868                 printf("\n");\
869                 break; \
870             } \
871             call_c( dct_c.name, t1, enc, dec ); \
872             call_a( dct_asm.name, t2, enc, dec ); \
873             if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
874             { \
875                 ok = 0; \
876                 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
877                 break; \
878             } \
879             enc += 16*FENC_STRIDE; \
880             dec += 16*FDEC_STRIDE; \
881         } \
882     }
883     ok = 1; used_asm = 0;
884     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
885     TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
886     TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
887     TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
888     TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
889     report( "sub_dct4 :" );
890
891     ok = 1; used_asm = 0;
892     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
893     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
894     report( "sub_dct8 :" );
895 #undef TEST_DCT
896
897     // fdct and idct are denormalized by different factors, so quant/dequant
898     // is needed to force the coefs into the right range.
899     dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
900     dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
901     for( int i = 0; i < 16; i++ )
902     {
903         qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
904         qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
905     }
906     for( int i = 0; i < 4; i++ )
907     {
908         qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
909         qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
910     }
911     x264_cqm_delete( h );
912
913 #define TEST_IDCT( name, src ) \
914     if( dct_asm.name != dct_ref.name ) \
915     { \
916         set_func_name( #name ); \
917         used_asm = 1; \
918         memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
919         memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
920         memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
921         memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
922         call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
923         call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
924         if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
925         { \
926             ok = 0; \
927             fprintf( stderr, #name " [FAILED]\n" ); \
928         } \
929         call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
930         call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
931     }
932     ok = 1; used_asm = 0;
933     TEST_IDCT( add4x4_idct, dct4 );
934     TEST_IDCT( add8x8_idct, dct4 );
935     TEST_IDCT( add8x8_idct_dc, dct4 );
936     TEST_IDCT( add16x16_idct, dct4 );
937     TEST_IDCT( add16x16_idct_dc, dct4 );
938     report( "add_idct4 :" );
939
940     ok = 1; used_asm = 0;
941     TEST_IDCT( add8x8_idct8, dct8 );
942     TEST_IDCT( add16x16_idct8, dct8 );
943     report( "add_idct8 :" );
944 #undef TEST_IDCT
945
946 #define TEST_DCTDC( name )\
947     ok = 1; used_asm = 0;\
948     if( dct_asm.name != dct_ref.name )\
949     {\
950         set_func_name( #name );\
951         used_asm = 1;\
952         uint16_t *p = (uint16_t*)buf1;\
953         for( int i = 0; i < 16 && ok; i++ )\
954         {\
955             for( int j = 0; j < 16; j++ )\
956                 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
957                            : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
958                            : ((*p++)&0x1fff)-0x1000; /* general case */\
959             memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
960             call_c1( dct_c.name, dct1[0] );\
961             call_a1( dct_asm.name, dct2[0] );\
962             if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
963                 ok = 0;\
964         }\
965         call_c2( dct_c.name, dct1[0] );\
966         call_a2( dct_asm.name, dct2[0] );\
967     }\
968     report( #name " :" );
969
970     TEST_DCTDC(  dct4x4dc );
971     TEST_DCTDC( idct4x4dc );
972 #undef TEST_DCTDC
973
974 #define TEST_DCTDC_CHROMA( name )\
975     ok = 1; used_asm = 0;\
976     if( dct_asm.name != dct_ref.name )\
977     {\
978         set_func_name( #name );\
979         used_asm = 1;\
980         uint16_t *p = (uint16_t*)buf1;\
981         for( int i = 0; i < 16 && ok; i++ )\
982         {\
983             for( int j = 0; j < 8; j++ )\
984                 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
985                            : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
986                            : ((*p++)&0x1fff)-0x1000; /* general case */\
987             memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
988             call_c1( dct_c.name, dctdc[0], dct1 );\
989             call_a1( dct_asm.name, dctdc[1], dct2 );\
990             if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
991             {\
992                 ok = 0;\
993                 fprintf( stderr, #name " [FAILED]\n" ); \
994             }\
995         }\
996         call_c2( dct_c.name, dctdc[0], dct1 );\
997         call_a2( dct_asm.name, dctdc[1], dct2 );\
998     }\
999     report( #name " :" );
1000
1001     TEST_DCTDC_CHROMA( dct2x4dc );
1002 #undef TEST_DCTDC_CHROMA
1003
1004     x264_zigzag_function_t zigzag_c[2];
1005     x264_zigzag_function_t zigzag_ref[2];
1006     x264_zigzag_function_t zigzag_asm[2];
1007
1008     ALIGNED_16( dctcoef level1[64] );
1009     ALIGNED_16( dctcoef level2[64] );
1010
1011 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
1012     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1013     { \
1014         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1015         used_asm = 1; \
1016         for( int i = 0; i < size*size; i++ ) \
1017             dct[i] = i; \
1018         call_c( zigzag_c[interlace].name, t1, dct ); \
1019         call_a( zigzag_asm[interlace].name, t2, dct ); \
1020         if( memcmp( t1, t2, size*size*sizeof(dctcoef) ) ) \
1021         { \
1022             ok = 0; \
1023             for( int i = 0; i < 2; i++ ) \
1024             { \
1025                 dctcoef *d = (dctcoef*)(i ? t2 : t1); \
1026                 for( int j = 0; j < size; j++ ) \
1027                 { \
1028                     for( int k = 0; k < size; k++ ) \
1029                         fprintf( stderr, "%2d ", d[k+j*8] ); \
1030                     fprintf( stderr, "\n" ); \
1031                 } \
1032                 fprintf( stderr, "\n" ); \
1033             } \
1034             fprintf( stderr, #name " [FAILED]\n" ); \
1035         } \
1036     }
1037
1038 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
1039     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1040     { \
1041         int nz_a, nz_c; \
1042         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1043         used_asm = 1; \
1044         memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
1045         memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
1046         nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
1047         nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
1048         if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
1049         { \
1050             ok = 0; \
1051             fprintf( stderr, #name " [FAILED]\n" ); \
1052         } \
1053         call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
1054         call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
1055     }
1056
1057 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
1058     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1059     { \
1060         int nz_a, nz_c; \
1061         dctcoef dc_a, dc_c; \
1062         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1063         used_asm = 1; \
1064         for( int i = 0; i < 2; i++ ) \
1065         { \
1066             memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
1067             memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
1068             for( int j = 0; j < 4; j++ ) \
1069             { \
1070                 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1071                 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1072             } \
1073             nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1074             nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1075             if( memcmp( t1+1, t2+1, 15*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE * sizeof(pixel) ) || nz_c != nz_a || dc_c != dc_a ) \
1076             { \
1077                 ok = 0; \
1078                 fprintf( stderr, #name " [FAILED]\n" ); \
1079                 break; \
1080             } \
1081         } \
1082         call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1083         call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1084     }
1085
1086 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
1087     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1088     { \
1089         for( int j = 0; j < 100; j++ ) \
1090         { \
1091             set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1092             used_asm = 1; \
1093             memcpy(dct, buf1, size*sizeof(dctcoef)); \
1094             for( int i = 0; i < size; i++ ) \
1095                 dct[i] = rand()&0x1F ? 0 : dct[i]; \
1096             memcpy(buf3, buf4, 10); \
1097             call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
1098             call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
1099             if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
1100             { \
1101                 ok = 0; printf("%d: %d %d %d %d\n%d %d %d %d\n\n",memcmp( t1, t2, size*sizeof(dctcoef) ),buf3[0], buf3[1], buf3[8], buf3[9], buf4[0], buf4[1], buf4[8], buf4[9]);break;\
1102             } \
1103         } \
1104     }
1105
1106     x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
1107     x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
1108     x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
1109
1110     ok = 1; used_asm = 0;
1111     TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct8[0], 64 );
1112     report( "zigzag_interleave :" );
1113
1114     for( interlace = 0; interlace <= 1; interlace++ )
1115     {
1116         ok = 1; used_asm = 0;
1117         TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, dct8[0], 8 );
1118         TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 4 );
1119         TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
1120         TEST_ZIGZAG_SUB( sub_8x8, level1, level2, 64 );
1121         TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
1122         report( interlace ? "zigzag_field :" : "zigzag_frame :" );
1123     }
1124 #undef TEST_ZIGZAG_SCAN
1125 #undef TEST_ZIGZAG_SUB
1126
1127     return ret;
1128 }
1129
1130 static int check_mc( int cpu_ref, int cpu_new )
1131 {
1132     x264_mc_functions_t mc_c;
1133     x264_mc_functions_t mc_ref;
1134     x264_mc_functions_t mc_a;
1135     x264_pixel_function_t pixf;
1136
1137     pixel *src     = &(pbuf1)[2*64+2];
1138     pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1139                        &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1140     pixel *dst1    = pbuf3;
1141     pixel *dst2    = pbuf4;
1142
1143     int ret = 0, ok, used_asm;
1144
1145     x264_mc_init( 0, &mc_c, 0 );
1146     x264_mc_init( cpu_ref, &mc_ref, 0 );
1147     x264_mc_init( cpu_new, &mc_a, 0 );
1148     x264_pixel_init( 0, &pixf );
1149
1150 #define MC_TEST_LUMA( w, h ) \
1151         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1152         { \
1153             const x264_weight_t *weight = x264_weight_none; \
1154             set_func_name( "mc_luma_%dx%d", w, h ); \
1155             used_asm = 1; \
1156             for( int i = 0; i < 1024; i++ ) \
1157                 pbuf3[i] = pbuf4[i] = 0xCD; \
1158             call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1159             call_a( mc_a.mc_luma, dst2, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1160             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1161             { \
1162                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
1163                 ok = 0; \
1164             } \
1165         } \
1166         if( mc_a.get_ref != mc_ref.get_ref ) \
1167         { \
1168             pixel *ref = dst2; \
1169             intptr_t ref_stride = 32; \
1170             int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1171             const x264_weight_t *weight = x264_weight_none; \
1172             set_func_name( "get_ref_%dx%d", w_checked, h ); \
1173             used_asm = 1; \
1174             for( int i = 0; i < 1024; i++ ) \
1175                 pbuf3[i] = pbuf4[i] = 0xCD; \
1176             call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1177             ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1178             for( int i = 0; i < h; i++ ) \
1179                 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1180                 { \
1181                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w_checked, h ); \
1182                     ok = 0; \
1183                     break; \
1184                 } \
1185         }
1186
1187 #define MC_TEST_CHROMA( w, h ) \
1188         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1189         { \
1190             set_func_name( "mc_chroma_%dx%d", w, h ); \
1191             used_asm = 1; \
1192             for( int i = 0; i < 1024; i++ ) \
1193                 pbuf3[i] = pbuf4[i] = 0xCD; \
1194             call_c( mc_c.mc_chroma, dst1, dst1+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1195             call_a( mc_a.mc_chroma, dst2, dst2+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1196             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1197             for( int j = 0; j < h; j++ ) \
1198                 for( int i = w; i < 8; i++ ) \
1199                 { \
1200                     dst2[i+j*16+8] = dst1[i+j*16+8]; \
1201                     dst2[i+j*16  ] = dst1[i+j*16  ]; \
1202                 } \
1203             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1204             { \
1205                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
1206                 ok = 0; \
1207             } \
1208         }
1209     ok = 1; used_asm = 0;
1210     for( int dy = -8; dy < 8; dy++ )
1211         for( int dx = -128; dx < 128; dx++ )
1212         {
1213             if( rand()&15 ) continue; // running all of them is too slow
1214             MC_TEST_LUMA( 20, 18 );
1215             MC_TEST_LUMA( 16, 16 );
1216             MC_TEST_LUMA( 16, 8 );
1217             MC_TEST_LUMA( 12, 10 );
1218             MC_TEST_LUMA( 8, 16 );
1219             MC_TEST_LUMA( 8, 8 );
1220             MC_TEST_LUMA( 8, 4 );
1221             MC_TEST_LUMA( 4, 8 );
1222             MC_TEST_LUMA( 4, 4 );
1223         }
1224     report( "mc luma :" );
1225
1226     ok = 1; used_asm = 0;
1227     for( int dy = -1; dy < 9; dy++ )
1228         for( int dx = -128; dx < 128; dx++ )
1229         {
1230             if( rand()&15 ) continue;
1231             MC_TEST_CHROMA( 8, 8 );
1232             MC_TEST_CHROMA( 8, 4 );
1233             MC_TEST_CHROMA( 4, 8 );
1234             MC_TEST_CHROMA( 4, 4 );
1235             MC_TEST_CHROMA( 4, 2 );
1236             MC_TEST_CHROMA( 2, 4 );
1237             MC_TEST_CHROMA( 2, 2 );
1238         }
1239     report( "mc chroma :" );
1240 #undef MC_TEST_LUMA
1241 #undef MC_TEST_CHROMA
1242
1243 #define MC_TEST_AVG( name, weight ) \
1244 { \
1245     for( int i = 0; i < 12; i++ ) \
1246     { \
1247         memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1248         memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1249         if( mc_a.name[i] != mc_ref.name[i] ) \
1250         { \
1251             set_func_name( "%s_%s", #name, pixel_names[i] ); \
1252             used_asm = 1; \
1253             call_c1( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1254             call_a1( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1255             if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1256             { \
1257                 ok = 0; \
1258                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1259             } \
1260             call_c2( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1261             call_a2( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1262         } \
1263     } \
1264 }
1265
1266     ok = 1, used_asm = 0;
1267     for( int w = -63; w <= 127 && ok; w++ )
1268         MC_TEST_AVG( avg, w );
1269     report( "mc wpredb :" );
1270
1271 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1272     int align_off = (aligned ? 0 : rand()%16); \
1273     for( int i = 1; i <= 5; i++ ) \
1274     { \
1275         ALIGNED_16( pixel buffC[640] ); \
1276         ALIGNED_16( pixel buffA[640] ); \
1277         int j = X264_MAX( i*4, 2 ); \
1278         memset( buffC, 0, 640 * sizeof(pixel) ); \
1279         memset( buffA, 0, 640 * sizeof(pixel) ); \
1280         x264_t ha; \
1281         ha.mc = mc_a; \
1282         /* w12 is the same as w16 in some cases */ \
1283         if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1284             continue; \
1285         if( mc_a.name[i] != mc_ref.name[i] ) \
1286         { \
1287             set_func_name( "%s_w%d", #name, j ); \
1288             used_asm = 1; \
1289             call_c1( mc_c.weight[i],     buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1290             mc_a.weight_cache(&ha, &weight); \
1291             call_a1( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1292             for( int k = 0; k < 16; k++ ) \
1293                 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1294                 { \
1295                     ok = 0; \
1296                     fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1297                     break; \
1298                 } \
1299             /* omit unlikely high scales for benchmarking */ \
1300             if( (s << (8-d)) < 512 ) \
1301             { \
1302                 call_c2( mc_c.weight[i],     buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1303                 call_a2( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1304             } \
1305         } \
1306     }
1307
1308     ok = 1; used_asm = 0;
1309
1310     int align_cnt = 0;
1311     for( int s = 0; s <= 127 && ok; s++ )
1312     {
1313         for( int o = -128; o <= 127 && ok; o++ )
1314         {
1315             if( rand() & 2047 ) continue;
1316             for( int d = 0; d <= 7 && ok; d++ )
1317             {
1318                 if( s == 1<<d )
1319                     continue;
1320                 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1321                 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1322             }
1323         }
1324
1325     }
1326     report( "mc weight :" );
1327
1328     ok = 1; used_asm = 0;
1329     for( int o = 0; o <= 127 && ok; o++ )
1330     {
1331         int s = 1, d = 0;
1332         if( rand() & 15 ) continue;
1333         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1334         MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1335     }
1336     report( "mc offsetadd :" );
1337     ok = 1; used_asm = 0;
1338     for( int o = -128; o < 0 && ok; o++ )
1339     {
1340         int s = 1, d = 0;
1341         if( rand() & 15 ) continue;
1342         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1343         MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1344     }
1345     report( "mc offsetsub :" );
1346
1347     ok = 1; used_asm = 0;
1348     for( int height = 8; height <= 16; height += 8 )
1349     {
1350         if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1351         {
1352             set_func_name( "store_interleave_chroma" );
1353             used_asm = 1;
1354             memset( pbuf3, 0, 64*height );
1355             memset( pbuf4, 0, 64*height );
1356             call_c( mc_c.store_interleave_chroma, pbuf3, (intptr_t)64, pbuf1, pbuf1+16, height );
1357             call_a( mc_a.store_interleave_chroma, pbuf4, (intptr_t)64, pbuf1, pbuf1+16, height );
1358             if( memcmp( pbuf3, pbuf4, 64*height ) )
1359             {
1360                 ok = 0;
1361                 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1362                 break;
1363             }
1364         }
1365         if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1366         {
1367             set_func_name( "load_deinterleave_chroma_fenc" );
1368             used_asm = 1;
1369             call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, (intptr_t)64, height );
1370             call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, (intptr_t)64, height );
1371             if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1372             {
1373                 ok = 0;
1374                 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1375                 break;
1376             }
1377         }
1378         if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1379         {
1380             set_func_name( "load_deinterleave_chroma_fdec" );
1381             used_asm = 1;
1382             call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, (intptr_t)64, height );
1383             call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, (intptr_t)64, height );
1384             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1385             {
1386                 ok = 0;
1387                 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1388                 break;
1389             }
1390         }
1391     }
1392     report( "store_interleave :" );
1393
1394     struct plane_spec {
1395         int w, h, src_stride;
1396     } plane_specs[] = { {2,2,2}, {8,6,8}, {20,31,24}, {32,8,40}, {256,10,272}, {504,7,505}, {528,6,528}, {256,10,-256}, {263,9,-264}, {1904,1,0} };
1397     ok = 1; used_asm = 0;
1398     if( mc_a.plane_copy != mc_ref.plane_copy )
1399     {
1400         set_func_name( "plane_copy" );
1401         used_asm = 1;
1402         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1403         {
1404             int w = plane_specs[i].w;
1405             int h = plane_specs[i].h;
1406             intptr_t src_stride = plane_specs[i].src_stride;
1407             intptr_t dst_stride = (w + 127) & ~63;
1408             assert( dst_stride * h <= 0x1000 );
1409             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1410             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1411             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1412             call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1413             call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1414             for( int y = 0; y < h; y++ )
1415                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1416                 {
1417                     ok = 0;
1418                     fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1419                     break;
1420                 }
1421         }
1422     }
1423
1424     if( mc_a.plane_copy_swap != mc_ref.plane_copy_swap )
1425     {
1426         set_func_name( "plane_copy_swap" );
1427         used_asm = 1;
1428         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1429         {
1430             int w = (plane_specs[i].w + 1) >> 1;
1431             int h = plane_specs[i].h;
1432             intptr_t src_stride = plane_specs[i].src_stride;
1433             intptr_t dst_stride = (2*w + 127) & ~63;
1434             assert( dst_stride * h <= 0x1000 );
1435             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1436             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1437             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1438             call_c( mc_c.plane_copy_swap, pbuf3, dst_stride, src1, src_stride, w, h );
1439             call_a( mc_a.plane_copy_swap, pbuf4, dst_stride, src1, src_stride, w, h );
1440             for( int y = 0; y < h; y++ )
1441                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1442                 {
1443                     ok = 0;
1444                     fprintf( stderr, "plane_copy_swap FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1445                     break;
1446                 }
1447         }
1448     }
1449
1450     if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1451     {
1452         set_func_name( "plane_copy_interleave" );
1453         used_asm = 1;
1454         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1455         {
1456             int w = (plane_specs[i].w + 1) >> 1;
1457             int h = plane_specs[i].h;
1458             intptr_t src_stride = (plane_specs[i].src_stride + 1) >> 1;
1459             intptr_t dst_stride = (2*w + 127) & ~63;
1460             assert( dst_stride * h <= 0x1000 );
1461             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1462             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1463             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1464             call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1465             call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1466             for( int y = 0; y < h; y++ )
1467                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1468                 {
1469                     ok = 0;
1470                     fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1471                     break;
1472                 }
1473         }
1474     }
1475
1476     if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1477     {
1478         set_func_name( "plane_copy_deinterleave" );
1479         used_asm = 1;
1480         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1481         {
1482             int w = (plane_specs[i].w + 1) >> 1;
1483             int h = plane_specs[i].h;
1484             intptr_t dst_stride = w;
1485             intptr_t src_stride = (2*w + 127) & ~63;
1486             intptr_t offv = (dst_stride*h + 31) & ~15;
1487             memset( pbuf3, 0, 0x1000 );
1488             memset( pbuf4, 0, 0x1000 );
1489             call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1490             call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1491             for( int y = 0; y < h; y++ )
1492                 if( memcmp( pbuf3+y*dst_stride,      pbuf4+y*dst_stride, w ) ||
1493                     memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1494                 {
1495                     ok = 0;
1496                     fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1497                     break;
1498                 }
1499         }
1500     }
1501
1502     if( mc_a.plane_copy_deinterleave_rgb != mc_ref.plane_copy_deinterleave_rgb )
1503     {
1504         set_func_name( "plane_copy_deinterleave_rgb" );
1505         used_asm = 1;
1506         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1507         {
1508             int w = (plane_specs[i].w + 2) >> 2;
1509             int h = plane_specs[i].h;
1510             intptr_t src_stride = plane_specs[i].src_stride;
1511             intptr_t dst_stride = ALIGN( w, 16 );
1512             intptr_t offv = dst_stride*h + 16;
1513
1514             for( int pw = 3; pw <= 4; pw++ )
1515             {
1516                 memset( pbuf3, 0, 0x1000 );
1517                 memset( pbuf4, 0, 0x1000 );
1518                 call_c( mc_c.plane_copy_deinterleave_rgb, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf3+2*offv, dst_stride, pbuf1, src_stride, pw, w, h );
1519                 call_a( mc_a.plane_copy_deinterleave_rgb, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf4+2*offv, dst_stride, pbuf1, src_stride, pw, w, h );
1520                 for( int y = 0; y < h; y++ )
1521                     if( memcmp( pbuf3+y*dst_stride+0*offv, pbuf4+y*dst_stride+0*offv, w ) ||
1522                         memcmp( pbuf3+y*dst_stride+1*offv, pbuf4+y*dst_stride+1*offv, w ) ||
1523                         memcmp( pbuf3+y*dst_stride+2*offv, pbuf4+y*dst_stride+2*offv, w ) )
1524                     {
1525                         ok = 0;
1526                         fprintf( stderr, "plane_copy_deinterleave_rgb FAILED: w=%d h=%d stride=%d pw=%d\n", w, h, (int)src_stride, pw );
1527                         break;
1528                     }
1529             }
1530         }
1531     }
1532     report( "plane_copy :" );
1533
1534     if( mc_a.plane_copy_deinterleave_v210 != mc_ref.plane_copy_deinterleave_v210 )
1535     {
1536         set_func_name( "plane_copy_deinterleave_v210" );
1537         ok = 1; used_asm = 1;
1538         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1539         {
1540             int w = (plane_specs[i].w + 1) >> 1;
1541             int h = plane_specs[i].h;
1542             intptr_t dst_stride = ALIGN( w, 16 );
1543             intptr_t src_stride = (w + 47) / 48 * 128 / sizeof(uint32_t);
1544             intptr_t offv = dst_stride*h + 32;
1545             memset( pbuf3, 0, 0x1000 );
1546             memset( pbuf4, 0, 0x1000 );
1547             call_c( mc_c.plane_copy_deinterleave_v210, pbuf3, dst_stride, pbuf3+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h );
1548             call_a( mc_a.plane_copy_deinterleave_v210, pbuf4, dst_stride, pbuf4+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h );
1549             for( int y = 0; y < h; y++ )
1550                 if( memcmp( pbuf3+y*dst_stride,      pbuf4+y*dst_stride,      w*sizeof(uint16_t) ) ||
1551                     memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w*sizeof(uint16_t) ) )
1552                 {
1553                     ok = 0;
1554                     fprintf( stderr, "plane_copy_deinterleave_v210 FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1555                     break;
1556                 }
1557         }
1558         report( "v210 :" );
1559     }
1560
1561     if( mc_a.hpel_filter != mc_ref.hpel_filter )
1562     {
1563         pixel *srchpel = pbuf1+8+2*64;
1564         pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1565         pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1566         void *tmp = pbuf3+49*64;
1567         set_func_name( "hpel_filter" );
1568         ok = 1; used_asm = 1;
1569         memset( pbuf3, 0, 4096 * sizeof(pixel) );
1570         memset( pbuf4, 0, 4096 * sizeof(pixel) );
1571         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, (intptr_t)64, 48, 10, tmp );
1572         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, (intptr_t)64, 48, 10, tmp );
1573         for( int i = 0; i < 3; i++ )
1574             for( int j = 0; j < 10; j++ )
1575                 //FIXME ideally the first pixels would match too, but they aren't actually used
1576                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1577                 {
1578                     ok = 0;
1579                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1580                     for( int k = 0; k < 48; k++ )
1581                         printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1582                     printf( "\n" );
1583                     for( int k = 0; k < 48; k++ )
1584                         printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1585                     printf( "\n" );
1586                     break;
1587                 }
1588         report( "hpel filter :" );
1589     }
1590
1591     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1592     {
1593         pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1594         pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1595         set_func_name( "lowres_init" );
1596         ok = 1; used_asm = 1;
1597         for( int w = 96; w <= 96+24; w += 8 )
1598         {
1599             intptr_t stride = (w*2+31)&~31;
1600             intptr_t stride_lowres = (w+31)&~31;
1601             call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], stride, stride_lowres, w, 8 );
1602             call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], stride, stride_lowres, w, 8 );
1603             for( int i = 0; i < 8; i++ )
1604             {
1605                 for( int j = 0; j < 4; j++ )
1606                     if( memcmp( dstc[j]+i*stride_lowres, dsta[j]+i*stride_lowres, w * sizeof(pixel) ) )
1607                     {
1608                         ok = 0;
1609                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1610                         for( int k = 0; k < w; k++ )
1611                             printf( "%d ", dstc[j][k+i*stride_lowres] );
1612                         printf( "\n" );
1613                         for( int k = 0; k < w; k++ )
1614                             printf( "%d ", dsta[j][k+i*stride_lowres] );
1615                         printf( "\n" );
1616                         break;
1617                     }
1618             }
1619         }
1620         report( "lowres init :" );
1621     }
1622
1623 #define INTEGRAL_INIT( name, size, offset, cmp_len, ... )\
1624     if( mc_a.name != mc_ref.name )\
1625     {\
1626         intptr_t stride = 96;\
1627         set_func_name( #name );\
1628         used_asm = 1;\
1629         memcpy( buf3, buf1, size*2*stride );\
1630         memcpy( buf4, buf1, size*2*stride );\
1631         uint16_t *sum = (uint16_t*)buf3;\
1632         call_c1( mc_c.name, sum+offset, __VA_ARGS__ );\
1633         sum = (uint16_t*)buf4;\
1634         call_a1( mc_a.name, sum+offset, __VA_ARGS__ );\
1635         if( memcmp( buf3+2*offset, buf4+2*offset, cmp_len*2 )\
1636             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1637             ok = 0;\
1638         call_c2( mc_c.name, sum+offset, __VA_ARGS__ );\
1639         call_a2( mc_a.name, sum+offset, __VA_ARGS__ );\
1640     }
1641     ok = 1; used_asm = 0;
1642     INTEGRAL_INIT( integral_init4h, 2, stride, stride-4, pbuf2, stride );
1643     INTEGRAL_INIT( integral_init8h, 2, stride, stride-8, pbuf2, stride );
1644     INTEGRAL_INIT( integral_init4v, 14, 0, stride-8, sum+9*stride, stride );
1645     INTEGRAL_INIT( integral_init8v, 9, 0, stride-8, stride );
1646     report( "integral init :" );
1647
1648     ok = 1; used_asm = 0;
1649     if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1650     {
1651         used_asm = 1;
1652         x264_emms();
1653         for( int i = 0; i < 10; i++ )
1654         {
1655             float fps_factor = (rand()&65535) / 65535.0f;
1656             set_func_name( "mbtree_propagate_cost" );
1657             int16_t *dsta = (int16_t*)buf3;
1658             int16_t *dstc = dsta+400;
1659             uint16_t *prop = (uint16_t*)buf1;
1660             uint16_t *intra = (uint16_t*)buf4;
1661             uint16_t *inter = intra+128;
1662             uint16_t *qscale = inter+128;
1663             uint16_t *rnd = (uint16_t*)buf2;
1664             x264_emms();
1665             for( int j = 0; j < 100; j++ )
1666             {
1667                 intra[j]  = *rnd++ & 0x7fff;
1668                 intra[j] += !intra[j];
1669                 inter[j]  = *rnd++ & 0x7fff;
1670                 qscale[j] = *rnd++ & 0x7fff;
1671             }
1672             call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1673             call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1674             // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1675             x264_emms();
1676             for( int j = 0; j < 100 && ok; j++ )
1677             {
1678                 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1679                 if( !ok )
1680                     fprintf( stderr, "mbtree_propagate_cost FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1681             }
1682         }
1683     }
1684
1685     if( mc_a.mbtree_propagate_list != mc_ref.mbtree_propagate_list )
1686     {
1687         used_asm = 1;
1688         for( int i = 0; i < 8; i++ )
1689         {
1690             set_func_name( "mbtree_propagate_list" );
1691             x264_t h;
1692             int height = 4;
1693             int width = 128;
1694             int size = width*height;
1695             h.mb.i_mb_stride = width;
1696             h.mb.i_mb_width = width;
1697             h.mb.i_mb_height = height;
1698
1699             uint16_t *ref_costsc = (uint16_t*)buf3;
1700             uint16_t *ref_costsa = (uint16_t*)buf4;
1701             int16_t (*mvs)[2] = (int16_t(*)[2])(ref_costsc + size);
1702             int16_t *propagate_amount = (int16_t*)(mvs + width);
1703             uint16_t *lowres_costs = (uint16_t*)(propagate_amount + width);
1704             h.scratch_buffer2 = (uint8_t*)(ref_costsa + size);
1705             int bipred_weight = (rand()%63)+1;
1706             int list = i&1;
1707             for( int j = 0; j < size; j++ )
1708                 ref_costsc[j] = ref_costsa[j] = rand()&32767;
1709             for( int j = 0; j < width; j++ )
1710             {
1711                 static const uint8_t list_dist[2][8] = {{0,1,1,1,1,1,1,1},{1,1,3,3,3,3,3,2}};
1712                 for( int k = 0; k < 2; k++ )
1713                     mvs[j][k] = (rand()&127) - 64;
1714                 propagate_amount[j] = rand()&32767;
1715                 lowres_costs[j] = list_dist[list][rand()&7] << LOWRES_COST_SHIFT;
1716             }
1717
1718             call_c1( mc_c.mbtree_propagate_list, &h, ref_costsc, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1719             call_a1( mc_a.mbtree_propagate_list, &h, ref_costsa, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1720
1721             for( int j = 0; j < size && ok; j++ )
1722             {
1723                 ok &= abs(ref_costsa[j] - ref_costsc[j]) <= 1;
1724                 if( !ok )
1725                     fprintf( stderr, "mbtree_propagate_list FAILED at %d: %d !~= %d\n", j, ref_costsc[j], ref_costsa[j] );
1726             }
1727
1728             call_c2( mc_c.mbtree_propagate_list, &h, ref_costsc, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1729             call_a2( mc_a.mbtree_propagate_list, &h, ref_costsa, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1730         }
1731     }
1732     report( "mbtree :" );
1733
1734     if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1735     {
1736         set_func_name( "memcpy_aligned" );
1737         ok = 1; used_asm = 1;
1738         for( size_t size = 16; size < 256; size += 16 )
1739         {
1740             memset( buf4, 0xAA, size + 1 );
1741             call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1742             call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1743             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1744             {
1745                 ok = 0;
1746                 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", (int)size );
1747                 break;
1748             }
1749         }
1750         report( "memcpy aligned :" );
1751     }
1752
1753     if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1754     {
1755         set_func_name( "memzero_aligned" );
1756         ok = 1; used_asm = 1;
1757         for( size_t size = 128; size < 1024; size += 128 )
1758         {
1759             memset( buf4, 0xAA, size + 1 );
1760             call_c( mc_c.memzero_aligned, buf3, size );
1761             call_a( mc_a.memzero_aligned, buf4, size );
1762             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1763             {
1764                 ok = 0;
1765                 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", (int)size );
1766                 break;
1767             }
1768         }
1769         report( "memzero aligned :" );
1770     }
1771
1772     return ret;
1773 }
1774
1775 static int check_deblock( int cpu_ref, int cpu_new )
1776 {
1777     x264_deblock_function_t db_c;
1778     x264_deblock_function_t db_ref;
1779     x264_deblock_function_t db_a;
1780     int ret = 0, ok = 1, used_asm = 0;
1781     int alphas[36], betas[36];
1782     int8_t tcs[36][4];
1783
1784     x264_deblock_init( 0, &db_c, 0 );
1785     x264_deblock_init( cpu_ref, &db_ref, 0 );
1786     x264_deblock_init( cpu_new, &db_a, 0 );
1787
1788     /* not exactly the real values of a,b,tc but close enough */
1789     for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1790     {
1791         alphas[i] = a << (BIT_DEPTH-8);
1792         betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1793         tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1794         tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1795         tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1796         a = a*9/10;
1797         c = c*9/10;
1798     }
1799
1800 #define TEST_DEBLOCK( name, align, ... ) \
1801     for( int i = 0; i < 36; i++ ) \
1802     { \
1803         intptr_t off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1804         for( int j = 0; j < 1024; j++ ) \
1805             /* two distributions of random to excersize different failure modes */ \
1806             pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1807         memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1808         if( db_a.name != db_ref.name ) \
1809         { \
1810             set_func_name( #name ); \
1811             used_asm = 1; \
1812             call_c1( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1813             call_a1( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1814             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1815             { \
1816                 ok = 0; \
1817                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1818                 break; \
1819             } \
1820             call_c2( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1821             call_a2( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1822         } \
1823     }
1824
1825     TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1826     TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1827     TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1828     TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1829     TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1830     TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1831     TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1832     TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1833     TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1834     TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1835     TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1836     TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1837     TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1838     TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1839
1840     if( db_a.deblock_strength != db_ref.deblock_strength )
1841     {
1842         for( int i = 0; i < 100; i++ )
1843         {
1844             ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1845             ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1846             ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1847             ALIGNED_ARRAY_N( uint8_t, bs, [2],[2][8][4] );
1848             memset( bs, 99, sizeof(uint8_t)*2*4*8*2 );
1849             for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1850                 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1851             for( int j = 0; j < 2; j++ )
1852                 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1853                 {
1854                     ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1855                     for( int l = 0; l < 2; l++ )
1856                         mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1857                 }
1858             set_func_name( "deblock_strength" );
1859             call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1860             call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1861             if( memcmp( bs[0], bs[1], sizeof(uint8_t)*2*4*8 ) )
1862             {
1863                 ok = 0;
1864                 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1865                 for( int j = 0; j < 2; j++ )
1866                 {
1867                     for( int k = 0; k < 2; k++ )
1868                         for( int l = 0; l < 4; l++ )
1869                         {
1870                             for( int m = 0; m < 4; m++ )
1871                                 printf("%d ",bs[j][k][l][m]);
1872                             printf("\n");
1873                         }
1874                     printf("\n");
1875                 }
1876                 break;
1877             }
1878         }
1879     }
1880
1881     report( "deblock :" );
1882
1883     return ret;
1884 }
1885
1886 static int check_quant( int cpu_ref, int cpu_new )
1887 {
1888     x264_quant_function_t qf_c;
1889     x264_quant_function_t qf_ref;
1890     x264_quant_function_t qf_a;
1891     ALIGNED_ARRAY_N( dctcoef, dct1,[64] );
1892     ALIGNED_ARRAY_N( dctcoef, dct2,[64] );
1893     ALIGNED_ARRAY_N( dctcoef, dct3,[8],[16] );
1894     ALIGNED_ARRAY_N( dctcoef, dct4,[8],[16] );
1895     ALIGNED_ARRAY_N( uint8_t, cqm_buf,[64] );
1896     int ret = 0, ok, used_asm;
1897     int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1898     x264_t h_buf;
1899     x264_t *h = &h_buf;
1900     memset( h, 0, sizeof(*h) );
1901     h->sps->i_chroma_format_idc = 1;
1902     x264_param_default( &h->param );
1903     h->chroma_qp_table = i_chroma_qp_table + 12;
1904     h->param.analyse.b_transform_8x8 = 1;
1905
1906     for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1907     {
1908         if( i_cqm == 0 )
1909         {
1910             for( int i = 0; i < 6; i++ )
1911                 h->pps->scaling_list[i] = x264_cqm_flat16;
1912             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1913         }
1914         else if( i_cqm == 1 )
1915         {
1916             for( int i = 0; i < 6; i++ )
1917                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1918             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1919         }
1920         else
1921         {
1922             int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1923             if( i_cqm == 2 )
1924                 for( int i = 0; i < 64; i++ )
1925                     cqm_buf[i] = 10 + rand() % (max_scale - 9);
1926             else
1927                 for( int i = 0; i < 64; i++ )
1928                     cqm_buf[i] = 1;
1929             for( int i = 0; i < 6; i++ )
1930                 h->pps->scaling_list[i] = cqm_buf;
1931             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1932         }
1933
1934         h->param.rc.i_qp_min = 0;
1935         h->param.rc.i_qp_max = QP_MAX_SPEC;
1936         x264_cqm_init( h );
1937         x264_quant_init( h, 0, &qf_c );
1938         x264_quant_init( h, cpu_ref, &qf_ref );
1939         x264_quant_init( h, cpu_new, &qf_a );
1940
1941 #define INIT_QUANT8(j,max) \
1942         { \
1943             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1944             for( int i = 0; i < max; i++ ) \
1945             { \
1946                 unsigned int scale = (255*scale1d[(i>>3)&7]*scale1d[i&7])/16; \
1947                 dct1[i] = dct2[i] = (j>>(i>>6))&1 ? (rand()%(2*scale+1))-scale : 0; \
1948             } \
1949         }
1950
1951 #define INIT_QUANT4(j,max) \
1952         { \
1953             static const int scale1d[4] = {4,6,4,6}; \
1954             for( int i = 0; i < max; i++ ) \
1955             { \
1956                 unsigned int scale = 255*scale1d[(i>>2)&3]*scale1d[i&3]; \
1957                 dct1[i] = dct2[i] = (j>>(i>>4))&1 ? (rand()%(2*scale+1))-scale : 0; \
1958             } \
1959         }
1960
1961 #define TEST_QUANT_DC( name, cqm ) \
1962         if( qf_a.name != qf_ref.name ) \
1963         { \
1964             set_func_name( #name ); \
1965             used_asms[0] = 1; \
1966             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1967             { \
1968                 for( int j = 0; j < 2; j++ ) \
1969                 { \
1970                     int result_c, result_a; \
1971                     for( int i = 0; i < 16; i++ ) \
1972                         dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1973                     result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1974                     result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1975                     if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1976                     { \
1977                         oks[0] = 0; \
1978                         fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1979                         break; \
1980                     } \
1981                     call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1982                     call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1983                 } \
1984             } \
1985         }
1986
1987 #define TEST_QUANT( qname, block, type, w, maxj ) \
1988         if( qf_a.qname != qf_ref.qname ) \
1989         { \
1990             set_func_name( #qname ); \
1991             used_asms[0] = 1; \
1992             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1993             { \
1994                 for( int j = 0; j < maxj; j++ ) \
1995                 { \
1996                     INIT_QUANT##type(j, w*w) \
1997                     int result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1998                     int result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1999                     if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
2000                     { \
2001                         oks[0] = 0; \
2002                         fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
2003                         break; \
2004                     } \
2005                     call_c2( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
2006                     call_a2( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
2007                 } \
2008             } \
2009         }
2010
2011         TEST_QUANT( quant_8x8, CQM_8IY, 8, 8, 2 );
2012         TEST_QUANT( quant_8x8, CQM_8PY, 8, 8, 2 );
2013         TEST_QUANT( quant_4x4, CQM_4IY, 4, 4, 2 );
2014         TEST_QUANT( quant_4x4, CQM_4PY, 4, 4, 2 );
2015         TEST_QUANT( quant_4x4x4, CQM_4IY, 4, 8, 16 );
2016         TEST_QUANT( quant_4x4x4, CQM_4PY, 4, 8, 16 );
2017         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
2018         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
2019
2020 #define TEST_DEQUANT( qname, dqname, block, w ) \
2021         if( qf_a.dqname != qf_ref.dqname ) \
2022         { \
2023             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
2024             used_asms[1] = 1; \
2025             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2026             { \
2027                 INIT_QUANT##w(1, w*w) \
2028                 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
2029                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
2030                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2031                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2032                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
2033                 { \
2034                     oks[1] = 0; \
2035                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
2036                     break; \
2037                 } \
2038                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2039                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2040             } \
2041         }
2042
2043         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
2044         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
2045         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
2046         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
2047
2048 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
2049         if( qf_a.dqname != qf_ref.dqname ) \
2050         { \
2051             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
2052             used_asms[1] = 1; \
2053             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2054             { \
2055                 for( int i = 0; i < 16; i++ ) \
2056                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
2057                 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
2058                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
2059                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2060                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2061                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
2062                 { \
2063                     oks[1] = 0; \
2064                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
2065                 } \
2066                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2067                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2068             } \
2069         }
2070
2071         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
2072
2073         if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
2074         {
2075             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
2076             used_asms[1] = 1;
2077             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
2078             {
2079                 for( int i = 0; i < 8; i++ )
2080                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
2081                 qf_c.quant_2x2_dc( &dct1[0], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
2082                 qf_c.quant_2x2_dc( &dct1[4], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
2083                 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
2084                 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
2085                 for( int i = 0; i < 8; i++ )
2086                     if( dct3[i][0] != dct4[i][0] )
2087                     {
2088                         oks[1] = 0;
2089                         fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
2090                         break;
2091                     }
2092             }
2093         }
2094
2095         if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
2096         {
2097             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
2098             used_asms[1] = 1;
2099             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
2100             {
2101                 for( int i = 0; i < 8; i++ )
2102                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
2103                 qf_c.quant_2x2_dc( &dct1[0], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
2104                 qf_c.quant_2x2_dc( &dct1[4], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
2105                 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
2106                 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
2107                 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
2108                 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
2109                 {
2110                     oks[1] = 0;
2111                     fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
2112                     break;
2113                 }
2114                 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
2115                 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
2116             }
2117         }
2118
2119 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
2120         if( qf_a.optname != qf_ref.optname ) \
2121         { \
2122             set_func_name( #optname ); \
2123             used_asms[2] = 1; \
2124             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2125             { \
2126                 int qpdc = qp + (size == 8 ? 3 : 0); \
2127                 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
2128                 if( dmf > 32*64 ) \
2129                     continue; \
2130                 for( int i = 16; ; i <<= 1 ) \
2131                 { \
2132                     int res_c, res_asm; \
2133                     int max = X264_MIN( i, PIXEL_MAX*16 ); \
2134                     for( int j = 0; j < size; j++ ) \
2135                         dct1[j] = rand()%(max*2+1) - max; \
2136                     for( int j = 0; i <= size; j += 4 ) \
2137                         qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
2138                     memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
2139                     res_c   = call_c1( qf_c.optname, dct1, dmf ); \
2140                     res_asm = call_a1( qf_a.optname, dct2, dmf ); \
2141                     if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
2142                     { \
2143                         oks[2] = 0; \
2144                         fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
2145                     } \
2146                     call_c2( qf_c.optname, dct1, dmf ); \
2147                     call_a2( qf_a.optname, dct2, dmf ); \
2148                     if( i >= PIXEL_MAX*16 ) \
2149                         break; \
2150                 } \
2151             } \
2152         }
2153
2154         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
2155         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
2156
2157         x264_cqm_delete( h );
2158     }
2159
2160     ok = oks[0]; used_asm = used_asms[0];
2161     report( "quant :" );
2162
2163     ok = oks[1]; used_asm = used_asms[1];
2164     report( "dequant :" );
2165
2166     ok = oks[2]; used_asm = used_asms[2];
2167     report( "optimize chroma dc :" );
2168
2169     ok = 1; used_asm = 0;
2170     if( qf_a.denoise_dct != qf_ref.denoise_dct )
2171     {
2172         used_asm = 1;
2173         for( int size = 16; size <= 64; size += 48 )
2174         {
2175             set_func_name( "denoise_dct" );
2176             memcpy( dct1, buf1, size*sizeof(dctcoef) );
2177             memcpy( dct2, buf1, size*sizeof(dctcoef) );
2178             memcpy( buf3+256, buf3, 256 );
2179             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3,       (udctcoef*)buf2, size );
2180             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2181             if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
2182                 ok = 0;
2183             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3,       (udctcoef*)buf2, size );
2184             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2185         }
2186     }
2187     report( "denoise dct :" );
2188
2189 #define TEST_DECIMATE( decname, w, ac, thresh ) \
2190     if( qf_a.decname != qf_ref.decname ) \
2191     { \
2192         set_func_name( #decname ); \
2193         used_asm = 1; \
2194         for( int i = 0; i < 100; i++ ) \
2195         { \
2196             static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
2197             static const int zerorate_lut[4] = {3,7,15,31};\
2198             int zero_rate = zerorate_lut[i&3];\
2199             for( int idx = 0; idx < w*w; idx++ ) \
2200             { \
2201                 int sign = (rand()&1) ? -1 : 1; \
2202                 int abs_level = distrib[rand()&15]; \
2203                 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
2204                 int zero = !(rand()&zero_rate); \
2205                 dct1[idx] = zero * abs_level * sign; \
2206             } \
2207             if( ac ) \
2208                 dct1[0] = 0; \
2209             int result_c = call_c( qf_c.decname, dct1 ); \
2210             int result_a = call_a( qf_a.decname, dct1 ); \
2211             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
2212             { \
2213                 ok = 0; \
2214                 fprintf( stderr, #decname ": [FAILED]\n" ); \
2215                 break; \
2216             } \
2217         } \
2218     }
2219
2220     ok = 1; used_asm = 0;
2221     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
2222     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
2223     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
2224     report( "decimate_score :" );
2225
2226 #define TEST_LAST( last, lastname, size, ac ) \
2227     if( qf_a.last != qf_ref.last ) \
2228     { \
2229         set_func_name( #lastname ); \
2230         used_asm = 1; \
2231         for( int i = 0; i < 100; i++ ) \
2232         { \
2233             int nnz = 0; \
2234             int max = rand() & (size-1); \
2235             memset( dct1, 0, size*sizeof(dctcoef) ); \
2236             for( int idx = ac; idx < max; idx++ ) \
2237                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2238             if( !nnz ) \
2239                 dct1[ac] = 1; \
2240             int result_c = call_c( qf_c.last, dct1+ac ); \
2241             int result_a = call_a( qf_a.last, dct1+ac ); \
2242             if( result_c != result_a ) \
2243             { \
2244                 ok = 0; \
2245                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2246                 break; \
2247             } \
2248         } \
2249     }
2250
2251     ok = 1; used_asm = 0;
2252     TEST_LAST( coeff_last4              , coeff_last4,   4, 0 );
2253     TEST_LAST( coeff_last8              , coeff_last8,   8, 0 );
2254     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 16, 1 );
2255     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2256     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2257     report( "coeff_last :" );
2258
2259 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2260     if( qf_a.lastname != qf_ref.lastname ) \
2261     { \
2262         set_func_name( #name ); \
2263         used_asm = 1; \
2264         for( int i = 0; i < 100; i++ ) \
2265         { \
2266             x264_run_level_t runlevel_c, runlevel_a; \
2267             int nnz = 0; \
2268             int max = rand() & (size-1); \
2269             memset( dct1, 0, size*sizeof(dctcoef) ); \
2270             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2271             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2272             for( int idx = ac; idx < max; idx++ ) \
2273                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2274             if( !nnz ) \
2275                 dct1[ac] = 1; \
2276             int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2277             int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2278             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2279                 runlevel_c.mask != runlevel_a.mask || \
2280                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c)) \
2281             { \
2282                 ok = 0; \
2283                 fprintf( stderr, #name ": [FAILED]\n" ); \
2284                 break; \
2285             } \
2286         } \
2287     }
2288
2289     ok = 1; used_asm = 0;
2290     TEST_LEVELRUN( coeff_level_run4              , coeff_level_run4,   4, 0 );
2291     TEST_LEVELRUN( coeff_level_run8              , coeff_level_run8,   8, 0 );
2292     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2293     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2294     report( "coeff_level_run :" );
2295
2296     return ret;
2297 }
2298
2299 static int check_intra( int cpu_ref, int cpu_new )
2300 {
2301     int ret = 0, ok = 1, used_asm = 0;
2302     ALIGNED_ARRAY_32( pixel, edge,[36] );
2303     ALIGNED_ARRAY_32( pixel, edge2,[36] );
2304     ALIGNED_ARRAY_32( pixel, fdec,[FDEC_STRIDE*20] );
2305     struct
2306     {
2307         x264_predict_t      predict_16x16[4+3];
2308         x264_predict_t      predict_8x8c[4+3];
2309         x264_predict_t      predict_8x16c[4+3];
2310         x264_predict8x8_t   predict_8x8[9+3];
2311         x264_predict_t      predict_4x4[9+3];
2312         x264_predict_8x8_filter_t predict_8x8_filter;
2313     } ip_c, ip_ref, ip_a;
2314
2315     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2316     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2317     x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2318     x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2319     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2320
2321     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2322     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2323     x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2324     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2325     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2326
2327     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2328     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2329     x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2330     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2331     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2332
2333     memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2334
2335     ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2336
2337 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2338     if( ip_a.name[dir] != ip_ref.name[dir] )\
2339     {\
2340         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2341         used_asm = 1;\
2342         memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2343         memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2344         for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2345         {\
2346             call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2347             call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2348             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2349             {\
2350                 fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
2351                 ok = 0;\
2352                 if( ip_c.name == (void *)ip_c.predict_8x8 )\
2353                 {\
2354                     for( int k = -1; k < 16; k++ )\
2355                         printf( "%2x ", edge[16+k] );\
2356                     printf( "\n" );\
2357                 }\
2358                 for( int j = 0; j < h; j++ )\
2359                 {\
2360                     if( ip_c.name == (void *)ip_c.predict_8x8 )\
2361                         printf( "%2x ", edge[14-j] );\
2362                     for( int k = 0; k < w; k++ )\
2363                         printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2364                     printf( "\n" );\
2365                 }\
2366                 printf( "\n" );\
2367                 for( int j = 0; j < h; j++ )\
2368                 {\
2369                     if( ip_c.name == (void *)ip_c.predict_8x8 )\
2370                         printf( "   " );\
2371                     for( int k = 0; k < w; k++ )\
2372                         printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2373                     printf( "\n" );\
2374                 }\
2375                 break;\
2376             }\
2377         }\
2378     }
2379
2380     for( int i = 0; i < 12; i++ )
2381         INTRA_TEST(   predict_4x4, i,  4,  4,  4, );
2382     for( int i = 0; i < 7; i++ )
2383         INTRA_TEST(  predict_8x8c, i,  8,  8, 16, );
2384     for( int i = 0; i < 7; i++ )
2385         INTRA_TEST( predict_8x16c, i,  8, 16, 16, );
2386     for( int i = 0; i < 7; i++ )
2387         INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2388     for( int i = 0; i < 12; i++ )
2389         INTRA_TEST(   predict_8x8, i,  8,  8,  8, , edge );
2390
2391     set_func_name("intra_predict_8x8_filter");
2392     if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2393     {
2394         used_asm = 1;
2395         for( int i = 0; i < 32; i++ )
2396         {
2397             if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2398                 continue;
2399             int neighbor = (i&24)>>1;
2400             memset( edge,  0, 36*sizeof(pixel) );
2401             memset( edge2, 0, 36*sizeof(pixel) );
2402             call_c( ip_c.predict_8x8_filter, pbuf1+48, edge,  neighbor, i&7 );
2403             call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2404             if( !(neighbor&MB_TOPLEFT) )
2405                 edge[15] = edge2[15] = 0;
2406             if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2407             {
2408                 fprintf( stderr, "predict_8x8_filter :  [FAILED] %d %d\n", (i&24)>>1, i&7);
2409                 ok = 0;
2410             }
2411         }
2412     }
2413
2414 #define EXTREMAL_PLANE( w, h ) \
2415     { \
2416         int max[7]; \
2417         for( int j = 0; j < 7; j++ ) \
2418             max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2419         fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2420         for( int j = 0; j < w/2; j++ ) \
2421             fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2422         for( int j = w/2; j < w-1; j++ ) \
2423             fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2424         fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2425         for( int j = 0; j < h/2; j++ ) \
2426             fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2427         for( int j = h/2; j < h-1; j++ ) \
2428             fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2429         fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2430     }
2431     /* Extremal test case for planar prediction. */
2432     for( int test = 0; test < 100 && ok; test++ )
2433         for( int i = 0; i < 128 && ok; i++ )
2434         {
2435             EXTREMAL_PLANE(  8,  8 );
2436             INTRA_TEST(  predict_8x8c, I_PRED_CHROMA_P,  8,  8, 64, 1 );
2437             EXTREMAL_PLANE(  8, 16 );
2438             INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P,  8, 16, 64, 1 );
2439             EXTREMAL_PLANE( 16, 16 );
2440             INTRA_TEST( predict_16x16,  I_PRED_16x16_P, 16, 16, 64, 1 );
2441         }
2442     report( "intra pred :" );
2443     return ret;
2444 }
2445
2446 #define DECL_CABAC(cpu) \
2447 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2448 {\
2449     x264_cabac_t cb;\
2450     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2451     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2452     for( int i = 0; i < 0x1000; i++ )\
2453         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2454 }\
2455 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2456 {\
2457     x264_cabac_t cb;\
2458     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2459     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2460     for( int i = 0; i < 0x1000; i++ )\
2461         x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2462 }\
2463 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2464 {\
2465     x264_cabac_t cb;\
2466     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2467     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2468     for( int i = 0; i < 0x1000; i++ )\
2469         x264_cabac_encode_terminal_##cpu( &cb );\
2470 }
2471 DECL_CABAC(c)
2472 #if HAVE_MMX
2473 DECL_CABAC(asm)
2474 #elif defined(ARCH_AARCH64)
2475 DECL_CABAC(asm)
2476 #else
2477 #define run_cabac_decision_asm run_cabac_decision_c
2478 #define run_cabac_bypass_asm run_cabac_bypass_c
2479 #define run_cabac_terminal_asm run_cabac_terminal_c
2480 #endif
2481
2482 extern const uint8_t x264_count_cat_m1[14];
2483 void x264_cabac_block_residual_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2484 void x264_cabac_block_residual_8x8_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2485 void x264_cabac_block_residual_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2486
2487 static int check_cabac( int cpu_ref, int cpu_new )
2488 {
2489     int ret = 0, ok = 1, used_asm = 0;
2490     x264_t h;
2491     h.sps->i_chroma_format_idc = 3;
2492
2493     x264_bitstream_function_t bs_ref;
2494     x264_bitstream_function_t bs_a;
2495     x264_bitstream_init( cpu_ref, &bs_ref );
2496     x264_bitstream_init( cpu_new, &bs_a );
2497     x264_quant_init( &h, cpu_new, &h.quantf );
2498     h.quantf.coeff_last[DCT_CHROMA_DC] = h.quantf.coeff_last4;
2499
2500 #define CABAC_RESIDUAL(name, start, end, rd)\
2501 {\
2502     if( bs_a.name##_internal && (bs_a.name##_internal != bs_ref.name##_internal || (cpu_new&X264_CPU_SSE2_IS_SLOW)) )\
2503     {\
2504         used_asm = 1;\
2505         set_func_name( #name );\
2506         for( int i = 0; i < 2; i++ )\
2507         {\
2508             for( intptr_t ctx_block_cat = start; ctx_block_cat <= end; ctx_block_cat++ )\
2509             {\
2510                 for( int j = 0; j < 256; j++ )\
2511                 {\
2512                     ALIGNED_ARRAY_N( dctcoef, dct, [2],[64] );\
2513                     uint8_t bitstream[2][1<<16];\
2514                     static const uint8_t ctx_ac[14] = {0,1,0,0,1,0,0,1,0,0,0,1,0,0};\
2515                     int ac = ctx_ac[ctx_block_cat];\
2516                     int nz = 0;\
2517                     while( !nz )\
2518                     {\
2519                         for( int k = 0; k <= x264_count_cat_m1[ctx_block_cat]; k++ )\
2520                         {\
2521                             /* Very rough distribution that covers possible inputs */\
2522                             int rnd = rand();\
2523                             int coef = !(rnd&3);\
2524                             coef += !(rnd&  15) * (rand()&0x0006);\
2525                             coef += !(rnd&  63) * (rand()&0x0008);\
2526                             coef += !(rnd& 255) * (rand()&0x00F0);\
2527                             coef += !(rnd&1023) * (rand()&0x7F00);\
2528                             nz |= dct[0][ac+k] = dct[1][ac+k] = coef * ((rand()&1) ? 1 : -1);\
2529                         }\
2530                     }\
2531                     h.mb.b_interlaced = i;\
2532                     x264_cabac_t cb[2];\
2533                     x264_cabac_context_init( &h, &cb[0], SLICE_TYPE_P, 26, 0 );\
2534                     x264_cabac_context_init( &h, &cb[1], SLICE_TYPE_P, 26, 0 );\
2535                     x264_cabac_encode_init( &cb[0], bitstream[0], bitstream[0]+0xfff0 );\
2536                     x264_cabac_encode_init( &cb[1], bitstream[1], bitstream[1]+0xfff0 );\
2537                     cb[0].f8_bits_encoded = 0;\
2538                     cb[1].f8_bits_encoded = 0;\
2539                     if( !rd ) memcpy( bitstream[1], bitstream[0], 0x400 );\
2540                     call_c1( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2541                     call_a1( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2542                     ok = cb[0].f8_bits_encoded == cb[1].f8_bits_encoded && !memcmp(cb[0].state, cb[1].state, 1024);\
2543                     if( !rd ) ok |= !memcmp( bitstream[1], bitstream[0], 0x400 ) && !memcmp( &cb[1], &cb[0], offsetof(x264_cabac_t, p_start) );\
2544                     if( !ok )\
2545                     {\
2546                         fprintf( stderr, #name " :  [FAILED] ctx_block_cat %d", (int)ctx_block_cat );\
2547                         if( rd && cb[0].f8_bits_encoded != cb[1].f8_bits_encoded )\
2548                             fprintf( stderr, " (%d != %d)", cb[0].f8_bits_encoded, cb[1].f8_bits_encoded );\
2549                         fprintf( stderr, "\n");\
2550                         goto name##fail;\
2551                     }\
2552                     if( (j&15) == 0 )\
2553                     {\
2554                         call_c2( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2555                         call_a2( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2556                     }\
2557                 }\
2558             }\
2559         }\
2560     }\
2561 }\
2562 name##fail:
2563
2564     CABAC_RESIDUAL( cabac_block_residual, 0, DCT_LUMA_8x8, 0 )
2565     report( "cabac residual:" );
2566
2567     ok = 1; used_asm = 0;
2568     CABAC_RESIDUAL( cabac_block_residual_rd, 0, DCT_LUMA_8x8-1, 1 )
2569     CABAC_RESIDUAL( cabac_block_residual_8x8_rd, DCT_LUMA_8x8, DCT_LUMA_8x8, 1 )
2570     report( "cabac residual rd:" );
2571
2572     if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2573         return ret;
2574     ok = 1; used_asm = 0;
2575     x264_cabac_init( &h );
2576
2577     set_func_name( "cabac_encode_decision" );
2578     memcpy( buf4, buf3, 0x1000 );
2579     call_c( run_cabac_decision_c, &h, buf3 );
2580     call_a( run_cabac_decision_asm, &h, buf4 );
2581     ok = !memcmp( buf3, buf4, 0x1000 );
2582     report( "cabac decision:" );
2583
2584     set_func_name( "cabac_encode_bypass" );
2585     memcpy( buf4, buf3, 0x1000 );
2586     call_c( run_cabac_bypass_c, &h, buf3 );
2587     call_a( run_cabac_bypass_asm, &h, buf4 );
2588     ok = !memcmp( buf3, buf4, 0x1000 );
2589     report( "cabac bypass:" );
2590
2591     set_func_name( "cabac_encode_terminal" );
2592     memcpy( buf4, buf3, 0x1000 );
2593     call_c( run_cabac_terminal_c, &h, buf3 );
2594     call_a( run_cabac_terminal_asm, &h, buf4 );
2595     ok = !memcmp( buf3, buf4, 0x1000 );
2596     report( "cabac terminal:" );
2597
2598     return ret;
2599 }
2600
2601 static int check_bitstream( int cpu_ref, int cpu_new )
2602 {
2603     x264_bitstream_function_t bs_c;
2604     x264_bitstream_function_t bs_ref;
2605     x264_bitstream_function_t bs_a;
2606
2607     int ret = 0, ok = 1, used_asm = 0;
2608
2609     x264_bitstream_init( 0, &bs_c );
2610     x264_bitstream_init( cpu_ref, &bs_ref );
2611     x264_bitstream_init( cpu_new, &bs_a );
2612     if( bs_a.nal_escape != bs_ref.nal_escape )
2613     {
2614         int size = 0x4000;
2615         uint8_t *input = malloc(size+100);
2616         uint8_t *output1 = malloc(size*2);
2617         uint8_t *output2 = malloc(size*2);
2618         used_asm = 1;
2619         set_func_name( "nal_escape" );
2620         for( int i = 0; i < 100; i++ )
2621         {
2622             /* Test corner-case sizes */
2623             int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2624             /* Test 8 different probability distributions of zeros */
2625             for( int j = 0; j < test_size+32; j++ )
2626                 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2627             uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2628             uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2629             int size_c = end_c-output1;
2630             int size_a = end_a-output2;
2631             if( size_c != size_a || memcmp( output1, output2, size_c ) )
2632             {
2633                 fprintf( stderr, "nal_escape :  [FAILED] %d %d\n", size_c, size_a );
2634                 ok = 0;
2635                 break;
2636             }
2637         }
2638         for( int j = 0; j < size+32; j++ )
2639             input[j] = rand();
2640         call_c2( bs_c.nal_escape, output1, input, input+size );
2641         call_a2( bs_a.nal_escape, output2, input, input+size );
2642         free(input);
2643         free(output1);
2644         free(output2);
2645     }
2646     report( "nal escape:" );
2647
2648     return ret;
2649 }
2650
2651 static int check_all_funcs( int cpu_ref, int cpu_new )
2652 {
2653     return check_pixel( cpu_ref, cpu_new )
2654          + check_dct( cpu_ref, cpu_new )
2655          + check_mc( cpu_ref, cpu_new )
2656          + check_intra( cpu_ref, cpu_new )
2657          + check_deblock( cpu_ref, cpu_new )
2658          + check_quant( cpu_ref, cpu_new )
2659          + check_cabac( cpu_ref, cpu_new )
2660          + check_bitstream( cpu_ref, cpu_new );
2661 }
2662
2663 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2664 {
2665     *cpu_ref = *cpu_new;
2666     *cpu_new |= flags;
2667 #if STACK_ALIGNMENT < 16
2668     *cpu_new |= X264_CPU_STACK_MOD4;
2669 #endif
2670     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2671         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2672     if( !quiet )
2673         fprintf( stderr, "x264: %s\n", name );
2674     return check_all_funcs( *cpu_ref, *cpu_new );
2675 }
2676
2677 static int check_all_flags( void )
2678 {
2679     int ret = 0;
2680     int cpu0 = 0, cpu1 = 0;
2681     uint32_t cpu_detect = x264_cpu_detect();
2682 #if HAVE_MMX
2683     if( cpu_detect & X264_CPU_MMX2 )
2684     {
2685         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2686         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2687         cpu1 &= ~X264_CPU_CACHELINE_64;
2688 #if ARCH_X86
2689         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2690         cpu1 &= ~X264_CPU_CACHELINE_32;
2691 #endif
2692         if( cpu_detect & X264_CPU_LZCNT )
2693         {
2694             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX LZCNT" );
2695             cpu1 &= ~X264_CPU_LZCNT;
2696         }
2697         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2698         cpu1 &= ~X264_CPU_SLOW_CTZ;
2699     }
2700     if( cpu_detect & X264_CPU_SSE )
2701         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE, "SSE" );
2702     if( cpu_detect & X264_CPU_SSE2 )
2703     {
2704         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2705         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2706         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2707         cpu1 &= ~X264_CPU_CACHELINE_64;
2708         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSE2 SlowShuffle" );
2709         cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2710         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2711         cpu1 &= ~X264_CPU_SLOW_CTZ;
2712         if( cpu_detect & X264_CPU_LZCNT )
2713         {
2714             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE2 LZCNT" );
2715             cpu1 &= ~X264_CPU_LZCNT;
2716         }
2717     }
2718     if( cpu_detect & X264_CPU_SSE3 )
2719     {
2720         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2721         cpu1 &= ~X264_CPU_CACHELINE_64;
2722     }
2723     if( cpu_detect & X264_CPU_SSSE3 )
2724     {
2725         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2726         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2727         cpu1 &= ~X264_CPU_CACHELINE_64;
2728         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSSE3 SlowShuffle" );
2729         cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2730         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2731         cpu1 &= ~X264_CPU_SLOW_CTZ;
2732         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2733         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64 SlowAtom" );
2734         cpu1 &= ~X264_CPU_CACHELINE_64;
2735         cpu1 &= ~X264_CPU_SLOW_ATOM;
2736         if( cpu_detect & X264_CPU_LZCNT )
2737         {
2738             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSSE3 LZCNT" );
2739             cpu1 &= ~X264_CPU_LZCNT;
2740         }
2741     }
2742     if( cpu_detect & X264_CPU_SSE4 )
2743         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
2744     if( cpu_detect & X264_CPU_SSE42 )
2745         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE42, "SSE4.2" );
2746     if( cpu_detect & X264_CPU_AVX )
2747         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2748     if( cpu_detect & X264_CPU_XOP )
2749         ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2750     if( cpu_detect & X264_CPU_FMA4 )
2751     {
2752         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2753         cpu1 &= ~X264_CPU_FMA4;
2754     }
2755     if( cpu_detect & X264_CPU_FMA3 )
2756     {
2757         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3, "FMA3" );
2758         cpu1 &= ~X264_CPU_FMA3;
2759     }
2760     if( cpu_detect & X264_CPU_AVX2 )
2761     {
2762         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3 | X264_CPU_AVX2, "AVX2" );
2763         if( cpu_detect & X264_CPU_LZCNT )
2764         {
2765             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "AVX2 LZCNT" );
2766             cpu1 &= ~X264_CPU_LZCNT;
2767         }
2768     }
2769     if( cpu_detect & X264_CPU_BMI1 )
2770     {
2771         ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1, "BMI1" );
2772         cpu1 &= ~X264_CPU_BMI1;
2773     }
2774     if( cpu_detect & X264_CPU_BMI2 )
2775     {
2776         ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1|X264_CPU_BMI2, "BMI2" );
2777         cpu1 &= ~(X264_CPU_BMI1|X264_CPU_BMI2);
2778     }
2779 #elif ARCH_PPC
2780     if( cpu_detect & X264_CPU_ALTIVEC )
2781     {
2782         fprintf( stderr, "x264: ALTIVEC against C\n" );
2783         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2784     }
2785 #elif ARCH_ARM
2786     if( cpu_detect & X264_CPU_ARMV6 )
2787         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2788     if( cpu_detect & X264_CPU_NEON )
2789         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2790     if( cpu_detect & X264_CPU_FAST_NEON_MRC )
2791         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2792 #elif ARCH_AARCH64
2793     if( cpu_detect & X264_CPU_ARMV8 )
2794         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV8, "ARMv8" );
2795     if( cpu_detect & X264_CPU_NEON )
2796         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2797 #elif ARCH_MIPS
2798     if( cpu_detect & X264_CPU_MSA )
2799         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MSA, "MSA" );
2800 #endif
2801     return ret;
2802 }
2803
2804 int main(int argc, char *argv[])
2805 {
2806     int ret = 0;
2807
2808     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2809     {
2810 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM && !ARCH_AARCH64 && !ARCH_MIPS
2811         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2812         return 1;
2813 #endif
2814         do_bench = 1;
2815         if( argv[1][7] == '=' )
2816         {
2817             bench_pattern = argv[1]+8;
2818             bench_pattern_len = strlen(bench_pattern);
2819         }
2820         argc--;
2821         argv++;
2822     }
2823
2824     int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2825     fprintf( stderr, "x264: using random seed %u\n", seed );
2826     srand( seed );
2827
2828     buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 32*BENCH_ALIGNS );
2829     pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 32*BENCH_ALIGNS );
2830     if( !buf1 || !pbuf1 )
2831     {
2832         fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2833         return -1;
2834     }
2835 #define INIT_POINTER_OFFSETS\
2836     buf2 = buf1 + 0xf00;\
2837     buf3 = buf2 + 0xf00;\
2838     buf4 = buf3 + 0x1000*sizeof(pixel);\
2839     pbuf2 = pbuf1 + 0xf00;\
2840     pbuf3 = (pixel*)buf3;\
2841     pbuf4 = (pixel*)buf4;
2842     INIT_POINTER_OFFSETS;
2843     for( int i = 0; i < 0x1e00; i++ )
2844     {
2845         buf1[i] = rand() & 0xFF;
2846         pbuf1[i] = rand() & PIXEL_MAX;
2847     }
2848     memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2849
2850     /* 32-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2851     if( do_bench )
2852         for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2853         {
2854             INIT_POINTER_OFFSETS;
2855             ret |= x264_stack_pagealign( check_all_flags, i*32 );
2856             buf1 += 32;
2857             pbuf1 += 32;
2858             quiet = 1;
2859             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2860         }
2861     else
2862         ret = x264_stack_pagealign( check_all_flags, 0 );
2863
2864     if( ret )
2865     {
2866         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2867         return -1;
2868     }
2869     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
2870     if( do_bench )
2871         print_bench();
2872     return 0;
2873 }
2874