2
* Copyright (c) 2003, 2006 Matteo Frigo
3
* Copyright (c) 2003, 2006 Massachusetts Institute of Technology
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program 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
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#error "ALTIVEC only works in single precision"
25
/* define these unconditionally, because they are used by
26
taint.c which is compiled without altivec */
27
#define VL 2 /* SIMD complex vector length */
28
#define ALIGNMENT 8 /* alignment for LD/ST */
29
#define ALIGNMENTA 16 /* alignment for LDA/STA */
30
#define SIMD_VSTRIDE_OKA(x) ((x) == 2)
31
#define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OKA
33
#define RIGHT_CPU X(have_altivec)
34
extern int RIGHT_CPU(void);
36
#if defined(__VEC__) || defined(FAKE__VEC__)
42
typedef vector float V;
43
#define VLIT(x0, x1, x2, x3) {x0, x1, x2, x3}
45
#define DVK(var, val) const V var = VLIT(val, val, val, val)
47
static inline V VADD(V a, V b) { return vec_add(a, b); }
48
static inline V VSUB(V a, V b) { return vec_sub(a, b); }
49
static inline V VFMA(V a, V b, V c) { return vec_madd(a, b, c); }
50
static inline V VFNMS(V a, V b, V c) { return vec_nmsub(a, b, c); }
52
static inline V VMUL(V a, V b)
55
return VFMA(a, b, zero);
58
static inline V VFMS(V a, V b, V c) { return VSUB(VMUL(a, b), c); }
60
static inline V LDA(const R *x, INT ivs, const R *aligned_like)
67
static inline V LD(const R *x, INT ivs, const R *aligned_like)
69
/* common subexpressions */
70
const INT fivs = sizeof(R) * ivs;
71
/* you are not expected to understand this: */
72
const vector unsigned int perm = VLIT(0, 0, 0xFFFFFFFF, 0xFFFFFFFF);
73
vector unsigned char ml = vec_lvsr(fivs + 8, aligned_like);
74
vector unsigned char mh = vec_lvsl(0, aligned_like);
75
vector unsigned char msk =
76
(vector unsigned char)vec_sel((V)mh, (V)ml, perm);
77
/* end of common subexpressions */
79
return vec_perm(vec_ld(0, x), vec_ld(fivs, x), msk);
82
/* store lower half */
83
static inline void STH(R *x, V v, R *aligned_like)
85
v = vec_perm(v, v, vec_lvsr(0, aligned_like));
87
vec_ste(v, sizeof(R), x);
90
static inline void STL(R *x, V v, INT ovs, R *aligned_like)
92
const INT fovs = sizeof(R) * ovs;
93
v = vec_perm(v, v, vec_lvsr(fovs + 8, aligned_like));
95
vec_ste(v, sizeof(R) + fovs, x);
98
static inline void STA(R *x, V v, INT ovs, R *aligned_like)
101
UNUSED(aligned_like);
105
static inline void ST(R *x, V v, INT ovs, R *aligned_like)
107
STH(x, v, aligned_like);
108
STL(x, v, ovs, aligned_like);
111
#define STM2(x, v, ovs, aligned_like) /* no-op */
113
static inline void STN2(R *x, V v0, V v1, INT ovs)
115
const INT fovs = sizeof(R) * ovs;
116
const vector unsigned int even =
117
VLIT(0x00010203, 0x04050607, 0x10111213, 0x14151617);
118
const vector unsigned int odd =
119
VLIT(0x08090a0b, 0x0c0d0e0f, 0x18191a1b, 0x1c1d1e1f);
120
vec_st(vec_perm(v0, v1, (vector unsigned char)even), 0, x);
121
vec_st(vec_perm(v0, v1, (vector unsigned char)odd), fovs, x);
124
#define STM4(x, v, ovs, aligned_like) /* no-op */
126
static inline void STN4(R *x, V v0, V v1, V v2, V v3, INT ovs)
128
const INT fovs = sizeof(R) * ovs;
129
V x0 = vec_mergeh(v0, v2);
130
V x1 = vec_mergel(v0, v2);
131
V x2 = vec_mergeh(v1, v3);
132
V x3 = vec_mergel(v1, v3);
133
V y0 = vec_mergeh(x0, x2);
134
V y1 = vec_mergel(x0, x2);
135
V y2 = vec_mergeh(x1, x3);
136
V y3 = vec_mergel(x1, x3);
139
vec_st(y2, 2 * fovs, x);
140
vec_st(y3, 3 * fovs, x);
143
static inline V FLIP_RI(V x)
145
const vector unsigned int perm =
146
VLIT(0x04050607, 0x00010203, 0x0c0d0e0f, 0x08090a0b);
147
return vec_perm(x, x, (vector unsigned char)perm);
150
static inline V CHS_R(V x)
152
const V pmpm = VLIT(-0.0, 0.0, -0.0, 0.0);
153
return vec_xor(x, pmpm);
156
static inline V VBYI(V x)
158
return CHS_R(FLIP_RI(x));
161
static inline V VFMAI(V b, V c)
163
const V pmpm = VLIT(-1.0, 1.0, -1.0, 1.0);
164
return VFMA(FLIP_RI(b), pmpm, c);
167
static inline V VFNMSI(V b, V c)
169
const V pmpm = VLIT(-1.0, 1.0, -1.0, 1.0);
170
return VFNMS(FLIP_RI(b), pmpm, c);
173
static inline V VZMUL(V tx, V sr)
175
const vector unsigned int real =
176
VLIT(0x00010203, 0x00010203, 0x08090a0b, 0x08090a0b);
177
const vector unsigned int imag =
178
VLIT(0x04050607, 0x04050607, 0x0c0d0e0f, 0x0c0d0e0f);
180
V tr = vec_perm(tx, tx, (vector unsigned char)real);
181
V ti = vec_perm(tx, tx, (vector unsigned char)imag);
182
return VFMA(ti, si, VMUL(tr, sr));
185
static inline V VZMULJ(V tx, V sr)
187
const vector unsigned int real =
188
VLIT(0x00010203, 0x00010203, 0x08090a0b, 0x08090a0b);
189
const vector unsigned int imag =
190
VLIT(0x04050607, 0x04050607, 0x0c0d0e0f, 0x0c0d0e0f);
192
V tr = vec_perm(tx, tx, (vector unsigned char)real);
193
V ti = vec_perm(tx, tx, (vector unsigned char)imag);
194
return VFNMS(ti, si, VMUL(tr, sr));
197
/* twiddle storage #1: compact, slower */
198
#define VTW1(x) {TW_COS, 0, x}, {TW_COS, 1, x}, {TW_SIN, 0, x}, {TW_SIN, 1, x}
201
static inline V BYTW1(const R *t, V sr)
203
const V *twp = (const V *)t;
206
V tr = vec_mergeh(tx, tx);
207
V ti = vec_mergel(tx, tx);
208
return VFMA(ti, si, VMUL(tr, sr));
211
static inline V BYTWJ1(const R *t, V sr)
213
const V *twp = (const V *)t;
216
V tr = vec_mergeh(tx, tx);
217
V ti = vec_mergel(tx, tx);
218
return VFNMS(ti, si, VMUL(tr, sr));
221
/* twiddle storage #2: twice the space, faster (when in cache) */
223
{TW_COS, 0, x}, {TW_COS, 0, x}, {TW_COS, 1, x}, {TW_COS, 1, x}, \
224
{TW_SIN, 0, -x}, {TW_SIN, 0, x}, {TW_SIN, 1, -x}, {TW_SIN, 1, x}
225
#define TWVL2 (2 * VL)
227
static inline V BYTW2(const R *t, V sr)
229
const V *twp = (const V *)t;
231
V tr = twp[0], ti = twp[1];
232
return VFMA(ti, si, VMUL(tr, sr));
235
static inline V BYTWJ2(const R *t, V sr)
237
const V *twp = (const V *)t;
239
V tr = twp[0], ti = twp[1];
240
return VFNMS(ti, si, VMUL(tr, sr));
243
/* twiddle storage #3 */
244
#define VTW3(x) {TW_CEXP, 0, x}, {TW_CEXP, 1, x}
247
/* twiddle storage for split arrays */
249
{TW_COS, 0, x}, {TW_COS, 1, x}, {TW_COS, 2, x}, {TW_COS, 3, x}, \
250
{TW_SIN, 0, x}, {TW_SIN, 1, x}, {TW_SIN, 2, x}, {TW_SIN, 3, x}
251
#define TWVLS (2 * VL)
253
#endif /* #ifdef __VEC__ */