Skip to content

Commit

Permalink
Implement swap_bytes() for bit widths that are multiple of 8 (#16)
Browse files Browse the repository at this point in the history
Implement swap_bytes() for arbitrary ints with a bit-width that is a multiple of 8
  • Loading branch information
danlehmann authored May 15, 2023
1 parent ce9c1b0 commit 054acbf
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,39 @@ where
}
}

// swap_bytes: This is available for all integer multiples of 8
// We can fairly easily do this by using the swap_bytes() of the underlying type and shifting right
macro_rules! swap_bytes_impl {
($base_data_type:ty, $bits:expr) => {
impl UInt<$base_data_type, $bits>
{
/// Reverses the byte order of the integer.
#[inline]
pub const fn swap_bytes(&self) -> Self {
const SHIFT_RIGHT: usize = (core::mem::size_of::<$base_data_type>() << 3) - $bits;
Self { value: self.value.swap_bytes() >> SHIFT_RIGHT }
}
}
};
}

swap_bytes_impl!(u32, 24);
swap_bytes_impl!(u64, 24);
swap_bytes_impl!(u128, 24);
swap_bytes_impl!(u64, 40);
swap_bytes_impl!(u128, 40);
swap_bytes_impl!(u64, 48);
swap_bytes_impl!(u128, 48);
swap_bytes_impl!(u64, 56);
swap_bytes_impl!(u128, 56);
swap_bytes_impl!(u128, 72);
swap_bytes_impl!(u128, 80);
swap_bytes_impl!(u128, 88);
swap_bytes_impl!(u128, 96);
swap_bytes_impl!(u128, 104);
swap_bytes_impl!(u128, 112);
swap_bytes_impl!(u128, 120);

// Conversions

#[cfg(feature = "nightly")]
Expand Down
78 changes: 78 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,81 @@ fn debug() {
assert_eq!("Value: 5", format!("Value: {:?}", u38::new(5)));
assert_eq!("Value: 60", format!("Value: {:?}", u65::new(60)));
}

#[test]
fn swap_bytes() {
assert_eq!(
u24::new(0x12_34_56).swap_bytes(),
u24::new(0x56_34_12)
);
assert_eq!(
UInt::<u64, 24>::new(0x12_34_56).swap_bytes(),
UInt::<u64, 24>::new(0x56_34_12)
);
assert_eq!(
UInt::<u128, 24>::new(0x12_34_56).swap_bytes(),
UInt::<u128, 24>::new(0x56_34_12)
);

assert_eq!(
u40::new(0x12_34_56_78_9A).swap_bytes(),
u40::new(0x9A_78_56_34_12)
);
assert_eq!(
UInt::<u128, 40>::new(0x12_34_56_78_9A).swap_bytes(),
UInt::<u128, 40>::new(0x9A_78_56_34_12)
);

assert_eq!(
u48::new(0x12_34_56_78_9A_BC).swap_bytes(),
u48::new(0xBC_9A_78_56_34_12)
);
assert_eq!(
UInt::<u128, 48>::new(0x12_34_56_78_9A_BC).swap_bytes(),
UInt::<u128, 48>::new(0xBC_9A_78_56_34_12)
);

assert_eq!(
u56::new(0x12_34_56_78_9A_BC_DE).swap_bytes(),
u56::new(0xDE_BC_9A_78_56_34_12)
);
assert_eq!(
UInt::<u128, 56>::new(0x12_34_56_78_9A_BC_DE).swap_bytes(),
UInt::<u128, 56>::new(0xDE_BC_9A_78_56_34_12)
);

assert_eq!(
u72::new(0x12_34_56_78_9A_BC_DE_FE_DC).swap_bytes(),
u72::new(0xDC_FE_DE_BC_9A_78_56_34_12)
);

assert_eq!(
u80::new(0x12_34_56_78_9A_BC_DE_FE_DC_BA).swap_bytes(),
u80::new(0xBA_DC_FE_DE_BC_9A_78_56_34_12)
);

assert_eq!(
u88::new(0x12_34_56_78_9A_BC_DE_FE_DC_BA_98).swap_bytes(),
u88::new(0x98_BA_DC_FE_DE_BC_9A_78_56_34_12)
);

assert_eq!(
u96::new(0x12_34_56_78_9A_BC_DE_FE_DC_BA_98_76).swap_bytes(),
u96::new(0x76_98_BA_DC_FE_DE_BC_9A_78_56_34_12)
);

assert_eq!(
u104::new(0x12_34_56_78_9A_BC_DE_FE_DC_BA_98_76_54).swap_bytes(),
u104::new(0x54_76_98_BA_DC_FE_DE_BC_9A_78_56_34_12)
);

assert_eq!(
u112::new(0x12_34_56_78_9A_BC_DE_FE_DC_BA_98_76_54_32).swap_bytes(),
u112::new(0x32_54_76_98_BA_DC_FE_DE_BC_9A_78_56_34_12)
);

assert_eq!(
u120::new(0x12_34_56_78_9A_BC_DE_FE_DC_BA_98_76_54_32_10).swap_bytes(),
u120::new(0x10_32_54_76_98_BA_DC_FE_DE_BC_9A_78_56_34_12)
);
}

0 comments on commit 054acbf

Please sign in to comment.