From 9bd198f5efa116441da68035c7b54c55c49f68e1 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 9 Jul 2024 17:47:53 +0200 Subject: [PATCH] Constant log2 (#49) --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 870ed57..322d5f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,8 @@ pub use num_traits::{One, Zero}; /// assert_eq!(log2(1 << 15), 15); /// assert_eq!(log2(2usize.pow(18)), 18); /// ``` -pub fn log2(x: usize) -> u32 { +#[inline(always)] +pub const fn log2(x: usize) -> u32 { if x == 0 { 0 } else if x.is_power_of_two() {