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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// This file is part of linux-support. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT. No part of linux-support, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2020 The developers of linux-support. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT.


/// A Q-format `Q32.32` signed fixed point number, viz:-
///
/// * Integer component is signed and 32 bits (`i32`).
/// * Fraction component is 32 bits.
///
/// See upstream library `https://github.com/PetteriAimonen/libfixmath` for example code to implement trigonometric functions.
#[derive(Default, Debug, Copy, Clone)]
#[derive(Deserialize, Serialize)]
#[repr(C)]
pub struct Signed3232FixedPoint(i64);

impl From<i64> for Signed3232FixedPoint
{
	#[inline(always)]
	fn from(value: i64) -> Self
	{
		Self::from_i64(value)
	}
}

impl Into<i64> for Signed3232FixedPoint
{
	#[inline(always)]
	fn into(self) -> i64
	{
		self.into_i64()
	}
}

impl From<u64> for Signed3232FixedPoint
{
	#[inline(always)]
	fn from(value: u64) -> Self
	{
		Self::from_u64(value)
	}
}

impl Into<u64> for Signed3232FixedPoint
{
	#[inline(always)]
	fn into(self) -> u64
	{
		self.into_u64()
	}
}

impl From<(BigEndianI32, BigEndianU32)> for Signed3232FixedPoint
{
	#[inline(always)]
	fn from(value: (BigEndianI32, BigEndianU32)) -> Self
	{
		Self::new(i32::from_be_bytes(value.0), u32::from_be_bytes(value.1))
	}
}

impl Into<(BigEndianI32, BigEndianU32)> for Signed3232FixedPoint
{
	#[inline(always)]
	fn into(self) -> (BigEndianI32, BigEndianU32)
	{
		(
			self.integer().to_be_bytes(),
			self.fraction().to_be_bytes(),
		)
	}
}

impl Into<(i32, u32)> for Signed3232FixedPoint
{
	#[inline(always)]
	fn into(self) -> (i32, u32)
	{
		(
			self.integer(),
			self.fraction(),
		)
	}
}

impl TryFrom<Unsigned3232FixedPoint> for Signed3232FixedPoint
{
	type Error = ParseNumberError;
	
	#[inline(always)]
	fn try_from(value: Unsigned3232FixedPoint) -> Result<Self, Self::Error>
	{
		if value.integer() > (Self::LargestInteger as u32)
		{
			Err(ParseNumberError::TooLarge)
		}
		else
		{
			Ok(unsafe { transmute(value) })
		}
	}
}

impl PartialEq for Signed3232FixedPoint
{
	#[inline(always)]
	fn eq(&self, rhs: &Self) -> bool
	{
		let left = (*self).into_i64();
		let right = (*rhs).into_i64();
		left == right
	}
}

impl Eq for Signed3232FixedPoint
{
}

impl PartialOrd for Signed3232FixedPoint
{
	#[inline(always)]
	fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>
	{
		Some(self.cmp(rhs))
	}
}

impl Ord for Signed3232FixedPoint
{
	#[inline(always)]
	fn cmp(&self, rhs: &Self) -> Ordering
	{
		use self::Ordering::*;
		
		match self.integer().cmp(&rhs.integer())
		{
			Less => Less,
			
			Greater => Greater,
			
			Equal =>
			{
				let compare = self.fraction().cmp(&rhs.fraction());
				
				let both_are_negative = self.is_negative();
				if both_are_negative
				{
					compare.reverse()
				}
				else
				{
					compare
				}
			}
		}
	}
}

impl Hash for Signed3232FixedPoint
{
	#[inline(always)]
	fn hash<H: Hasher>(&self, state: &mut H)
	{
		let this = (*self).into_i64();
		this.hash(state)
	}
}

impl Add for Signed3232FixedPoint
{
	type Output = Self;
	
	#[inline(always)]
	fn add(self, rhs: Self) -> Self::Output
	{
		self.checked_add(rhs).unwrap()
	}
}

impl Sub for Signed3232FixedPoint
{
	type Output = Self;
	
	#[inline(always)]
	fn sub(self, rhs: Self) -> Self::Output
	{
		self.checked_sub(rhs).unwrap()
	}
}

impl Rem for Signed3232FixedPoint
{
	type Output = Self;
	
	#[inline(always)]
	fn rem(self, rhs: Self) -> Self::Output
	{
		let this = self.into_i64();
		
		Self::from_i64(this % (rhs.into_i64()))
	}
}

impl Neg for Signed3232FixedPoint
{
	type Output = Self;
	
	#[inline(always)]
	fn neg(self) -> Self
	{
		Self::new(-self.integer(), self.fraction())
	}
}

impl AddAssign for Signed3232FixedPoint
{
	#[inline(always)]
	fn add_assign(&mut self, rhs: Self)
	{
		*self = (*self).add(rhs)
	}
}

impl SubAssign for Signed3232FixedPoint
{
	#[inline(always)]
	fn sub_assign(&mut self, rhs: Self)
	{
		*self = (*self).sub(rhs)
	}
}

impl RemAssign for Signed3232FixedPoint
{
	#[inline(always)]
	fn rem_assign(&mut self, rhs: Self)
	{
		*self = (*self).rem(rhs)
	}
}

impl Signed3232FixedPoint
{
	const LargestInteger: i32 = 0x7FFF_FFFF;
	
	const ZeroFraction: u32 = 0x0000_0000;
	
	const SignBit: u64 = 0x8000_0000_0000_0000;
	
	/// Inclusive minimum.
	pub const InclusiveMinimum: Self = Self::new(0x8000_0000u32 as i32, Self::ZeroFraction);
	
	/// Inclusive maximum.
	pub const InclusiveMaximum: Self = Self::new(Self::LargestInteger, 0xFFFF_FFFF);
	
	/// Zero, `0`.
	pub const Zero: Self = Self::new(0x0000_0000, Self::ZeroFraction);
	
	/// One, `1`.
	pub const One: Self = Self::new(0x0000_0001, Self::ZeroFraction);
	
	/// New instance.
	#[inline(always)]
	pub const fn new(integer: i32, fraction: u32) -> Self
	{
		Self((((integer as u64) << 32) | (fraction as u64)) as i64)
	}
	
	/// Integer.
	#[inline(always)]
	pub const fn integer(self) -> i32
	{
		((self.0 as u64) >> 32) as u32 as i32
	}
	
	/// Fraction.
	#[inline(always)]
	pub const fn fraction(self) -> u32
	{
		(self.0 as u32) as u32
	}
	
	/// Constant from i64.
	#[inline(always)]
	pub const fn from_i64(value: i64) -> Self
	{
		unsafe { transmute(value) }
	}
	
	/// Constant into i64.
	#[inline(always)]
	pub const fn into_i64(self) -> i64
	{
		unsafe { transmute(self) }
	}
	
	/// Constant from u64.
	#[inline(always)]
	pub const fn from_u64(value: u64) -> Self
	{
		unsafe { transmute(value) }
	}
	
	/// Constant into u64.
	#[inline(always)]
	pub const fn into_u64(self) -> u64
	{
		unsafe { transmute(self) }
	}
	
	/// Computes the absolute value of self.
	///
	/// The absolute value of `Self::InclusiveMinimum` cannot be represented without a sign, and attempting to calculate it will cause an overflow.
	/// This means that code in debug mode will trigger a panic on this case and optimized code will return `Self::InclusiveMinimum` without a panic.
	#[inline(always)]
	pub const fn abs(self) -> Self
	{
		Self::new(self.integer().abs(), self.fraction())
	}
	
	/// Returns true if self is positive and false if the number is zero or negative.
	#[inline(always)]
	pub const fn is_positive(self) -> bool
	{
		self.integer().is_positive()
	}
	
	/// Returns true if self is negative and false if the number is zero or positive.
	#[inline(always)]
	pub const fn is_negative(self) -> bool
	{
		self.integer().is_negative()
	}
	
	/// Returns a number representing sign of self.
	///
	/// * `0` if the number is zero.
	/// * `1` if the number is positive.
	/// * `-1` if the number is negative.
	#[inline(always)]
	pub const fn signum(self) -> i32
	{
		self.integer().signum()
	}
	
	/// Overflow can only happen if `sign_of_left == sign_of_left`, and then it causes `sign_of_sum_or_difference != sign_of_left`.
	#[inline(always)]
	const fn detect_overflow(left: u64, right: u64, sum_or_difference: u64) -> bool
	{
		((left ^ right) & Self::SignBit == 0) && ((left ^ sum_or_difference) & Self::SignBit != 0)
	}
	
	/// Saturating addition.
	#[inline(always)]
	pub fn saturating_add(self, rhs: Self) -> Self
	{
		match self.checked_add(rhs)
		{
			None => if self.into_i64() >= 0
			{
				Self::InclusiveMaximum
			}
			else
			{
				Self::InclusiveMinimum
			},
			
			Some(sum) => sum,
		}
	}
	
	/// Saturating subtraction.
	#[inline(always)]
	pub fn saturating_sub(self, rhs: Self) -> Self
	{
		match self.checked_sub(rhs)
		{
			None => if self.into_i64() >= 0
			{
				Self::InclusiveMaximum
			}
			else
			{
				Self::InclusiveMinimum
			},
			
			Some(difference) => difference,
		}
	}
	
	/// Checked addition.
	#[inline(always)]
	pub fn checked_add(self, rhs: Self) -> Option<Self>
	{
		let left = self.into_u64();
		let right = rhs.into_u64();
		let sum = left.wrapping_add(right);
		
		if Self::detect_overflow(left, right, sum)
		{
			None
		}
		else
		{
			Some(Self::from_u64(sum))
		}
	}
	
	/// Checked subtraction.
	#[inline(always)]
	pub fn checked_sub(self, rhs: Self) -> Option<Self>
	{
		let left = self.into_u64();
		let right = rhs.into_u64();
		let difference = left.wrapping_sub(right);
		
		if Self::detect_overflow(left, right, difference)
		{
			None
		}
		else
		{
			Some(Self::from_u64(difference))
		}
	}
}