Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ppc / dsputil_ppc.c @ 186447f8

History | View | Annotate | Download (9.21 KB)

1 05c4072b Michael Niedermayer
/*
2
 * Copyright (c) 2002 Brian Foley
3
 * Copyright (c) 2002 Dieter Shirley
4
 *
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.
9
 *
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.
14
 *
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
18
 */
19
20 ab6c65f6 Brian Foley
#include "../dsputil.h"
21
22 35e5fb06 Romain Dolbeau
#include "dsputil_ppc.h"
23
24 ab6c65f6 Brian Foley
#ifdef HAVE_ALTIVEC
25
#include "dsputil_altivec.h"
26
#endif
27
28 b0368839 Michael Niedermayer
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);
30
31 404d2241 Brian Foley
int mm_flags = 0;
32
33 e629ab68 Romain Dolbeau
int mm_support(void)
34
{
35
    int result = 0;
36
#if HAVE_ALTIVEC
37
    if (has_altivec()) {
38
        result |= MM_ALTIVEC;
39
    }
40
#endif /* result */
41
    return result;
42
}
43
44 e45a2872 Romain Dolbeau
#ifdef POWERPC_PERFORMANCE_REPORT
45
unsigned long long perfdata[POWERPC_NUM_PMC_ENABLED][powerpc_perf_total][powerpc_data_total];
46 fe50f385 Romain Dolbeau
/* list below must match enum in dsputil_ppc.h */
47 35e5fb06 Romain Dolbeau
static unsigned char* perfname[] = {
48
  "fft_calc_altivec",
49
  "gmc1_altivec",
50
  "dct_unquantize_h263_altivec",
51
  "idct_add_altivec",
52
  "idct_put_altivec",
53
  "put_pixels16_altivec",
54
  "avg_pixels16_altivec",
55
  "avg_pixels8_altivec",
56
  "put_pixels8_xy2_altivec",
57 fe50f385 Romain Dolbeau
  "put_no_rnd_pixels8_xy2_altivec",
58
  "put_pixels16_xy2_altivec",
59
  "put_no_rnd_pixels16_xy2_altivec",
60 a4adb608 Michael Niedermayer
  "clear_blocks_dcbz32_ppc",
61
  "clear_blocks_dcbz128_ppc"
62 35e5fb06 Romain Dolbeau
};
63
#include <stdio.h>
64
#endif
65
66 e45a2872 Romain Dolbeau
#ifdef POWERPC_PERFORMANCE_REPORT
67 35e5fb06 Romain Dolbeau
void powerpc_display_perf_report(void)
68
{
69 e45a2872 Romain Dolbeau
  int i, j;
70 fe50f385 Romain Dolbeau
  fprintf(stderr, "PowerPC performance report\n Values are from the PMC registers, and represent whatever the registers are set to record.\n");
71 35e5fb06 Romain Dolbeau
  for(i = 0 ; i < powerpc_perf_total ; i++)
72
  {
73 e45a2872 Romain Dolbeau
    for (j = 0; j < POWERPC_NUM_PMC_ENABLED ; j++)
74
      {
75
        if (perfdata[j][i][powerpc_data_num] != (unsigned long long)0)
76
          fprintf(stderr,
77
                  " Function \"%s\" (pmc%d):\n\tmin: %llu\n\tmax: %llu\n\tavg: %1.2lf (%llu)\n",
78
                  perfname[i],
79
                  j+1,
80
                  perfdata[j][i][powerpc_data_min],
81
                  perfdata[j][i][powerpc_data_max],
82
                  (double)perfdata[j][i][powerpc_data_sum] /
83
                  (double)perfdata[j][i][powerpc_data_num],
84
                  perfdata[j][i][powerpc_data_num]);
85
      }
86 35e5fb06 Romain Dolbeau
  }
87
}
88 e45a2872 Romain Dolbeau
#endif /* POWERPC_PERFORMANCE_REPORT */
89 35e5fb06 Romain Dolbeau
90
/* ***** WARNING ***** WARNING ***** WARNING ***** */
91
/*
92
  clear_blocks_dcbz32_ppc will not work properly
93
  on PowerPC processors with a cache line size
94
  not equal to 32 bytes.
95
  Fortunately all processor used by Apple up to
96
  at least the 7450 (aka second generation G4)
97
  use 32 bytes cache line.
98
  This is due to the use of the 'dcbz' instruction.
99
  It simply clear to zero a single cache line,
100
  so you need to know the cache line size to use it !
101
  It's absurd, but it's fast...
102 a4adb608 Michael Niedermayer

103
  update 24/06/2003 : Apple released yesterday the G5,
104
  with a PPC970. cache line size : 128 bytes. Oups.
105
  The semantic of dcbz was changed, it always clear
106
  32 bytes. so the function below will work, but will
107
  be slow. So I fixed check_dcbz_effect to use dcbzl,
108
  which is defined to clear a cache line (as dcbz before).
109
  So we still can distinguish, and use dcbz (32 bytes)
110
  or dcbzl (one cache line) as required.
111

112
  see <http://developer.apple.com/technotes/tn/tn2087.html>
113
  and <http://developer.apple.com/technotes/tn/tn2086.html>
114 35e5fb06 Romain Dolbeau
*/
115
void clear_blocks_dcbz32_ppc(DCTELEM *blocks)
116
{
117 e45a2872 Romain Dolbeau
POWERPC_PERF_DECLARE(powerpc_clear_blocks_dcbz32, 1);
118 35e5fb06 Romain Dolbeau
    register int misal = ((unsigned long)blocks & 0x00000010);
119
    register int i = 0;
120 e45a2872 Romain Dolbeau
POWERPC_PERF_START_COUNT(powerpc_clear_blocks_dcbz32, 1);
121 35e5fb06 Romain Dolbeau
#if 1
122
    if (misal) {
123
      ((unsigned long*)blocks)[0] = 0L;
124
      ((unsigned long*)blocks)[1] = 0L;
125
      ((unsigned long*)blocks)[2] = 0L;
126
      ((unsigned long*)blocks)[3] = 0L;
127
      i += 16;
128
    }
129
    for ( ; i < sizeof(DCTELEM)*6*64 ; i += 32) {
130 3efd4952 Romain Dolbeau
      asm volatile("dcbz %0,%1" : : "b" (blocks), "r" (i) : "memory");
131 35e5fb06 Romain Dolbeau
    }
132
    if (misal) {
133
      ((unsigned long*)blocks)[188] = 0L;
134
      ((unsigned long*)blocks)[189] = 0L;
135
      ((unsigned long*)blocks)[190] = 0L;
136
      ((unsigned long*)blocks)[191] = 0L;
137
      i += 16;
138
    }
139
#else
140
    memset(blocks, 0, sizeof(DCTELEM)*6*64);
141
#endif
142 e45a2872 Romain Dolbeau
POWERPC_PERF_STOP_COUNT(powerpc_clear_blocks_dcbz32, 1);
143 35e5fb06 Romain Dolbeau
}
144
145 a4adb608 Michael Niedermayer
/* same as above, when dcbzl clear a whole 128B cache line
146
   i.e. the PPC970 aka G5 */
147
#ifndef NO_DCBZL
148
void clear_blocks_dcbz128_ppc(DCTELEM *blocks)
149
{
150 e45a2872 Romain Dolbeau
POWERPC_PERF_DECLARE(powerpc_clear_blocks_dcbz128, 1);
151 a4adb608 Michael Niedermayer
    register int misal = ((unsigned long)blocks & 0x0000007f);
152
    register int i = 0;
153 e45a2872 Romain Dolbeau
POWERPC_PERF_START_COUNT(powerpc_clear_blocks_dcbz128, 1);
154 a4adb608 Michael Niedermayer
#if 1
155
 if (misal) {
156
   // we could probably also optimize this case,
157
   // but there's not much point as the machines
158
   // aren't available yet (2003-06-26)
159
      memset(blocks, 0, sizeof(DCTELEM)*6*64);
160
    }
161
    else
162
      for ( ; i < sizeof(DCTELEM)*6*64 ; i += 128) {
163 3efd4952 Romain Dolbeau
        asm volatile("dcbzl %0,%1" : : "b" (blocks), "r" (i) : "memory");
164 a4adb608 Michael Niedermayer
      }
165
#else
166
    memset(blocks, 0, sizeof(DCTELEM)*6*64);
167
#endif
168 e45a2872 Romain Dolbeau
POWERPC_PERF_STOP_COUNT(powerpc_clear_blocks_dcbz128, 1);
169 a4adb608 Michael Niedermayer
}
170
#else
171
void clear_blocks_dcbz128_ppc(DCTELEM *blocks)
172
{
173
  memset(blocks, 0, sizeof(DCTELEM)*6*64);
174
}
175
#endif
176
177
#ifndef NO_DCBZL
178 35e5fb06 Romain Dolbeau
/* check dcbz report how many bytes are set to 0 by dcbz */
179 a4adb608 Michael Niedermayer
/* update 24/06/2003 : replace dcbz by dcbzl to get
180
   the intended effect (Apple "fixed" dcbz)
181
   unfortunately this cannot be used unless the assembler
182
   knows about dcbzl ... */
183
long check_dcbzl_effect(void)
184 35e5fb06 Romain Dolbeau
{
185 3b991c54 Romain Dolbeau
  register char *fakedata = (char*)av_malloc(1024);
186 35e5fb06 Romain Dolbeau
  register char *fakedata_middle;
187
  register long zero = 0;
188
  register long i = 0;
189
  long count = 0;
190
191 3b991c54 Romain Dolbeau
  if (!fakedata)
192 35e5fb06 Romain Dolbeau
  {
193
    return 0L;
194
  }
195
196
  fakedata_middle = (fakedata + 512);
197
198
  memset(fakedata, 0xFF, 1024);
199
200 3efd4952 Romain Dolbeau
  /* below the constraint "b" seems to mean "Address base register"
201
     in gcc-3.3 / RS/6000 speaks. seems to avoid using r0, so.... */
202
  asm volatile("dcbzl %0, %1" : : "b" (fakedata_middle), "r" (zero));
203 35e5fb06 Romain Dolbeau
204
  for (i = 0; i < 1024 ; i ++)
205
  {
206
    if (fakedata[i] == (char)0)
207
      count++;
208
  }
209
210 3b991c54 Romain Dolbeau
  av_free(fakedata);
211 35e5fb06 Romain Dolbeau
  
212
  return count;
213
}
214 a4adb608 Michael Niedermayer
#else
215
long check_dcbzl_effect(void)
216
{
217
  return 0;
218
}
219
#endif
220 35e5fb06 Romain Dolbeau
221 b0368839 Michael Niedermayer
void dsputil_init_ppc(DSPContext* c, AVCodecContext *avctx)
222 ab6c65f6 Brian Foley
{
223 a4adb608 Michael Niedermayer
    // Common optimizations whether Altivec is available or not
224 05c4072b Michael Niedermayer
225 a4adb608 Michael Niedermayer
  switch (check_dcbzl_effect()) {
226 35e5fb06 Romain Dolbeau
  case 32:
227
    c->clear_blocks = clear_blocks_dcbz32_ppc;
228
    break;
229 a4adb608 Michael Niedermayer
  case 128:
230
    c->clear_blocks = clear_blocks_dcbz128_ppc;
231
    break;
232 35e5fb06 Romain Dolbeau
  default:
233
    break;
234
  }
235
  
236 ab6c65f6 Brian Foley
#if HAVE_ALTIVEC
237
    if (has_altivec()) {
238 404d2241 Brian Foley
        mm_flags |= MM_ALTIVEC;
239
        
240 05c4072b Michael Niedermayer
        // Altivec specific optimisations
241 f2677d6b Brian Foley
        c->pix_abs16x16_x2 = pix_abs16x16_x2_altivec;
242
        c->pix_abs16x16_y2 = pix_abs16x16_y2_altivec;
243 9c76bd48 Brian Foley
        c->pix_abs16x16_xy2 = pix_abs16x16_xy2_altivec;
244
        c->pix_abs16x16 = pix_abs16x16_altivec;
245 af19f78f Zdenek Kabelac
        c->pix_abs8x8 = pix_abs8x8_altivec;
246 e629ab68 Romain Dolbeau
        c->sad[0]= sad16x16_altivec;
247
        c->sad[1]= sad8x8_altivec;
248 f2677d6b Brian Foley
        c->pix_norm1 = pix_norm1_altivec;
249 4013fcf4 Fabrice Bellard
        c->sse[1]= sse8_altivec;
250
        c->sse[0]= sse16_altivec;
251 af19f78f Zdenek Kabelac
        c->pix_sum = pix_sum_altivec;
252
        c->diff_pixels = diff_pixels_altivec;
253
        c->get_pixels = get_pixels_altivec;
254 fe50f385 Romain Dolbeau
// next one disabled as it's untested.
255 e629ab68 Romain Dolbeau
#if 0
256
        c->add_bytes= add_bytes_altivec;
257 fe50f385 Romain Dolbeau
#endif /* 0 */
258 db40a39a Michael Niedermayer
        c->put_pixels_tab[0][0] = put_pixels16_altivec;
259 e45a2872 Romain Dolbeau
        /* the tow functions do the same thing, so use the same code */
260
        c->put_no_rnd_pixels_tab[0][0] = put_pixels16_altivec;
261 db40a39a Michael Niedermayer
        c->avg_pixels_tab[0][0] = avg_pixels16_altivec;
262 35e5fb06 Romain Dolbeau
// next one disabled as it's untested.
263
#if 0
264
        c->avg_pixels_tab[1][0] = avg_pixels8_altivec;
265 fe50f385 Romain Dolbeau
#endif /* 0 */
266 35e5fb06 Romain Dolbeau
        c->put_pixels_tab[1][3] = put_pixels8_xy2_altivec;
267 fe50f385 Romain Dolbeau
        c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_altivec;
268
        c->put_pixels_tab[0][3] = put_pixels16_xy2_altivec;
269
        c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_altivec;
270 35e5fb06 Romain Dolbeau
        
271 e629ab68 Romain Dolbeau
        c->gmc1 = gmc1_altivec;
272 b0368839 Michael Niedermayer
273
        if ((avctx->idct_algo == FF_IDCT_AUTO) ||
274
                (avctx->idct_algo == FF_IDCT_ALTIVEC))
275
        {
276
            c->idct_put = idct_put_altivec;
277
            c->idct_add = idct_add_altivec;
278
#ifndef ALTIVEC_USE_REFERENCE_C_CODE
279
            c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
280
#else /* ALTIVEC_USE_REFERENCE_C_CODE */
281
            c->idct_permutation_type = FF_NO_IDCT_PERM;
282
#endif /* ALTIVEC_USE_REFERENCE_C_CODE */
283
        }
284 fe50f385 Romain Dolbeau
        
285 e45a2872 Romain Dolbeau
#ifdef POWERPC_PERFORMANCE_REPORT
286 db40a39a Michael Niedermayer
        {
287 e45a2872 Romain Dolbeau
          int i, j;
288 35e5fb06 Romain Dolbeau
          for (i = 0 ; i < powerpc_perf_total ; i++)
289 db40a39a Michael Niedermayer
          {
290 e45a2872 Romain Dolbeau
            for (j = 0; j < POWERPC_NUM_PMC_ENABLED ; j++)
291
              {
292
                perfdata[j][i][powerpc_data_min] = (unsigned long long)0xFFFFFFFFFFFFFFFF;
293
                perfdata[j][i][powerpc_data_max] = (unsigned long long)0x0000000000000000;
294
                perfdata[j][i][powerpc_data_sum] = (unsigned long long)0x0000000000000000;
295
                perfdata[j][i][powerpc_data_num] = (unsigned long long)0x0000000000000000;
296
              }
297
          }
298 db40a39a Michael Niedermayer
        }
299 e45a2872 Romain Dolbeau
#endif /* POWERPC_PERFORMANCE_REPORT */
300 ab6c65f6 Brian Foley
    } else
301 fe50f385 Romain Dolbeau
#endif /* HAVE_ALTIVEC */
302 ab6c65f6 Brian Foley
    {
303 05c4072b Michael Niedermayer
        // Non-AltiVec PPC optimisations
304
305
        // ... pending ...
306 ab6c65f6 Brian Foley
    }
307
}