Why does ">=" generate so much code (compared to "<=" and "cmp")?
In the following code:
use std::num::NonZeroUsize;
use std::cmp::Ordering;
#[inline(never)]
pub fn test1(y: Option<NonZeroUsize>, x: Option<NonZeroUsize>) -> bool {
x >= y
}
#[inline(never)]
pub fn test2(x: Option<NonZeroUsize>, y: Option<NonZeroUsize>) -> bool {
x <= y
}
#[inline(never)]
pub fn test3(y: Option<NonZeroUsize>, x: Option<NonZeroUsize>) -> bool {
x.cmp(&y) == Ordering::Greater
}
In Rust 1.80.0, \test1\\ compiles to 25 lines of x86 assembly, while \test2\\ and \test3\\ compile to just 3 lines. Can anyone explain this? Note that the first two functions do the same computation: check if the first argument is at most the second, or equivalently, the second is at least the first.
(Context: a performance sensitive application where a lot of \Option<T>\\ comparisons are being done, where \T\\ is a wrapper around \NonZeroU8\\. Although I have figured out that you can do comparisons on \x.map(|v| v.get()).unwrap\\_or(0)\\ and this gets it back to one comparison instruction, so this question is just out of curiosity).
Godbolt: https://godbolt.org/z/vfxe7WrPn