]> git.sesse.net Git - ffmpeg/blob - libavcodec/ppc/int_altivec.c
Merge commit '15a29c39d9ef15b0783c04b3228e1c55f6701ee3'
[ffmpeg] / libavcodec / ppc / int_altivec.c
1 /*
2  * Copyright (c) 2007 Luca Barbato <lu_zero@gentoo.org>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * miscellaneous integer operations
24  */
25
26 #include "config.h"
27 #if HAVE_ALTIVEC_H
28 #include <altivec.h>
29 #endif
30
31 #include "libavutil/attributes.h"
32 #include "libavutil/ppc/types_altivec.h"
33 #include "libavcodec/dsputil.h"
34 #include "dsputil_altivec.h"
35
36 static int ssd_int8_vs_int16_altivec(const int8_t *pix1, const int16_t *pix2,
37                                      int size)
38 {
39     int i, size16 = size >> 4;
40     vector signed char vpix1;
41     vector signed short vpix2, vdiff, vpix1l, vpix1h;
42     union {
43         vector signed int vscore;
44         int32_t score[4];
45     } u = { .vscore = vec_splat_s32(0) };
46
47 // XXX lazy way, fix it later
48
49 #define vec_unaligned_load(b)                                   \
50     vec_perm(vec_ld(0, b), vec_ld(15, b), vec_lvsl(0, b));
51
52     while (size16) {
53         // score += (pix1[i] - pix2[i]) * (pix1[i] - pix2[i]);
54         // load pix1 and the first batch of pix2
55
56         vpix1 = vec_unaligned_load(pix1);
57         vpix2 = vec_unaligned_load(pix2);
58         pix2 += 8;
59         // unpack
60         vpix1h = vec_unpackh(vpix1);
61         vdiff  = vec_sub(vpix1h, vpix2);
62         vpix1l = vec_unpackl(vpix1);
63         // load another batch from pix2
64         vpix2    = vec_unaligned_load(pix2);
65         u.vscore = vec_msum(vdiff, vdiff, u.vscore);
66         vdiff    = vec_sub(vpix1l, vpix2);
67         u.vscore = vec_msum(vdiff, vdiff, u.vscore);
68         pix1    += 16;
69         pix2    += 8;
70         size16--;
71     }
72     u.vscore = vec_sums(u.vscore, vec_splat_s32(0));
73
74     size %= 16;
75     for (i = 0; i < size; i++)
76         u.score[3] += (pix1[i] - pix2[i]) * (pix1[i] - pix2[i]);
77
78     return u.score[3];
79 }
80
81 static int32_t scalarproduct_int16_altivec(const int16_t *v1, const int16_t *v2,
82                                            int order)
83 {
84     int i;
85     LOAD_ZERO;
86     register vec_s16 vec1;
87     register vec_s32 res = vec_splat_s32(0), t;
88     int32_t ires;
89
90     for (i = 0; i < order; i += 8) {
91         vec1 = vec_unaligned_load(v1);
92         t    = vec_msum(vec1, vec_ld(0, v2), zero_s32v);
93         res  = vec_sums(t, res);
94         v1  += 8;
95         v2  += 8;
96     }
97     res = vec_splat(res, 3);
98     vec_ste(res, 0, &ires);
99
100     return ires;
101 }
102
103 static int32_t scalarproduct_and_madd_int16_altivec(int16_t *v1,
104                                                     const int16_t *v2,
105                                                     const int16_t *v3,
106                                                     int order, int mul)
107 {
108     LOAD_ZERO;
109     vec_s16 *pv1 = (vec_s16 *) v1;
110     register vec_s16 muls = { mul, mul, mul, mul, mul, mul, mul, mul };
111     register vec_s16 t0, t1, i0, i1, i4;
112     register vec_s16 i2 = vec_ld(0, v2), i3 = vec_ld(0, v3);
113     register vec_s32 res = zero_s32v;
114     register vec_u8 align = vec_lvsl(0, v2);
115     int32_t ires;
116
117     order >>= 4;
118     do {
119         i1     = vec_ld(16, v2);
120         t0     = vec_perm(i2, i1, align);
121         i2     = vec_ld(32, v2);
122         t1     = vec_perm(i1, i2, align);
123         i0     = pv1[0];
124         i1     = pv1[1];
125         res    = vec_msum(t0, i0, res);
126         res    = vec_msum(t1, i1, res);
127         i4     = vec_ld(16, v3);
128         t0     = vec_perm(i3, i4, align);
129         i3     = vec_ld(32, v3);
130         t1     = vec_perm(i4, i3, align);
131         pv1[0] = vec_mladd(t0, muls, i0);
132         pv1[1] = vec_mladd(t1, muls, i1);
133         pv1   += 2;
134         v2    += 16;
135         v3    += 16;
136     } while (--order);
137     res = vec_splat(vec_sums(res, zero_s32v), 3);
138     vec_ste(res, 0, &ires);
139
140     return ires;
141 }
142
143 av_cold void ff_int_init_altivec(DSPContext *c, AVCodecContext *avctx)
144 {
145     c->ssd_int8_vs_int16 = ssd_int8_vs_int16_altivec;
146
147     c->scalarproduct_int16 = scalarproduct_int16_altivec;
148
149     c->scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_altivec;
150 }