Debian Codegraph @

clang/test/AST/Interp/vectors.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
2 // RUN: %clang_cc1 -verify=ref,both %s
3
4 typedef int __attribute__((vector_size(16))) VI4;
5 constexpr VI4 A = {1,2,3,4};
6 static_assert(A[0] == 1, ""); // ref-error {{not an integral constant expression}}
7 static_assert(A[1] == 2, ""); // ref-error {{not an integral constant expression}}
8 static_assert(A[2] == 3, ""); // ref-error {{not an integral constant expression}}
9 static_assert(A[3] == 4, ""); // ref-error {{not an integral constant expression}}
10
11
12 /// FIXME: It would be nice if the note said 'vector' instead of 'array'.
13 static_assert(A[12] == 4, ""); // ref-error {{not an integral constant expression}} \
14 // expected-error {{not an integral constant expression}} \
15 // expected-note {{cannot refer to element 12 of array of 4 elements in a constant expression}}
16
17
18 /// VectorSplat casts
19 typedef __attribute__(( ext_vector_type(4) )) float float4;
20 constexpr float4 vec4_0 = (float4)0.5f;
21 static_assert(vec4_0[0] == 0.5, ""); // ref-error {{not an integral constant expression}}
22 static_assert(vec4_0[1] == 0.5, ""); // ref-error {{not an integral constant expression}}
23 static_assert(vec4_0[2] == 0.5, ""); // ref-error {{not an integral constant expression}}
24 static_assert(vec4_0[3] == 0.5, ""); // ref-error {{not an integral constant expression}}
25 constexpr int vec4_0_discarded = ((float4)12.0f, 0);
26
27
28 /// ImplicitValueInitExpr of vector type
29 constexpr float4 arr4[2] = {
30 {1,2,3,4},
31 };
32 static_assert(arr4[0][0] == 1, ""); // ref-error {{not an integral constant expression}}
33 static_assert(arr4[0][1] == 2, ""); // ref-error {{not an integral constant expression}}
34 static_assert(arr4[0][2] == 3, ""); // ref-error {{not an integral constant expression}}
35 static_assert(arr4[0][3] == 4, ""); // ref-error {{not an integral constant expression}}
36 static_assert(arr4[1][0] == 0, ""); // ref-error {{not an integral constant expression}}
37 static_assert(arr4[1][0] == 0, ""); // ref-error {{not an integral constant expression}}
38 static_assert(arr4[1][0] == 0, ""); // ref-error {{not an integral constant expression}}
39 static_assert(arr4[1][0] == 0, ""); // ref-error {{not an integral constant expression}}
40
41
42 /// From constant-expression-cxx11.cpp
43 namespace Vector {
44 typedef int __attribute__((vector_size(16))) VI4;
45 constexpr VI4 f(int n) {
46 return VI4 { n * 3, n + 4, n - 5, n / 6 };
47 }
48 constexpr auto v1 = f(10);
49 static_assert(__builtin_vectorelements(v1) == (16 / sizeof(int)), "");
50
51 typedef double __attribute__((vector_size(32))) VD4;
52 constexpr VD4 g(int n) {
53 return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 };
54 }
55 constexpr auto v2 = g(4);
56 static_assert(__builtin_vectorelements(v2) == (32 / sizeof(double)), "");
57 }
58
59 /// FIXME: We need to support BitCasts between vector types.
60 namespace {
61 typedef float __attribute__((vector_size(16))) VI42;
62 constexpr VI42 A2 = A; // expected-error {{must be initialized by a constant expression}}
63 }
64
65 namespace BoolToSignedIntegralCast{
66 typedef __attribute__((__ext_vector_type__(4))) unsigned int int4;
67 constexpr int4 intsT = (int4)true;
68 static_assert(intsT[0] == -1, "");// ref-error {{not an integral constant expression}}
69 static_assert(intsT[1] == -1, "");// ref-error {{not an integral constant expression}}
70 static_assert(intsT[2] == -1, "");// ref-error {{not an integral constant expression}}
71 static_assert(intsT[3] == -1, "");// ref-error {{not an integral constant expression}}
72 }
73
74 namespace VectorElementExpr {
75 typedef int int2 __attribute__((ext_vector_type(2)));
76 typedef int int4 __attribute__((ext_vector_type(4)));
77 constexpr int oneElt = int4(3).x;
78 static_assert(oneElt == 3);
79
80 constexpr int2 twoElts = ((int4){11, 22, 33, 44}).yz;
81 static_assert(twoElts.x == 22, ""); // ref-error {{not an integral constant expression}}
82 static_assert(twoElts.y == 33, ""); // ref-error {{not an integral constant expression}}
83 }
84
85 namespace Temporaries {
86 typedef __attribute__((vector_size(16))) int vi4a;
87 typedef __attribute__((ext_vector_type(4))) int vi4b;
88 struct S {
89 vi4a v;
90 vi4b w;
91 };
92 int &&s = S().w[1];
93 }