Skip to content

Commit

Permalink
fn decode_coefs_class: Fix branch misprediction (#1246)
Browse files Browse the repository at this point in the history
* Part of #1180.

Move the `tok != 0` check before the `*= 0x17ff41` so that `cmov` is
used, not a mispredicted branch.
  • Loading branch information
kkysen authored Jun 27, 2024
2 parents a43b380 + ec6c712 commit 6be08b8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/recon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,12 +916,23 @@ fn decode_coefs<BD: BitDepth>(
rc = rc_i;
} else {
// `0x1` for `tok`, `0x7ff` as bitmask for `rc`, `0x41` for `level_tok`.

// If we do this after the `tok *= 0x17ff41`,
// it uses a mispredicted branch instead of `cmov`.
let tok_non_zero = tok != 0;

let mut tok = tok as u32;
tok *= 0x17ff41;
level[0] = tok as u8;
// `tok ? (tok << 11) | rc : 0`
tok = (tok >> 9) & (rc as u32).wrapping_add(!0x7ff);
if tok != 0 {
let tok_check = if tok != 0 {
((tok as u16) << 11) | rc
} else {
0
};
tok = (tok >> 9) & (rc as u32 + !0x7ff);
debug_assert!(tok == tok_check as u32);
debug_assert!(tok_non_zero == (tok != 0));
if tok_non_zero {
rc = rc_i;
}
cf.set::<BD>(f, t_cf, rc_i as usize, tok.as_::<BD::Coef>());
Expand Down

0 comments on commit 6be08b8

Please sign in to comment.