Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / AST / Interp / intap.cpp
blob02a860eb0986c159e5d6c218dd35da4146a14803
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++11 -verify %s
2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++20 -verify %s
3 // RUN: %clang_cc1 -std=c++11 -fms-extensions -verify=ref %s
4 // RUN: %clang_cc1 -std=c++20 -fms-extensions -verify=ref %s
7 using MaxBitInt = _BitInt(128);
9 constexpr _BitInt(2) A = 0;
10 constexpr _BitInt(2) B = A + 1;
11 constexpr _BitInt(2) C = B + 1; // expected-warning {{from 2 to -2}} \
12 // ref-warning {{from 2 to -2}}
13 static_assert(C == -2, "");
16 constexpr MaxBitInt A_ = 0;
17 constexpr MaxBitInt B_ = A_ + 1;
18 static_assert(B_ == 1, "");
20 constexpr MaxBitInt BitIntZero{};
21 static_assert(BitIntZero == 0, "");
22 constexpr unsigned _BitInt(128) UBitIntZero{};
23 static_assert(UBitIntZero == 0, "");
25 constexpr _BitInt(2) BitIntZero2{};
26 static_assert(BitIntZero2 == 0, "");
27 constexpr unsigned _BitInt(1) UBitIntZero1{};
28 static_assert(UBitIntZero1 == 0, "");
30 constexpr unsigned _BitInt(2) BI1 = 3u;
31 static_assert(BI1 == 3, "");
34 #ifdef __SIZEOF_INT128__
35 namespace i128 {
36 typedef __int128 int128_t;
37 typedef unsigned __int128 uint128_t;
38 constexpr int128_t I128_1 = 12;
39 static_assert(I128_1 == 12, "");
40 static_assert(I128_1 != 10, "");
41 static_assert(I128_1 != 12, ""); // expected-error{{failed}} \
42 // ref-error{{failed}} \
43 // expected-note{{evaluates to}} \
44 // ref-note{{evaluates to}}
46 static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
47 static_assert(UINT128_MAX == -1, "");
49 static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
50 static_assert(INT128_MAX != 0, "");
51 static const __int128_t INT128_MIN = -INT128_MAX - 1;
52 constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \
53 // expected-note {{outside the range}} \
54 // ref-error {{must be initialized by a constant expression}} \
55 // ref-note {{outside the range}}
56 constexpr int128_t Two = (int128_t)1 << 1ul;
57 static_assert(Two == 2, "");
58 static_assert(Two, "");
59 constexpr bool CastedToBool = Two;
60 static_assert(CastedToBool, "");
62 constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
63 static_assert(AllOnes == UINT128_MAX, "");
65 constexpr uint128_t i128Zero{};
66 static_assert(i128Zero == 0, "");
67 constexpr uint128_t ui128Zero{};
68 static_assert(ui128Zero == 0, "");
70 #if __cplusplus >= 201402L
71 template <typename T>
72 constexpr T CastFrom(__int128_t A) {
73 T B = (T)A;
74 return B;
76 static_assert(CastFrom<char>(12) == 12, "");
77 static_assert(CastFrom<unsigned char>(12) == 12, "");
78 static_assert(CastFrom<long>(12) == 12, "");
79 static_assert(CastFrom<unsigned short>(12) == 12, "");
80 static_assert(CastFrom<int128_t>(12) == 12, "");
81 static_assert(CastFrom<float>(12) == 12, "");
82 static_assert(CastFrom<double>(12) == 12, "");
83 static_assert(CastFrom<long double>(12) == 12, "");
85 static_assert(CastFrom<char>(AllOnes) == -1, "");
86 static_assert(CastFrom<unsigned char>(AllOnes) == 0xFF, "");
87 static_assert(CastFrom<long>(AllOnes) == -1, "");
88 static_assert(CastFrom<unsigned short>(AllOnes) == 0xFFFF, "");
89 static_assert(CastFrom<int>(AllOnes) == -1, "");
90 static_assert(CastFrom<int128_t>(AllOnes) == -1, "");
91 static_assert(CastFrom<uint128_t>(AllOnes) == AllOnes, "");
93 template <typename T>
94 constexpr __int128 CastTo(T A) {
95 int128_t B = (int128_t)A;
96 return B;
98 static_assert(CastTo<char>(12) == 12, "");
99 static_assert(CastTo<unsigned char>(12) == 12, "");
100 static_assert(CastTo<long>(12) == 12, "");
101 static_assert(CastTo<unsigned long long>(12) == 12, "");
102 static_assert(CastTo<float>(12) == 12, "");
103 static_assert(CastTo<double>(12) == 12, "");
104 static_assert(CastTo<long double>(12) == 12, "");
105 #endif
107 constexpr int128_t Error = __LDBL_MAX__; // ref-warning {{implicit conversion of out of range value}} \
108 // ref-error {{must be initialized by a constant expression}} \
109 // ref-note {{is outside the range of representable values of type}} \
110 // expected-warning {{implicit conversion of out of range value}} \
111 // expected-error {{must be initialized by a constant expression}} \
112 // expected-note {{is outside the range of representable values of type}}
115 namespace AddSubOffset {
116 constexpr __int128 A = 1;
117 constexpr int arr[] = {1,2,3};
118 constexpr const int *P = arr + A;
119 static_assert(*P == 2, "");
120 constexpr const int *P2 = P - A;
121 static_assert(*P2 == 1,"");
124 #endif