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