malachite_base/num/arithmetic/overflowing_div.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::arithmetic::traits::{OverflowingDiv, OverflowingDivAssign};
10
11macro_rules! impl_overflowing_div {
12 ($t:ident) => {
13 impl OverflowingDiv<$t> for $t {
14 type Output = $t;
15
16 /// This is a wrapper over the `overflowing_div` functions in the standard library, for
17 /// example [this one](u32::overflowing_div).
18 #[inline]
19 fn overflowing_div(self, other: $t) -> ($t, bool) {
20 $t::overflowing_div(self, other)
21 }
22 }
23
24 impl OverflowingDivAssign<$t> for $t {
25 /// Divides a number by another number, in place.
26 ///
27 /// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow
28 /// occurred, then the wrapped value is assigned. Overflow only occurs when `Self` is
29 /// signed, `self` is `Self::MIN`, and `other` is -1. The "actual" result, `-Self::MIN`,
30 /// can't be represented and is wrapped back to `Self::MIN`.
31 ///
32 /// # Worst-case complexity
33 /// Constant time and additional memory.
34 ///
35 /// # Examples
36 /// See [here](super::overflowing_div#overflowing_div_assign).
37 #[inline]
38 fn overflowing_div_assign(&mut self, other: $t) -> bool {
39 let overflow;
40 (*self, overflow) = self.overflowing_div(other);
41 overflow
42 }
43 }
44 };
45}
46apply_to_primitive_ints!(impl_overflowing_div);