mpir/ops/
mpz.rs

1//! mpz
2//!
3
4pub mod sub;
5pub mod add;
6pub mod mul;
7pub mod div;
8pub mod rem;
9pub mod cmp;
10
11use std::ops::{Neg, Not};
12use crate::ops::onforward_ref_unop;
13use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign};
14use std::ops::{Shl, ShlAssign, Shr, ShrAssign};
15use crate::ops::{onforward_ref_binop, onforward_ref_op_assign};
16// use std::ops::{Index, IndexMut};
17use crate::prim::{typ::*, mpz::*};
18
19onforward_ref_unop!{impl Neg, neg for mpz_s}
20
21/// impl Neg for mpz_r
22impl<'a> Neg for &'a mpz_s {
23  type Output = <mpz_s as Neg>::Output;
24
25  /// neg mpz_r
26  #[inline]
27  fn neg(self) -> <mpz_s as Neg>::Output {
28    let mut t = mpz_s::init();
29    mpz_neg(&mut t, self);
30    t
31  }
32}
33
34onforward_ref_unop!{impl Not, not for mpz_s}
35
36/// impl Not for mpz_r
37impl<'a> Not for &'a mpz_s {
38  type Output = <mpz_s as Not>::Output;
39
40  /// not mpz_r
41  #[inline]
42  fn not(self) -> <mpz_s as Not>::Output {
43    let mut t = mpz_s::init();
44    mpz_com(&mut t, self);
45    t
46  }
47}
48
49onforward_ref_binop!{impl BitAnd, bitand for mpz_s, mpz_s, mpz_s}
50
51/// impl BitAnd for mpz_r
52impl<'a, 'b> BitAnd<&'b mpz_s> for &'a mpz_s {
53  type Output = <mpz_s as BitAnd<mpz_s>>::Output;
54
55  /// bitand mpz_r &amp; mpz_r
56  #[inline]
57  fn bitand(self, rhs: &'b mpz_s) -> <mpz_s as BitAnd<mpz_s>>::Output {
58    let mut t = mpz_s::init();
59    mpz_and(&mut t, self, rhs);
60    t
61  }
62}
63
64onforward_ref_op_assign!{impl BitAndAssign, bitand_assign for mpz_s, mpz_s}
65
66/// impl BitAndAssign for mpz_s
67impl<'a> BitAndAssign<&'a Self> for mpz_s {
68  /// bitand_assign mpz_s &amp;= mpz_r
69  #[inline]
70  fn bitand_assign(&mut self, rhs: &'a Self) -> () {
71    self.set(&self.and(rhs));
72  }
73}
74
75onforward_ref_binop!{impl BitOr, bitor for mpz_s, mpz_s, mpz_s}
76
77/// impl BitOr for mpz_r
78impl<'a, 'b> BitOr<&'b mpz_s> for &'a mpz_s {
79  type Output = <mpz_s as BitOr<mpz_s>>::Output;
80
81  /// bitor mpz_r | mpz_r
82  #[inline]
83  fn bitor(self, rhs: &'b mpz_s) -> <mpz_s as BitOr<mpz_s>>::Output {
84    let mut t = mpz_s::init();
85    mpz_ior(&mut t, self, rhs);
86    t
87  }
88}
89
90onforward_ref_op_assign!{impl BitOrAssign, bitor_assign for mpz_s, mpz_s}
91
92/// impl BitOrAssign for mpz_s
93impl<'a> BitOrAssign<&'a Self> for mpz_s {
94  /// bitor_assign mpz_s |= mpz_r
95  #[inline]
96  fn bitor_assign(&mut self, rhs: &'a Self) -> () {
97    self.set(&self.ior(rhs));
98  }
99}
100
101onforward_ref_binop!{impl BitXor, bitxor for mpz_s, mpz_s, mpz_s}
102
103/// impl BitXor for mpz_r
104impl<'a, 'b> BitXor<&'b mpz_s> for &'a mpz_s {
105  type Output = <mpz_s as BitXor<mpz_s>>::Output;
106
107  /// bitxor mpz_r &amp; mpz_r
108  #[inline]
109  fn bitxor(self, rhs: &'b mpz_s) -> <mpz_s as BitXor<mpz_s>>::Output {
110    let mut t = mpz_s::init();
111    mpz_xor(&mut t, self, rhs);
112    t
113  }
114}
115
116onforward_ref_op_assign!{impl BitXorAssign, bitxor_assign for mpz_s, mpz_s}
117
118/// impl BitXorAssign for mpz_s
119impl<'a> BitXorAssign<&'a Self> for mpz_s {
120  /// bitxor_assign mpz_s &amp;= mpz_r
121  #[inline]
122  fn bitxor_assign(&mut self, rhs: &'a Self) -> () {
123    self.set(&self.xor(rhs));
124  }
125}
126
127onforward_ref_binop!{impl Shl, shl for mpz_s, mp_bitcnt_t, mpz_s}
128
129/// impl Shl for mpz_r
130impl<'a, 'b> Shl<&'b mp_bitcnt_t> for &'a mpz_s {
131  type Output = <mpz_s as Shl<mp_bitcnt_t>>::Output;
132
133  /// shl mpz_r << mp_bitcnt_t
134  #[inline]
135  fn shl(self, rhs: &'b mp_bitcnt_t) -> <mpz_s as Shl<mp_bitcnt_t>>::Output {
136    let mut t = mpz_s::init();
137    mpz_mul_2exp(&mut t, self, *rhs);
138    t
139  }
140}
141
142onforward_ref_op_assign!{impl ShlAssign, shl_assign for mpz_s, mp_bitcnt_t}
143
144/// impl ShlAssign for mpz_s
145impl<'a> ShlAssign<&'a mp_bitcnt_t> for mpz_s {
146  /// shl_assign mpz_s <<= mp_bitcnt_t
147  #[inline]
148  fn shl_assign(&mut self, rhs: &'a mp_bitcnt_t) -> () {
149    self.mul_2exp(*rhs);
150  }
151}
152
153onforward_ref_binop!{impl Shr, shr for mpz_s, mp_bitcnt_t, mpz_s}
154
155/// impl Shr for mpz_r
156impl<'a, 'b> Shr<&'b mp_bitcnt_t> for &'a mpz_s {
157  type Output = <mpz_s as Shr<mp_bitcnt_t>>::Output;
158
159  /// shr mpz_r >> mp_bitcnt_t
160  #[inline]
161  fn shr(self, rhs: &'b mp_bitcnt_t) -> <mpz_s as Shr<mp_bitcnt_t>>::Output {
162    let mut t = mpz_s::init();
163    mpz_tdiv_q_2exp(&mut t, self, *rhs);
164    t
165  }
166}
167
168onforward_ref_op_assign!{impl ShrAssign, shr_assign for mpz_s, mp_bitcnt_t}
169
170/// impl ShrAssign for mpz_s
171impl<'a> ShrAssign<&'a mp_bitcnt_t> for mpz_s {
172  /// shr_assign mpz_s >>= mp_bitcnt_t
173  #[inline]
174  fn shr_assign(&mut self, rhs: &'a mp_bitcnt_t) -> () {
175    self.set(&self.tdiv_q_2exp(*rhs));
176  }
177}