1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use apint::{APInt};
use apint::utils::{ModelMut, ZipModelMut};
use traits::{APIntMutImpl};
use errors::{Result};

use std::ops::{
	Neg,
	Add,
	Mul,
	AddAssign,
	MulAssign
};

impl Neg for APInt {
	type Output = APInt;

	fn neg(mut self) -> Self::Output {
		self.negate_inplace();
		self
	}
}

impl<'a> Add<&'a APInt> for APInt {
	type Output = APInt;

	fn add(mut self, rhs: &'a APInt) -> Self::Output {
		self.checked_add_assign(rhs).unwrap();
		self
	}
}

impl<'a, 'b> Add<&'a APInt> for &'b APInt {
	type Output = APInt;

	fn add(self, rhs: &'a APInt) -> Self::Output {
		let mut cloned = self.clone();
		cloned.checked_add_assign(rhs).unwrap();
		cloned
	}
}

impl<'a> AddAssign<&'a APInt> for APInt {
	fn add_assign(&mut self, rhs: &'a APInt) {
		self.checked_add_assign(rhs).unwrap();
	}
}

impl<'a> Mul<&'a APInt> for APInt {
	type Output = APInt;

	fn mul(mut self, rhs: &'a APInt) -> Self::Output {
		self.checked_mul_assign(rhs).unwrap();
		self
	}
}

impl<'a, 'b> Mul<&'a APInt> for &'b APInt {
	type Output = APInt;

	fn mul(self, rhs: &'a APInt) -> Self::Output {
		let mut cloned = self.clone();
		cloned.checked_mul_assign(rhs).unwrap();
		cloned
	}
}

impl<'a> MulAssign<&'a APInt> for APInt {
	fn mul_assign(&mut self, rhs: &'a APInt) {
		self.checked_mul_assign(rhs).unwrap();
	}
}

//  =======================================================================
///  Arithmetic Operations
/// =======================================================================
impl APInt {

	/// Returns a new `APInt` that represents the negation of this `APInt`.
	/// 
	/// This may allocate heap memory!
	pub fn negate(&self) -> APInt {
		let mut cloned = self.clone();
		cloned.negate_inplace();
		cloned
	}

	/// Negates this `APInt` inplace as if it was a signed integer.
	/// 
	/// This does not allocate heap memory!
	pub fn negate_inplace(&mut self) {
		match self.model_mut() {
			ModelMut::Inl(mut small) => {
				small.neg_inplace()
			}
			ModelMut::Ext(mut large) => {
				large.neg_inplace()
			}
		}
	}

	/// Creates a new `APInt` that represents the addition of both given `APInt`s.
	/// 
	/// # Note
	/// 
	/// In the low-level bit-wise representation there is no difference between signed
	/// and unsigned addition of fixed bit-width integers. (Cite: LLVM)
	pub fn checked_add(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_add_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_add_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.add_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.add_inplace(&rhs)
			}
		}
	}

	/// Creates a new `APInt` that represents the signed subtraction of both given `APInt`s.
	/// 
	/// # Note
	/// 
	/// In the low-level bit-wise representation there is no difference between signed
	/// and unsigned subtraction of fixed bit-width integers. (Cite: LLVM)
	pub fn checked_sub(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_sub_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_sub_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.sub_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.sub_inplace(&rhs)
			}
		}
	}

	/// Creates a new `APInt` that represents the multiplication of both given `APInt`s.
	/// 
	/// # Note
	/// 
	/// In the low-level bit-wise representation there is no difference between signed
	/// and unsigned multiplication of fixed bit-width integers. (Cite: LLVM)
	pub fn checked_mul(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_mul_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_mul_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.mul_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.mul_inplace(&rhs)
			}
		}
	}

	/// Creates a new `APInt` that represents the unsigned multiplication of both given `APInt`s.
	pub fn checked_udiv(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_udiv_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_udiv_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.udiv_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.udiv_inplace(&rhs)
			}
		}
	}

	/// Creates a new `APInt` that represents the signed multiplication of both given `APInt`s.
	pub fn checked_sdiv(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_sdiv_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_sdiv_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.sdiv_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.sdiv_inplace(&rhs)
			}
		}
	}

	/// Creates a new `APInt` that represents the unsigned remainder of both given `APInt`s.
	pub fn checked_urem(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_urem_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_urem_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.urem_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.urem_inplace(&rhs)
			}
		}
	}

	/// Creates a new `APInt` that represents the signed remainder of both given `APInt`s.
	pub fn checked_srem(&self, other: &APInt) -> Result<APInt> {
		let mut cloned = self.clone();
		cloned.checked_srem_assign(other)?;
		Ok(cloned)
	}

	pub fn checked_srem_assign(&mut self, other: &APInt) -> Result<()> {
		match self.zip_model_mut(other)? {
			ZipModelMut::Inl(mut lhs, rhs) => {
				lhs.srem_inplace(&rhs)
			}
			ZipModelMut::Ext(mut lhs, rhs) => {
				lhs.srem_inplace(&rhs)
			}
		}
	}

}