]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/run/int.rs
Rollup merge of #99192 - Amanieu:fix-asm-srcloc, r=petrochenkov
[rust.git] / compiler / rustc_codegen_gcc / tests / run / int.rs
1 // Compiler:
2 //
3 // Run-time:
4 //   status: 0
5
6 #![feature(bench_black_box, const_black_box, core_intrinsics, start)]
7
8 #![no_std]
9
10 #[panic_handler]
11 fn panic_handler(_: &core::panic::PanicInfo) -> ! {
12     core::intrinsics::abort();
13 }
14
15 /*
16  * Code
17  */
18
19 #[start]
20 fn main(_argc: isize, _argv: *const *const u8) -> isize {
21     use core::hint::black_box;
22
23     macro_rules! check {
24         ($ty:ty, $expr:expr) => {
25             {
26                 const EXPECTED: $ty = $expr;
27                 assert_eq!($expr, EXPECTED);
28             }
29         };
30     }
31
32     check!(u32, (2220326408_u32 + black_box(1)) >> (32 - 6));
33
34     /// Generate `check!` tests for integer types at least as wide as 128 bits.
35     macro_rules! check_ops128 {
36         () => {
37             check_ops64!();
38
39             // Shifts.
40             check!(T, VAL1 << black_box(64));
41             check!(T, VAL1 << black_box(81));
42             check!(T, VAL3 << black_box(63));
43             check!(T, VAL3 << black_box(64));
44
45             check!(T, VAL1 >> black_box(64));
46             check!(T, VAL2 >> black_box(64));
47             check!(T, VAL3 >> black_box(64));
48             check!(T, VAL3 >> black_box(81));
49         };
50     }
51
52     /// Generate `check!` tests for integer types at least as wide as 64 bits.
53     macro_rules! check_ops64 {
54         () => {
55             check_ops32!();
56
57             // Shifts.
58             check!(T, VAL2 << black_box(33));
59             check!(T, VAL2 << black_box(49));
60             check!(T, VAL2 << black_box(61));
61             check!(T, VAL2 << black_box(63));
62
63             check!(T, VAL3 << black_box(33));
64             check!(T, VAL3 << black_box(49));
65             check!(T, VAL3 << black_box(61));
66
67             check!(T, VAL1 >> black_box(33));
68             check!(T, VAL1 >> black_box(49));
69             check!(T, VAL1 >> black_box(61));
70             check!(T, VAL1 >> black_box(63));
71
72             check!(T, VAL2 >> black_box(33));
73             check!(T, VAL2 >> black_box(49));
74             check!(T, VAL2 >> black_box(61));
75             check!(T, VAL2 >> black_box(63));
76
77             check!(T, VAL3 >> black_box(33));
78             check!(T, VAL3 >> black_box(49));
79             check!(T, VAL3 >> black_box(61));
80             check!(T, VAL3 >> black_box(63));
81         };
82     }
83
84     /// Generate `check!` tests for integer types at least as wide as 32 bits.
85     macro_rules! check_ops32 {
86         () => {
87             // Shifts.
88             check!(T, VAL2 << black_box(1));
89             check!(T, VAL2 << black_box(0));
90
91             check!(T, VAL3 << black_box(1));
92             check!(T, VAL3 << black_box(0));
93
94             check!(T, VAL1.wrapping_shl(black_box(0)));
95             check!(T, VAL1.wrapping_shl(black_box(1)));
96             check!(T, VAL1.wrapping_shl(black_box(33)));
97             check!(T, VAL1.wrapping_shl(black_box(49)));
98             check!(T, VAL1.wrapping_shl(black_box(61)));
99             check!(T, VAL1.wrapping_shl(black_box(63)));
100             check!(T, VAL1.wrapping_shl(black_box(64)));
101             check!(T, VAL1.wrapping_shl(black_box(81)));
102
103             check!(Option<T>, VAL1.checked_shl(black_box(0)));
104             check!(Option<T>, VAL1.checked_shl(black_box(1)));
105             check!(Option<T>, VAL1.checked_shl(black_box(33)));
106             check!(Option<T>, VAL1.checked_shl(black_box(49)));
107             check!(Option<T>, VAL1.checked_shl(black_box(61)));
108             check!(Option<T>, VAL1.checked_shl(black_box(63)));
109             check!(Option<T>, VAL1.checked_shl(black_box(64)));
110             check!(Option<T>, VAL1.checked_shl(black_box(81)));
111
112             check!(T, VAL1 >> black_box(0));
113             check!(T, VAL1 >> black_box(1));
114
115             check!(T, VAL2 >> black_box(1));
116             check!(T, VAL2 >> black_box(0));
117
118             check!(T, VAL3 >> black_box(0));
119             check!(T, VAL3 >> black_box(1));
120
121             check!(T, VAL1.wrapping_shr(black_box(0)));
122             check!(T, VAL1.wrapping_shr(black_box(1)));
123             check!(T, VAL1.wrapping_shr(black_box(33)));
124             check!(T, VAL1.wrapping_shr(black_box(49)));
125             check!(T, VAL1.wrapping_shr(black_box(61)));
126             check!(T, VAL1.wrapping_shr(black_box(63)));
127             check!(T, VAL1.wrapping_shr(black_box(64)));
128             check!(T, VAL1.wrapping_shr(black_box(81)));
129
130             check!(Option<T>, VAL1.checked_shr(black_box(0)));
131             check!(Option<T>, VAL1.checked_shr(black_box(1)));
132             check!(Option<T>, VAL1.checked_shr(black_box(33)));
133             check!(Option<T>, VAL1.checked_shr(black_box(49)));
134             check!(Option<T>, VAL1.checked_shr(black_box(61)));
135             check!(Option<T>, VAL1.checked_shr(black_box(63)));
136             check!(Option<T>, VAL1.checked_shr(black_box(64)));
137             check!(Option<T>, VAL1.checked_shr(black_box(81)));
138
139             // Casts
140             check!(u64, (VAL1 >> black_box(1)) as u64);
141
142             // Addition.
143             check!(T, VAL1 + black_box(1));
144             check!(T, VAL2 + black_box(1));
145             check!(T, VAL2 + (VAL2 + black_box(1)));
146             check!(T, VAL3 + black_box(1));
147
148             check!(Option<T>, VAL1.checked_add(black_box(1)));
149             check!(Option<T>, VAL2.checked_add(black_box(1)));
150             check!(Option<T>, VAL2.checked_add(VAL2 + black_box(1)));
151             check!(Option<T>, VAL3.checked_add(T::MAX));
152             check!(Option<T>, VAL3.checked_add(T::MIN));
153
154             check!(T, VAL1.wrapping_add(black_box(1)));
155             check!(T, VAL2.wrapping_add(black_box(1)));
156             check!(T, VAL2.wrapping_add(VAL2 + black_box(1)));
157             check!(T, VAL3.wrapping_add(T::MAX));
158             check!(T, VAL3.wrapping_add(T::MIN));
159
160             check!((T, bool), VAL1.overflowing_add(black_box(1)));
161             check!((T, bool), VAL2.overflowing_add(black_box(1)));
162             check!((T, bool), VAL2.overflowing_add(VAL2 + black_box(1)));
163             check!((T, bool), VAL3.overflowing_add(T::MAX));
164             check!((T, bool), VAL3.overflowing_add(T::MIN));
165
166             check!(T, VAL1.saturating_add(black_box(1)));
167             check!(T, VAL2.saturating_add(black_box(1)));
168             check!(T, VAL2.saturating_add(VAL2 + black_box(1)));
169             check!(T, VAL3.saturating_add(T::MAX));
170             check!(T, VAL3.saturating_add(T::MIN));
171
172             // Subtraction
173             check!(T, VAL1 - black_box(1));
174             check!(T, VAL2 - black_box(1));
175             check!(T, VAL3 - black_box(1));
176
177             check!(Option<T>, VAL1.checked_sub(black_box(1)));
178             check!(Option<T>, VAL2.checked_sub(black_box(1)));
179             check!(Option<T>, VAL2.checked_sub(VAL2 + black_box(1)));
180             check!(Option<T>, VAL3.checked_sub(T::MAX));
181             check!(Option<T>, VAL3.checked_sub(T::MIN));
182
183             check!(T, VAL1.wrapping_sub(black_box(1)));
184             check!(T, VAL2.wrapping_sub(black_box(1)));
185             check!(T, VAL2.wrapping_sub(VAL2 + black_box(1)));
186             check!(T, VAL3.wrapping_sub(T::MAX));
187             check!(T, VAL3.wrapping_sub(T::MIN));
188
189             check!((T, bool), VAL1.overflowing_sub(black_box(1)));
190             check!((T, bool), VAL2.overflowing_sub(black_box(1)));
191             check!((T, bool), VAL2.overflowing_sub(VAL2 + black_box(1)));
192             check!((T, bool), VAL3.overflowing_sub(T::MAX));
193             check!((T, bool), VAL3.overflowing_sub(T::MIN));
194
195             check!(T, VAL1.saturating_sub(black_box(1)));
196             check!(T, VAL2.saturating_sub(black_box(1)));
197             check!(T, VAL2.saturating_sub(VAL2 + black_box(1)));
198             check!(T, VAL3.saturating_sub(T::MAX));
199             check!(T, VAL3.saturating_sub(T::MIN));
200
201             // Multiplication
202             check!(T, VAL1 * black_box(2));
203             check!(T, VAL1 * (black_box(1) + VAL2));
204             check!(T, VAL2 * black_box(2));
205             check!(T, VAL2 * (black_box(1) + VAL2));
206             check!(T, VAL3 * black_box(1));
207             check!(T, VAL4 * black_box(2));
208             check!(T, VAL5 * black_box(2));
209
210             check!(Option<T>, VAL1.checked_mul(black_box(2)));
211             check!(Option<T>, VAL1.checked_mul(black_box(1) + VAL2));
212             check!(Option<T>, VAL3.checked_mul(VAL3));
213             check!(Option<T>, VAL4.checked_mul(black_box(2)));
214             check!(Option<T>, VAL5.checked_mul(black_box(2)));
215
216             check!(T, VAL1.wrapping_mul(black_box(2)));
217             check!(T, VAL1.wrapping_mul((black_box(1) + VAL2)));
218             check!(T, VAL3.wrapping_mul(VAL3));
219             check!(T, VAL4.wrapping_mul(black_box(2)));
220             check!(T, VAL5.wrapping_mul(black_box(2)));
221
222             check!((T, bool), VAL1.overflowing_mul(black_box(2)));
223             check!((T, bool), VAL1.overflowing_mul(black_box(1) + VAL2));
224             check!((T, bool), VAL3.overflowing_mul(VAL3));
225             check!((T, bool), VAL4.overflowing_mul(black_box(2)));
226             check!((T, bool), VAL5.overflowing_mul(black_box(2)));
227
228             check!(T, VAL1.saturating_mul(black_box(2)));
229             check!(T, VAL1.saturating_mul(black_box(1) + VAL2));
230             check!(T, VAL3.saturating_mul(VAL3));
231             check!(T, VAL4.saturating_mul(black_box(2)));
232             check!(T, VAL5.saturating_mul(black_box(2)));
233
234             // Division.
235             check!(T, VAL1 / black_box(2));
236             check!(T, VAL1 / black_box(3));
237
238             check!(T, VAL2 / black_box(2));
239             check!(T, VAL2 / black_box(3));
240
241             check!(T, VAL3 / black_box(2));
242             check!(T, VAL3 / black_box(3));
243             check!(T, VAL3 / (black_box(1) + VAL4));
244             check!(T, VAL3 / (black_box(1) + VAL2));
245
246             check!(T, VAL4 / black_box(2));
247             check!(T, VAL4 / black_box(3));
248
249             check!(Option<T>, VAL1.checked_div(black_box(2)));
250             check!(Option<T>, VAL1.checked_div(black_box(1) + VAL2));
251             check!(Option<T>, VAL3.checked_div(VAL3));
252             check!(Option<T>, VAL4.checked_div(black_box(2)));
253             check!(Option<T>, VAL5.checked_div(black_box(2)));
254             check!(Option<T>, (T::MIN).checked_div(black_box(0 as T).wrapping_sub(1)));
255             check!(Option<T>, VAL5.checked_div(black_box(0))); // var5 / 0
256
257             check!(T, VAL1.wrapping_div(black_box(2)));
258             check!(T, VAL1.wrapping_div(black_box(1) + VAL2));
259             check!(T, VAL3.wrapping_div(VAL3));
260             check!(T, VAL4.wrapping_div(black_box(2)));
261             check!(T, VAL5.wrapping_div(black_box(2)));
262             check!(T, (T::MIN).wrapping_div(black_box(0 as T).wrapping_sub(1)));
263
264             check!((T, bool), VAL1.overflowing_div(black_box(2)));
265             check!((T, bool), VAL1.overflowing_div(black_box(1) + VAL2));
266             check!((T, bool), VAL3.overflowing_div(VAL3));
267             check!((T, bool), VAL4.overflowing_div(black_box(2)));
268             check!((T, bool), VAL5.overflowing_div(black_box(2)));
269             check!((T, bool), (T::MIN).overflowing_div(black_box(0 as T).wrapping_sub(1)));
270
271             check!(T, VAL1.saturating_div(black_box(2)));
272             check!(T, VAL1.saturating_div((black_box(1) + VAL2)));
273             check!(T, VAL3.saturating_div(VAL3));
274             check!(T, VAL4.saturating_div(black_box(2)));
275             check!(T, VAL5.saturating_div(black_box(2)));
276             check!(T, (T::MIN).saturating_div((0 as T).wrapping_sub(black_box(1))));
277         };
278     }
279
280     {
281         type T = u32;
282         const VAL1: T = 14162_u32;
283         const VAL2: T = 14556_u32;
284         const VAL3: T = 323656954_u32;
285         const VAL4: T = 2023651954_u32;
286         const VAL5: T = 1323651954_u32;
287         check_ops32!();
288     }
289
290     {
291         type T = i32;
292         const VAL1: T = 13456_i32;
293         const VAL2: T = 10475_i32;
294         const VAL3: T = 923653954_i32;
295         const VAL4: T = 993198738_i32;
296         const VAL5: T = 1023653954_i32;
297         check_ops32!();
298     }
299
300     {
301         type T = u64;
302         const VAL1: T = 134217856_u64;
303         const VAL2: T = 104753732_u64;
304         const VAL3: T = 12323651988970863954_u64;
305         const VAL4: T = 7323651988970863954_u64;
306         const VAL5: T = 8323651988970863954_u64;
307         check_ops64!();
308     }
309
310     {
311         type T = i64;
312         const VAL1: T = 134217856_i64;
313         const VAL2: T = 104753732_i64;
314         const VAL3: T = 6323651988970863954_i64;
315         const VAL4: T = 2323651988970863954_i64;
316         const VAL5: T = 3323651988970863954_i64;
317         check_ops64!();
318     }
319
320     {
321         type T = u128;
322         const VAL1: T = 134217856_u128;
323         const VAL2: T = 10475372733397991552_u128;
324         const VAL3: T = 193236519889708027473620326106273939584_u128;
325         const VAL4: T = 123236519889708027473620326106273939584_u128;
326         const VAL5: T = 153236519889708027473620326106273939584_u128;
327         check_ops128!();
328     }
329     {
330         type T = i128;
331         const VAL1: T = 134217856_i128;
332         const VAL2: T = 10475372733397991552_i128;
333         const VAL3: T = 83236519889708027473620326106273939584_i128;
334         const VAL4: T = 63236519889708027473620326106273939584_i128;
335         const VAL5: T = 73236519889708027473620326106273939584_i128;
336         check_ops128!();
337     }
338
339     0
340 }