File tree 1 file changed +23
-0
lines changed
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,29 @@ pub mod error;
45
45
#[ doc( hidden) ]
46
46
pub use std:: * ;
47
47
48
+ /// Returns the base-2 logarithm of `x`.
49
+ ///
50
+ /// ```
51
+ /// use ark_std::log2;
52
+ ///
53
+ /// assert_eq!(log2(16), 4);
54
+ /// assert_eq!(log2(17), 5);
55
+ /// assert_eq!(log2(1), 0);
56
+ /// assert_eq!(log2(0), 0);
57
+ /// assert_eq!(log2(usize::MAX), (core::mem::size_of::<usize>() * 8) as u32);
58
+ /// assert_eq!(log2(1 << 15), 15);
59
+ /// assert_eq!(log2(2usize.pow(18)), 18);
60
+ /// ```
61
+ pub fn log2 ( x : usize ) -> u32 {
62
+ if x == 0 {
63
+ 0
64
+ } else if x. is_power_of_two ( ) {
65
+ 1usize . leading_zeros ( ) - x. leading_zeros ( )
66
+ } else {
67
+ 0usize . leading_zeros ( ) - x. leading_zeros ( )
68
+ }
69
+ }
70
+
48
71
/// Creates parallel iterator over refs if `parallel` feature is enabled.
49
72
#[ macro_export]
50
73
macro_rules! cfg_iter {
You can’t perform that action at this time.
0 commit comments