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
use crate::num::arithmetic::traits::{ArithmeticCheckedShl, ArithmeticCheckedShr, UnsignedAbs};
use crate::num::basic::signeds::PrimitiveSigned;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use std::ops::Shr;

fn arithmetic_checked_shr_unsigned_signed<
    T: ArithmeticCheckedShl<U, Output = T> + PrimitiveUnsigned + Shr<U, Output = T>,
    U: PrimitiveUnsigned,
    S: PrimitiveSigned + UnsignedAbs<Output = U>,
>(
    x: T,
    bits: S,
) -> Option<T> {
    if bits < S::ZERO {
        x.arithmetic_checked_shl(bits.unsigned_abs())
    } else {
        let abs_bits = bits.unsigned_abs();
        Some(if abs_bits >= U::wrapping_from(T::WIDTH) {
            T::ZERO
        } else {
            x >> abs_bits
        })
    }
}

macro_rules! impl_arithmetic_checked_shr_unsigned_signed {
    ($t:ident) => {
        macro_rules! impl_arithmetic_checked_shr_unsigned_signed_inner {
            ($u:ident) => {
                impl ArithmeticCheckedShr<$u> for $t {
                    type Output = $t;

                    /// Shifts a number right (divides it by a power of 2). If the result is too
                    /// large to be represented, `None` is returned.
                    ///
                    /// Zero may be shifted by any amount, and any number may be shifted by any non-
                    /// negative amount; shifting by a large amount returns `Some(0)`.
                    ///
                    /// $$
                    /// f(x, b) = \\begin{cases}
                    ///     \operatorname{Some}(\lfloor x/2^b \rfloor) &
                    ///         \text{if} \\quad b \geq 0, \\\\
                    ///     \operatorname{Some}(2^{-b} x) &
                    ///         \text{if} \\quad b < 0 \\ \mathrm{and} \\ 2^{-b} x < 2^W, \\\\
                    ///     \operatorname{None} &
                    ///         \text{if} \\quad b < 0 \\ \mathrm{and} \\ 2^{-b} x \geq 2^W, \\\\
                    /// \\end{cases}
                    /// $$
                    /// where $W$ is `Self::WIDTH`.
                    ///
                    /// # Worst-case complexity
                    /// Constant time and additional memory.
                    ///
                    /// # Examples
                    /// See [here](super::arithmetic_checked_shr#arithmetic_checked_shr).
                    #[inline]
                    fn arithmetic_checked_shr(self, bits: $u) -> Option<$t> {
                        arithmetic_checked_shr_unsigned_signed(self, bits)
                    }
                }
            };
        }
        apply_to_signeds!(impl_arithmetic_checked_shr_unsigned_signed_inner);
    };
}
apply_to_unsigneds!(impl_arithmetic_checked_shr_unsigned_signed);

fn arithmetic_checked_shr_signed_signed<
    T: ArithmeticCheckedShl<U, Output = T> + PrimitiveSigned + Shr<U, Output = T>,
    U: PrimitiveUnsigned,
    S: PrimitiveSigned + UnsignedAbs<Output = U>,
>(
    x: T,
    bits: S,
) -> Option<T> {
    if bits < S::ZERO {
        x.arithmetic_checked_shl(bits.unsigned_abs())
    } else {
        let width = U::wrapping_from(T::WIDTH);
        let abs_bits = bits.unsigned_abs();
        Some(if width != U::ZERO && abs_bits >= width {
            -T::iverson(x < T::ZERO)
        } else {
            x >> abs_bits
        })
    }
}

macro_rules! impl_arithmetic_checked_shr_signed_signed {
    ($t:ident) => {
        macro_rules! impl_arithmetic_checked_shr_signed_signed_inner {
            ($u:ident) => {
                impl ArithmeticCheckedShr<$u> for $t {
                    type Output = $t;

                    /// Shifts a number right (divides it by a power of 2). If the result is too
                    /// large to be represented, `None` is returned.
                    ///
                    /// Zero may be shifted by any amount, and any number may be shifted by any non-
                    /// negative amount; shifting by a large amount returns `Some(0)` if `self` is
                    /// positive, and `Some(-1)` if `self` is negative.
                    ///
                    /// $$
                    /// f(x, b) = \\begin{cases}
                    ///     \operatorname{Some}(\lfloor x/2^b \rfloor) &
                    ///         \text{if} \\quad b \geq 0, \\\\
                    ///     \operatorname{Some}(2^{-b} x) &
                    ///         \text{if} \\quad b < 0 \\ \mathrm{and}
                    ///         \\ -2^{W-1} \leq 2^{-b} x < 2^{W-1}, \\\\
                    ///     \operatorname{None} &
                    ///         \text{if} \\quad b < 0 \\ \mathrm{and}
                    ///         \\ (2^{-b} x < -2^{W-1} \\ \mathrm{or}
                    ///         \\ 2^{-b} x \geq 2^{W-1}), \\\\
                    /// \\end{cases}
                    /// $$
                    /// where $W$ is `Self::WIDTH`.
                    ///
                    /// # Worst-case complexity
                    /// Constant time and additional memory.
                    ///
                    /// # Examples
                    /// See [here](super::arithmetic_checked_shr#arithmetic_checked_shr).
                    #[inline]
                    fn arithmetic_checked_shr(self, bits: $u) -> Option<$t> {
                        arithmetic_checked_shr_signed_signed(self, bits)
                    }
                }
            };
        }
        apply_to_signeds!(impl_arithmetic_checked_shr_signed_signed_inner);
    };
}
apply_to_signeds!(impl_arithmetic_checked_shr_signed_signed);