I want to get the value of the carry flag after performing a bit-shift.
Rust has stable functions that check for overflow like so:
pub fn add_with_carry(foo : &mut u8, bar : u8, cf : &mut u8) {
let(result, overflow) = foo.overflowing_add(bar);
*foo = result;
*cf = overflow as u8;
}
which generates assembly like this:
example::add_with_carry:
add byte ptr [rdi], sil
setb byte ptr [rdx]
ret
however overflowing_shr
only returns true when the value is shifted by its size or more, I only care about the least significant bit, which would be stored in CF, currently the best i've come up with is:
pub fn shr(foo : &mut u8, cf : &mut u8) {
*cf = *foo & 1;
*foo >>=1
}
however that is somewhat less efficient:
example::shr:
mov al, byte ptr [rdi]
and al, 1
mov byte ptr [rsi], al
shr byte ptr [rdi]
ret
I would rather have something like:
example::shr:
shr byte ptr [rdi]
setc byte ptr [rdx]
ret
There doesn't seem to be an intrinsic to get CF and inline assembly is not stable at the moment.