logo
  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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]

#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
use core::arch::asm;

/// Move if zero.
///
/// Uses a `test` instruction to check if the given `condition` value is
/// equal to zero, then calls `cmovz` (a.k.a. `cmove`) to conditionally move
/// `src` to `dst` when `condition` is equal to zero.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[inline(always)]
pub fn cmovz(condition: usize, src: usize, dst: &mut usize) {
    unsafe {
        asm! {
            "test {0}, {0}",
            "cmovz {1}, {2}",
            in(reg) condition,
            inlateout(reg) *dst,
            in(reg) src
        };
    }
}

/// Move if zero.
///
/// Uses a `cmp` instruction to check if the given `condition` value is
/// equal to zero, then calls `csel` to conditionally move
/// `src` to `dst` when `condition` is equal to zero.
#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub fn cmovz(condition: usize, src: usize, dst: &mut usize) {
    unsafe {
        asm! {
            "cmp {0}, 0",
            "csel {1}, {2}, {3}, EQ",
            in(reg) condition,
            inlateout(reg) *dst,
            in(reg) src,
            in(reg) *dst,
        };
    }
}

/// Move if not zero.
///
/// Uses a `cmp` instruction to check if the given `condition` value is not
/// equal to zero, then calls `csel` to conditionally move
/// `src` to `dst` when `condition` is nonzero.
#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub fn cmovnz(condition: usize, src: usize, dst: &mut usize) {
    unsafe {
        asm! {
            "cmp {0}, 0",
            "csel {1}, {2}, {3}, NE",
            in(reg) condition,
            inlateout(reg) *dst,
            in(reg) src,
            in(reg) *dst,
        };
    }
}

/// Move if not zero.
///
/// Uses a `test` instruction to check if the given `condition` value is not
/// equal to zero, then calls `cmovnz` (a.k.a. `cmovne`) to conditionally move
/// `src` to `dst` when `condition` is nonzero.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[inline(always)]
pub fn cmovnz(condition: usize, src: usize, dst: &mut usize) {
    unsafe {
        asm! {
            "test {0}, {0}",
            "cmovnz {1}, {2}",
            in(reg) condition,
            inlateout(reg) *dst,
            in(reg) src
        };
    }
}

/// Move if zero (portable fallback implementation).
///
/// This implementation is based on portable bitwise arithmetic but cannot
/// guarantee that the resulting generated assembly is free of branch
/// instructions.
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
#[inline(never)]
pub fn cmovz(condition: usize, src: usize, dst: &mut usize) {
    let mask = (1 ^ is_non_zero(condition)).wrapping_sub(1);
    *dst = (*dst & mask) | (src & !mask);
}

/// Move if not zero (portable fallback implementation).
///
/// This implementation is based on portable bitwise arithmetic but cannot
/// guarantee that the resulting generated assembly is free of branch
/// instructions.
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
#[inline(never)]
pub fn cmovnz(condition: usize, src: usize, dst: &mut usize) {
    let mask = is_non_zero(condition).wrapping_sub(1);
    *dst = (*dst & mask) | (src & !mask);
}

/// Check if the given condition value is non-zero
///
/// # Returns
/// - `condition` is zero: `0`
/// - `condition` is non-zero: `1`
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
#[inline(always)]
fn is_non_zero(condition: usize) -> usize {
    const SHIFT_BITS: usize = core::mem::size_of::<usize>() - 1;
    ((condition | (!condition).wrapping_add(1)) >> SHIFT_BITS) & 1
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cmovz_works() {
        let mut n = 24;
        cmovz(42, 42, &mut n);
        assert_eq!(n, 24);
        cmovz(0, 42, &mut n);
        assert_eq!(n, 42);
    }

    #[test]
    fn cmovnz_works() {
        let mut n = 24;
        cmovnz(0, 42, &mut n);
        assert_eq!(n, 24);
        cmovnz(42, 42, &mut n);
        assert_eq!(n, 42);
    }
}