1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! Math utilities.

/// Variadic version of [`core::cmp::max`].
///
/// # Examples
///
/// ```
/// let max = mycelium_util::math::max![1, 2, 3, 4];
/// assert_eq!(max, 4);
/// ```
#[macro_export]
macro_rules! max {
    ($arg:expr) => { $arg };
    ($arg1:expr, $($arg:expr),+) => {
        core::cmp::max($arg1, $crate::max!( $($arg),+ ))
    };
}

/// Variadic version of [`core::cmp::min`].
///
/// # Examples
///
/// ```
/// let min = mycelium_util::math::min![1, 2, 3, 4];
/// assert_eq!(min, 1);
/// ```
#[macro_export]
macro_rules! min {
    ($arg:expr) => { $arg };
    ($arg1:expr, $($arg:expr),+) => {
        core::cmp::min($arg1, $crate::min!( $($arg),+ ))
    };
}

#[doc(inline)]
pub use max;

#[doc(inline)]
pub use min;

/// Extension trait adding logarithm methods to integers.
pub trait Logarithm: Sized {
    /// Returns `ceiling(log2(self))`.
    fn log2_ceil(self) -> Self;

    /// Returns `log2(self)`.
    fn log2(self) -> Self;

    /// Returns the integer logarithm base `base` of `self`, or `None` if it is
    /// not possible  to take the log base `base` of `self`.
    fn checked_log(self, base: Self) -> Option<Self>;

    /// Returns the integer logarithm base `base` of `self`.
    fn log(self, base: Self) -> Self;
}

impl Logarithm for usize {
    #[inline(always)]
    #[must_use = "this returns the result of the operation, \
                    without modifying the original"]
    fn log2_ceil(self) -> usize {
        usize_const_log2_ceil(self)
    }

    #[inline(always)]
    #[must_use = "this returns the result of the operation, \
                    without modifying the original"]
    fn log2(self) -> usize {
        usize_const_log2(self)
    }

    #[inline(always)]
    #[must_use = "this returns the result of the operation, \
                    without modifying the original"]
    fn checked_log(self, base: usize) -> Option<Self> {
        usize_const_checked_log(self, base)
    }

    #[inline(always)]
    #[must_use = "this returns the result of the operation, \
                    without modifying the original"]
    fn log(self, base: usize) -> Self {
        match self.checked_log(base) {
            Some(log) => log,
            None => panic!("cannot take log base {base} of {self}"),
        }
    }
}

/// Returns `ceiling(log2(n))`.
///
/// This is exposed in addition to the [`Logarithm`] extension trait because it
/// is a  `const fn`, while trait methods cannot be `const fn`s.
#[inline(always)]
#[must_use = "this returns the result of the operation, \
                without modifying the original"]
pub const fn usize_const_log2_ceil(n: usize) -> usize {
    n.next_power_of_two().trailing_zeros() as usize
}

/// Returns `log2(n)`.
///
/// This is exposed in addition to the [`Logarithm`] extension trait because it
/// is a
/// `const fn`, while trait methods cannot be `const fn`s.
#[inline(always)]
#[must_use = "this returns the result of the operation, \
                without modifying the original"]
pub const fn usize_const_log2(n: usize) -> usize {
    (usize::BITS - 1) as usize - n.leading_zeros() as usize
}

/// Returns the `base` logarithm of `n`.
///
/// This is exposed in addition to the [`Logarithm`] extension trait because it
/// is a `const fn`, while trait methods cannot be `const fn`s.
#[inline(always)]
#[must_use = "this returns the result of the operation, \
                without modifying the original"]
pub const fn usize_const_checked_log(mut n: usize, base: usize) -> Option<usize> {
    if n == 0 || base <= 1 {
        return None;
    }

    if base == 2 {
        return Some(usize_const_log2(n));
    }

    let mut log = 0;
    while n >= base {
        n /= base;
        log += 1;
    }
    Some(log)
}

#[cfg(all(test, not(loom)))]
#[test]
fn test_log2_ceil() {
    assert_eq!(0, 0.log2_ceil());
    assert_eq!(0, 1.log2_ceil());
    assert_eq!(1, 2.log2_ceil());
    assert_eq!(5, 32.log2_ceil());
    assert_eq!(10, 1024.log2_ceil());
}