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