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
// 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` unsigned fixed point number, viz:-
///
/// * Integer component is unsigned and 32 bits (`u32`).
/// * 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 Unsigned3232FixedPoint(u64);

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

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

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

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

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

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

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

impl Eq for Unsigned3232FixedPoint
{
}

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

impl Ord for Unsigned3232FixedPoint
{
	#[inline(always)]
	fn cmp(&self, rhs: &Self) -> Ordering
	{
		let left = (*self).into_u64();
		let right = (*rhs).into_u64();
		left.cmp(&right)
	}
}

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

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

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

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

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

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

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

impl Unsigned3232FixedPoint
{
	const SmallestInteger: u32 = 0x0000_0000;
	
	const LargestInteger: u32 = 0xFFFF_FFFF;
	
	const FractionSizeInBits: u64 = 32;
	
	const FractionsPerInteger: u64 = 1 << Self::FractionSizeInBits;
	
	/// Inclusive minimum.
	pub const InclusiveMinimum: Self = Self::new(Self::SmallestInteger, 0x0000_0000);
	
	/// Inclusive maximum.
	pub const InclusiveMaximum: Self = Self::new(Self::LargestInteger, 0xFFFF_FFFF);
	
	/// Zero, `0`.
	pub const Zero: Self = Self::new(Self::SmallestInteger, 0x0000_0000);
	
	/// One, `1`.
	pub const One: Self = Self::new(0x0000_0001, 0x0000_0000);
	
	/// New instance.
	#[inline(always)]
	pub const fn new(integer: u32, fraction: u32) -> Self
	{
		Self(((integer as u64) << 32) | (fraction as u64))
	}
	
	/// Integer.
	#[inline(always)]
	pub const fn integer(self) -> u32
	{
		(self.0 >> 32) as u32
	}
	
	/// Fraction.
	#[inline(always)]
	pub const fn fraction(self) -> u32
	{
		self.0 as u32
	}
	
	/// Constant from u64.
	#[inline(always)]
	pub const fn from_u64(value: u64) -> Self
	{
		Self(value)
	}
	
	/// Constant into u64.
	#[inline(always)]
	pub const fn into_u64(self) -> u64
	{
		self.0
	}
	
	/// Saturating addition.
	#[inline(always)]
	pub fn saturating_add(self, rhs: Self) -> Self
	{
		match self.checked_add(rhs)
		{
			None => Self::InclusiveMaximum,
			
			Some(sum) => sum,
		}
	}
	
	/// Saturating subtraction.
	#[inline(always)]
	pub fn saturating_sub(self, rhs: Self) -> Self
	{
		match self.checked_sub(rhs)
		{
			None => Self::InclusiveMaximum,
			
			Some(difference) => difference,
		}
	}
	
	/// Checked addition.
	#[inline(always)]
	pub fn checked_add(self, rhs: Self) -> Option<Self>
	{
		let left_integer = self.integer();
		let right_integer = rhs.integer();
		
		if let Some(integer) = left_integer.checked_add(right_integer)
		{
			let left_fraction = self.fraction() as u64;
			let right_fraction = rhs.fraction() as u64;
			
			let fraction = left_fraction + right_fraction;
			if fraction < Self::FractionsPerInteger
			{
				Some(Self::new(integer, fraction as u32))
			}
			else
			{
				if integer == Self::LargestInteger
				{
					None
				}
				else
				{
					let fraction = fraction - Self::FractionsPerInteger;
					debug_assert!(fraction < Self::FractionsPerInteger);
					Some(Self::new(integer + 1, fraction as u32))
				}
			}
		}
		else
		{
			None
		}
	}
	
	/// Checked subtraction.
	#[inline(always)]
	pub fn checked_sub(self, rhs: Self) -> Option<Self>
	{
		let left_integer = self.integer();
		let right_integer = rhs.integer();
		
		match left_integer.checked_sub(right_integer)
		{
			None => None,
			
			Some(integer) =>
			{
				let left_fraction = self.fraction();
				let right_fraction = rhs.fraction();
				
				if left_fraction >= right_fraction
				{
					let fraction = left_fraction - right_fraction;
					Some(Self::new(integer, fraction))
				}
				else
				{
					if integer == Self::SmallestInteger
					{
						None
					}
					else
					{
						let integer = integer - 1;
						let fraction = (left_fraction as u64) + Self::FractionsPerInteger - (right_fraction as u64);
						Some(Self::new(integer, fraction as u32))
					}
				}
			}
		}
	}
	
	/// Saturating multiply by a scalar.
	#[inline(always)]
	pub fn saturating_mul_by_scalar(self, scalar: u32) -> Self
	{
		match self.checked_mul_by_scalar(scalar)
		{
			Some(result) => result,
			
			None => Self::InclusiveMinimum,
		}
	}
	
	/// Checked multiply by a scalar.
	#[inline(always)]
	pub fn checked_mul_by_scalar(self, scalar: u32) -> Option<Self>
	{
		let original_integer = self.integer() as u64;
		let original_fraction = self.fraction() as u64;
		let scalar = scalar as u64;
		
		let total_fraction = original_fraction * scalar;
		let extra_integer = total_fraction / Self::FractionsPerInteger;
		let fraction = (total_fraction % Self::FractionsPerInteger) as u32;
		
		match original_integer.checked_mul(scalar)
		{
			Some(multiplied) => match multiplied.checked_add(extra_integer)
			{
				Some(integer) => return Some(Self::new(integer as u32, fraction as u32)),
				
				None => None,
			},
			
			None => None,
		}
	}
	
	/// Checked divide by a scalar.
	#[inline(always)]
	pub fn checked_div_by_scalar(self, scalar: u32) -> Option<Self>
	{
		if scalar == 0
		{
			None
		}
		else
		{
			let original_integer = self.integer() as u64;
			let original_fraction = self.fraction() as u64;
			let scalar = scalar as u64;
			
			let integer = original_integer / scalar;
			let carry = original_integer - integer * scalar;
			let extra_fraction = carry * Self::FractionsPerInteger / scalar;
			let fraction = original_fraction / scalar + extra_fraction;
			Some(Self::new(integer as u32, fraction as u32))
		}
	}
}