Skip to main content

malachite_nz/gaussian_integer/arithmetic/
shl.rs

1// Copyright © 2026 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::gaussian_integer::GaussianInteger;
10use core::ops::{Shl, ShlAssign};
11
12macro_rules! impl_shl_unsigned {
13    ($t:ident) => {
14        impl Shl<$t> for GaussianInteger {
15            type Output = GaussianInteger;
16
17            /// Left-shifts a [`GaussianInteger`] (multiplies it by a power of 2), taking it by
18            /// value. Both parts are shifted.
19            ///
20            /// $$
21            /// f(x, k) = x2^k.
22            /// $$
23            ///
24            /// # Worst-case complexity
25            /// $T(n, m) = O(n + m)$
26            ///
27            /// $M(n, m) = O(n + m)$
28            ///
29            /// where $T$ is time, $M$ is additional memory, $n$ is the maximum number of
30            /// significant bits of the real and imaginary parts of `self`, and $m$ is `bits`.
31            ///
32            /// # Examples
33            /// See [here](super::shl#shl).
34            #[inline]
35            fn shl(self, bits: $t) -> GaussianInteger {
36                GaussianInteger {
37                    real: self.real << bits,
38                    imaginary: self.imaginary << bits,
39                }
40            }
41        }
42
43        impl Shl<$t> for &GaussianInteger {
44            type Output = GaussianInteger;
45
46            /// Left-shifts a [`GaussianInteger`] (multiplies it by a power of 2), taking it by
47            /// reference. Both parts are shifted.
48            ///
49            /// $$
50            /// f(x, k) = x2^k.
51            /// $$
52            ///
53            /// # Worst-case complexity
54            /// $T(n, m) = O(n + m)$
55            ///
56            /// $M(n, m) = O(n + m)$
57            ///
58            /// where $T$ is time, $M$ is additional memory, $n$ is the maximum number of
59            /// significant bits of the real and imaginary parts of `self`, and $m$ is `bits`.
60            ///
61            /// # Examples
62            /// See [here](super::shl#shl).
63            #[inline]
64            fn shl(self, bits: $t) -> GaussianInteger {
65                GaussianInteger {
66                    real: &self.real << bits,
67                    imaginary: &self.imaginary << bits,
68                }
69            }
70        }
71
72        impl ShlAssign<$t> for GaussianInteger {
73            /// Left-shifts a [`GaussianInteger`] (multiplies it by a power of 2), in place. Both
74            /// parts are shifted.
75            ///
76            /// $$
77            /// x \gets x2^k.
78            /// $$
79            ///
80            /// # Worst-case complexity
81            /// $T(n, m) = O(n + m)$
82            ///
83            /// $M(n, m) = O(n + m)$
84            ///
85            /// where $T$ is time, $M$ is additional memory, $n$ is the maximum number of
86            /// significant bits of the real and imaginary parts of `self`, and $m$ is `bits`.
87            ///
88            /// # Examples
89            /// See [here](super::shl#shl_assign).
90            #[inline]
91            fn shl_assign(&mut self, bits: $t) {
92                self.real <<= bits;
93                self.imaginary <<= bits;
94            }
95        }
96    };
97}
98apply_to_unsigneds!(impl_shl_unsigned);