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