1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#[cfg(test)]
use stdarch_test::assert_instr;

/// Returns the bit in position `b` of the memory addressed by `p`.
#[inline]
#[cfg_attr(test, assert_instr(bt))]
#[unstable(feature = "simd_x86_bittest", issue = "59414")]
pub unsafe fn _bittest(p: *const i32, b: i32) -> u8 {
    let r: u8;
    llvm_asm!("btl $2, $1\n\tsetc ${0:b}"
              : "=r"(r)
              : "*m"(p), "r"(b)
              : "cc", "memory");
    r
}

/// Returns the bit in position `b` of the memory addressed by `p`, then sets the bit to `1`.
#[inline]
#[cfg_attr(test, assert_instr(bts))]
#[unstable(feature = "simd_x86_bittest", issue = "59414")]
pub unsafe fn _bittestandset(p: *mut i32, b: i32) -> u8 {
    let r: u8;
    llvm_asm!("btsl $2, $1\n\tsetc ${0:b}"
              : "=r"(r), "+*m"(p)
              : "r"(b)
              : "cc", "memory");
    r
}

/// Returns the bit in position `b` of the memory addressed by `p`, then resets that bit to `0`.
#[inline]
#[cfg_attr(test, assert_instr(btr))]
#[unstable(feature = "simd_x86_bittest", issue = "59414")]
pub unsafe fn _bittestandreset(p: *mut i32, b: i32) -> u8 {
    let r: u8;
    llvm_asm!("btrl $2, $1\n\tsetc ${0:b}"
              : "=r"(r), "+*m"(p)
              : "r"(b)
              : "cc", "memory");
    r
}

/// Returns the bit in position `b` of the memory addressed by `p`, then inverts that bit.
#[inline]
#[cfg_attr(test, assert_instr(btc))]
#[unstable(feature = "simd_x86_bittest", issue = "59414")]
pub unsafe fn _bittestandcomplement(p: *mut i32, b: i32) -> u8 {
    let r: u8;
    llvm_asm!("btcl $2, $1\n\tsetc ${0:b}"
              : "=r"(r), "+*m"(p)
              : "r"(b)
              : "cc", "memory");
    r
}

#[cfg(test)]
mod tests {
    use crate::core_arch::x86::*;

    #[test]
    fn test_bittest() {
        unsafe {
            let a = 0b0101_0000i32;
            assert_eq!(_bittest(&a as _, 4), 1);
            assert_eq!(_bittest(&a as _, 5), 0);
        }
    }

    #[test]
    fn test_bittestandset() {
        unsafe {
            let mut a = 0b0101_0000i32;
            assert_eq!(_bittestandset(&mut a as _, 4), 1);
            assert_eq!(_bittestandset(&mut a as _, 4), 1);
            assert_eq!(_bittestandset(&mut a as _, 5), 0);
            assert_eq!(_bittestandset(&mut a as _, 5), 1);
        }
    }

    #[test]
    fn test_bittestandreset() {
        unsafe {
            let mut a = 0b0101_0000i32;
            assert_eq!(_bittestandreset(&mut a as _, 4), 1);
            assert_eq!(_bittestandreset(&mut a as _, 4), 0);
            assert_eq!(_bittestandreset(&mut a as _, 5), 0);
            assert_eq!(_bittestandreset(&mut a as _, 5), 0);
        }
    }

    #[test]
    fn test_bittestandcomplement() {
        unsafe {
            let mut a = 0b0101_0000i32;
            assert_eq!(_bittestandcomplement(&mut a as _, 4), 1);
            assert_eq!(_bittestandcomplement(&mut a as _, 4), 0);
            assert_eq!(_bittestandcomplement(&mut a as _, 4), 1);
            assert_eq!(_bittestandcomplement(&mut a as _, 5), 0);
            assert_eq!(_bittestandcomplement(&mut a as _, 5), 1);
        }
    }
}