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