1 // RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -triple x86_64-apple-darwin10
2 
3 // Test the compatibility of clang's vector extensions with gcc's vector
4 // extensions for C. Notably &&, ||, ?: and ! are not available.
5 typedef long long v2i64 __attribute__((vector_size(16)));
6 typedef int v2i32 __attribute__((vector_size(8)));
7 typedef short v2i16 __attribute__((vector_size(4)));
8 typedef char v2i8 __attribute__((vector_size(2)));
9 
10 typedef unsigned long long v2u64 __attribute__((vector_size(16)));
11 typedef unsigned int v2u32 __attribute__((vector_size(8)));
12 typedef unsigned short v2u16 __attribute__((vector_size(4)));
13 typedef unsigned char v2u8 __attribute__((vector_size(2)));
14 
15 typedef float v4f32 __attribute__((vector_size(16)));
16 typedef double v2f64 __attribute__((vector_size(16)));
17 typedef double v4f64 __attribute__((vector_size(32)));
18 typedef int v4i32 __attribute((vector_size(16)));
19 
20 void arithmeticTest(void);
21 void logicTest(void);
22 void comparisonTest(void);
23 void floatTestSignedType(char a, short b, int c, long long d);
24 void floatTestUnsignedType(unsigned char a, unsigned short b, unsigned int c,
25                            unsigned long long d);
26 void floatTestConstant(void);
27 void intTestType(char a, short b, int c, long long d);
28 void intTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
29                          unsigned long long d);
30 void uintTestType(char a, short b, int c, long long d);
31 void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
32                           unsigned long long d);
33 void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a, v2u8 v2u8_a);
34 void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a);
35 
arithmeticTest(void)36 void arithmeticTest(void) {
37   v2i64 v2i64_a = (v2i64){0, 1};
38   v2i64 v2i64_r;
39 
40   v2i64_r = v2i64_a + 1;
41   v2i64_r = v2i64_a - 1;
42   v2i64_r = v2i64_a * 1;
43   v2i64_r = v2i64_a / 1;
44   v2i64_r = v2i64_a % 1;
45 
46   v2i64_r = 1 + v2i64_a;
47   v2i64_r = 1 - v2i64_a;
48   v2i64_r = 1 * v2i64_a;
49   v2i64_r = 1 / v2i64_a;
50   v2i64_r = 1 % v2i64_a;
51 
52   v2i64_a += 1;
53   v2i64_a -= 1;
54   v2i64_a *= 1;
55   v2i64_a /= 1;
56   v2i64_a %= 1;
57 }
58 
comparisonTest(void)59 void comparisonTest(void) {
60   v2i64 v2i64_a = (v2i64){0, 1};
61   v2i64 v2i64_r;
62 
63   v2i64_r = v2i64_a == 1;
64   v2i64_r = v2i64_a != 1;
65   v2i64_r = v2i64_a < 1;
66   v2i64_r = v2i64_a > 1;
67   v2i64_r = v2i64_a <= 1;
68   v2i64_r = v2i64_a >= 1;
69 
70   v2i64_r = 1 == v2i64_a;
71   v2i64_r = 1 != v2i64_a;
72   v2i64_r = 1 < v2i64_a;
73   v2i64_r = 1 > v2i64_a;
74   v2i64_r = 1 <= v2i64_a;
75   v2i64_r = 1 >= v2i64_a;
76 }
77 
logicTest(void)78 void logicTest(void) {
79   v2i64 v2i64_a = (v2i64){0, 1};
80   v2i64 v2i64_b = (v2i64){2, 1};
81   v2i64 v2i64_c = (v2i64){3, 1};
82   v2i64 v2i64_r;
83 
84   v2i64_r = !v2i64_a; // expected-error {{invalid argument type 'v2i64' (vector of 2 'long long' values) to unary expression}}
85   v2i64_r = ~v2i64_a;
86 
87   v2i64_r = v2i64_a ? v2i64_b : v2i64_c; // expected-error {{used type 'v2i64' (vector of 2 'long long' values) where arithmetic or pointer type is required}}
88 
89   v2i64_r = v2i64_a & 1;
90   v2i64_r = v2i64_a | 1;
91   v2i64_r = v2i64_a ^ 1;
92 
93   v2i64_r = 1 & v2i64_a;
94   v2i64_r = 1 | v2i64_a;
95   v2i64_r = 1 ^ v2i64_a;
96 
97   v2i64_a &= 1;
98   v2i64_a |= 1;
99   v2i64_a ^= 1;
100 
101   v2i64_r = v2i64_a && 1; // expected-error {{logical expression with vector type 'v2i64' (vector of 2 'long long' values) and non-vector type 'int' is only supported in C++}}
102   v2i64_r = v2i64_a || 1; // expected-error {{logical expression with vector type 'v2i64' (vector of 2 'long long' values) and non-vector type 'int' is only supported in C++}}
103 
104   v2i64_r = v2i64_a && v2i64_a; // expected-error {{logical expression with vector types 'v2i64' (vector of 2 'long long' values) and 'v2i64' is only supported in C++}}
105   v2i64_r = v2i64_a || v2i64_a; // expected-error {{logical expression with vector types 'v2i64' (vector of 2 'long long' values) and 'v2i64' is only supported in C++}}
106 
107   v2i64_r = v2i64_a << 1;
108   v2i64_r = v2i64_a >> 1;
109 
110   v2i64_r = 1 << v2i64_a;
111   v2i64_r = 1 >> v2i64_a;
112 
113   v2i64_a <<= 1;
114   v2i64_a >>= 1;
115 }
116 
117 // For operations with floating point types, we check that integer constants
118 // can be respresented, or failing that checking based on the integer types.
floatTestConstant(void)119 void floatTestConstant(void) {
120   // Test that constants added to floats must be expressible as floating point
121   // numbers.
122   v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
123   v4f32_a = v4f32_a + 1;
124   v4f32_a = v4f32_a + 0xFFFFFF;
125   v4f32_a = v4f32_a + (-1567563LL);
126   v4f32_a = v4f32_a + (16777208);
127   v4f32_a = v4f32_a + (16777219); // expected-error {{cannot convert between scalar type 'int' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
128 }
129 
130 void floatTestConstantComparison(void);
131 void doubleTestConstantComparison(void);
132 
floatTestConstantComparison(void)133 void floatTestConstantComparison(void) {
134   v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
135   v4i32 v4i32_r;
136   v4i32_r = v4f32_a > 0.4f;
137   v4i32_r = v4f32_a >= 0.4f;
138   v4i32_r = v4f32_a < 0.4f;
139   v4i32_r = v4f32_a <= 0.4f;
140   v4i32_r = v4f32_a == 0.4f; // expected-warning {{comparing floating point with == or != is unsafe}}
141   v4i32_r = v4f32_a != 0.4f; // expected-warning {{comparing floating point with == or != is unsafe}}
142 }
143 
doubleTestConstantComparison(void)144 void doubleTestConstantComparison(void) {
145   v2f64 v2f64_a = {0.4, 0.4};
146   v2i64 v2i64_r;
147   v2i64_r = v2f64_a > 0.4;
148   v2i64_r = v2f64_a >= 0.4;
149   v2i64_r = v2f64_a < 0.4;
150   v2i64_r = v2f64_a <= 0.4;
151   v2i64_r = v2f64_a == 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}
152   v2i64_r = v2f64_a != 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}
153 }
154 
floatTestUnsignedType(unsigned char a,unsigned short b,unsigned int c,unsigned long long d)155 void floatTestUnsignedType(unsigned char a, unsigned short b, unsigned int c,
156                            unsigned long long d) {
157   v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
158   v4f64 v4f64_b = {0.4, 0.4, 0.4, 0.4};
159 
160   v4f32_a = v4f32_a + a;
161   v4f32_a = v4f32_a + b;
162   v4f32_a = v4f32_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
163   v4f32_a = v4f32_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
164 
165   v4f64_b = v4f64_b + a;
166   v4f64_b = v4f64_b + b;
167   v4f64_b = v4f64_b + c;
168   v4f64_b = v4f64_b + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v4f64' (vector of 4 'double' values) as implicit conversion would cause truncation}}
169 }
170 
floatTestSignedType(char a,short b,int c,long long d)171 void floatTestSignedType(char a, short b, int c, long long d) {
172   v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
173   v4f64 v4f64_b = {0.4, 0.4, 0.4, 0.4};
174 
175   v4f32_a = v4f32_a + a;
176   v4f32_a = v4f32_a + b;
177   v4f32_a = v4f32_a + c; // expected-error {{cannot convert between scalar type 'int' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
178   v4f32_a = v4f32_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
179 
180   v4f64_b = v4f64_b + a;
181   v4f64_b = v4f64_b + b;
182   v4f64_b = v4f64_b + c;
183   v4f64_b = v4f64_b + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v4f64' (vector of 4 'double' values) as implicit conversion would cause truncation}}
184 }
185 
intTestType(char a,short b,int c,long long d)186 void intTestType(char a, short b, int c, long long d) {
187   v2i64 v2i64_a = {1, 2};
188   v2i32 v2i32_a = {1, 2};
189   v2i16 v2i16_a = {1, 2};
190   v2i8 v2i8_a = {1, 2};
191 
192   v2i64_a = v2i64_a + d;
193   v2i64_a = v2i64_a + c;
194   v2i64_a = v2i64_a + b;
195   v2i64_a = v2i64_a + a;
196 
197   v2i32_a = v2i32_a + d; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2i32' (vector of 2 'int' values)}}
198   v2i32_a = v2i32_a + c;
199   v2i32_a = v2i32_a + b;
200   v2i32_a = v2i32_a + a;
201 
202   v2i16_a = v2i16_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2i16' (vector of 2 'short' values) as implicit conversion would cause truncation}}
203   v2i16_a = v2i16_a + c; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2i16' (vector of 2 'short' values)}}
204   v2i16_a = v2i16_a + b;
205   v2i16_a = v2i16_a + a;
206 
207   v2i8_a = v2i8_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
208   v2i8_a = v2i8_a + c; // expected-error {{cannot convert between scalar type 'int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
209   v2i8_a = v2i8_a + b; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2i8' (vector of 2 'char' values)}}
210   v2i8_a = v2i8_a + a;
211 }
212 
intTestTypeUnsigned(unsigned char a,unsigned short b,unsigned int c,unsigned long long d)213 void intTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
214                          unsigned long long d) {
215   v2i64 v2i64_a = {1, 2};
216   v2i32 v2i32_a = {1, 2};
217   v2i16 v2i16_a = {1, 2};
218   v2i8 v2i8_a = {1, 2};
219 
220   v2i64_a = v2i64_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2i64' (vector of 2 'long long' values) as implicit conversion would cause truncation}}
221 
222   v2i64_a = v2i64_a + c;
223   v2i64_a = v2i64_a + b;
224   v2i64_a = v2i64_a + a;
225 
226   v2i32_a = v2i32_a + d; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2i32' (vector of 2 'int' values)}}
227   v2i32_a = v2i32_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v2i32' (vector of 2 'int' values) as implicit conversion would cause truncation}}
228   v2i32_a = v2i32_a + b;
229   v2i32_a = v2i32_a + a;
230 
231   v2i16_a = v2i16_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2i16' (vector of 2 'short' values) as implicit conversion would cause truncation}}
232   v2i16_a = v2i16_a + c; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2i16' (vector of 2 'short' values)}}
233   v2i16_a = v2i16_a + b; // expected-error {{cannot convert between scalar type 'unsigned short' and vector type 'v2i16' (vector of 2 'short' values) as implicit conversion would cause truncation}}
234   v2i16_a = v2i16_a + a;
235 
236   v2i8_a = v2i8_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
237   v2i8_a = v2i8_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
238   v2i8_a = v2i8_a + b; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2i8' (vector of 2 'char' values)}}
239   v2i8_a = v2i8_a + a; // expected-error {{cannot convert between scalar type 'unsigned char' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
240 }
241 
uintTestType(char a,short b,int c,long long d)242 void uintTestType(char a, short b, int c, long long d) {
243   v2u64 v2u64_a = {1, 2};
244   v2u32 v2u32_a = {1, 2};
245   v2u16 v2u16_a = {1, 2};
246   v2u8 v2u8_a = {1, 2};
247 
248   v2u64_a = v2u64_a + d; // expected-warning {{implicit conversion changes signedness: 'long long' to 'v2u64' (vector of 2 'unsigned long long' values)}}
249   v2u64_a = v2u64_a + c; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u64' (vector of 2 'unsigned long long' values)}}
250   v2u64_a = v2u64_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u64' (vector of 2 'unsigned long long' values)}}
251   v2u64_a = v2u64_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u64' (vector of 2 'unsigned long long' values)}}
252 
253   v2u32_a = v2u32_a + d; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2u32' (vector of 2 'unsigned int' values)}}
254   v2u32_a = v2u32_a + c; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u32' (vector of 2 'unsigned int' values)}}
255   v2u32_a = v2u32_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u32' (vector of 2 'unsigned int' values)}}
256   v2u32_a = v2u32_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u32' (vector of 2 'unsigned int' values)}}
257 
258   v2u16_a = v2u16_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2u16' (vector of 2 'unsigned short' values) as implicit conversion would cause truncation}}
259   v2u16_a = v2u16_a + c; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2u16' (vector of 2 'unsigned short' values)}}
260   v2u16_a = v2u16_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u16' (vector of 2 'unsigned short' values)}}
261   v2u16_a = v2u16_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u16' (vector of 2 'unsigned short' values)}}
262 
263   v2u8_a = v2u8_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
264   v2u8_a = v2u8_a + c; // expected-error {{cannot convert between scalar type 'int' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
265   v2u8_a = v2u8_a + b; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2u8' (vector of 2 'unsigned char' values)}}
266   v2u8_a = v2u8_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u8' (vector of 2 'unsigned char' values)}}
267 }
268 
uintTestTypeUnsigned(unsigned char a,unsigned short b,unsigned int c,unsigned long long d)269 void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
270                           unsigned long long d) {
271   v2u64 v2u64_a = {1, 2};
272   v2u32 v2u32_a = {1, 2};
273   v2u16 v2u16_a = {1, 2};
274   v2u8 v2u8_a = {1, 2};
275 
276   v2u64_a = v2u64_a + d;
277   v2u64_a = v2u64_a + c;
278   v2u64_a = v2u64_a + b;
279   v2u64_a = v2u64_a + a;
280 
281   v2u32_a = v2u32_a + d; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2u32' (vector of 2 'unsigned int' values)}}
282   v2u32_a = v2u32_a + c;
283   v2u32_a = v2u32_a + b;
284   v2u32_a = v2u32_a + a;
285 
286   v2u16_a = v2u16_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2u16' (vector of 2 'unsigned short' values) as implicit conversion would cause truncation}}
287   v2u16_a = v2u16_a + c; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2u16' (vector of 2 'unsigned short' values)}}
288   v2u16_a = v2u16_a + b;
289   v2u16_a = v2u16_a + a;
290 
291   v2u8_a = v2u8_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
292   v2u8_a = v2u8_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
293   v2u8_a = v2u8_a + b; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2u8' (vector of 2 'unsigned char' values)}}
294   v2u8_a = v2u8_a + a;
295 }
296 
uintTestConstant(v2u64 v2u64_a,v2u32 v2u32_a,v2u16 v2u16_a,v2u8 v2u8_a)297 void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a,
298                       v2u8 v2u8_a) {
299   v2u64_a = v2u64_a + 0xFFFFFFFFFFFFFFFF;
300   v2u32_a = v2u32_a + 0xFFFFFFFF;
301   v2u16_a = v2u16_a + 0xFFFF;
302   v2u8_a = v2u8_a + 0xFF;
303 
304   v2u32_a = v2u32_a + 0x1FFFFFFFF; // expected-warning {{implicit conversion from 'long' to 'v2u32' (vector of 2 'unsigned int' values) changes value from 8589934591 to 4294967295}}
305   v2u16_a = v2u16_a + 0x1FFFF;     // expected-warning {{implicit conversion from 'int' to 'v2u16' (vector of 2 'unsigned short' values) changes value from 131071 to 65535}}
306   v2u8_a = v2u8_a + 0x1FF;         // expected-error {{cannot convert between scalar type 'int' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
307 }
308 
intTestConstant(v2i64 v2i64_a,v2i32 v2i32_a,v2i16 v2i16_a,v2i8 v2i8_a)309 void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a) {
310   // Legal upper bounds.
311   v2i64_a = v2i64_a + (long long)0x7FFFFFFFFFFFFFFF;
312   v2i32_a = v2i32_a + (int)0x7FFFFFFF;
313   v2i16_a = v2i16_a + (short)0x7FFF;
314   v2i8_a = v2i8_a + (char)0x7F;
315 
316   // Legal lower bounds.
317   v2i64_a = v2i64_a + (-9223372036854775807);
318   v2i32_a = v2i32_a + (-2147483648);
319   v2i16_a = v2i16_a + (-32768);
320   v2i8_a = v2i8_a + (-128);
321 
322   // One increment/decrement more than the type can hold
323   v2i32_a = v2i32_a + 2147483648; // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from 2147483648 to -2147483648}}
324   v2i16_a = v2i16_a + 32768;      // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from 32768 to -32768}}
325   v2i8_a = v2i8_a + 128;          // expected-warning {{implicit conversion from 'int' to 'v2i8' (vector of 2 'char' values) changes value from 128 to -128}}
326 
327   v2i32_a = v2i32_a + (-2147483649); // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from -2147483649 to 2147483647}}
328   v2i16_a = v2i16_a + (-32769);      // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from -32769 to 32767}}
329   v2i8_a = v2i8_a + (-129);          // expected-error {{cannot convert between scalar type 'int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
330 }
331