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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use num::arithmetic::traits::{CeilingLogBase, CheckedLogBase, FloorLogBase};
use num::basic::unsigneds::PrimitiveUnsigned;

pub_test! {floor_log_base_naive<T: PrimitiveUnsigned>(x: T, base: T) -> u64 {
    assert_ne!(x, T::ZERO);
    assert!(base > T::ONE);
    let mut result = 0;
    let mut p = T::ONE;
    // loop always executes at least once
    while p <= x {
        result += 1;
        if let Some(next_p) = p.checked_mul(base) {
            p = next_p;
        } else {
            break;
        }
    }
    result - 1
}}

pub_test! {ceiling_log_base_naive<T: PrimitiveUnsigned>(x: T, base: T) -> u64 {
    assert_ne!(x, T::ZERO);
    assert!(base > T::ONE);
    let mut result = 0;
    let mut p = T::ONE;
    while p < x {
        result += 1;
        if let Some(next_p) = p.checked_mul(base) {
            p = next_p;
        } else {
            break;
        }
    }
    result
}}

pub_test! {checked_log_base_naive<T: PrimitiveUnsigned>(x: T, base: T) -> Option<u64> {
    assert_ne!(x, T::ZERO);
    assert!(base > T::ONE);
    let mut result = 0;
    let mut p = T::ONE;
    while p < x {
        result += 1;
        if let Some(next_p) = p.checked_mul(base) {
            p = next_p;
        } else {
            return None;
        }
    }
    if p == x {
        Some(result)
    } else {
        None
    }
}}

fn floor_log_base<T: PrimitiveUnsigned>(x: T, base: T) -> u64 {
    if let Some(log_base) = base.checked_log_base_2() {
        x.floor_log_base_power_of_2(log_base)
    } else {
        floor_log_base_naive(x, base)
    }
}

fn ceiling_log_base<T: PrimitiveUnsigned>(x: T, base: T) -> u64 {
    if let Some(log_base) = base.checked_log_base_2() {
        x.ceiling_log_base_power_of_2(log_base)
    } else {
        ceiling_log_base_naive(x, base)
    }
}

fn checked_log_base<T: PrimitiveUnsigned>(x: T, base: T) -> Option<u64> {
    if let Some(log_base) = base.checked_log_base_2() {
        x.checked_log_base_power_of_2(log_base)
    } else {
        checked_log_base_naive(x, base)
    }
}

macro_rules! impl_log_base_unsigned {
    ($t:ident) => {
        impl FloorLogBase for $t {
            type Output = u64;

            /// Returns the floor of the base-$b$ logarithm of a positive integer.
            ///
            /// $f(x, b) = \lfloor\log_b x\rfloor$.
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n)$
            ///
            /// $M(n) = O(1)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is
            /// `self.significant_bits() / base.significant_bits()`.
            ///
            /// # Panics
            /// Panics if `self` is 0 or `base` is less than 2.
            ///
            /// # Examples
            /// See [here](super::log_base#floor_log_base).
            #[inline]
            fn floor_log_base(self, base: $t) -> u64 {
                floor_log_base(self, base)
            }
        }

        impl CeilingLogBase for $t {
            type Output = u64;

            /// Returns the ceiling of the base-$b$ logarithm of a positive integer.
            ///
            /// $f(x, b) = \lceil\log_b x\rceil$.
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n)$
            ///
            /// $M(n) = O(1)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is
            /// `self.significant_bits() / base.significant_bits()`.
            ///
            /// # Panics
            /// Panics if `self` is 0 or `base` is less than 2.
            ///
            /// # Examples
            /// See [here](super::log_base#ceiling_log_base).
            #[inline]
            fn ceiling_log_base(self, base: $t) -> u64 {
                ceiling_log_base(self, base)
            }
        }

        impl CheckedLogBase for $t {
            type Output = u64;

            /// Returns the base-$b$ logarithm of a positive integer. If the integer is not a
            /// power of $b$, `None` is returned.
            ///
            /// $$
            /// f(x, b) = \\begin{cases}
            ///     \operatorname{Some}(\log_b x) & \text{if} \\quad \log_b x \in \Z, \\\\
            ///     \operatorname{None} & \textrm{otherwise}.
            /// \\end{cases}
            /// $$
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n)$
            ///
            /// $M(n) = O(1)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is
            /// `self.significant_bits() / base.significant_bits()`.
            ///
            /// # Panics
            /// Panics if `self` is 0 or `base` is less than 2.
            ///
            /// # Examples
            /// See [here](super::log_base#checked_log_base).
            #[inline]
            fn checked_log_base(self, base: $t) -> Option<u64> {
                checked_log_base(self, base)
            }
        }
    };
}
apply_to_unsigneds!(impl_log_base_unsigned);