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/vmx
6    instructions.  Intentionally not specifying cpu in order to test
7    all code generation paths.  */
8 
9 #include <stdio.h>
10 #include <altivec.h>
11 
12 extern void abort (void);
13 
14 #define CONST0		(0)
15 #define CONST1		(1)
16 #define CONST2		(2)
17 #define CONST3		(3)
18 #define CONST4		(4)
19 #define CONST5		(5)
20 #define CONST6		(6)
21 #define CONST7		(7)
22 #define CONST8		(8)
23 #define CONST9		(9)
24 #define CONSTA		(10)
25 #define CONSTB		(11)
26 #define CONSTC		(12)
27 #define CONSTD		(13)
28 #define CONSTE		(14)
29 #define CONSTF		(15)
30 
31 
32 /* Test that indices > length of vector are applied modulo the vector
33    length.  */
34 
35 /* Test for vector residing in register.  */
c0(vector unsigned char v,unsigned char x)36 vector unsigned char c0 (vector unsigned char v, unsigned char x)
37 {
38   return vec_insert (x, v, 0);
39 }
40 
c9(vector unsigned char v,unsigned char x)41 vector unsigned char c9 (vector unsigned char v, unsigned char x)
42 {
43   return vec_insert (x, v, 9);
44 }
45 
c21(vector unsigned char v,unsigned char x)46 vector unsigned char c21 (vector unsigned char v, unsigned char x)
47 {
48   return vec_insert (x, v, 21);
49 }
50 
c30(vector unsigned char v,unsigned char x)51 vector unsigned char c30 (vector unsigned char v, unsigned char x)
52 {
53   return vec_insert (x, v, 30);
54 }
55 
56 /* Test for vector residing in memory.  */
mc0(vector unsigned char * vp,unsigned char x)57 vector unsigned char mc0 (vector unsigned char *vp, unsigned char x)
58 {
59   return vec_insert (x, *vp, 0);
60 }
61 
mc9(vector unsigned char * vp,unsigned char x)62 vector unsigned char mc9 (vector unsigned char *vp, unsigned char x)
63 {
64   return vec_insert (x, *vp, 9);
65 }
66 
mc21(vector unsigned char * vp,unsigned char x)67 vector unsigned char mc21 (vector unsigned char *vp, unsigned char x)
68 {
69   return vec_insert (x, *vp, 21);
70 }
71 
mc30(vector unsigned char * vp,unsigned char x)72 vector unsigned char mc30 (vector unsigned char *vp, unsigned char x)
73 {
74   return vec_insert (x, *vp, 30);
75 }
76 
77 /* Test the same with variable indices.  */
78 
79 /* Test for variable selector and vector residing in register.  */
80 __attribute__((noinline))
ci(vector unsigned char v,int i,unsigned char x)81 vector unsigned char ci (vector unsigned char v, int i, unsigned char x)
82 {
83   return vec_insert (x, v, i);
84 }
85 
86 /* Test for variable selector and vector residing in memory.  */
87 __attribute__((noinline))
mci(vector unsigned char * vp,int i,unsigned char x)88 vector unsigned char mci (vector unsigned char *vp, int i, unsigned char x)
89 {
90   return vec_insert (x, *vp, i);
91 }
92 
93 
main(int argc,char * argv[])94 int main (int argc, char *argv[]) {
95   vector unsigned char cv = { CONST0, CONST1, CONST2, CONST3,
96 			      CONST4, CONST5, CONST6, CONST7,
97 			      CONST8, CONST9, CONSTA, CONSTB,
98 			      CONSTC, CONSTD, CONSTE, CONSTF };
99   printf ("A\n");
100   cv = c0 (cv, CONST3);
101   if (cv [0] != CONST3)
102     abort ();
103 
104   printf ("B\n");
105   cv = c9 (cv, CONST2);
106   if (cv [9] != CONST2)
107     abort ();
108 
109   printf ("C\n");
110   cv = c21 (cv, CONSTF);
111   if (cv [5] != CONSTF)
112     abort ();
113 
114   printf ("D\n");
115   cv = c30 (cv, CONST3);
116   if (cv [14] != CONST3)
117     abort ();
118 
119   printf ("E\n");
120   cv = mc0 (&cv, CONST4);
121   if (cv [0] != CONST4)
122     abort ();
123 
124   printf ("F\n");
125   cv = mc9 (&cv, CONST3);
126   if (cv [9] != CONST3)
127     abort ();
128 
129   printf ("G\n");
130   cv = mc21 (&cv, CONST1);
131   if (cv [5] != CONST1)
132     abort ();
133 
134   printf ("H\n");
135   cv = mc30 (&cv, CONSTC);
136   if (cv [14] != CONSTC)
137     abort ();
138 
139   printf ("I\n");
140   cv = ci (cv, 8, CONSTD);
141   if (cv [8] != CONSTD)
142     abort ();
143 
144   printf ("J\n");
145   cv = ci (cv, 13, CONST5);
146   if (cv [13] != CONST5)
147     abort ();
148 
149   printf ("K\n");
150   cv = ci (cv, 23, CONST6);
151   if (cv [7] != CONST6)
152     abort ();
153 
154   printf ("L\n");
155   cv = ci (cv, 31, CONST7);
156   if (cv [15] != CONST7)
157     abort ();
158 
159   printf ("M\n");
160   cv = mci (&cv, 5, CONST8);
161   if (cv [5] != CONST8)
162     abort ();
163 
164   printf ("N\n");
165   cv = mci (&cv, 12, CONST9);
166   if (cv [12] != CONST9)
167     abort ();
168 
169   printf ("O\n");
170   cv = mci (&cv, 25, CONSTA);
171   if (cv [9] != CONSTA)
172     abort ();
173 
174   printf ("P\n");
175   cv = mci (&cv, 16, CONSTB);
176   if (cv [0] != CONSTB)
177     abort ();
178 
179   return 0;
180 }
181