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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// \file src/macros.rs
//! macros for
/*
macro_rules! forward_val_val_binop {
    (impl $imp:ident for $res:ty, $method:ident) => {
        impl $imp<$res> for $res {
            type Output = $res;

            #[inline]
            fn $method(self, other: $res) -> $res {
                // forward to val-ref
                $imp::$method(self, &other)
            }
        }
    };
}

*/
macro_rules! forward_ref_val_binop {
    (impl $imp:ident for $res:ty, $method:ident) => {
        impl<'a> $imp<$res> for &'a $res {
            type Output = $res;

            #[inline]
            fn $method(self, other: $res) -> $res {
                // forward to ref-ref
                $imp::$method(self, &other)
            }
        }
    };
}

/*
macro_rules! forward_val_ref_binop {
    (impl $imp:ident for $res:ty, $method:ident) => {
        impl<'a> $imp<&'a $res> for $res {
            type Output = $res;

            #[inline]
            fn $method(self, other: &$res) -> $res {
                // forward to ref-ref
                $imp::$method(&self, other)
            }
        }
    };
}

// Forward everything to ref-ref, when reusing storage is not helpful
macro_rules! forward_all_binop_to_ref_ref {
    (impl $imp:ident for $res:ty, $method:ident) => {
        forward_val_val_binop!(impl $imp for $res, $method);
        forward_val_ref_binop!(impl $imp for $res, $method);
        forward_ref_val_binop!(impl $imp for $res, $method);
    };
}
*/

macro_rules! forward_val_assignop {
    (impl $imp:ident for $res:ty, $method:ident) => {
        impl $imp<$res> for $res {
            #[inline]
            fn $method(&mut self, other: $res) {
                // forward to mutref-ref
                $imp::$method(self, &other)
            }
        }
    };
}

macro_rules! impl_div_for_uint_primitive {
    // (impl $imp:ident for $res:ty, $method:ident) => {
    ($res:ty) => {
        impl<'a> Div<$res> for &'a BigDecimal {
            type Output = BigDecimal;

            #[inline]
            fn div(self, den: $res) -> Self::Output {
                if den == 1 {
                    self.clone()
                } else if den == 2 {
                    self.half()
                } else {
                    self / BigDecimal::from(den)
                }
            }
        }

        impl<'a> Div<&'a BigDecimal> for $res {
            type Output = BigDecimal;

            #[inline(always)]
            fn div(self, den: &'a BigDecimal) -> Self::Output {
                BigDecimal::from(self) / den
            }
        }

        impl Div<BigDecimal> for $res {
            type Output = BigDecimal;

            #[inline(always)]
            fn div(self, den: BigDecimal) -> Self::Output {
                BigDecimal::from(self) / den
            }
        }
    };
}

macro_rules! impl_div_for_int_primitive {
    // (impl $imp:ident for $res:ty, $method:ident) => {
    ($res:ty) => {
        impl<'a> Div<$res> for BigDecimal {
            type Output = BigDecimal;

            #[inline(always)]
            fn div(self, den: $res) -> Self::Output {
                if den < 0 {
                    -Div::div(self, -den)
                } else if den == 1 {
                    self
                } else if den == 2 {
                    self.half()
                } else {
                    self / BigDecimal::from(den)
                }
            }
        }

        impl<'a> Div<$res> for &'a BigDecimal {
            type Output = BigDecimal;

            #[inline(always)]
            fn div(self, den: $res) -> Self::Output {
                if den < 0 {
                    -Div::div(self, -den)
                } else if den == 1 {
                    self.clone()
                } else if den == 2 {
                    self.half()
                } else {
                    self / BigDecimal::from(den)
                }
            }
        }

        impl<'a> Div<&'a BigDecimal> for $res {
            type Output = BigDecimal;

            #[inline(always)]
            fn div(self, den: &'a BigDecimal) -> Self::Output {
                match (self < 0, den.is_negative()) {
                    (true, true) => -self / -den,
                    (true, false) => (-self / den).neg(),
                    (false, true) => (-self / den.abs()),
                    (false, false) => BigDecimal::from(self) / den,
                }
            }
        }

        impl Div<BigDecimal> for $res {
            type Output = BigDecimal;

            #[inline(always)]
            fn div(self, den: BigDecimal) -> Self::Output {
                match (self < 0, den.is_negative()) {
                    (true, true) => -self / -den,
                    (true, false) => (-self / den).neg(),
                    (false, true) => (-self / den.abs()),
                    (false, false) => BigDecimal::from(self) / den,
                }
            }
        }
    };
}

macro_rules! impl_div_for_float_primitive {
    // (impl $imp:ident for $res:ty, $method:ident) => {
    ($res:ty) => {
        impl<'a> Div<$res> for &'a BigDecimal {
            type Output = BigDecimal;

            #[inline]
            #[allow(clippy::float_cmp)]
            fn div(self, den: $res) -> Self::Output {
                if den.is_nan() {
                    BigDecimal::zero()
                } else if den == 1.0 {
                    self.clone()
                } else if den == 0.5 {
                    self.double()
                } else if den == 2.0 {
                    self.half()
                } else if den == -1.0 {
                    -self
                } else if den < 0.0 && self.is_positive() {
                    -(self / -den)
                } else {
                    // Unwrap is safe, because `is_nan` checked above
                    self / BigDecimal::try_from(den).unwrap()
                }
            }
        }

        impl<'a> Div<&'a BigDecimal> for $res {
            type Output = BigDecimal;
            #[inline(always)]
            fn div(self, den: &'a BigDecimal) -> Self::Output {
                if self.is_nan() {
                    BigDecimal::zero()
                } else {
                    BigDecimal::try_from(self).unwrap() / den
                }
            }
        }

        impl Div<BigDecimal> for $res {
            type Output = BigDecimal;
            #[inline(always)]
            fn div(self, den: BigDecimal) -> Self::Output {
                if self.is_nan() {
                    BigDecimal::zero()
                } else {
                    BigDecimal::try_from(self).unwrap() / den
                }
            }
        }
    };
}

macro_rules! forward_primitive_types {
    (floats => $macro_name:ident) => {
        $macro_name!(f32);
        $macro_name!(f64);
    };
    (ints => $macro_name:ident) => {
        $macro_name!(i8);
        $macro_name!(i16);
        $macro_name!(i32);
        $macro_name!(i64);
    };
    (uints => $macro_name:ident) => {
        $macro_name!(u8);
        $macro_name!(u16);
        $macro_name!(u32);
        $macro_name!(u64);
    };
}

macro_rules! impl_div_for_primitives {
    () => {
        forward_primitive_types!(floats => impl_div_for_float_primitive);
        forward_primitive_types!(ints => impl_div_for_int_primitive);
        forward_primitive_types!(uints => impl_div_for_uint_primitive);
    };
}