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