]> git.proxmox.com Git - rustc.git/blob - src/stdarch/crates/core_arch/src/x86_64/bt.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / stdarch / crates / core_arch / src / x86_64 / bt.rs
1 #[cfg(test)]
2 use stdarch_test::assert_instr;
3
4 /// Returns the bit in position `b` of the memory addressed by `p`.
5 #[inline]
6 #[cfg_attr(test, assert_instr(bt))]
7 #[unstable(feature = "simd_x86_bittest", issue = "59414")]
8 pub unsafe fn _bittest64(p: *const i64, b: i64) -> u8 {
9 let r: u8;
10 asm!("btq $2, $1\n\tsetc ${0:b}"
11 : "=r"(r)
12 : "*m"(p), "r"(b)
13 : "cc", "memory");
14 r
15 }
16
17 /// Returns the bit in position `b` of the memory addressed by `p`, then sets the bit to `1`.
18 #[inline]
19 #[cfg_attr(test, assert_instr(bts))]
20 #[unstable(feature = "simd_x86_bittest", issue = "59414")]
21 pub unsafe fn _bittestandset64(p: *mut i64, b: i64) -> u8 {
22 let r: u8;
23 asm!("btsq $2, $1\n\tsetc ${0:b}"
24 : "=r"(r), "+*m"(p)
25 : "r"(b)
26 : "cc", "memory");
27 r
28 }
29
30 /// Returns the bit in position `b` of the memory addressed by `p`, then resets that bit to `0`.
31 #[inline]
32 #[cfg_attr(test, assert_instr(btr))]
33 #[unstable(feature = "simd_x86_bittest", issue = "59414")]
34 pub unsafe fn _bittestandreset64(p: *mut i64, b: i64) -> u8 {
35 let r: u8;
36 asm!("btrq $2, $1\n\tsetc ${0:b}"
37 : "=r"(r), "+*m"(p)
38 : "r"(b)
39 : "cc", "memory");
40 r
41 }
42
43 /// Returns the bit in position `b` of the memory addressed by `p`, then inverts that bit.
44 #[inline]
45 #[cfg_attr(test, assert_instr(btc))]
46 #[unstable(feature = "simd_x86_bittest", issue = "59414")]
47 pub unsafe fn _bittestandcomplement64(p: *mut i64, b: i64) -> u8 {
48 let r: u8;
49 asm!("btcq $2, $1\n\tsetc ${0:b}"
50 : "=r"(r), "+*m"(p)
51 : "r"(b)
52 : "cc", "memory");
53 r
54 }
55
56 #[cfg(test)]
57 mod tests {
58 use crate::core_arch::x86_64::*;
59
60 #[test]
61 fn test_bittest64() {
62 unsafe {
63 let a = 0b0101_0000i64;
64 assert_eq!(_bittest64(&a as _, 4), 1);
65 assert_eq!(_bittest64(&a as _, 5), 0);
66 }
67 }
68
69 #[test]
70 fn test_bittestandset64() {
71 unsafe {
72 let mut a = 0b0101_0000i64;
73 assert_eq!(_bittestandset64(&mut a as _, 4), 1);
74 assert_eq!(_bittestandset64(&mut a as _, 4), 1);
75 assert_eq!(_bittestandset64(&mut a as _, 5), 0);
76 assert_eq!(_bittestandset64(&mut a as _, 5), 1);
77 }
78 }
79
80 #[test]
81 fn test_bittestandreset64() {
82 unsafe {
83 let mut a = 0b0101_0000i64;
84 assert_eq!(_bittestandreset64(&mut a as _, 4), 1);
85 assert_eq!(_bittestandreset64(&mut a as _, 4), 0);
86 assert_eq!(_bittestandreset64(&mut a as _, 5), 0);
87 assert_eq!(_bittestandreset64(&mut a as _, 5), 0);
88 }
89 }
90
91 #[test]
92 fn test_bittestandcomplement64() {
93 unsafe {
94 let mut a = 0b0101_0000i64;
95 assert_eq!(_bittestandcomplement64(&mut a as _, 4), 1);
96 assert_eq!(_bittestandcomplement64(&mut a as _, 4), 0);
97 assert_eq!(_bittestandcomplement64(&mut a as _, 4), 1);
98 assert_eq!(_bittestandcomplement64(&mut a as _, 5), 0);
99 assert_eq!(_bittestandcomplement64(&mut a as _, 5), 1);
100 }
101 }
102 }