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