1 // RUN: %clang_cc1 %s -verify -fsyntax-only -fdouble-square-bracket-attributes -Weverything -Wno-unused-but-set-variable -triple x86_64-apple-darwin10
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)));
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)));
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)));
20 // Verify that we can use the [[]] spelling of the attribute.
21 // We intentionally use the same type alias name to check that both versions
22 // define the same type.
23 // FIXME: Warnings are nuisance warnings due to the `-Weverything` flag, but
24 // we shouldn't really be emitting them in C mode with the
25 // `-fdouble-square-bracket-attributes` flag.
26 typedef long long v2i64
[[gnu::vector_size(16)]]; // expected-warning{{C++11 attribute syntax is incompatible with C++98}}
27 typedef int v2i32
[[gnu::vector_size(8)]]; // expected-warning{{C++11 attribute syntax is incompatible with C++98}}
29 // Check various positions where the [[]] spelling can or cannot be used.
30 [[gnu::vector_size(16)]] typedef long long v2i64
; // expected-warning{{C++11 attribute syntax is incompatible with C++98}}
31 typedef long long [[gnu::vector_size(16)]] v2i64_ignored
;
32 // expected-warning@-1{{'vector_size' attribute ignored}}
33 // expected-warning@-2{{C++11 attribute syntax is incompatible with C++98}}
34 // FIXME: Contrary to the error message that we emit, GCC does actually allow
35 // the attribute in the following position. Somewhat surprisingly, the attribute
36 // is applied not to the pointer but to the base type, i.e. this declaration has
37 // the same effect in GCC as the other declarations for `v2i64`.
38 typedef long long *[[gnu::vector_size(16)]] v2i64_doesnt_work
;
39 // expected-error@-1{{invalid vector element type 'long long *'}}
40 // expected-warning@-2{{GCC does not allow the 'vector_size' attribute to be written on a type}}
41 // expected-warning@-3{{C++11 attribute syntax is incompatible with C++98}}
43 // Verify that we can use the attribute outside of a typedef.
44 static int v2i32_var
[[gnu::vector_size(8)]]; // expected-warning{{C++11 attribute syntax is incompatible with C++98}}
46 void arithmeticTest(void);
48 void comparisonTest(void);
49 void floatTestSignedType(char a
, short b
, int c
, long long d
);
50 void floatTestUnsignedType(unsigned char a
, unsigned short b
, unsigned int c
,
51 unsigned long long d
);
52 void floatTestConstant(void);
53 void intTestType(char a
, short b
, int c
, long long d
);
54 void intTestTypeUnsigned(unsigned char a
, unsigned short b
, unsigned int c
,
55 unsigned long long d
);
56 void uintTestType(char a
, short b
, int c
, long long d
);
57 void uintTestTypeUnsigned(unsigned char a
, unsigned short b
, unsigned int c
,
58 unsigned long long d
);
59 void uintTestConstant(v2u64 v2u64_a
, v2u32 v2u32_a
, v2u16 v2u16_a
, v2u8 v2u8_a
);
60 void intTestConstant(v2i64 v2i64_a
, v2i32 v2i32_a
, v2i16 v2i16_a
, v2i8 v2i8_a
);
62 void arithmeticTest(void) {
63 v2i64 v2i64_a
= (v2i64
){0, 1};
66 v2i64_r
= v2i64_a
+ 1;
67 v2i64_r
= v2i64_a
- 1;
68 v2i64_r
= v2i64_a
* 1;
69 v2i64_r
= v2i64_a
/ 1;
70 v2i64_r
= v2i64_a
% 1;
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 v2i64_r
= 1 % v2i64_a
;
85 void comparisonTest(void) {
86 v2i64 v2i64_a
= (v2i64
){0, 1};
89 v2i64_r
= v2i64_a
== 1;
90 v2i64_r
= v2i64_a
!= 1;
91 v2i64_r
= v2i64_a
< 1;
92 v2i64_r
= v2i64_a
> 1;
93 v2i64_r
= v2i64_a
<= 1;
94 v2i64_r
= v2i64_a
>= 1;
96 v2i64_r
= 1 == v2i64_a
;
97 v2i64_r
= 1 != v2i64_a
;
98 v2i64_r
= 1 < v2i64_a
;
99 v2i64_r
= 1 > v2i64_a
;
100 v2i64_r
= 1 <= v2i64_a
;
101 v2i64_r
= 1 >= v2i64_a
;
104 void logicTest(void) {
105 v2i64 v2i64_a
= (v2i64
){0, 1};
106 v2i64 v2i64_b
= (v2i64
){2, 1};
107 v2i64 v2i64_c
= (v2i64
){3, 1};
110 v2i64_r
= !v2i64_a
; // expected-error {{invalid argument type 'v2i64' (vector of 2 'long long' values) to unary expression}}
113 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}}
115 v2i64_r
= v2i64_a
& 1;
116 v2i64_r
= v2i64_a
| 1;
117 v2i64_r
= v2i64_a
^ 1;
119 v2i64_r
= 1 & v2i64_a
;
120 v2i64_r
= 1 | v2i64_a
;
121 v2i64_r
= 1 ^ v2i64_a
;
127 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++}}
128 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++}}
130 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++}}
131 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++}}
133 v2i64_r
= v2i64_a
<< 1;
134 v2i64_r
= v2i64_a
>> 1;
136 v2i64_r
= 1 << v2i64_a
;
137 v2i64_r
= 1 >> v2i64_a
;
143 // For operations with floating point types, we check that integer constants
144 // can be respresented, or failing that checking based on the integer types.
145 void floatTestConstant(void) {
146 // Test that constants added to floats must be expressible as floating point
148 v4f32 v4f32_a
= {0.4f
, 0.4f
, 0.4f
, 0.4f
};
149 v4f32_a
= v4f32_a
+ 1;
150 v4f32_a
= v4f32_a
+ 0xFFFFFF;
151 v4f32_a
= v4f32_a
+ (-1567563LL);
152 v4f32_a
= v4f32_a
+ (16777208);
153 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}}
156 void floatTestConstantComparison(void);
157 void doubleTestConstantComparison(void);
159 void floatTestConstantComparison(void) {
160 v4f32 v4f32_a
= {0.4f
, 0.4f
, 0.4f
, 0.4f
};
162 v4i32_r
= v4f32_a
> 0.4f
;
163 v4i32_r
= v4f32_a
>= 0.4f
;
164 v4i32_r
= v4f32_a
< 0.4f
;
165 v4i32_r
= v4f32_a
<= 0.4f
;
166 v4i32_r
= v4f32_a
== 0.4f
; // expected-warning {{comparing floating point with == or != is unsafe}}
167 v4i32_r
= v4f32_a
!= 0.4f
; // expected-warning {{comparing floating point with == or != is unsafe}}
170 void doubleTestConstantComparison(void) {
171 v2f64 v2f64_a
= {0.4, 0.4};
173 v2i64_r
= v2f64_a
> 0.4;
174 v2i64_r
= v2f64_a
>= 0.4;
175 v2i64_r
= v2f64_a
< 0.4;
176 v2i64_r
= v2f64_a
<= 0.4;
177 v2i64_r
= v2f64_a
== 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}
178 v2i64_r
= v2f64_a
!= 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}
181 void floatTestUnsignedType(unsigned char a
, unsigned short b
, unsigned int c
,
182 unsigned long long d
) {
183 v4f32 v4f32_a
= {0.4f
, 0.4f
, 0.4f
, 0.4f
};
184 v4f64 v4f64_b
= {0.4, 0.4, 0.4, 0.4};
186 v4f32_a
= v4f32_a
+ a
;
187 v4f32_a
= v4f32_a
+ b
;
188 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}}
189 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}}
191 v4f64_b
= v4f64_b
+ a
;
192 v4f64_b
= v4f64_b
+ b
;
193 v4f64_b
= v4f64_b
+ c
;
194 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}}
197 void floatTestSignedType(char a
, short b
, int c
, long long d
) {
198 v4f32 v4f32_a
= {0.4f
, 0.4f
, 0.4f
, 0.4f
};
199 v4f64 v4f64_b
= {0.4, 0.4, 0.4, 0.4};
201 v4f32_a
= v4f32_a
+ a
;
202 v4f32_a
= v4f32_a
+ b
;
203 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}}
204 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}}
206 v4f64_b
= v4f64_b
+ a
;
207 v4f64_b
= v4f64_b
+ b
;
208 v4f64_b
= v4f64_b
+ c
;
209 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}}
212 void intTestType(char a
, short b
, int c
, long long d
) {
213 v2i64 v2i64_a
= {1, 2};
214 v2i32 v2i32_a
= {1, 2};
215 v2i16 v2i16_a
= {1, 2};
216 v2i8 v2i8_a
= {1, 2};
218 v2i64_a
= v2i64_a
+ d
;
219 v2i64_a
= v2i64_a
+ c
;
220 v2i64_a
= v2i64_a
+ b
;
221 v2i64_a
= v2i64_a
+ a
;
223 v2i32_a
= v2i32_a
+ d
; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2i32' (vector of 2 'int' values)}}
224 v2i32_a
= v2i32_a
+ c
;
225 v2i32_a
= v2i32_a
+ b
;
226 v2i32_a
= v2i32_a
+ a
;
228 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}}
229 v2i16_a
= v2i16_a
+ c
; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2i16' (vector of 2 'short' values)}}
230 v2i16_a
= v2i16_a
+ b
;
231 v2i16_a
= v2i16_a
+ a
;
233 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}}
234 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}}
235 v2i8_a
= v2i8_a
+ b
; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2i8' (vector of 2 'char' values)}}
239 void intTestTypeUnsigned(unsigned char a
, unsigned short b
, unsigned int c
,
240 unsigned long long d
) {
241 v2i64 v2i64_a
= {1, 2};
242 v2i32 v2i32_a
= {1, 2};
243 v2i16 v2i16_a
= {1, 2};
244 v2i8 v2i8_a
= {1, 2};
246 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}}
248 v2i64_a
= v2i64_a
+ c
;
249 v2i64_a
= v2i64_a
+ b
;
250 v2i64_a
= v2i64_a
+ a
;
252 v2i32_a
= v2i32_a
+ d
; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2i32' (vector of 2 'int' values)}}
253 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}}
254 v2i32_a
= v2i32_a
+ b
;
255 v2i32_a
= v2i32_a
+ a
;
257 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}}
258 v2i16_a
= v2i16_a
+ c
; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2i16' (vector of 2 'short' values)}}
259 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}}
260 v2i16_a
= v2i16_a
+ a
;
262 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}}
263 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}}
264 v2i8_a
= v2i8_a
+ b
; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2i8' (vector of 2 'char' values)}}
265 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}}
268 void uintTestType(char a
, short b
, int c
, long long d
) {
269 v2u64 v2u64_a
= {1, 2};
270 v2u32 v2u32_a
= {1, 2};
271 v2u16 v2u16_a
= {1, 2};
272 v2u8 v2u8_a
= {1, 2};
274 v2u64_a
= v2u64_a
+ d
; // expected-warning {{implicit conversion changes signedness: 'long long' to 'v2u64' (vector of 2 'unsigned long long' values)}}
275 v2u64_a
= v2u64_a
+ c
; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u64' (vector of 2 'unsigned long long' values)}}
276 v2u64_a
= v2u64_a
+ b
; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u64' (vector of 2 'unsigned long long' values)}}
277 v2u64_a
= v2u64_a
+ a
; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u64' (vector of 2 'unsigned long long' values)}}
279 v2u32_a
= v2u32_a
+ d
; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2u32' (vector of 2 'unsigned int' values)}}
280 v2u32_a
= v2u32_a
+ c
; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u32' (vector of 2 'unsigned int' values)}}
281 v2u32_a
= v2u32_a
+ b
; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u32' (vector of 2 'unsigned int' values)}}
282 v2u32_a
= v2u32_a
+ a
; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u32' (vector of 2 'unsigned int' values)}}
284 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}}
285 v2u16_a
= v2u16_a
+ c
; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2u16' (vector of 2 'unsigned short' values)}}
286 v2u16_a
= v2u16_a
+ b
; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u16' (vector of 2 'unsigned short' values)}}
287 v2u16_a
= v2u16_a
+ a
; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u16' (vector of 2 'unsigned short' values)}}
289 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}}
290 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}}
291 v2u8_a
= v2u8_a
+ b
; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2u8' (vector of 2 'unsigned char' values)}}
292 v2u8_a
= v2u8_a
+ a
; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u8' (vector of 2 'unsigned char' values)}}
295 void uintTestTypeUnsigned(unsigned char a
, unsigned short b
, unsigned int c
,
296 unsigned long long d
) {
297 v2u64 v2u64_a
= {1, 2};
298 v2u32 v2u32_a
= {1, 2};
299 v2u16 v2u16_a
= {1, 2};
300 v2u8 v2u8_a
= {1, 2};
302 v2u64_a
= v2u64_a
+ d
;
303 v2u64_a
= v2u64_a
+ c
;
304 v2u64_a
= v2u64_a
+ b
;
305 v2u64_a
= v2u64_a
+ a
;
307 v2u32_a
= v2u32_a
+ d
; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2u32' (vector of 2 'unsigned int' values)}}
308 v2u32_a
= v2u32_a
+ c
;
309 v2u32_a
= v2u32_a
+ b
;
310 v2u32_a
= v2u32_a
+ a
;
312 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}}
313 v2u16_a
= v2u16_a
+ c
; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2u16' (vector of 2 'unsigned short' values)}}
314 v2u16_a
= v2u16_a
+ b
;
315 v2u16_a
= v2u16_a
+ a
;
317 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}}
318 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}}
319 v2u8_a
= v2u8_a
+ b
; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2u8' (vector of 2 'unsigned char' values)}}
323 void uintTestConstant(v2u64 v2u64_a
, v2u32 v2u32_a
, v2u16 v2u16_a
,
325 v2u64_a
= v2u64_a
+ 0xFFFFFFFFFFFFFFFF;
326 v2u32_a
= v2u32_a
+ 0xFFFFFFFF;
327 v2u16_a
= v2u16_a
+ 0xFFFF;
328 v2u8_a
= v2u8_a
+ 0xFF;
330 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}}
331 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}}
332 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}}
335 void intTestConstant(v2i64 v2i64_a
, v2i32 v2i32_a
, v2i16 v2i16_a
, v2i8 v2i8_a
) {
336 // Legal upper bounds.
337 v2i64_a
= v2i64_a
+ (long long)0x7FFFFFFFFFFFFFFF;
338 v2i32_a
= v2i32_a
+ (int)0x7FFFFFFF;
339 v2i16_a
= v2i16_a
+ (short)0x7FFF;
340 v2i8_a
= v2i8_a
+ (char)0x7F;
342 // Legal lower bounds.
343 v2i64_a
= v2i64_a
+ (-9223372036854775807);
344 v2i32_a
= v2i32_a
+ (-2147483648);
345 v2i16_a
= v2i16_a
+ (-32768);
346 v2i8_a
= v2i8_a
+ (-128);
348 // One increment/decrement more than the type can hold
349 v2i32_a
= v2i32_a
+ 2147483648; // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from 2147483648 to -2147483648}}
350 v2i16_a
= v2i16_a
+ 32768; // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from 32768 to -32768}}
351 v2i8_a
= v2i8_a
+ 128; // expected-warning {{implicit conversion from 'int' to 'v2i8' (vector of 2 'char' values) changes value from 128 to -128}}
353 v2i32_a
= v2i32_a
+ (-2147483649); // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from -2147483649 to 2147483647}}
354 v2i16_a
= v2i16_a
+ (-32769); // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from -32769 to 32767}}
355 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}}