malachite_base/num/arithmetic/overflowing_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::{
10 OverflowingMulAssign, OverflowingSquare, OverflowingSquareAssign,
11};
12
13macro_rules! impl_overflowing_square {
14 ($t:ident) => {
15 impl OverflowingSquare for $t {
16 type Output = $t;
17
18 /// Squares a number.
19 ///
20 /// Returns a tuple containing the result and a boolean indicating whether an arithmetic
21 /// occurred. If an overflow occurred, then the wrapped value is returned.
22 ///
23 /// # Worst-case complexity
24 /// Constant time and additional memory.
25 ///
26 /// # Examples
27 /// See [here](super::overflowing_square#overflowing_square).
28 #[inline]
29 fn overflowing_square(self) -> ($t, bool) {
30 self.overflowing_mul(self)
31 }
32 }
33
34 impl OverflowingSquareAssign for $t {
35 /// Squares a number in place.
36 ///
37 /// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow
38 /// occurred, then the wrapped value is assigned.
39 ///
40 /// # Worst-case complexity
41 /// Constant time and additional memory.
42 ///
43 /// # Examples
44 /// See [here](super::overflowing_square#overflowing_square_assign).
45 #[inline]
46 fn overflowing_square_assign(&mut self) -> bool {
47 self.overflowing_mul_assign(*self)
48 }
49 }
50 };
51}
52apply_to_primitive_ints!(impl_overflowing_square);