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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#[inline]
pub const fn is_set(v: usize, i: u8) -> bool {
    ((v >> i) & 1) != 0
}

#[inline]
pub const fn unset(v: usize, i: u8) -> usize {
    v & !(1 << i)
}

#[inline]
pub const fn log2(v: usize) -> u32 {
    usize::BITS - 1 - v.leading_zeros()
}

#[cfg(test)]
mod tests {
    use super::is_set;

    #[test]
    fn test_is_set() {
        assert!(!is_set(0, 0));
        assert!(!is_set(0b10, 0));
        assert!(is_set(1, 0));
        assert!(is_set(0b1110_1111, 0));
        //
        assert!(!is_set(0, 1));
        assert!(!is_set(1, 1));
        assert!(is_set(0b10, 1));
        assert!(is_set(0b1010_1010, 1));
        assert!(is_set(0b1010_1011, 1));
        //
        assert!(!is_set(0, (usize::BITS - 1) as u8));
        assert!(is_set(usize::MAX, (usize::BITS - 1) as u8));
        assert!(is_set(
            usize::MAX - (usize::MAX >> 1),
            (usize::BITS - 1) as u8
        ));
    }

    const fn trailing_zeros_8e((mut v, mut extra): (u8, u8)) -> u8 {
        {
            let lo = v & 0xF;
            v = if lo == 0 {
                extra += 4;
                v >> 4
            } else {
                lo
            }
        };
        {
            let lo = v & 0x3;
            v = if lo == 0 {
                extra += 2;
                v >> 2
            } else {
                lo
            }
        };
        extra + (if v & 1 == 0 { 2 - (v >> 1) } else { 0 })
    }

    const fn trailing_zeros_8(v: u8) -> u8 {
        trailing_zeros_8e((v, 0))
    }

    const fn trailing_zeros_16e((v, extra): (u16, u8)) -> u8 {
        trailing_zeros_8e({
            let lo = v as u8;
            if lo == 0 {
                ((v >> 8) as u8, extra + 8)
            } else {
                (lo, extra)
            }
        })
    }

    const fn trailing_zeros_16(v: u16) -> u8 {
        trailing_zeros_16e((v, 0))
    }

    const fn trailing_zeros_32e((v, extra): (u32, u8)) -> u8 {
        trailing_zeros_16e({
            let lo = v as u16;
            if lo == 0 {
                ((v >> 16) as u16, extra + 16)
            } else {
                (lo, extra)
            }
        })
    }

    const fn trailing_zeros_32(v: u32) -> u8 {
        trailing_zeros_32e((v, 0))
    }

    const fn trailing_zeros_64(v: u64) -> u8 {
        trailing_zeros_32e({
            let lo = v as u32;
            if lo == 0 {
                ((v >> 32) as u32, 32)
            } else {
                (lo, 0)
            }
        })
    }

    #[test]
    fn soft_test64() {
        assert_eq!(trailing_zeros_64(0), 64);
        assert_eq!(trailing_zeros_64(1), 0);
        assert_eq!(trailing_zeros_64(2), 1);
        assert_eq!(trailing_zeros_64(3), 0);
        assert_eq!(trailing_zeros_64(4), 2);
        assert_eq!(trailing_zeros_64(7), 0);
        assert_eq!(trailing_zeros_64(8), 3);
        assert_eq!(trailing_zeros_64(0x10), 4);
        assert_eq!(trailing_zeros_64(0x20), 5);
        assert_eq!(trailing_zeros_64(0x40), 6);
        assert_eq!(trailing_zeros_64(0x80), 7);
        assert_eq!(trailing_zeros_64(0x8000_0000_0000_0000), 63);
        assert_eq!(trailing_zeros_64(0x8000_0000_0000_0001), 0);
        assert_eq!(trailing_zeros_64(0xC000_0000_0000_0000), 62);
        assert_eq!(trailing_zeros_64(0x4000_0000_0000_0000), 62);
        assert_eq!(trailing_zeros_64(0xE000_0000_0000_0000), 61);
        assert_eq!(trailing_zeros_64(0xF000_0000_0000_0000), 60);
    }

    #[test]
    fn soft_test32() {
        assert_eq!(trailing_zeros_32(0), 32);
        assert_eq!(trailing_zeros_32(1), 0);
        assert_eq!(trailing_zeros_32(2), 1);
        assert_eq!(trailing_zeros_32(3), 0);
        assert_eq!(trailing_zeros_32(4), 2);
        assert_eq!(trailing_zeros_32(7), 0);
        assert_eq!(trailing_zeros_32(8), 3);
        assert_eq!(trailing_zeros_32(0x10), 4);
        assert_eq!(trailing_zeros_32(0x20), 5);
        assert_eq!(trailing_zeros_32(0x40), 6);
        assert_eq!(trailing_zeros_32(0x80), 7);
        assert_eq!(trailing_zeros_32(0x8000_0000), 31);
        assert_eq!(trailing_zeros_32(0x8000_0001), 0);
        assert_eq!(trailing_zeros_32(0xC000_0000), 30);
        assert_eq!(trailing_zeros_32(0x4000_0000), 30);
        assert_eq!(trailing_zeros_32(0xE000_0000), 29);
        assert_eq!(trailing_zeros_32(0xF000_0000), 28);
    }

    #[test]
    fn soft_test16() {
        assert_eq!(trailing_zeros_16(0), 16);
        assert_eq!(trailing_zeros_16(1), 0);
        assert_eq!(trailing_zeros_16(2), 1);
        assert_eq!(trailing_zeros_16(3), 0);
        assert_eq!(trailing_zeros_16(4), 2);
        assert_eq!(trailing_zeros_16(7), 0);
        assert_eq!(trailing_zeros_16(8), 3);
        assert_eq!(trailing_zeros_16(0x10), 4);
        assert_eq!(trailing_zeros_16(0x20), 5);
        assert_eq!(trailing_zeros_16(0x40), 6);
        assert_eq!(trailing_zeros_16(0x80), 7);
        assert_eq!(trailing_zeros_16(0x8000), 15);
        assert_eq!(trailing_zeros_16(0x8001), 0);
        assert_eq!(trailing_zeros_16(0xC000), 14);
        assert_eq!(trailing_zeros_16(0x4000), 14);
        assert_eq!(trailing_zeros_16(0xE000), 13);
        assert_eq!(trailing_zeros_16(0xF000), 12);
    }

    #[test]
    fn soft_test8() {
        assert_eq!(trailing_zeros_8(0), 8);
        assert_eq!(trailing_zeros_8(1), 0);
        assert_eq!(trailing_zeros_8(2), 1);
        assert_eq!(trailing_zeros_8(3), 0);
        assert_eq!(trailing_zeros_8(4), 2);
        assert_eq!(trailing_zeros_8(7), 0);
        assert_eq!(trailing_zeros_8(8), 3);
        assert_eq!(trailing_zeros_8(0x10), 4);
        assert_eq!(trailing_zeros_8(0x20), 5);
        assert_eq!(trailing_zeros_8(0x40), 6);
        assert_eq!(trailing_zeros_8(0x80), 7);
        assert_eq!(trailing_zeros_8(0x82), 1);
        assert_eq!(trailing_zeros_16(0x81), 0);
        assert_eq!(trailing_zeros_16(0xC0), 6);
        assert_eq!(trailing_zeros_16(0x43), 0);
        assert_eq!(trailing_zeros_16(0xE0), 5);
        assert_eq!(trailing_zeros_16(0xF0), 4);
    }
}