Rust std lib has some handy functions that often map to single CPU instructions, like u64::count_ones, leading_zeros, rotate_left, etc. Some CPUs also have a divq operation that divides a 128 bit number by a 64 bit number, and returns a pair of 64 bit quotient and remainder. I think currently (unlike mulq and addq [1]) it’s not easy to access this specific instruction from Rust code (unless you use asm). So is it a good idea to somehow add such function to rust std lib?
Instead of adding a special function the usual alternative solution is to improve the compiler so it’s able to spot code like:
let (q, r) = ((a / u128::from(b)) as u64,
(a % u128::from(b)) as u64);
This seems even harder than spotting cases where the programmer meant to perform a rotate_left and rotate_right (as D compiler sometimes fails to do).
A possible name:
let a: u128 = ...;
let b: u64 = ...;
let (q, r): (u64, u64) = a.div_mod_64(b);
[1] Example:
fn foo(x: u64, y: u64) -> u64 {
((u128::from(x) * u128::from(y)) >> 64) as u64
}
Gives:
foo:
mov rax, rsi
mul rdi
mov rax, rdx
ret