354 lines · c
1// RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -Wno-unused-but-set-variable -triple x86_64-apple-darwin102 3// Test the compatibility of clang's vector extensions with gcc's vector4// extensions for C. Notably &&, ||, ?: and ! are not available.5typedef long long v2i64 __attribute__((vector_size(16)));6typedef int v2i32 __attribute__((vector_size(8)));7typedef short v2i16 __attribute__((vector_size(4)));8typedef char v2i8 __attribute__((vector_size(2)));9 10typedef unsigned long long v2u64 __attribute__((vector_size(16)));11typedef unsigned int v2u32 __attribute__((vector_size(8)));12typedef unsigned short v2u16 __attribute__((vector_size(4)));13typedef unsigned char v2u8 __attribute__((vector_size(2)));14 15typedef float v4f32 __attribute__((vector_size(16)));16typedef double v2f64 __attribute__((vector_size(16)));17typedef double v4f64 __attribute__((vector_size(32)));18typedef int v4i32 __attribute((vector_size(16)));19 20// Verify that we can use the [[]] spelling of the attribute.21// We intentionally use the same type alias name to check that both versions22// define the same type.23typedef long long v2i64 [[gnu::vector_size(16)]]; // expected-warning{{[[]] attributes are a C23 extension}}24typedef int v2i32 [[gnu::vector_size(8)]]; // expected-warning{{[[]] attributes are a C23 extension}}25 26// Check various positions where the [[]] spelling can or cannot be used.27[[gnu::vector_size(16)]] typedef long long v2i64; // expected-warning{{[[]] attributes are a C23 extension}}28typedef long long [[gnu::vector_size(16)]] v2i64_ignored;29 // expected-warning@-1{{'gnu::vector_size' attribute ignored}}30 // expected-warning@-2{{[[]] attributes are a C23 extension}}31// FIXME: Contrary to the error message that we emit, GCC does actually allow32// the attribute in the following position. Somewhat surprisingly, the attribute33// is applied not to the pointer but to the base type, i.e. this declaration has34// the same effect in GCC as the other declarations for `v2i64`.35typedef long long *[[gnu::vector_size(16)]] v2i64_doesnt_work;36 // expected-error@-1{{invalid vector element type 'long long *'}}37 // expected-warning@-2{{GCC does not allow the 'gnu::vector_size' attribute to be written on a type}}38 // expected-warning@-3{{[[]] attributes are a C23 extension}}39 40// Verify that we can use the attribute outside of a typedef.41static int v2i32_var [[gnu::vector_size(8)]]; // expected-warning{{[[]] attributes are a C23 extension}}42 43void arithmeticTest(void);44void logicTest(void);45void comparisonTest(void);46void floatTestSignedType(char a, short b, int c, long long d);47void floatTestUnsignedType(unsigned char a, unsigned short b, unsigned int c,48 unsigned long long d);49void floatTestConstant(void);50void intTestType(char a, short b, int c, long long d);51void intTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,52 unsigned long long d);53void uintTestType(char a, short b, int c, long long d);54void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,55 unsigned long long d);56void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a, v2u8 v2u8_a);57void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a);58 59void arithmeticTest(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 69 v2i64_r = 1 + v2i64_a;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 75 v2i64_a += 1;76 v2i64_a -= 1;77 v2i64_a *= 1;78 v2i64_a /= 1;79 v2i64_a %= 1;80}81 82void comparisonTest(void) {83 v2i64 v2i64_a = (v2i64){0, 1};84 v2i64 v2i64_r;85 86 v2i64_r = v2i64_a == 1;87 v2i64_r = v2i64_a != 1;88 v2i64_r = v2i64_a < 1;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 v2i64_r = 1 > v2i64_a;97 v2i64_r = 1 <= v2i64_a;98 v2i64_r = 1 >= v2i64_a;99}100 101void logicTest(void) {102 v2i64 v2i64_a = (v2i64){0, 1};103 v2i64 v2i64_b = (v2i64){2, 1};104 v2i64 v2i64_c = (v2i64){3, 1};105 v2i64 v2i64_r;106 107 v2i64_r = !v2i64_a; // expected-error {{invalid argument type 'v2i64' (vector of 2 'long long' values) to unary expression}}108 v2i64_r = ~v2i64_a;109 110 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}}111 112 v2i64_r = v2i64_a & 1;113 v2i64_r = v2i64_a | 1;114 v2i64_r = v2i64_a ^ 1;115 116 v2i64_r = 1 & v2i64_a;117 v2i64_r = 1 | v2i64_a;118 v2i64_r = 1 ^ v2i64_a;119 120 v2i64_a &= 1;121 v2i64_a |= 1;122 v2i64_a ^= 1;123 124 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++}}125 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++}}126 127 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++}}128 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++}}129 130 v2i64_r = v2i64_a << 1;131 v2i64_r = v2i64_a >> 1;132 133 v2i64_r = 1 << v2i64_a;134 v2i64_r = 1 >> v2i64_a;135 136 v2i64_a <<= 1;137 v2i64_a >>= 1;138}139 140// For operations with floating point types, we check that integer constants141// can be respresented, or failing that checking based on the integer types.142void floatTestConstant(void) {143 // Test that constants added to floats must be expressible as floating point144 // numbers.145 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};146 v4f32_a = v4f32_a + 1;147 v4f32_a = v4f32_a + 0xFFFFFF;148 v4f32_a = v4f32_a + (-1567563LL);149 v4f32_a = v4f32_a + (16777208);150 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}}151}152 153void floatTestConstantComparison(void);154void doubleTestConstantComparison(void);155 156void floatTestConstantComparison(void) {157 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};158 v4i32 v4i32_r;159 v4i32_r = v4f32_a > 0.4f;160 v4i32_r = v4f32_a >= 0.4f;161 v4i32_r = v4f32_a < 0.4f;162 v4i32_r = v4f32_a <= 0.4f;163 v4i32_r = v4f32_a == 0.4f; // expected-warning {{comparing floating point with == or != is unsafe}}164 v4i32_r = v4f32_a != 0.4f; // expected-warning {{comparing floating point with == or != is unsafe}}165}166 167void doubleTestConstantComparison(void) {168 v2f64 v2f64_a = {0.4, 0.4};169 v2i64 v2i64_r;170 v2i64_r = v2f64_a > 0.4;171 v2i64_r = v2f64_a >= 0.4;172 v2i64_r = v2f64_a < 0.4;173 v2i64_r = v2f64_a <= 0.4;174 v2i64_r = v2f64_a == 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}175 v2i64_r = v2f64_a != 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}176}177 178void floatTestUnsignedType(unsigned char a, unsigned short b, unsigned int c,179 unsigned long long d) {180 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};181 v4f64 v4f64_b = {0.4, 0.4, 0.4, 0.4};182 183 v4f32_a = v4f32_a + a;184 v4f32_a = v4f32_a + b;185 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}}186 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}}187 188 v4f64_b = v4f64_b + a;189 v4f64_b = v4f64_b + b;190 v4f64_b = v4f64_b + c;191 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}}192}193 194void floatTestSignedType(char a, short b, int c, long long d) {195 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};196 v4f64 v4f64_b = {0.4, 0.4, 0.4, 0.4};197 198 v4f32_a = v4f32_a + a;199 v4f32_a = v4f32_a + b;200 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}}201 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}}202 203 v4f64_b = v4f64_b + a;204 v4f64_b = v4f64_b + b;205 v4f64_b = v4f64_b + c;206 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}}207}208 209void intTestType(char a, short b, int c, long long d) {210 v2i64 v2i64_a = {1, 2};211 v2i32 v2i32_a = {1, 2};212 v2i16 v2i16_a = {1, 2};213 v2i8 v2i8_a = {1, 2};214 215 v2i64_a = v2i64_a + d;216 v2i64_a = v2i64_a + c;217 v2i64_a = v2i64_a + b;218 v2i64_a = v2i64_a + a;219 220 v2i32_a = v2i32_a + d; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2i32' (vector of 2 'int' values)}}221 v2i32_a = v2i32_a + c;222 v2i32_a = v2i32_a + b;223 v2i32_a = v2i32_a + a;224 225 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}}226 v2i16_a = v2i16_a + c; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2i16' (vector of 2 'short' values)}}227 v2i16_a = v2i16_a + b;228 v2i16_a = v2i16_a + a;229 230 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}}231 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}}232 v2i8_a = v2i8_a + b; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2i8' (vector of 2 'char' values)}}233 v2i8_a = v2i8_a + a;234}235 236void intTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,237 unsigned long long d) {238 v2i64 v2i64_a = {1, 2};239 v2i32 v2i32_a = {1, 2};240 v2i16 v2i16_a = {1, 2};241 v2i8 v2i8_a = {1, 2};242 243 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}}244 245 v2i64_a = v2i64_a + c;246 v2i64_a = v2i64_a + b;247 v2i64_a = v2i64_a + a;248 249 v2i32_a = v2i32_a + d; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2i32' (vector of 2 'int' values)}}250 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}}251 v2i32_a = v2i32_a + b;252 v2i32_a = v2i32_a + a;253 254 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}}255 v2i16_a = v2i16_a + c; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2i16' (vector of 2 'short' values)}}256 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}}257 v2i16_a = v2i16_a + a;258 259 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}}260 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}}261 v2i8_a = v2i8_a + b; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2i8' (vector of 2 'char' values)}}262 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}}263}264 265void uintTestType(char a, short b, int c, long long d) {266 v2u64 v2u64_a = {1, 2};267 v2u32 v2u32_a = {1, 2};268 v2u16 v2u16_a = {1, 2};269 v2u8 v2u8_a = {1, 2};270 271 v2u64_a = v2u64_a + d; // expected-warning {{implicit conversion changes signedness: 'long long' to 'v2u64' (vector of 2 'unsigned long long' values)}}272 v2u64_a = v2u64_a + c; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u64' (vector of 2 'unsigned long long' values)}}273 v2u64_a = v2u64_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u64' (vector of 2 'unsigned long long' values)}}274 v2u64_a = v2u64_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u64' (vector of 2 'unsigned long long' values)}}275 276 v2u32_a = v2u32_a + d; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2u32' (vector of 2 'unsigned int' values)}}277 v2u32_a = v2u32_a + c; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u32' (vector of 2 'unsigned int' values)}}278 v2u32_a = v2u32_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u32' (vector of 2 'unsigned int' values)}}279 v2u32_a = v2u32_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u32' (vector of 2 'unsigned int' values)}}280 281 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}}282 v2u16_a = v2u16_a + c; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2u16' (vector of 2 'unsigned short' values)}}283 v2u16_a = v2u16_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u16' (vector of 2 'unsigned short' values)}}284 v2u16_a = v2u16_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u16' (vector of 2 'unsigned short' values)}}285 286 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}}287 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}}288 v2u8_a = v2u8_a + b; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2u8' (vector of 2 'unsigned char' values)}}289 v2u8_a = v2u8_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u8' (vector of 2 'unsigned char' values)}}290}291 292void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,293 unsigned long long d) {294 v2u64 v2u64_a = {1, 2};295 v2u32 v2u32_a = {1, 2};296 v2u16 v2u16_a = {1, 2};297 v2u8 v2u8_a = {1, 2};298 299 v2u64_a = v2u64_a + d;300 v2u64_a = v2u64_a + c;301 v2u64_a = v2u64_a + b;302 v2u64_a = v2u64_a + a;303 304 v2u32_a = v2u32_a + d; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2u32' (vector of 2 'unsigned int' values)}}305 v2u32_a = v2u32_a + c;306 v2u32_a = v2u32_a + b;307 v2u32_a = v2u32_a + a;308 309 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}}310 v2u16_a = v2u16_a + c; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2u16' (vector of 2 'unsigned short' values)}}311 v2u16_a = v2u16_a + b;312 v2u16_a = v2u16_a + a;313 314 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}}315 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}}316 v2u8_a = v2u8_a + b; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2u8' (vector of 2 'unsigned char' values)}}317 v2u8_a = v2u8_a + a;318}319 320void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a,321 v2u8 v2u8_a) {322 v2u64_a = v2u64_a + 0xFFFFFFFFFFFFFFFF;323 v2u32_a = v2u32_a + 0xFFFFFFFF;324 v2u16_a = v2u16_a + 0xFFFF;325 v2u8_a = v2u8_a + 0xFF;326 327 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}}328 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}}329 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}}330}331 332void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a) {333 // Legal upper bounds.334 v2i64_a = v2i64_a + (long long)0x7FFFFFFFFFFFFFFF;335 v2i32_a = v2i32_a + (int)0x7FFFFFFF;336 v2i16_a = v2i16_a + (short)0x7FFF;337 v2i8_a = v2i8_a + (char)0x7F;338 339 // Legal lower bounds.340 v2i64_a = v2i64_a + (-9223372036854775807);341 v2i32_a = v2i32_a + (-2147483648);342 v2i16_a = v2i16_a + (-32768);343 v2i8_a = v2i8_a + (-128);344 345 // One increment/decrement more than the type can hold346 v2i32_a = v2i32_a + 2147483648; // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from 2147483648 to -2147483648}}347 v2i16_a = v2i16_a + 32768; // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from 32768 to -32768}}348 v2i8_a = v2i8_a + 128; // expected-warning {{implicit conversion from 'int' to 'v2i8' (vector of 2 'char' values) changes value from 128 to -128}}349 350 v2i32_a = v2i32_a + (-2147483649); // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from -2147483649 to 2147483647}}351 v2i16_a = v2i16_a + (-32769); // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from -32769 to 32767}}352 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}}353}354