Add stable likely/unlikely to utils module

This commit is contained in:
Ivan Smirnov 2021-11-30 14:30:13 +00:00
parent 1bb6ee118c
commit 09976fb889
2 changed files with 24 additions and 0 deletions

View file

@ -5,6 +5,7 @@ mod encode;
mod error; mod error;
mod header; mod header;
mod pixel; mod pixel;
mod utils;
pub mod canonical; pub mod canonical;

23
src/utils.rs Normal file
View file

@ -0,0 +1,23 @@
#[inline(always)]
#[allow(unused)]
pub const fn likely(b: bool) -> bool {
// borrowed from `likely_stable` crate
#[allow(clippy::needless_bool)]
if (1i32).checked_div(if b { 1 } else { 0 }).is_some() {
true
} else {
false
}
}
#[inline(always)]
#[allow(unused)]
pub const fn unlikely(b: bool) -> bool {
// borrowed from `likely_stable` crate
#[allow(clippy::needless_bool)]
if (1i32).checked_div(if b { 0 } else { 1 }).is_none() {
true
} else {
false
}
}