More instructions generated for Ord::clamp
than manual max(X).min(Y)
for saturating truncating cast from i32
to u8
#125738
Labels
A-autovectorization
Area: Autovectorization, which can impact perf or code size
A-codegen
Area: Code generation
C-optimization
Category: An issue highlighting optimization opportunities or PRs implementing such
S-has-mcve
Status: A Minimal Complete and Verifiable Example has been found for this issue
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Originally reported here
rust-lang/rust-clippy#12826
Related PR spawned from that issue
#125455
cc @blyxyas
Clamping and casting from
i32
tou8
, usingclamp(0, 255) as u8
produces unnecessary instructions compared to.max(0).min(255) as u8
when a loop is autovectorized.clippy
'smanual_clamp
lint in the beta toolchain warns on this pattern to useclamp
instead which can regress performance.Minimal example
https://rust.godbolt.org/z/zf73jsqjq
Manual clamp
`Ord::clamp`
Real code examples from functions in the
image-webp
cratehttps://rust.godbolt.org/z/3rnY8d94v
https://rust.godbolt.org/z/53T7n9PGx
The text was updated successfully, but these errors were encountered: