]> git.lizzy.rs Git - rust.git/blob - example/std_example.rs
Fix simd comparison
[rust.git] / example / std_example.rs
1 #![feature(core_intrinsics)]
2
3 use std::io::Write;
4 use std::intrinsics;
5
6
7 fn main() {
8     let _ = ::std::iter::repeat('a' as u8).take(10).collect::<Vec<_>>();
9     let stderr = ::std::io::stderr();
10     let mut stderr = stderr.lock();
11
12     writeln!(stderr, "some {} text", "<unknown>").unwrap();
13
14     let _ = std::process::Command::new("true").env("c", "d").spawn();
15
16     println!("cargo:rustc-link-lib=z");
17
18     static ONCE: std::sync::Once = std::sync::ONCE_INIT;
19     ONCE.call_once(|| {});
20
21     LoopState::Continue(()) == LoopState::Break(());
22
23     // Make sure ByValPair values with differently sized components are correctly passed
24     map(None::<(u8, Box<Instruction>)>);
25
26     println!("{}", 2.3f32.exp());
27     println!("{}", 2.3f32.exp2());
28     println!("{}", 2.3f32.abs());
29     println!("{}", 2.3f32.sqrt());
30     println!("{}", 2.3f32.floor());
31     println!("{}", 2.3f32.ceil());
32     println!("{}", 2.3f32.min(1.0));
33     println!("{}", 2.3f32.max(1.0));
34     println!("{}", 2.3f32.powi(2));
35
36     assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26);
37     assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7);
38
39     0i128.checked_div(2i128);
40     0u128.checked_div(2u128);
41     assert_eq!(1u128 + 2, 3);
42
43     assert_eq!(0b100010000000000000000000000000000u128 >> 10, 0b10001000000000000000000u128);
44     assert_eq!(0xFEDCBA987654321123456789ABCDEFu128 >> 64, 0xFEDCBA98765432u128);
45     assert_eq!(0xFEDCBA987654321123456789ABCDEFu128 as i128 >> 64, 0xFEDCBA98765432i128);
46     assert_eq!(353985398u128 * 932490u128, 330087843781020u128);
47
48     unsafe {
49         test_simd();
50     }
51 }
52
53 #[target_feature(enable = "sse2")]
54 unsafe fn test_simd() {
55     use std::arch::x86_64::*;
56
57     let x = _mm_setzero_si128();
58     let y = _mm_set1_epi16(7);
59     let or = _mm_or_si128(x, y);
60     let cmp_eq = _mm_cmpeq_epi8(y, y);
61     let cmp_lt = _mm_cmplt_epi8(y, y);
62
63     assert_eq!(std::mem::transmute::<_, [u16; 8]>(or), [7, 7, 7, 7, 7, 7, 7, 7]);
64     assert_eq!(std::mem::transmute::<_, [u16; 8]>(cmp_eq), [0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff]);
65     assert_eq!(std::mem::transmute::<_, [u16; 8]>(cmp_lt), [0, 0, 0, 0, 0, 0, 0, 0]);
66 }
67
68 #[derive(PartialEq)]
69 enum LoopState {
70     Continue(()),
71     Break(())
72 }
73
74 pub enum Instruction {
75     Increment,
76     Loop,
77 }
78
79 fn map(a: Option<(u8, Box<Instruction>)>) -> Option<Box<Instruction>> {
80     match a {
81         None => None,
82         Some((_, instr)) => Some(instr),
83     }
84 }