Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/attr-assume1.C

raw
1 // P1774R8 - Portable assumptions
2 // { dg-do run { target c++11 } }
3
4 namespace std
5 {
6 constexpr bool
7 isfinite (float x)
8 { return __builtin_isfinite (x); }
9
10 constexpr bool
11 isfinite (double x)
12 { return __builtin_isfinite (x); }
13
14 constexpr bool
15 isfinite (long double x)
16 { return __builtin_isfinite (x); }
17
18 constexpr float
19 sqrt (float x)
20 { return __builtin_sqrtf (x); }
21
22 constexpr double
23 sqrt (double x)
24 { return __builtin_sqrt (x); }
25
26 constexpr long double
27 sqrt (long double x)
28 { return __builtin_sqrtl (x); }
29
30 extern "C" void
31 abort ();
32 }
33
34 constexpr int
35 f1 (int i)
36 {
37 #if __cpp_constexpr >= 201603L
38 auto f = [=] { [[assume (i == 0)]]; };
39 return sizeof (f);
40 #else
41 return sizeof (int);
42 #endif
43 }
44
45 void
46 f2 ()
47 {
48 static_assert (f1 (0) >= sizeof (int), "");
49 }
50
51 int
52 f3 (int i)
53 {
54 [[assume (i == 42)]];
55 return i;
56 }
57
58 int
59 f4 (int i)
60 {
61 [[assume (++i == 44)]];
62 return i;
63 }
64
65 int a;
66 int *volatile c;
67
68 bool
69 f5 ()
70 {
71 ++a;
72 return true;
73 }
74
75 constexpr int
76 f6 ()
77 {
78 #if __cpp_constexpr >= 201304L
79 [[assume (f5 ())]];
80 #endif
81 return 1;
82 }
83
84 template <int ...args>
85 bool
86 f7 ()
87 {
88 #if __cpp_fold_expressions >= 201411L
89 [[assume (((args >= 0) && ...))]];
90 return ((args >= 0) && ...);
91 #else
92 return true;
93 #endif
94 }
95
96 bool
97 f8 (double x)
98 {
99 [[assume (std::isfinite (x) && x >= 0.0)]];
100 return std::isfinite (std::sqrt (x));
101 }
102
103 double
104 f9 (double x)
105 {
106 [[assume (std::isfinite (std::sqrt (x)))]];
107 return std::sqrt (x);
108 }
109
110 template <typename T, T N>
111 T
112 f10 (T x)
113 {
114 [[assume (x == N)]];
115 return x;
116 }
117
118 int
119 f11 (int x)
120 {
121 [[assume (x == 93 ? true : throw 1)]];
122 return x;
123 }
124
125 constexpr int
126 f12 (int x)
127 {
128 #if __cpp_constexpr >= 201304L
129 [[assume (++x == 43)]];
130 #endif
131 return x;
132 }
133
134 static_assert (f12 (42) == 42, "");
135
136 struct S
137 {
138 operator bool () { return true; }
139 };
140
141 int
142 f13 ()
143 {
144 S s;
145 [[assume (s)]];
146 return 0;
147 }
148
149 template <typename T>
150 int
151 f14 ()
152 {
153 T t;
154 [[assume (t)]];
155 return 0;
156 }
157
158 int
159 main ()
160 {
161 int b = 42;
162 double d = 42.0, e = 43.0;
163 c = &b;
164 [[assume (f5 ())]];
165 if (a)
166 std::abort ();
167 [[assume (++b == 43)]];
168 if (b != 42 || *c != 42)
169 std::abort ();
170 static_assert (f6 () == 1, "");
171 if (f6 () != 1)
172 std::abort ();
173 if (a)
174 std::abort ();
175 if (!f7 <0> () || !f7 <1, 2, 3, 4> ())
176 std::abort ();
177 [[assume (d < e)]];
178 if (f10 <int, 45> (45) != 45
179 || f10 <long long, 128LL> (128LL) != 128LL
180 #if __cpp_nontype_template_args >= 201911L
181 || f10 <long double, -42.0L> (-42.0L) != -42.0L
182 #endif
183 || false)
184 std::abort ();
185 int i = 90, j = 91, k = 92;
186 [[assume (i == 90), assume (j <= 91)]] [[assume (k >= 92)]];
187 if (f11 (93) != 93)
188 std::abort ();
189 if (f14 <S> () != 0)
190 std::abort ();
191 }