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