<commit_before>//! Various utility function with a specific architecture implementation

pub unsafe fn atomic_set_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock bts $1, $0"
         : "=*m" (addr as *mut u32)
         : "Ir" (nr)
         : "memory"
         : "volatile");
}

pub unsafe fn atomic_clear_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock btr $1, $0"
         : "=*m" (addr as *mut u32)
         : "Ir" (nr)
         : "memory"
         : "volatile");
}

pub fn first_bit(data: usize) -> usize {
    unsafe {
        let ret;

        asm!("bsf $1, $0\n\t\
              jnz 1f\n\t
              mov $$0, $0\n\t
              1:\n\t"
             : "=r" (ret)
             : "r" (data)
             :: "volatile");

        ret
    }
}

pub fn wmb() {
    unsafe {
        asm!("sfence" ::: "memory" : "volatile");
    }
}

#[test]
pub fn test_set_and_clear() {
    let mut array = [0u32; 4];<commit_msg>x86/utils: Add mb() and rmb()<commit_after>//! Various utility function with a specific architecture implementation

pub unsafe fn atomic_set_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock bts $1, $0"
         : "=*m" (addr as *mut u32)
         : "Ir" (nr)
         : "memory"
         : "volatile");
}

pub unsafe fn atomic_clear_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock btr $1, $0"
         : "=*m" (addr as *mut u32)
         : "Ir" (nr)
         : "memory"
         : "volatile");
}

pub fn first_bit(data: usize) -> usize {
    unsafe {
        let ret;

        asm!("bsf $1, $0\n\t\
              jnz 1f\n\t
              mov $$0, $0\n\t
              1:\n\t"
             : "=r" (ret)
             : "r" (data)
             :: "volatile");

        ret
    }
}

/// Memory barrier
pub fn mb() {
    unsafe {
        asm!("mfence" ::: "memory" : "volatile");
    }
}

/// Read memory barrier
pub fn rmb() {
    unsafe {
        asm!("lfence" ::: "memory" : "volatile");
    }
}

/// Write memory barrier
pub fn wmb() {
    unsafe {
        asm!("sfence" ::: "memory" : "volatile");
    }
}

#[test]
pub fn test_set_and_clear() {
    let mut array = [0u32; 4];
{'fraction_non_alphanumeric': 0.1797806390081068, 'fraction_numerical': 0.018121125417262757, 'mean_word_length': 1.6760204081632653, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '312756', 'n_tokens_mistral': 803, 'n_tokens_neox': 780, 'n_words': 205}