]> git.sesse.net Git - x264/blob - common/ppc/ppccommon.h
Add AltiVec implementation of add4x4_idct, add8x8_idct, add16x16_idct, 3.2x faster...
[x264] / common / ppc / ppccommon.h
1 /*****************************************************************************
2  * ppccommon.h: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: ppccommon.h,v 1.1 2004/06/03 19:27:07 fenrir Exp $
6  *
7  * Authors: Eric Petit <titer@m0k.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /***********************************************************************
25  * For constant vectors, use parentheses on OS X and braces on Linux
26  **********************************************************************/
27 #ifdef SYS_MACOSX
28 #define CV(a...) (a)
29 #else
30 #define CV(a...) {a}
31 #endif
32
33 /***********************************************************************
34  * Vector types
35  **********************************************************************/
36 #define vec_u8_t  vector unsigned char
37 #define vec_s8_t  vector signed char
38 #define vec_u16_t vector unsigned short
39 #define vec_s16_t vector signed short
40 #define vec_u32_t vector unsigned int
41 #define vec_s32_t vector signed int
42
43 /***********************************************************************
44  * Null vector
45  **********************************************************************/
46 #define LOAD_ZERO const vec_u8_t zerov = vec_splat_u8( 0 )
47
48 #define zero_u8v  (vec_u8_t)  zerov
49 #define zero_s8v  (vec_s8_t)  zerov
50 #define zero_u16v (vec_u16_t) zerov
51 #define zero_s16v (vec_s16_t) zerov
52 #define zero_u32v (vec_u32_t) zerov
53 #define zero_s32v (vec_s32_t) zerov
54
55 /***********************************************************************
56  * 8 <-> 16 bits conversions
57  **********************************************************************/
58 #define vec_u8_to_u16_h(v) (vec_u16_t) vec_mergeh( zero_u8v, (vec_u8_t) v )
59 #define vec_u8_to_u16_l(v) (vec_u16_t) vec_mergel( zero_u8v, (vec_u8_t) v )
60 #define vec_u8_to_s16_h(v) (vec_s16_t) vec_mergeh( zero_u8v, (vec_u8_t) v )
61 #define vec_u8_to_s16_l(v) (vec_s16_t) vec_mergel( zero_u8v, (vec_u8_t) v )
62
63 #define vec_u8_to_u16(v) vec_u8_to_u16_h(v)
64 #define vec_u8_to_s16(v) vec_u8_to_s16_h(v)
65
66 #define vec_u16_to_u8(v) vec_pack( v, zero_u16v )
67 #define vec_s16_to_u8(v) vec_packsu( v, zero_s16v )
68
69 /***********************************************************************
70  * PREP_LOAD: declares two vectors required to perform unaligned loads
71  * VEC_LOAD:  loads n bytes from u8 * p into vector v of type t
72  **********************************************************************/
73 #define PREP_LOAD \
74     vec_u8_t _hv, _lv
75
76 #define VEC_LOAD( p, v, n, t )                  \
77     _hv = vec_ld( 0, p );                       \
78     v   = (t) vec_lvsl( 0, p );                 \
79     _lv = vec_ld( n - 1, p );                   \
80     v   = (t) vec_perm( _hv, _lv, (vec_u8_t) v )
81
82 /***********************************************************************
83  * PREP_STORE##n: declares required vectors to store n bytes to a
84  *                potentially unaligned address
85  * VEC_STORE##n:  stores n bytes from vector v to address p
86  **********************************************************************/
87 #define PREP_STORE16 \
88     vec_u8_t _tmp1v, _tmp2v \
89
90 #define VEC_STORE16( v, p ) \
91     _hv    = vec_ld( 0, p ); \
92     _tmp2v = vec_lvsl( 0, p ); \
93     _lv    = vec_ld( 15, p ); \
94     _tmp1v = vec_perm( _lv, _hv, _tmp2v ); \
95     _tmp2v = vec_lvsr( 0, p ); \
96     _lv    = vec_perm( (vec_u8_t) v, _tmp1v, _tmp2v ); \
97     vec_st( _lv, 15, (uint8_t *) p ); \
98     _hv    = vec_perm( _tmp1v, (vec_u8_t) v, _tmp2v ); \
99     vec_st( _hv, 0, (uint8_t *) p )
100
101 #define PREP_STORE8 \
102     PREP_STORE16; \
103     vec_u8_t _tmp3v, _tmp4v; \
104     const vec_u8_t sel_h = \
105         (vec_u8_t) CV(-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0)
106
107 #define PREP_STORE8_HL \
108     PREP_STORE8; \
109     const vec_u8_t sel_l = \
110         (vec_u8_t) CV(0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1)
111
112 #define VEC_STORE8 VEC_STORE8_H
113
114 #define VEC_STORE8_H( v, p ) \
115     _tmp3v = vec_lvsr( 0, (uint8_t *) p ); \
116     _tmp4v = vec_perm( (vec_u8_t) v, (vec_u8_t) v, _tmp3v ); \
117     _lv    = vec_ld( 7, (uint8_t *) p ); \
118     _tmp1v = vec_perm( sel_h, zero_u8v, _tmp3v ); \
119     _lv    = vec_sel( _lv, _tmp4v, _tmp1v ); \
120     vec_st( _lv, 7, (uint8_t *) p ); \
121     _hv    = vec_ld( 0, (uint8_t *) p ); \
122     _tmp2v = vec_perm( zero_u8v, sel_h, _tmp3v ); \
123     _hv    = vec_sel( _hv, _tmp4v, _tmp2v ); \
124     vec_st( _hv, 0, (uint8_t *) p )
125
126 #define VEC_STORE8_L( v, p ) \
127     _tmp3v = vec_lvsr( 8, (uint8_t *) p ); \
128     _tmp4v = vec_perm( (vec_u8_t) v, (vec_u8_t) v, _tmp3v ); \
129     _lv    = vec_ld( 7, (uint8_t *) p ); \
130     _tmp1v = vec_perm( sel_l, zero_u8v, _tmp3v ); \
131     _lv    = vec_sel( _lv, _tmp4v, _tmp1v ); \
132     vec_st( _lv, 7, (uint8_t *) p ); \
133     _hv    = vec_ld( 0, (uint8_t *) p ); \
134     _tmp2v = vec_perm( zero_u8v, sel_l, _tmp3v ); \
135     _hv    = vec_sel( _hv, _tmp4v, _tmp2v ); \
136     vec_st( _hv, 0, (uint8_t *) p )
137
138 #define PREP_STORE4 \
139     PREP_STORE16; \
140     vec_u8_t _tmp3v; \
141     const vec_u8_t sel = \
142         (vec_u8_t) CV(-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0)
143
144 #define VEC_STORE4( v, p ) \
145     _tmp3v = vec_lvsr( 0, p ); \
146     v      = vec_perm( v, v, _tmp3v ); \
147     _lv    = vec_ld( 3, p ); \
148     _tmp1v = vec_perm( sel, zero_u8v, _tmp3v ); \
149     _lv    = vec_sel( _lv, v, _tmp1v ); \
150     vec_st( _lv, 3, p ); \
151     _hv    = vec_ld( 0, p ); \
152     _tmp2v = vec_perm( zero_u8v, sel, _tmp3v ); \
153     _hv    = vec_sel( _hv, v, _tmp2v ); \
154     vec_st( _hv, 0, p )
155
156 /***********************************************************************
157  * VEC_TRANSPOSE_8
158  ***********************************************************************
159  * Transposes a 8x8 matrix of s16 vectors
160  **********************************************************************/
161 #define VEC_TRANSPOSE_8(a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3,b4,b5,b6,b7) \
162     b0 = vec_mergeh( a0, a4 ); \
163     b1 = vec_mergel( a0, a4 ); \
164     b2 = vec_mergeh( a1, a5 ); \
165     b3 = vec_mergel( a1, a5 ); \
166     b4 = vec_mergeh( a2, a6 ); \
167     b5 = vec_mergel( a2, a6 ); \
168     b6 = vec_mergeh( a3, a7 ); \
169     b7 = vec_mergel( a3, a7 ); \
170     a0 = vec_mergeh( b0, b4 ); \
171     a1 = vec_mergel( b0, b4 ); \
172     a2 = vec_mergeh( b1, b5 ); \
173     a3 = vec_mergel( b1, b5 ); \
174     a4 = vec_mergeh( b2, b6 ); \
175     a5 = vec_mergel( b2, b6 ); \
176     a6 = vec_mergeh( b3, b7 ); \
177     a7 = vec_mergel( b3, b7 ); \
178     b0 = vec_mergeh( a0, a4 ); \
179     b1 = vec_mergel( a0, a4 ); \
180     b2 = vec_mergeh( a1, a5 ); \
181     b3 = vec_mergel( a1, a5 ); \
182     b4 = vec_mergeh( a2, a6 ); \
183     b5 = vec_mergel( a2, a6 ); \
184     b6 = vec_mergeh( a3, a7 ); \
185     b7 = vec_mergel( a3, a7 )
186
187 /***********************************************************************
188  * VEC_TRANSPOSE_4
189  ***********************************************************************
190  * Transposes a 4x4 matrix of s16 vectors.
191  * Actually source and destination are 8x4. The low elements of the
192  * source are discarded and the low elements of the destination mustn't
193  * be used.
194  **********************************************************************/
195 #define VEC_TRANSPOSE_4(a0,a1,a2,a3,b0,b1,b2,b3) \
196     b0 = vec_mergeh( a0, a0 ); \
197     b1 = vec_mergeh( a1, a0 ); \
198     b2 = vec_mergeh( a2, a0 ); \
199     b3 = vec_mergeh( a3, a0 ); \
200     a0 = vec_mergeh( b0, b2 ); \
201     a1 = vec_mergel( b0, b2 ); \
202     a2 = vec_mergeh( b1, b3 ); \
203     a3 = vec_mergel( b1, b3 ); \
204     b0 = vec_mergeh( a0, a2 ); \
205     b1 = vec_mergel( a0, a2 ); \
206     b2 = vec_mergeh( a1, a3 ); \
207     b3 = vec_mergel( a1, a3 )
208
209 /***********************************************************************
210  * VEC_DIFF_H
211  ***********************************************************************
212  * p1, p2:    u8 *
213  * i1, i2, n: int
214  * d:         s16v
215  *
216  * Loads n bytes from p1 and p2, do the diff of the high elements into
217  * d, increments p1 and p2 by i1 and i2
218  **********************************************************************/
219 #define PREP_DIFF           \
220     LOAD_ZERO;              \
221     PREP_LOAD;              \
222     vec_s16_t pix1v, pix2v;
223
224 #define VEC_DIFF_H(p1,i1,p2,i2,n,d)      \
225     VEC_LOAD( p1, pix1v, n, vec_s16_t ); \
226     pix1v = vec_u8_to_s16( pix1v );      \
227     VEC_LOAD( p2, pix2v, n, vec_s16_t ); \
228     pix2v = vec_u8_to_s16( pix2v );      \
229     d     = vec_sub( pix1v, pix2v );     \
230     p1   += i1;                          \
231     p2   += i2
232
233 /***********************************************************************
234  * VEC_DIFF_HL
235  ***********************************************************************
236  * p1, p2: u8 *
237  * i1, i2: int
238  * dh, dl: s16v
239  *
240  * Loads 16 bytes from p1 and p2, do the diff of the high elements into
241  * dh, the diff of the low elements into dl, increments p1 and p2 by i1
242  * and i2
243  **********************************************************************/
244 #define VEC_DIFF_HL(p1,i1,p2,i2,dh,dl)    \
245     VEC_LOAD( p1, pix1v, 16, vec_s16_t ); \
246     temp0v = vec_u8_to_s16_h( pix1v );    \
247     temp1v = vec_u8_to_s16_l( pix1v );    \
248     VEC_LOAD( p2, pix2v, 16, vec_s16_t ); \
249     temp2v = vec_u8_to_s16_h( pix2v );    \
250     temp3v = vec_u8_to_s16_l( pix2v );    \
251     dh     = vec_sub( temp0v, temp2v );   \
252     dl     = vec_sub( temp1v, temp3v );   \
253     p1    += i1;                          \
254     p2    += i2
255
256 /***********************************************************************
257 * VEC_DIFF_H_8BYTE_ALIGNED
258 ***********************************************************************
259 * p1, p2:    u8 *
260 * i1, i2, n: int
261 * d:         s16v
262 *
263 * Loads n bytes from p1 and p2, do the diff of the high elements into
264 * d, increments p1 and p2 by i1 and i2
265 * Slightly faster when we know we are loading/diffing 8bytes which
266 * are 8 byte aligned. Reduces need for two loads and two vec_lvsl()'s
267 **********************************************************************/
268 #define PREP_DIFF_8BYTEALIGNED \
269 LOAD_ZERO;                     \
270 vec_s16_t pix1v, pix2v;        \
271 vec_u8_t pix1v8, pix2v8;       \
272 vec_u8_t permPix1, permPix2;   \
273 permPix1 = vec_lvsl(0, pix1);  \
274 permPix2 = vec_lvsl(0, pix2);  \
275
276 #define VEC_DIFF_H_8BYTE_ALIGNED(p1,i1,p2,i2,n,d)     \
277 pix1v8 = vec_perm(vec_ld(0,p1), zero_u8v, permPix1);  \
278 pix2v8 = vec_perm(vec_ld(0, p2), zero_u8v, permPix2); \
279 pix1v = vec_u8_to_s16( pix1v8 );                      \
280 pix2v = vec_u8_to_s16( pix2v8 );                      \
281 d = vec_sub( pix1v, pix2v);                           \
282 p1 += i1;                                             \
283 p2 += i2;