]> git.sesse.net Git - x264/blob - testing/checkasm.c
Attempt to fix build on Linux PPC
[x264] / testing / checkasm.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #
6 #include "core/common.h"
7 #ifdef HAVE_MMXEXT
8 #include "core/i386/pixel.h"
9 #include "core/i386/dct.h"
10 #include "core/i386/mc.h"
11 #endif
12 #ifdef ARCH_PPC
13 #include "core/ppc/pixel.h"
14 #endif
15
16 /* buf1, buf2: initialised to randome data and shouldn't write into them */
17 uint8_t * buf1, * buf2;
18 /* buf3, buf4: used to store output */
19 uint8_t * buf3, * buf4;
20
21 static int check_pixel()
22 {
23     x264_pixel_function_t pixel_c = {{0},{0},{0}};
24     x264_pixel_function_t pixel_asm = {{0}, {0},{0}};
25     int ret = 0, ok;
26     int i;
27
28     memset( &pixel_asm, 0, sizeof( x264_pixel_function_t ) );
29     x264_pixel_init( 0, &pixel_c );
30 #ifdef HAVE_MMXEXT
31     x264_pixel_init( X264_CPU_MMX|X264_CPU_MMXEXT, &pixel_asm );
32 #endif
33 #ifdef ARCH_PPC
34     x264_pixel_altivec_init( &pixel_asm );
35 #endif
36
37     for( i = 0, ok = 1; i < 7; i++ )
38     {
39         int res_c, res_asm;
40         if( pixel_asm.sad[i] )
41         {
42             res_c   = pixel_c.sad[i]( buf1, 32, buf2, 32 );
43             res_asm =  pixel_asm.sad[i]( buf1, 32, buf2, 32 );
44             if( res_c != res_asm )
45             {
46                 ok = 0;
47                 fprintf( stderr, "sad[%d]: %d != %d [FAILED]\n", i, res_c, res_asm );
48             }
49         }
50     }
51     if( ok )
52         fprintf( stderr, " - pixel sad :           [OK]\n" );
53     else {
54         ret = -1;
55         fprintf( stderr, " - pixel sat :           [FAILED]\n" );
56     }
57
58     for( i = 0, ok = 1; i < 7; i++ )
59     {
60         int res_c, res_asm;
61         if( pixel_asm.satd[i] )
62         {
63             res_c   = pixel_c.satd[i]( buf1, 32, buf2, 32 );
64             res_asm = pixel_asm.satd[i]( buf1, 32, buf2, 32 );
65             if( res_c != res_asm )
66             {
67                 ok = 0;
68                 fprintf( stderr, "satd[%d]: %d != %d [FAILED]\n", i, res_c, res_asm );
69             }
70         }
71     }
72
73     if( ok )
74         fprintf( stderr, " - pixel satd :          [OK]\n" );
75     else {
76         ret = -1;
77         fprintf( stderr, " - pixel satd :          [FAILED]\n" );
78     }
79
80     for( i = 0, ok = 1; i < 7; i++ )
81     {
82         if( pixel_asm.avg[i] )
83         {
84             memcpy( buf3, buf1, 32*32 );
85             memcpy( buf4, buf1, 32*32 );
86             pixel_c.satd[i]( buf3, 32, buf2, 32 );
87             pixel_asm.satd[i]( buf4, 32, buf2, 32 );
88             if( memcmp( buf3, buf4, 32*32 ) )
89             {
90                 ok = 0;
91                 fprintf( stderr, "avg[%d]: [FAILED]\n", i );
92             }
93         }
94     }
95
96     if( ok )
97         fprintf( stderr, " - pixel avg :           [OK]\n" );
98     else {
99         ret = -1;
100         fprintf( stderr, " - pixel avg :           [FAILED]\n" );
101     }
102
103     return ret;
104 }
105
106 static int check_dct()
107 {
108     x264_dct_function_t dct_c;
109     x264_dct_function_t dct_asm;
110     int ret = 0, ok;
111     int16_t dct1[16][4][4] __attribute((aligned(16)));
112     int16_t dct2[16][4][4] __attribute((aligned(16)));
113
114     memset( &dct_asm, 0, sizeof( dct_asm ) );
115     x264_dct_init( 0, &dct_c );
116 #ifdef HAVE_MMXEXT
117     x264_dct_init( X264_CPU_MMX|X264_CPU_MMXEXT, &dct_asm );
118 #endif
119 #define TEST_DCT( name, t1, t2, size ) \
120     if( dct_asm.name ) \
121     { \
122         dct_c.name( t1, buf1, 32, buf2, 32 ); \
123         dct_asm.name( t2, buf1, 32, buf2, 32 ); \
124         if( memcmp( t1, t2, size ) ) \
125         { \
126             ok = 0; \
127             fprintf( stderr, #name " [FAILED]\n" ); \
128         } \
129     }
130     ok = 1;
131     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
132     TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
133     TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
134     if( ok )
135         fprintf( stderr, " - sub_dctXxX :          [OK]\n" );
136     else {
137         ret = -1;
138         fprintf( stderr, " - sub_dctXxX :          [FAILED]\n" );
139     }
140 #undef TEST_DCT
141
142 #define TEST_IDCT( name, t ) \
143     if( dct_asm.name ) \
144     { \
145         memcpy( buf3, buf1, 32*32 ); \
146         memcpy( buf4, buf1, 32*32 ); \
147         dct_c.name( buf3, 32, t ); \
148         dct_asm.name( buf4, 32, t ); \
149         if( memcmp( buf3, buf4, 32*32 ) ) \
150         { \
151             ok = 0; \
152             fprintf( stderr, #name " [FAILED]\n" ); \
153         } \
154     }
155     ok = 1;
156     TEST_IDCT( add4x4_idct, dct1[0] );
157     TEST_IDCT( add8x8_idct, dct1 );
158     TEST_IDCT( add16x16_idct, dct1 );
159     if( ok )
160         fprintf( stderr, " - add_idctXxX :         [OK]\n" );
161     else {
162         ret = -1;
163         fprintf( stderr, " - add_idctXxX :         [FAILED]\n" );
164     }
165 #undef TEST_IDCT
166
167     ok = 1;
168     if( dct_asm.dct4x4dc )
169     {
170         int16_t dct1[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
171         int16_t dct2[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
172
173         dct_c.dct4x4dc( dct1 );
174         dct_asm.dct4x4dc( dct2 );
175         if( memcmp( dct1, dct2, 32 ) )
176         {
177             ok = 0;
178             fprintf( stderr, " - dct4x4dc :        [FAILED]\n" );
179         }
180     }
181     if( dct_asm.idct4x4dc )
182     {
183         int16_t dct1[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
184         int16_t dct2[4][4] __attribute((aligned(16))) = { {-12, 42, 23, 67},{2, 90, 89,56}, {67,43,-76,91},{56,-78,-54,1}};
185
186         dct_c.idct4x4dc( dct1 );
187         dct_asm.idct4x4dc( dct2 );
188         if( memcmp( dct1, dct2, 32 ) )
189         {
190             ok = 0;
191             fprintf( stderr, " - idct4x4dc :        [FAILED]\n" );
192         }
193     }
194     if( ok )
195         fprintf( stderr, " - (i)dct4x4dc :         [OK]\n" );
196     else {
197         ret = -1;
198         fprintf( stderr, " - (i)dct4x4dc :         [FAILED]\n" );
199     }
200
201     ok = 1;
202     if( dct_asm.dct2x2dc )
203     {
204         int16_t dct1[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
205         int16_t dct2[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
206
207         dct_c.dct2x2dc( dct1 );
208         dct_asm.dct2x2dc( dct2 );
209         if( memcmp( dct1, dct2, 4*2 ) )
210         {
211             ok = 0;
212             fprintf( stderr, " - dct2x2dc :        [FAILED]\n" );
213         }
214     }
215     if( dct_asm.idct2x2dc )
216     {
217         int16_t dct1[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
218         int16_t dct2[2][2] __attribute((aligned(16))) = { {-12, 42},{2, 90}};
219
220         dct_c.idct2x2dc( dct1 );
221         dct_asm.idct2x2dc( dct2 );
222         if( memcmp( dct1, dct2, 4*2 ) )
223         {
224             ok = 0;
225             fprintf( stderr, " - idct2x2dc :       [FAILED]\n" );
226         }
227     }
228
229     if( ok )
230         fprintf( stderr, " - (i)dct2x2dc :         [OK]\n" );
231     else {
232         ret = -1;
233         fprintf( stderr, " - (i)dct2x2dc :         [FAILED]\n" );
234     }
235
236
237     return ret;
238 }
239
240 static int check_mc()
241 {
242     x264_mc_function_t mc_c[2] = {0};
243     x264_mc_function_t mc_asm[2] = {0};
244     uint8_t *src = &buf1[2*32+2];
245     uint8_t *dst1 = &buf3[2*32+2];
246     uint8_t *dst2 = &buf4[2*32+2];
247     int dx, dy;
248     int ret = 0, ok[2] = { 1, 1 };
249
250     x264_mc_init( 0, mc_c );
251 #ifdef HAVE_MMXEXT
252     x264_mc_mmxext_init( mc_asm );
253 #endif
254
255     /* Do the MC */
256 #define MC_TEST( t, w, h ) \
257         if( mc_asm[t] ) \
258         { \
259             memset(dst1, 0xCD, (h) * 16); \
260             mc_c[t]( src, 32, dst1, 16, dx, dy, w, h );     \
261             memset(dst2, 0xCD, (h) * 16); \
262             mc_asm[t]( src, 32, dst2, 16, dx, dy, w, h );   \
263             if( memcmp( dst1, dst2, 16*16 ) )               \
264             { \
265                 fprintf( stderr, "mc["#t"][mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h );   \
266                 ok[t] = 0; \
267             } \
268         }
269
270     for( dy = 0; dy < 4; dy++ )
271     {
272         for( dx = 0; dx < 4; dx++ )
273         {
274             MC_TEST( 0, 16, 16 );
275             MC_TEST( 0, 16, 8 );
276             MC_TEST( 0, 8, 16 );
277             MC_TEST( 0, 8, 8 );
278             MC_TEST( 0, 8, 4 );
279             MC_TEST( 0, 4, 8 );
280             MC_TEST( 0, 4, 4 );
281
282             MC_TEST( 1, 8, 8 );
283             MC_TEST( 1, 8, 4 );
284             MC_TEST( 1, 4, 8 );
285             MC_TEST( 1, 4, 4 );
286             MC_TEST( 1, 4, 2 );
287             MC_TEST( 1, 2, 4 );
288             MC_TEST( 1, 2, 2 );
289         }
290     }
291 #undef MC_TEST
292     if( ok[0] )
293         fprintf( stderr, " - mc luma :             [OK]\n" );
294     else {
295         ret = -1;
296         fprintf( stderr, " - mc luma :             [FAILED]\n" );
297     }
298     if( ok[1] )
299         fprintf( stderr, " - mc chroma :           [OK]\n" );
300     else {
301         ret = -1;
302         fprintf( stderr, " - mc chroma :           [FAILED]\n" );
303     }
304     return ret;
305 }
306
307 int main()
308 {
309     int ret;
310     int i;
311
312 #ifdef HAVE_MMXEXT
313     fprintf( stderr, "x264: MMXEXT against C\n" );
314 #elif ARCH_PPC
315     fprintf( stderr, "x264: ALTIVEC against C\n" );
316 #endif
317
318     buf1 = x264_malloc( 1024 ); /* 32 x 32 */
319     buf2 = x264_malloc( 1024 );
320     buf3 = x264_malloc( 1024 );
321     buf4 = x264_malloc( 1024 );
322
323     srand( x264_mdate() );
324
325     for( i = 0; i < 1024; i++ )
326     {
327         buf1[i] = rand() % 0xFF;
328         buf2[i] = rand() % 0xFF;
329         buf3[i] = buf4[i] = 0;
330     }
331
332     ret = check_pixel() +
333           check_dct() +
334           check_mc();
335
336     if( ret == 0 )
337     {
338         fprintf( stderr, "x264: All tests passed Yeah :)\n" );
339         return 0;
340     }
341     fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
342     return -1;
343 }
344