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
//! Integer splitting

//!

//! This module provides ways to split bigger integers

//! into smaller integers and then join them back together.


// Imports

use super::{Truncate, ZeroExtend};
use core::{
	mem,
	ops::{Shl, Shr},
};

/// Splits an integer into it's low and high part

pub trait Split: Sized {
	/// Output type for higher part

	type Hi;

	/// Output type for lower part

	type Lo;

	/// Returns the high part of this integer

	fn hi(self) -> Self::Hi;

	/// Returns the low part of this integer

	fn lo(self) -> Self::Lo;

	/// Returns the low and high part of this integer

	fn lo_hi(self) -> (Self::Lo, Self::Hi);
}

/// Joins two integers into a larger one.

pub trait Join: Split {
	/// Joins two parts of an integer

	fn join(lo: <Self as Split>::Lo, hi: <Self as Split>::Lo) -> Self;
}

// Macro to help implement `Split` / `Join`

macro_rules! impl_split_join {
	($T:ty => $Hi:ty : $Lo:ty) => {
		// Make sure that `T` is made up of `Lo` and `Hi`

		::static_assertions::assert_eq_size!($T, ($Lo, $Hi));

		impl Split for $T {
			type Hi = $Hi;
			type Lo = $Lo;

			#[inline]
			fn lo(self) -> Self::Lo {
				<Self as Truncate<Self::Lo>>::truncate(self)
			}

			#[inline]
			fn hi(self) -> Self::Hi {
				<Self as Truncate<Self::Lo>>::truncate(self.shr(8 * mem::size_of::<Self::Lo>()))
			}

			#[inline]
			fn lo_hi(self) -> (Self::Lo, Self::Hi) {
				let lo = self.lo();
				let hi = self.hi();
				(lo, hi)
			}
		}

		impl Join for $T {
			#[inline]
			fn join(lo: <Self as Split>::Lo, hi: <Self as Split>::Lo) -> Self {
				<$Hi as ZeroExtend<$T>>::zero_extend(hi).shl(8 * mem::size_of::<Self::Lo>()) | <$Lo as ZeroExtend<$T>>::zero_extend(lo)
			}
		}
	};
}

// Unsigned

impl_split_join! { u128 => u64 : u64 }
impl_split_join! { u64  => u32 : u32 }
impl_split_join! { u32  => u16 : u16 }
impl_split_join! { u16  => u8  : u8  }

// Signed

// TODO: Confirm these, should they even exist? Should `Lo` be unsigned?

//impl_split_join! { i128 => i64 : i64 }

//impl_split_join! { i64  => i32 : i32 }

//impl_split_join! { i32  => i16 : i16 }

//impl_split_join! { i16  => i8  : i8  }


// Check that they all implement `Split` / `Join`

//static_assertions::assert_impl_all! { i16  : Split, Join }

//static_assertions::assert_impl_all! { i32  : Split, Join }

//static_assertions::assert_impl_all! { i64  : Split, Join }

//static_assertions::assert_impl_all! { i128 : Split, Join }

static_assertions::assert_impl_all! { u16  : Split, Join }
static_assertions::assert_impl_all! { u32  : Split, Join }
static_assertions::assert_impl_all! { u64  : Split, Join }
static_assertions::assert_impl_all! { u128 : Split, Join }

// Check that all associated types are correct

//static_assertions::assert_type_eq_all! { <i16   as Split>::Lo, <i16   as Split>::Hi, i8  }

//static_assertions::assert_type_eq_all! { <i32   as Split>::Lo, <i32   as Split>::Hi, i16 }

//static_assertions::assert_type_eq_all! { <i64   as Split>::Lo, <i64   as Split>::Hi, i32 }

//static_assertions::assert_type_eq_all! { <i128  as Split>::Lo, <i128  as Split>::Hi, i64 }

static_assertions::assert_type_eq_all! { <u16   as Split>::Lo, <u16   as Split>::Hi, u8  }
static_assertions::assert_type_eq_all! { <u32   as Split>::Lo, <u32   as Split>::Hi, u16 }
static_assertions::assert_type_eq_all! { <u64   as Split>::Lo, <u64   as Split>::Hi, u32 }
static_assertions::assert_type_eq_all! { <u128  as Split>::Lo, <u128  as Split>::Hi, u64 }

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

	#[test]
	#[rustfmt::skip]
	fn split_lo() {
		assert_eq!(u128::lo(u128::MAX), u64::MAX);
		assert_eq!( u64::lo( u64::MAX), u32::MAX);
		assert_eq!( u32::lo( u32::MAX), u16::MAX);
		assert_eq!( u16::lo( u16::MAX),  u8::MAX);
	}

	#[test]
	#[rustfmt::skip]
	fn split_lo_half() {
		assert_eq!(u128::lo(u128::from(u64::MAX)), u64::MAX);
		assert_eq!( u64::lo( u64::from(u32::MAX)), u32::MAX);
		assert_eq!( u32::lo( u32::from(u16::MAX)), u16::MAX);
		assert_eq!( u16::lo( u16::from( u8::MAX)),  u8::MAX);
	}

	#[test]
	#[rustfmt::skip]
	fn split_hi() {
		assert_eq!(u128::hi(u128::MAX), u64::MAX);
		assert_eq!( u64::hi( u64::MAX), u32::MAX);
		assert_eq!( u32::hi( u32::MAX), u16::MAX);
		assert_eq!( u16::hi( u16::MAX),  u8::MAX);
	}

	#[test]
	#[rustfmt::skip]
	fn split_hi_half() {
		assert_eq!(u128::hi(u128::from(u64::MAX)), 0);
		assert_eq!( u64::hi( u64::from(u32::MAX)), 0);
		assert_eq!( u32::hi( u32::from(u16::MAX)), 0);
		assert_eq!( u16::hi( u16::from( u8::MAX)), 0);
	}

	#[test]
	#[rustfmt::skip]
	fn split_lo_hi() {
		assert_eq!(u128::lo_hi(u128::MAX), (u128::lo(u128::MAX), u128::hi(u128::MAX)));
		assert_eq!( u64::lo_hi( u64::MAX), ( u64::lo( u64::MAX),  u64::hi( u64::MAX)));
		assert_eq!( u32::lo_hi( u32::MAX), ( u32::lo( u32::MAX),  u32::hi( u32::MAX)));
		assert_eq!( u16::lo_hi( u16::MAX), ( u16::lo( u16::MAX),  u16::hi( u16::MAX)));
	}

	#[test]
	#[rustfmt::skip]
	fn split_lo_hi_half() {
		assert_eq!(u128::lo_hi(u128::from(u64::MAX)), (u128::lo(u128::from(u64::MAX)), u128::hi(u128::from(u64::MAX))));
		assert_eq!( u64::lo_hi( u64::from(u32::MAX)), ( u64::lo( u64::from(u32::MAX)),  u64::hi( u64::from(u32::MAX))));
		assert_eq!( u32::lo_hi( u32::from(u16::MAX)), ( u32::lo( u32::from(u16::MAX)),  u32::hi( u32::from(u16::MAX))));
		assert_eq!( u16::lo_hi( u16::from( u8::MAX)), ( u16::lo( u16::from( u8::MAX)),  u16::hi( u16::from( u8::MAX))));
	}
}