malachite_base/num/arithmetic/
rotate.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    RotateLeft, RotateLeftAssign, RotateRight, RotateRightAssign,
11};
12use crate::num::conversion::traits::WrappingFrom;
13
14macro_rules! impl_rotate {
15    ($t:ident) => {
16        impl RotateLeft for $t {
17            type Output = $t;
18
19            /// This is a wrapper over the `rotate_left` functions in the standard library, for
20            /// example [this one](u32::rotate_left).
21            #[inline]
22            fn rotate_left(self, n: u64) -> $t {
23                $t::rotate_left(self, u32::wrapping_from(n))
24            }
25        }
26
27        impl RotateLeftAssign for $t {
28            /// Rotates a number left, in place.
29            ///
30            /// # Worst-case complexity
31            /// Constant time and additional memory.
32            ///
33            /// # Examples
34            /// See [here](super::rotate#rotate_left_assign).
35            #[inline]
36            fn rotate_left_assign(&mut self, n: u64) {
37                *self = self.rotate_left(u32::wrapping_from(n));
38            }
39        }
40
41        impl RotateRight for $t {
42            type Output = $t;
43
44            /// This is a wrapper over the `rotate_right` functions in the standard library, for
45            /// example [this one](u32::rotate_right).
46            #[inline]
47            fn rotate_right(self, n: u64) -> $t {
48                $t::rotate_right(self, u32::wrapping_from(n))
49            }
50        }
51
52        impl RotateRightAssign for $t {
53            /// Rotates a number right, in place.
54            ///
55            /// # Worst-case complexity
56            /// Constant time and additional memory.
57            ///
58            /// # Examples
59            /// See [here](super::rotate#rotate_right_assign).
60            #[inline]
61            fn rotate_right_assign(&mut self, n: u64) {
62                *self = self.rotate_right(u32::wrapping_from(n));
63            }
64        }
65    };
66}
67apply_to_primitive_ints!(impl_rotate);