summaryrefslogtreecommitdiff
path: root/src/arm32/unsigned/mod.rs
blob: b195bf0337f5019dedbce67585ce2e084ec04aa6 (plain) (blame)
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
// Copyright 2024 Gabriel Bjørnager Jensen.
//
// This file is part of Pollex.
//
// Pollex is free software: you can redistribute it
// and/or modify it under the terms of the GNU Af-
// fero General Public License as published by the
// Free Software Foundation, either version 3 of
// the License, or (at your option) any later ver-
// sion.
//
// Pollex is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Af-
// fero General Public License along with Pollex.
// If not, see <https://www.gnu.org/licenses/>.

use crate::arm32::Signed;

use core::cmp::Ordering;
use core::fmt::{Display, Formatter};
use core::ops::{Add, Div, Mul, Sub};

/// An unsigned word.
///
/// This type defines the [`Add`], [`Sub`], [`Mul`], and [`Div`] traits for `Self` and [`u32`].
/// Internally, these implementations use wrapping arithemtic.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct Unsigned(u32);

impl Unsigned {
	/// Constructs a new word.
	#[inline(always)]
	#[must_use]
	pub const fn new(value: u32) -> Self { Self(value) }

	/// Retrieves the word's value.
	#[inline(always)]
	#[must_use]
	pub const fn get(self) -> u32 { self.0 }

	/// Sets the value of the word.
	#[inline(always)]
	pub fn set(&mut self, value: u32) { self.0 = value }

	/// Reinterprets the word as signed.
	#[allow(clippy::cast_possible_wrap)]
	#[inline(always)]
	#[must_use]
	pub const fn as_signed(self) -> Signed { Signed::new(self.0 as i32) }

	/// Adds a signed offset to the word.
	#[inline(always)]
	#[must_use]
	pub const fn offset(self, value: i32) -> Self { Self(self.get().wrapping_add_signed(value)) }
}

impl Add<Self> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn add(self, rhs: Self) -> Self::Output { self + rhs.get() }
}

impl Add<u32> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn add(self, rhs: u32) -> Self::Output { Self(self.0.wrapping_add(rhs)) }
}

impl Display for Unsigned {
	#[inline(always)]
	fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
		write!(f, "#{}", self.0)
	}
}

impl Div<Self> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn div(self, rhs: Self) -> Self::Output { self / rhs.get() }
}

impl Div<u32> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn div(self, rhs: u32) -> Self::Output { Self(self.0.wrapping_div(rhs)) }
}

impl Mul<Self> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn mul(self, rhs: Self) -> Self::Output { self * rhs.get() }
}

impl Mul<u32> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn mul(self, rhs: u32) -> Self::Output { Self(self.0.wrapping_mul(rhs)) }
}

impl PartialEq<u32> for Unsigned {
	#[inline(always)]
	fn eq(&self, other: &u32) -> bool { self.0 == *other }
}

impl PartialOrd<u32> for Unsigned {
	#[inline(always)]
	fn partial_cmp(&self, other: &u32) -> Option<Ordering> { self.0.partial_cmp(other) }
}

impl Sub<Self> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn sub(self, rhs: Self) -> Self::Output { self - rhs.get() }
}

impl Sub<u32> for Unsigned {
	type Output = Self;

	#[inline(always)]
	fn sub(self, rhs: u32) -> Self::Output { Self(self.0.wrapping_sub(rhs)) }
}