]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/example/std_example.rs
Rollup merge of #96763 - Abdur-rahmaanJ:patch-1, r=Mark-Simulacrum
[rust.git] / compiler / rustc_codegen_gcc / example / std_example.rs
1 #![feature(core_intrinsics, generators, generator_trait, is_sorted)]
2
3 use std::arch::x86_64::*;
4 use std::io::Write;
5 use std::ops::Generator;
6
7 extern {
8     pub fn printf(format: *const i8, ...) -> i32;
9 }
10
11 fn main() {
12     let mutex = std::sync::Mutex::new(());
13     let _guard = mutex.lock().unwrap();
14
15     let _ = ::std::iter::repeat('a' as u8).take(10).collect::<Vec<_>>();
16     let stderr = ::std::io::stderr();
17     let mut stderr = stderr.lock();
18
19     std::thread::spawn(move || {
20         println!("Hello from another thread!");
21     });
22
23     writeln!(stderr, "some {} text", "<unknown>").unwrap();
24
25     let _ = std::process::Command::new("true").env("c", "d").spawn();
26
27     println!("cargo:rustc-link-lib=z");
28
29     static ONCE: std::sync::Once = std::sync::Once::new();
30     ONCE.call_once(|| {});
31
32     let _eq = LoopState::Continue(()) == LoopState::Break(());
33
34     // Make sure ByValPair values with differently sized components are correctly passed
35     map(None::<(u8, Box<Instruction>)>);
36
37     println!("{}", 2.3f32.exp());
38     println!("{}", 2.3f32.exp2());
39     println!("{}", 2.3f32.abs());
40     println!("{}", 2.3f32.sqrt());
41     println!("{}", 2.3f32.floor());
42     println!("{}", 2.3f32.ceil());
43     println!("{}", 2.3f32.min(1.0));
44     println!("{}", 2.3f32.max(1.0));
45     println!("{}", 2.3f32.powi(2));
46     println!("{}", 2.3f32.log2());
47     assert_eq!(2.3f32.copysign(-1.0), -2.3f32);
48     println!("{}", 2.3f32.powf(2.0));
49
50     assert_eq!(-128i8, (-128i8).saturating_sub(1));
51     assert_eq!(127i8, 127i8.saturating_sub(-128));
52     assert_eq!(-128i8, (-128i8).saturating_add(-128));
53     assert_eq!(127i8, 127i8.saturating_add(1));
54
55     assert_eq!(-32768i16, (-32768i16).saturating_add(-32768));
56     assert_eq!(32767i16, 32767i16.saturating_add(1));
57
58     assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26);
59     assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7);
60
61     let _d = 0i128.checked_div(2i128);
62     let _d = 0u128.checked_div(2u128);
63     assert_eq!(1u128 + 2, 3);
64
65     assert_eq!(0b100010000000000000000000000000000u128 >> 10, 0b10001000000000000000000u128);
66     assert_eq!(0xFEDCBA987654321123456789ABCDEFu128 >> 64, 0xFEDCBA98765432u128);
67     assert_eq!(0xFEDCBA987654321123456789ABCDEFu128 as i128 >> 64, 0xFEDCBA98765432i128);
68
69     let tmp = 353985398u128;
70     assert_eq!(tmp * 932490u128, 330087843781020u128);
71
72     let tmp = -0x1234_5678_9ABC_DEF0i64;
73     assert_eq!(tmp as i128, -0x1234_5678_9ABC_DEF0i128);
74
75     // Check that all u/i128 <-> float casts work correctly.
76     let houndred_u128 = 100u128;
77     let houndred_i128 = 100i128;
78     let houndred_f32 = 100.0f32;
79     let houndred_f64 = 100.0f64;
80     assert_eq!(houndred_u128 as f32, 100.0);
81     assert_eq!(houndred_u128 as f64, 100.0);
82     assert_eq!(houndred_f32 as u128, 100);
83     assert_eq!(houndred_f64 as u128, 100);
84     assert_eq!(houndred_i128 as f32, 100.0);
85     assert_eq!(houndred_i128 as f64, 100.0);
86     assert_eq!(houndred_f32 as i128, 100);
87     assert_eq!(houndred_f64 as i128, 100);
88
89     let _a = 1u32 << 2u8;
90
91     let empty: [i32; 0] = [];
92     assert!(empty.is_sorted());
93
94     println!("{:?}", std::intrinsics::caller_location());
95
96     #[cfg(feature="master")]
97     unsafe {
98         test_simd();
99     }
100
101     Box::pin(move |mut _task_context| {
102         yield ();
103     }).as_mut().resume(0);
104
105     println!("End");
106 }
107
108 #[cfg(feature="master")]
109 #[target_feature(enable = "sse2")]
110 unsafe fn test_simd() {
111     let x = _mm_setzero_si128();
112     let y = _mm_set1_epi16(7);
113     let or = _mm_or_si128(x, y);
114     let cmp_eq = _mm_cmpeq_epi8(y, y);
115     let cmp_lt = _mm_cmplt_epi8(y, y);
116
117     assert_eq!(std::mem::transmute::<_, [u16; 8]>(or), [7, 7, 7, 7, 7, 7, 7, 7]);
118     assert_eq!(std::mem::transmute::<_, [u16; 8]>(cmp_eq), [0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff]);
119     assert_eq!(std::mem::transmute::<_, [u16; 8]>(cmp_lt), [0, 0, 0, 0, 0, 0, 0, 0]);
120
121     test_mm_slli_si128();
122     test_mm_movemask_epi8();
123     test_mm256_movemask_epi8();
124     test_mm_add_epi8();
125     test_mm_add_pd();
126     test_mm_cvtepi8_epi16();
127     test_mm_cvtsi128_si64();
128
129     test_mm_extract_epi8();
130     test_mm_insert_epi16();
131
132     let mask1 = _mm_movemask_epi8(dbg!(_mm_setr_epi8(255u8 as i8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
133     assert_eq!(mask1, 1);
134 }
135
136 #[cfg(feature="master")]
137 #[target_feature(enable = "sse2")]
138 unsafe fn test_mm_slli_si128() {
139     #[rustfmt::skip]
140     let a = _mm_setr_epi8(
141         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
142     );
143     let r = _mm_slli_si128(a, 1);
144     let e = _mm_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
145     assert_eq_m128i(r, e);
146
147     #[rustfmt::skip]
148     let a = _mm_setr_epi8(
149         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
150     );
151     let r = _mm_slli_si128(a, 15);
152     let e = _mm_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
153     assert_eq_m128i(r, e);
154
155     #[rustfmt::skip]
156     let a = _mm_setr_epi8(
157         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
158     );
159     let r = _mm_slli_si128(a, 16);
160     assert_eq_m128i(r, _mm_set1_epi8(0));
161 }
162
163
164 #[cfg(feature="master")]
165 #[target_feature(enable = "sse2")]
166 unsafe fn test_mm_movemask_epi8() {
167     #[rustfmt::skip]
168     let a = _mm_setr_epi8(
169         0b1000_0000u8 as i8, 0b0, 0b1000_0000u8 as i8, 0b01,
170         0b0101, 0b1111_0000u8 as i8, 0, 0,
171         0, 0, 0b1111_0000u8 as i8, 0b0101,
172         0b01, 0b1000_0000u8 as i8, 0b0, 0b1000_0000u8 as i8,
173     );
174     let r = _mm_movemask_epi8(a);
175     assert_eq!(r, 0b10100100_00100101);
176 }
177
178 #[cfg(feature="master")]
179 #[target_feature(enable = "avx2")]
180 unsafe fn test_mm256_movemask_epi8() {
181     let a = _mm256_set1_epi8(-1);
182     let r = _mm256_movemask_epi8(a);
183     let e = -1;
184     assert_eq!(r, e);
185 }
186
187 #[cfg(feature="master")]
188 #[target_feature(enable = "sse2")]
189 unsafe fn test_mm_add_epi8() {
190     let a = _mm_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
191     #[rustfmt::skip]
192     let b = _mm_setr_epi8(
193         16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
194     );
195     let r = _mm_add_epi8(a, b);
196     #[rustfmt::skip]
197     let e = _mm_setr_epi8(
198         16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46,
199     );
200     assert_eq_m128i(r, e);
201 }
202
203 #[cfg(feature="master")]
204 #[target_feature(enable = "sse2")]
205 unsafe fn test_mm_add_pd() {
206     let a = _mm_setr_pd(1.0, 2.0);
207     let b = _mm_setr_pd(5.0, 10.0);
208     let r = _mm_add_pd(a, b);
209     assert_eq_m128d(r, _mm_setr_pd(6.0, 12.0));
210 }
211
212 #[cfg(feature="master")]
213 fn assert_eq_m128i(x: std::arch::x86_64::__m128i, y: std::arch::x86_64::__m128i) {
214     unsafe {
215         assert_eq!(std::mem::transmute::<_, [u8; 16]>(x), std::mem::transmute::<_, [u8; 16]>(y));
216     }
217 }
218
219 #[cfg(feature="master")]
220 #[target_feature(enable = "sse2")]
221 pub unsafe fn assert_eq_m128d(a: __m128d, b: __m128d) {
222     if _mm_movemask_pd(_mm_cmpeq_pd(a, b)) != 0b11 {
223         panic!("{:?} != {:?}", a, b);
224     }
225 }
226
227 #[cfg(feature="master")]
228 #[target_feature(enable = "sse2")]
229 unsafe fn test_mm_cvtsi128_si64() {
230     let r = _mm_cvtsi128_si64(std::mem::transmute::<[i64; 2], _>([5, 0]));
231     assert_eq!(r, 5);
232 }
233
234 #[cfg(feature="master")]
235 #[target_feature(enable = "sse4.1")]
236 unsafe fn test_mm_cvtepi8_epi16() {
237     let a = _mm_set1_epi8(10);
238     let r = _mm_cvtepi8_epi16(a);
239     let e = _mm_set1_epi16(10);
240     assert_eq_m128i(r, e);
241     let a = _mm_set1_epi8(-10);
242     let r = _mm_cvtepi8_epi16(a);
243     let e = _mm_set1_epi16(-10);
244     assert_eq_m128i(r, e);
245 }
246
247 #[cfg(feature="master")]
248 #[target_feature(enable = "sse4.1")]
249 unsafe fn test_mm_extract_epi8() {
250     #[rustfmt::skip]
251     let a = _mm_setr_epi8(
252         -1, 1, 2, 3, 4, 5, 6, 7,
253         8, 9, 10, 11, 12, 13, 14, 15
254     );
255     let r1 = _mm_extract_epi8(a, 0);
256     let r2 = _mm_extract_epi8(a, 3);
257     assert_eq!(r1, 0xFF);
258     assert_eq!(r2, 3);
259 }
260
261 #[cfg(all(feature="master", target_arch = "x86_64"))]
262 #[target_feature(enable = "sse2")]
263 unsafe fn test_mm_insert_epi16() {
264     let a = _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7);
265     let r = _mm_insert_epi16(a, 9, 0);
266     let e = _mm_setr_epi16(9, 1, 2, 3, 4, 5, 6, 7);
267     assert_eq_m128i(r, e);
268 }
269
270 #[derive(PartialEq)]
271 enum LoopState {
272     Continue(()),
273     Break(())
274 }
275
276 pub enum Instruction {
277     Increment,
278     Loop,
279 }
280
281 fn map(a: Option<(u8, Box<Instruction>)>) -> Option<Box<Instruction>> {
282     match a {
283         None => None,
284         Some((_, instr)) => Some(instr),
285     }
286 }