malachite_base/num/arithmetic/
wrapping_square.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::{WrappingMulAssign, WrappingSquare, WrappingSquareAssign};
10
11macro_rules! impl_wrapping_square {
12    ($t:ident) => {
13        impl WrappingSquare for $t {
14            type Output = $t;
15
16            /// Squares a number, wrapping around at the boundary of the type.
17            ///
18            /// $f(x) = y$, where $y \equiv x^2 \mod 2^W$ and $W$ is `Self::WIDTH`.
19            ///
20            /// # Worst-case complexity
21            /// Constant time and additional memory.
22            ///
23            /// # Examples
24            /// See [here](super::wrapping_square#wrapping_square).
25            #[inline]
26            fn wrapping_square(self) -> $t {
27                self.wrapping_mul(self)
28            }
29        }
30
31        impl WrappingSquareAssign for $t {
32            /// Squares a number in place, wrapping around at the boundary of the type.
33            ///
34            /// $x \gets y$, where $y \equiv x^2 \mod 2^W$ and $W$ is `Self::WIDTH`.
35            ///
36            /// # Worst-case complexity
37            /// Constant time and additional memory.
38            ///
39            /// # Examples
40            /// See [here](super::wrapping_square#wrapping_square_assign).
41            #[inline]
42            fn wrapping_square_assign(&mut self) {
43                self.wrapping_mul_assign(*self);
44            }
45        }
46    };
47}
48apply_to_primitive_ints!(impl_wrapping_square);