bit_arch 0.2.0

High-level zero-cost bit manipulation
Documentation
arch_specific!(
#[doc = "Swaps bytes of `x`."]
swap_bytes_i32(x: i32) -> i32 {
    x.swap_bytes()
}, core::arch::x86_64::_bswap);

arch_specific!(
#[doc = "Swaps bytes of `x`."]
swap_bytes_i64(x: i64) -> i64 {
    x.swap_bytes()
}, core::arch::x86_64::_bswap64);

arch_specific!(
#[doc = "Reads bit `index` of the `i32` at `ptr`."]
bit_test_i32(ptr: *const i32, index: i32) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);

    let value = ptr.offset(word as isize).read();
    ((value as u32 >> bit) & 1) as u8
}, { unsafe "The address provided must be valid for reads at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittest);

arch_specific!(
#[doc = "Reads bit `index` of the `i64` at `ptr`."]
bit_test_i64(ptr: *const i64, index: i64) -> u8 {
    let word = index.div_euclid(64);
    let bit = index.rem_euclid(64);

    let value = ptr.offset(word as isize).read();
    ((value as u64 >> bit) & 1) as u8
}, { unsafe "The address provided must be valid for reads at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittest64);

arch_specific!(
#[doc = "Returns the bit at `index` of the `i32` at `ptr` and then sets it to 1."]
bit_test_set_i32(ptr: *mut i32, index: i32) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);
    let ptr = ptr.offset(word as isize);

    let value = ptr.read();
    let mask = 1u32 << bit;
    let old = (value as u32 & mask) != 0;

    ptr.write((value as u32 | mask) as i32);

    old as u8
}, { unsafe "The address provided must be valid for reads and writes at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittestandset);

arch_specific!(
#[doc = "Returns the bit at `index` of the `i64` at `ptr` and then sets it to 1."]
bit_test_set_i64(ptr: *mut i64, index: i64) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);
    let ptr = ptr.offset(word as isize);

    let value = ptr.read();
    let mask = 1u32 << bit;
    let old = (value as u32 & mask) != 0;

    ptr.write((value as u32 | mask) as i32);

    old as u8
}, { unsafe "The address provided must be valid for reads and writes at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittestandset64);

arch_specific!(
#[doc = "Returns the bit at `index` of the `i32` at `ptr` and then sets it to 0."]
bit_test_reset_i32(ptr: *mut i32, index: i32) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);
    let ptr = ptr.offset(word as isize);

    let value = ptr.read();
    let mask = 1u32 << bit;
    let old = (value as u32 & mask) != 0;

    ptr.write((value as u32 & !mask) as i32);

    old as u8
}, { unsafe "The address provided must be valid for reads and writes at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittestandreset);

arch_specific!(
#[doc = "Returns the bit at `index` of the `i64` at `ptr` and then sets it to 0."]
bit_test_reset_i64(ptr: *mut i64, index: i64) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);
    let ptr = ptr.offset(word as isize);

    let value = ptr.read();
    let mask = 1u32 << bit;
    let old = (value as u32 & mask) != 0;

    ptr.write((value as u32 & !mask) as i32);

    old as u8
}, { unsafe "The address provided must be valid for reads and writes at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittestandreset64);

arch_specific!(
#[doc = "Returns the bit at `index` of the `i32` at `ptr` and then inverts it."]
bit_test_complement_i32(ptr: *mut i32, index: i32) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);
    let ptr = ptr.offset(word as isize);

    let value = ptr.read();
    let mask = 1u32 << bit;
    let old = (value as u32 & mask) != 0;

    ptr.write((value as u32 ^ mask) as i32);

    old as u8
}, { unsafe "The address provided must be valid for reads and writes at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittestandcomplement);

arch_specific!(
#[doc = "Returns the bit at `index` of the `i64` at `ptr` and then inverts it."]
bit_test_complement_i64(ptr: *mut i64, index: i64) -> u8 {
    let word = index.div_euclid(32);
    let bit = index.rem_euclid(32);
    let ptr = ptr.offset(word as isize);

    let value = ptr.read();
    let mask = 1u32 << bit;
    let old = (value as u32 & mask) != 0;

    ptr.write((value as u32 ^ mask) as i32);

    old as u8
}, { unsafe "The address provided must be valid for reads and writes at offset `index` in bits.
Note this should not be considered atomic, even on `x86_64`." } core::arch::x86_64::_bittestandcomplement64);