2 * Copyright (c) 2002 Brian Foley
3 * Copyright (c) 2002 Dieter Shirley
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "../dsputil.h"
22 #include "dsputil_ppc.h"
25 #include "dsputil_altivec.h"
28 extern void idct_put_altivec(uint8_t *dest
, int line_size
, int16_t *block
);
29 extern void idct_add_altivec(uint8_t *dest
, int line_size
, int16_t *block
);
44 #ifdef POWERPC_TBL_PERFORMANCE_REPORT
45 unsigned long long perfdata
[powerpc_perf_total
][powerpc_data_total
];
46 /* list below must match enum in dsputil_ppc.h */
47 static unsigned char* perfname
[] = {
50 "dct_unquantize_h263_altivec",
53 "put_pixels16_altivec",
54 "avg_pixels16_altivec",
55 "avg_pixels8_altivec",
56 "put_pixels8_xy2_altivec",
57 "put_no_rnd_pixels8_xy2_altivec",
58 "put_pixels16_xy2_altivec",
59 "put_no_rnd_pixels16_xy2_altivec",
60 "clear_blocks_dcbz32_ppc",
61 "clear_blocks_dcbz128_ppc"
63 #ifdef POWERPC_PERF_USE_PMC
64 unsigned long long perfdata_miss
[powerpc_perf_total
][powerpc_data_total
];
69 #ifdef POWERPC_TBL_PERFORMANCE_REPORT
70 void powerpc_display_perf_report(void)
73 #ifndef POWERPC_PERF_USE_PMC
74 fprintf(stderr
, "PowerPC performance report\n Values are from the Time Base register, and represent 4 bus cycles.\n");
75 #else /* POWERPC_PERF_USE_PMC */
76 fprintf(stderr
, "PowerPC performance report\n Values are from the PMC registers, and represent whatever the registers are set to record.\n");
77 #endif /* POWERPC_PERF_USE_PMC */
78 for(i
= 0 ; i
< powerpc_perf_total
; i
++)
80 if (perfdata
[i
][powerpc_data_num
] != (unsigned long long)0)
81 fprintf(stderr
, " Function \"%s\" (pmc1):\n\tmin: %llu\n\tmax: %llu\n\tavg: %1.2lf (%llu)\n",
83 perfdata
[i
][powerpc_data_min
],
84 perfdata
[i
][powerpc_data_max
],
85 (double)perfdata
[i
][powerpc_data_sum
] /
86 (double)perfdata
[i
][powerpc_data_num
],
87 perfdata
[i
][powerpc_data_num
]);
88 #ifdef POWERPC_PERF_USE_PMC
89 if (perfdata_miss
[i
][powerpc_data_num
] != (unsigned long long)0)
90 fprintf(stderr
, " Function \"%s\" (pmc2):\n\tmin: %llu\n\tmax: %llu\n\tavg: %1.2lf (%llu)\n",
92 perfdata_miss
[i
][powerpc_data_min
],
93 perfdata_miss
[i
][powerpc_data_max
],
94 (double)perfdata_miss
[i
][powerpc_data_sum
] /
95 (double)perfdata_miss
[i
][powerpc_data_num
],
96 perfdata_miss
[i
][powerpc_data_num
]);
100 #endif /* POWERPC_TBL_PERFORMANCE_REPORT */
102 /* ***** WARNING ***** WARNING ***** WARNING ***** */
104 clear_blocks_dcbz32_ppc will not work properly
105 on PowerPC processors with a cache line size
106 not equal to 32 bytes.
107 Fortunately all processor used by Apple up to
108 at least the 7450 (aka second generation G4)
109 use 32 bytes cache line.
110 This is due to the use of the 'dcbz' instruction.
111 It simply clear to zero a single cache line,
112 so you need to know the cache line size to use it !
113 It's absurd, but it's fast...
115 update 24/06/2003 : Apple released yesterday the G5,
116 with a PPC970. cache line size : 128 bytes. Oups.
117 The semantic of dcbz was changed, it always clear
118 32 bytes. so the function below will work, but will
119 be slow. So I fixed check_dcbz_effect to use dcbzl,
120 which is defined to clear a cache line (as dcbz before).
121 So we still can distinguish, and use dcbz (32 bytes)
122 or dcbzl (one cache line) as required.
124 see <http://developer.apple.com/technotes/tn/tn2087.html>
125 and <http://developer.apple.com/technotes/tn/tn2086.html>
127 void clear_blocks_dcbz32_ppc(DCTELEM
*blocks
)
129 POWERPC_TBL_DECLARE(powerpc_clear_blocks_dcbz32
, 1);
130 register int misal
= ((unsigned long)blocks
& 0x00000010);
132 POWERPC_TBL_START_COUNT(powerpc_clear_blocks_dcbz32
, 1);
135 ((unsigned long*)blocks
)[0] = 0L;
136 ((unsigned long*)blocks
)[1] = 0L;
137 ((unsigned long*)blocks
)[2] = 0L;
138 ((unsigned long*)blocks
)[3] = 0L;
141 for ( ; i
< sizeof(DCTELEM
)*6*64 ; i
+= 32) {
142 asm volatile("dcbz %0,%1" : : "r" (i
), "r" (blocks
) : "memory");
145 ((unsigned long*)blocks
)[188] = 0L;
146 ((unsigned long*)blocks
)[189] = 0L;
147 ((unsigned long*)blocks
)[190] = 0L;
148 ((unsigned long*)blocks
)[191] = 0L;
152 memset(blocks
, 0, sizeof(DCTELEM
)*6*64);
154 POWERPC_TBL_STOP_COUNT(powerpc_clear_blocks_dcbz32
, 1);
157 /* same as above, when dcbzl clear a whole 128B cache line
158 i.e. the PPC970 aka G5 */
160 void clear_blocks_dcbz128_ppc(DCTELEM
*blocks
)
162 POWERPC_TBL_DECLARE(powerpc_clear_blocks_dcbz128
, 1);
163 register int misal
= ((unsigned long)blocks
& 0x0000007f);
165 POWERPC_TBL_START_COUNT(powerpc_clear_blocks_dcbz128
, 1);
168 // we could probably also optimize this case,
169 // but there's not much point as the machines
170 // aren't available yet (2003-06-26)
171 memset(blocks
, 0, sizeof(DCTELEM
)*6*64);
174 for ( ; i
< sizeof(DCTELEM
)*6*64 ; i
+= 128) {
175 asm volatile("dcbzl %0,%1" : : "r" (i
), "r" (blocks
) : "memory");
178 memset(blocks
, 0, sizeof(DCTELEM
)*6*64);
180 POWERPC_TBL_STOP_COUNT(powerpc_clear_blocks_dcbz128
, 1);
183 void clear_blocks_dcbz128_ppc(DCTELEM
*blocks
)
185 memset(blocks
, 0, sizeof(DCTELEM
)*6*64);
190 /* check dcbz report how many bytes are set to 0 by dcbz */
191 /* update 24/06/2003 : replace dcbz by dcbzl to get
192 the intended effect (Apple "fixed" dcbz)
193 unfortunately this cannot be used unless the assembler
194 knows about dcbzl ... */
195 long check_dcbzl_effect(void)
197 register char *fakedata
= (char*)av_malloc(1024);
198 register char *fakedata_middle
;
199 register long zero
= 0;
208 fakedata_middle
= (fakedata
+ 512);
210 memset(fakedata
, 0xFF, 1024);
212 asm volatile("dcbzl %0, %1" : : "r" (fakedata_middle
), "r" (zero
));
214 for (i
= 0; i
< 1024 ; i
++)
216 if (fakedata
[i
] == (char)0)
225 long check_dcbzl_effect(void)
231 void dsputil_init_ppc(DSPContext
* c
, AVCodecContext
*avctx
)
233 // Common optimizations whether Altivec is available or not
235 switch (check_dcbzl_effect()) {
237 c
->clear_blocks
= clear_blocks_dcbz32_ppc
;
240 c
->clear_blocks
= clear_blocks_dcbz128_ppc
;
248 mm_flags
|= MM_ALTIVEC
;
250 // Altivec specific optimisations
251 c
->pix_abs16x16_x2
= pix_abs16x16_x2_altivec
;
252 c
->pix_abs16x16_y2
= pix_abs16x16_y2_altivec
;
253 c
->pix_abs16x16_xy2
= pix_abs16x16_xy2_altivec
;
254 c
->pix_abs16x16
= pix_abs16x16_altivec
;
255 c
->pix_abs8x8
= pix_abs8x8_altivec
;
256 c
->sad
[0]= sad16x16_altivec
;
257 c
->sad
[1]= sad8x8_altivec
;
258 c
->pix_norm1
= pix_norm1_altivec
;
259 c
->sse
[1]= sse8_altivec
;
260 c
->sse
[0]= sse16_altivec
;
261 c
->pix_sum
= pix_sum_altivec
;
262 c
->diff_pixels
= diff_pixels_altivec
;
263 c
->get_pixels
= get_pixels_altivec
;
264 // next one disabled as it's untested.
266 c
->add_bytes
= add_bytes_altivec
;
268 c
->put_pixels_tab
[0][0] = put_pixels16_altivec
;
269 c
->avg_pixels_tab
[0][0] = avg_pixels16_altivec
;
270 // next one disabled as it's untested.
272 c
->avg_pixels_tab
[1][0] = avg_pixels8_altivec
;
274 c
->put_pixels_tab
[1][3] = put_pixels8_xy2_altivec
;
275 c
->put_no_rnd_pixels_tab
[1][3] = put_no_rnd_pixels8_xy2_altivec
;
276 c
->put_pixels_tab
[0][3] = put_pixels16_xy2_altivec
;
277 c
->put_no_rnd_pixels_tab
[0][3] = put_no_rnd_pixels16_xy2_altivec
;
279 c
->gmc1
= gmc1_altivec
;
281 if ((avctx
->idct_algo
== FF_IDCT_AUTO
) ||
282 (avctx
->idct_algo
== FF_IDCT_ALTIVEC
))
284 c
->idct_put
= idct_put_altivec
;
285 c
->idct_add
= idct_add_altivec
;
286 #ifndef ALTIVEC_USE_REFERENCE_C_CODE
287 c
->idct_permutation_type
= FF_TRANSPOSE_IDCT_PERM
;
288 #else /* ALTIVEC_USE_REFERENCE_C_CODE */
289 c
->idct_permutation_type
= FF_NO_IDCT_PERM
;
290 #endif /* ALTIVEC_USE_REFERENCE_C_CODE */
293 #ifdef POWERPC_TBL_PERFORMANCE_REPORT
296 for (i
= 0 ; i
< powerpc_perf_total
; i
++)
298 perfdata
[i
][powerpc_data_min
] = 0xFFFFFFFFFFFFFFFF;
299 perfdata
[i
][powerpc_data_max
] = 0x0000000000000000;
300 perfdata
[i
][powerpc_data_sum
] = 0x0000000000000000;
301 perfdata
[i
][powerpc_data_num
] = 0x0000000000000000;
302 #ifdef POWERPC_PERF_USE_PMC
303 perfdata_miss
[i
][powerpc_data_min
] = 0xFFFFFFFFFFFFFFFF;
304 perfdata_miss
[i
][powerpc_data_max
] = 0x0000000000000000;
305 perfdata_miss
[i
][powerpc_data_sum
] = 0x0000000000000000;
306 perfdata_miss
[i
][powerpc_data_num
] = 0x0000000000000000;
307 #endif /* POWERPC_PERF_USE_PMC */
310 #endif /* POWERPC_TBL_PERFORMANCE_REPORT */
312 #endif /* HAVE_ALTIVEC */
314 // Non-AltiVec PPC optimisations