Closed
Description
Godbolt link: https://godbolt.org/z/9rm4jF
So I expect these two functions has the same assembly:
pub fn foo(a: i32, b: i32) -> i32 {
a.saturating_add(b)
}
pub fn bar(a: i32, b: i32) -> i32 {
match a.checked_add(b) {
Some(v) => v,
None => i32::max_value(),
}
}
But the result is not:
example::foo:
xor eax, eax
mov ecx, edi
add ecx, esi
setns al
add eax, 2147483647
add edi, esi
cmovno eax, edi
ret
example::bar:
add edi, esi
mov eax, 2147483647
cmovno eax, edi
ret
Activity
tesuji commentedon Oct 1, 2019
hman523 commentedon Oct 1, 2019
Hey I tried solving this but wasn't really sure how to. I believe the implementation to the LLVM generation is right here.
rust/src/librustc_codegen_llvm/intrinsic.rs
Lines 471 to 508 in 22bc9e1
Hope someone can solve this!
tesuji commentedon Oct 1, 2019
Turn out I was wrong! The
saturating_add
is working correctly (i.e. those two functionsare not equivalent): https://github.com/rust-lang/rust/pull/58003/files#diff-01076f91a26400b2db49663d787c2576R885-R891
I would add some doctests to show that and close this issue.
[-]`saturating_add` has overhead?[/-][+]`saturating_add` doc is not clear enough[/+]saturating_{add,sub}
signed ints #64943Rollup merge of rust-lang#64943 - lzutao:doc-saturating, r=shepmaster
Rollup merge of rust-lang#64943 - lzutao:doc-saturating, r=shepmaster