malachite_base/num/arithmetic/wrapping_add.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::{WrappingAdd, WrappingAddAssign};
10
11macro_rules! impl_wrapping_add {
12 ($t:ident) => {
13 impl WrappingAdd<$t> for $t {
14 type Output = $t;
15
16 /// This is a wrapper over the `wrapping_add` functions in the standard library, for
17 /// example [this one](u32::wrapping_add).
18 #[inline]
19 fn wrapping_add(self, other: $t) -> $t {
20 $t::wrapping_add(self, other)
21 }
22 }
23
24 impl WrappingAddAssign<$t> for $t {
25 /// Adds a number to another number in place, wrapping around at the boundary of the
26 /// type.
27 ///
28 /// $x \gets z$, where $z \equiv x + y \mod 2^W$ and $W$ is `Self::WIDTH`.
29 ///
30 /// # Worst-case complexity
31 /// Constant time and additional memory.
32 ///
33 /// # Examples
34 /// See [here](super::wrapping_add#wrapping_add_assign).
35 #[inline]
36 fn wrapping_add_assign(&mut self, other: $t) {
37 *self = self.wrapping_add(other);
38 }
39 }
40 };
41}
42apply_to_primitive_ints!(impl_wrapping_add);