From 09976fb88990604935533dd36c3ab44d8b5f0864 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Tue, 30 Nov 2021 14:30:13 +0000 Subject: [PATCH] Add stable likely/unlikely to utils module --- src/lib.rs | 1 + src/utils.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/utils.rs diff --git a/src/lib.rs b/src/lib.rs index 14d4dc3..f1f7c5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod encode; mod error; mod header; mod pixel; +mod utils; pub mod canonical; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..224a456 --- /dev/null +++ b/src/utils.rs @@ -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 + } +}