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