1 /* { dg-do run } */
2 /* { dg-require-effective-target vmx_hw } */
3 /* { dg-options "-maltivec" } */
4 
5 /* This test should run the same on any target that supports altivec/dfp
6    instructions.  Unsigned Intentionally not specifying cpu in order to test
7    all code generation paths.  */
8 
9 #include <altivec.h>
10 
11 extern void abort (void);
12 
13 #define CONST0		(0)
14 #define CONST1		(1)
15 #define CONST2		(2)
16 #define CONST3		(3)
17 
18 /* Test that indices > length of vector are applied modulo the vector
19    length.  */
20 
21 /* Test for vector residing in register.  */
s3(vector unsigned int v)22 unsigned int s3 (vector unsigned int v)
23 {
24   return __builtin_vec_extract (v, 3);
25 }
26 
s1(vector unsigned int v)27 unsigned int s1 (vector unsigned int v)
28 {
29   return __builtin_vec_extract (v, 1);
30 }
31 
s21(vector unsigned int v)32 unsigned int s21 (vector unsigned int v)
33 {
34   return __builtin_vec_extract (v, 21);
35 }
36 
s30(vector unsigned int v)37 unsigned int s30 (vector unsigned int v)
38 {
39   return __builtin_vec_extract (v, 30);
40 }
41 
42 /* Test for vector residing in memory.  */
ms3(vector unsigned int * vp)43 unsigned int ms3 (vector unsigned int *vp)
44 {
45   return __builtin_vec_extract (*vp, 3);
46 }
47 
ms1(vector unsigned int * vp)48 unsigned int ms1(vector unsigned int *vp)
49 {
50   return __builtin_vec_extract (*vp, 1);
51 }
52 
ms21(vector unsigned int * vp)53 unsigned int ms21(vector unsigned int *vp)
54 {
55   return __builtin_vec_extract (*vp, 21);
56 }
57 
ms30(vector unsigned int * vp)58 unsigned int ms30(vector unsigned int *vp)
59 {
60   return __builtin_vec_extract (*vp, 30);
61 }
62 
63 /* Test the same with variable indices.  */
64 
65 /* Test for variable selector and vector residing in register.  */
66 __attribute__((noinline))
ci(vector unsigned int v,int i)67 unsigned int ci (vector unsigned int v, int i)
68 {
69   return __builtin_vec_extract (v, i);
70 }
71 
72 /* Test for variable selector and vector residing in memory.  */
73 __attribute__((noinline))
mci(vector unsigned int * vp,int i)74 unsigned int mci(vector unsigned int *vp, int i)
75 {
76   return __builtin_vec_extract (*vp, i);
77 }
78 
79 
main(int argc,unsigned char * argv[])80 unsigned int main (int argc, unsigned char *argv[]) {
81   vector unsigned int sv = { CONST0, CONST1, CONST2, CONST3 };
82   unsigned int s;
83 
84   s = s3 (sv);
85   if (s != CONST3)
86     abort ();
87 
88   s = s1 (sv);
89   if (s != CONST1)
90     abort ();
91 
92   s = s21 (sv);
93   if (s != CONST1)
94     abort ();
95 
96   s = s30 (sv);
97   if (s != CONST2)
98     abort ();
99 
100   s = ms3 (&sv);
101   if (s != CONST3)
102     abort ();
103 
104   s = ms1 (&sv);
105   if (s != CONST1)
106     abort ();
107 
108   s = ms21 (&sv);
109   if (s != CONST1)
110     abort ();
111 
112   s = ms30 (&sv);
113   if (s != CONST2)
114     abort ();
115 
116   s = ci (sv, 5);
117   if (s != CONST1)
118     abort ();
119 
120   s = ci (sv, 2);
121   if (s != CONST2)
122     abort ();
123 
124   s = ci (sv, 15);
125   if (s != CONST3)
126     abort ();
127 
128   s = ci (sv, 28);
129   if (s != CONST0)
130     abort ();
131 
132   s = mci (&sv, 5);
133   if (s != CONST1)
134     abort ();
135 
136   s = mci (&sv, 12);
137   if (s != CONST0)
138     abort ();
139 
140   s = mci (&sv, 25);
141   if (s != CONST1)
142     abort ();
143 
144   s = mci (&sv, 16);
145   if (s != CONST0)
146     abort ();
147 
148   return 0;
149 }
150