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