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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use apint::{ApInt};
use apint::utils::{
	ZipDataAccess
};
use errors::{Result};
use traits::Width;
use digit;
use digit::{Bit};

use std::cmp::Ordering;
use std::ops::Not;

impl PartialEq for ApInt {
	fn eq(&self, other: &ApInt) -> bool {
		if self.len_bits() != other.len_bits() {
			return false
		}
		self.as_digit_slice() == other.as_digit_slice()
	}
}

impl Eq for ApInt {}

/// # Comparison Operations
impl ApInt {

	/// Unsigned less-than (`ult`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self < rhs`.
	/// - Interprets both `ApInt` instances as **unsigned** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	pub fn checked_ult(&self, rhs: &ApInt) -> Result<bool> {
		match self
			.zip_access_data(rhs)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on unsigned less-than (slt) comparison with `lhs < rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))?
		{
			ZipDataAccess::Inl(lhs, rhs) => {
				Ok(lhs.repr() < rhs.repr())
			}
			ZipDataAccess::Ext(lhs, rhs) => {
				for (l, r) in lhs.into_iter().rev()
				                 .zip(rhs.into_iter().rev())
				{
					match l.cmp(r) {
						Ordering::Less    => return Ok(true),
						Ordering::Greater => return Ok(false),
						Ordering::Equal   => ()
					}
				}
				Ok(false)
			}
		}
	}

	/// Unsigned less-equals (`ule`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self <= rhs`.
	/// - Interprets both `ApInt` instances as **unsigned** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	#[inline]
	pub fn checked_ule(&self, rhs: &ApInt) -> Result<bool> {
		rhs.checked_ult(self).map(Not::not)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on unsigned less-than or equals (ule) comparison with `lhs <= rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))
	}

	/// Unsigned greater-than (`ugt`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self > rhs`.
	/// - Interprets both `ApInt` instances as **unsigned** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	#[inline]
	pub fn checked_ugt(&self, rhs: &ApInt) -> Result<bool> {
		rhs.checked_ult(self)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on unsigned greater-than (ugt) comparison with `lhs > rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))
	}

	/// Unsigned greater-equals (`uge`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self >= rhs`.
	/// - Interprets both `ApInt` instances as **unsigned** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	#[inline]
	pub fn checked_uge(&self, rhs: &ApInt) -> Result<bool> {
		self.checked_ult(rhs).map(Not::not)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on unsigned greater-than or equals (ule) comparison with `lhs >= rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))
	}

	/// Signed less-than (`slt`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self < rhs`.
	/// - Interprets both `ApInt` instances as **signed** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	pub fn checked_slt(&self, rhs: &ApInt) -> Result<bool> {
		let lhs = self;
		lhs.zip_access_data(rhs).and_then(|zipped| {
			match zipped {
				ZipDataAccess::Inl(lhs, rhs) => {
					let infate_abs = digit::BITS - self.width().to_usize();
					let lhs = (lhs.repr() << infate_abs) as i64;
					let rhs = (rhs.repr() << infate_abs) as i64;
					Ok(lhs < rhs)
				}
				ZipDataAccess::Ext(_, _) => {
					match (lhs.sign_bit(), rhs.sign_bit()) {
						(Bit::Unset, Bit::Unset) => lhs.checked_ult(rhs),
						(Bit::Unset, Bit::Set  ) => Ok(false),
						(Bit::Set  , Bit::Unset) => Ok(true),
						(Bit::Set  , Bit::Set  ) => rhs.checked_ugt(lhs)
					}
				}
			}
		})
		.map_err(|err| err.with_annotation(format!(
			"Error occured on signed less-than (slt) comparison with `lhs < rhs` where \
				\n\tlhs = {:?}\
				\n\trhs = {:?}",
			self, rhs)
		))
	}

	/// Signed less-equals (`sle`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self <= rhs`.
	/// - Interprets both `ApInt` instances as **signed** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	#[inline]
	pub fn checked_sle(&self, rhs: &ApInt) -> Result<bool> {
		rhs.checked_slt(self).map(Not::not)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on signed less-than or equals (ule) comparison with `lhs <= rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))
	}

	/// Signed greater-than (`sgt`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self > rhs`.
	/// - Interprets both `ApInt` instances as **signed** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	#[inline]
	pub fn checked_sgt(&self, rhs: &ApInt) -> Result<bool> {
		rhs.checked_slt(self)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on signed greater-than (ugt) comparison with `lhs > rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))
	}

	/// Signed greater-equals (`sge`) comparison between `self` and `rhs`.
	/// 
	/// # Note
	/// 
	/// - Returns `Ok(true)` if `self >= rhs`.
	/// - Interprets both `ApInt` instances as **signed** values.
	/// 
	/// # Errors
	/// 
	/// - If `self` and `rhs` have unmatching bit widths.
	#[inline]
	pub fn checked_sge(&self, rhs: &ApInt) -> Result<bool> {
		self.checked_slt(rhs).map(Not::not)
			.map_err(|err| err.with_annotation(format!(
				"Error occured on signed greater-than or equals (ule) comparison with `lhs >= rhs` where \
				 \n\tlhs = {:?}\
				 \n\trhs = {:?}",
				self, rhs)
			))
	}

}

#[cfg(test)]
mod tests {
	use super::*;

	mod partial_eq {
		use super::*;

		#[test]
		fn simple_small() {
			let a = ApInt::from_u8(42);
			let b = ApInt::from_u8(42);
			let c = ApInt::from_u8(77);
			let d = ApInt::from_u16(42);
			assert_eq!(a, b);
			assert_ne!(a, c);
			assert_ne!(a, d);
			assert_ne!(b, c);
			assert_ne!(b, d);
			assert_ne!(c, d);
		}

		#[test]
		fn simple_large() {
			let a = ApInt::from_u128(42);
			let b = ApInt::from_u128(42);
			let c = ApInt::from_u128(1337);
			let d = ApInt::from_u64(42);
			assert_eq!(a, b);
			assert_ne!(a, c);
			assert_ne!(a, d);
			assert_ne!(b, c);
			assert_ne!(b, d);
			assert_ne!(c, d);
		}
	}
}