-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added abs_diff for integer types. #88780
Conversation
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
Does either of these compile to a fn sad_iter(a: &[u8; 8], b: &[u8; 8]) -> u32 {
a.iter().zip(b).map(|&a, &b| a.abs_diff(b) as u32).sum()
}
fn sad_loop(a: &[u8; 8], b: &[u8; 8]) -> u32 {
let mut sum = 0;
for i in 0..a.len() {
sum += a[i].abs_diff(b[i]) as u32;
}
sum
} llvm does have some pattern detection to generate psadbw, so we should make sure we fall into that. |
@the8472 I can't get Rust to generate |
Yes, it's still there. Based on comments in tickets the pattern it expects for absolute difference should look like https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/X86/sad.ll |
It's a really fragile optimization. I've never seen it in practice, but: https://godbolt.org/z/qffbWvh4v |
@the8472 I've tried a bunch of things, and I can't generate it. Note that EDIT: it appears to only work for exactly this: pub fn abs_diff(slf: u8, other: u8) -> u8 {
(slf as i32).wrapping_sub(other as i32).abs() as u8
} If you widen to anything except |
I suspect it's written to match C's integer conversion rules. |
If we consider this worthwhile I could add that implementation for EDIT: or I could do a little hack using EDIT2: appears to work to generate the |
SAD is really important in video processing. Yes, all serious implementations will use platform-specific intrinsics but they have generic fallback paths and if those get used for any reason (wasm? riscv?) it should be in a pattern that optimizers recognize. |
@the8472 Implemented |
This looks good to me. Can you open a tracking issue, and add its number in the #[unstable] attributes? Thanks! |
Thanks! @bors r+ |
📌 Commit 6dd6e7c has been approved by |
Added abs_diff for integer types. Closes rust-lang#62111.
Added abs_diff for integer types. Closes rust-lang#62111.
Added abs_diff for integer types. Closes rust-lang#62111.
…ingjubilee Rollup of 15 pull requests Successful merges: - rust-lang#87993 (Stabilize try_reserve) - rust-lang#88090 (Perform type inference in range pattern) - rust-lang#88780 (Added abs_diff for integer types.) - rust-lang#89270 (path.push() should work as expected on windows verbatim paths) - rust-lang#89413 (Correctly handle supertraits for min_specialization) - rust-lang#89456 (Update to the final LLVM 13.0.0 release) - rust-lang#89466 (Fix bug with query modifier parsing) - rust-lang#89473 (Fix extra `non_snake_case` warning for shorthand field bindings) - rust-lang#89474 (rustdoc: Improve doctest pass's name and module's name) - rust-lang#89478 (Fixed numerus of error message) - rust-lang#89480 (Add test for issue 89118.) - rust-lang#89487 (Try to recover from a `=>` -> `=` or `->` typo in a match arm) - rust-lang#89494 (Deny `where` clauses on `auto` traits) - rust-lang#89511 (:arrow_up: rust-analyzer) - rust-lang#89536 (update Miri) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Added abs_diff for integer types. Closes rust-lang#62111.
Closes #62111.