Daily bump.
[official-gcc.git] / gcc / testsuite / g++.dg / cpp23 / attr-assume1.C
blob76b61e91f18b56350df4eeca3facb099468ddf36
1 // P1774R8 - Portable assumptions
2 // { dg-do run { target c++11 } }
4 namespace std
6   constexpr bool
7   isfinite (float x)
8   { return __builtin_isfinite (x); }
10   constexpr bool
11   isfinite (double x)
12   { return __builtin_isfinite (x); }
14   constexpr bool
15   isfinite (long double x)
16   { return __builtin_isfinite (x); }
18   constexpr float
19   sqrt (float x)
20   { return __builtin_sqrtf (x); }
22   constexpr double
23   sqrt (double x)
24   { return __builtin_sqrt (x); }
26   constexpr long double
27   sqrt (long double x)
28   { return __builtin_sqrtl (x); }
30   extern "C" void
31   abort ();
34 constexpr int
35 f1 (int i)
37 #if __cpp_constexpr >= 201603L
38   auto f = [=] { [[assume (i == 0)]]; };
39   return sizeof (f);
40 #else
41   return sizeof (int);
42 #endif
45 void
46 f2 ()
48   static_assert (f1 (0) >= sizeof (int), "");
51 int
52 f3 (int i)
54   [[assume (i == 42)]];
55   return i;
58 int
59 f4 (int i)
61   [[assume (++i == 44)]];
62   return i;
65 int a;
66 int *volatile c;
68 bool
69 f5 ()
71   ++a;
72   return true;
75 constexpr int
76 f6 ()
78 #if __cpp_constexpr >= 201304L
79   [[assume (f5 ())]];
80 #endif
81   return 1;
84 template <int ...args>
85 bool
86 f7 ()
88 #if __cpp_fold_expressions >= 201411L
89   [[assume (((args >= 0) && ...))]];
90   return ((args >= 0) && ...);
91 #else
92   return true;
93 #endif
96 bool
97 f8 (double x)
99   [[assume (std::isfinite (x) && x >= 0.0)]];
100   return std::isfinite (std::sqrt (x));
103 double
104 f9 (double x)
106   [[assume (std::isfinite (std::sqrt (x)))]];
107   return std::sqrt (x);
110 template <typename T, T N>
112 f10 (T x)
114   [[assume (x == N)]];
115   return x;
119 f11 (int x)
121   [[assume (x == 93 ? true : throw 1)]];
122   return x;
125 constexpr int
126 f12 (int x)
128 #if __cpp_constexpr >= 201304L
129   [[assume (++x == 43)]];
130 #endif
131   return x;
134 static_assert (f12 (42) == 42, "");
136 struct S
138   operator bool () { return true; }
142 f13 ()
144   S s;
145   [[assume (s)]];
146   return 0;
149 template <typename T>
151 f14 ()
153   T t;
154   [[assume (t)]];
155   return 0;
159 main ()
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 ();