From 6f0d50ebea9f8485fdce30931035742c18efbe23 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Wed, 17 Aug 2016 15:50:24 -0500 Subject: [PATCH] Move integer functions to separate module --- src/arm.rs | 4 +-- src/int/mod.rs | 73 ++++++++++++++++++++++++++++++++++++++++++ src/{ => int}/mul.rs | 2 +- src/{ => int}/sdiv.rs | 2 +- src/{ => int}/shift.rs | 2 +- src/{ => int}/udiv.rs | 2 +- src/lib.rs | 73 ++---------------------------------------- src/qc.rs | 2 +- 8 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 src/int/mod.rs rename src/{ => int}/mul.rs (99%) rename src/{ => int}/sdiv.rs (99%) rename src/{ => int}/shift.rs (99%) rename src/{ => int}/udiv.rs (99%) diff --git a/src/arm.rs b/src/arm.rs index 4555d5b..b46b0ea 100644 --- a/src/arm.rs +++ b/src/arm.rs @@ -60,12 +60,12 @@ pub unsafe fn __aeabi_ldivmod() { // TODO: These two functions should be defined as aliases #[cfg_attr(not(test), no_mangle)] pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 { - ::udiv::__udivsi3(a, b) + ::int::udiv::__udivsi3(a, b) } #[cfg_attr(not(test), no_mangle)] pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 { - ::sdiv::__divsi3(a, b) + ::int::sdiv::__divsi3(a, b) } extern "C" { diff --git a/src/int/mod.rs b/src/int/mod.rs new file mode 100644 index 0000000..37e0537 --- /dev/null +++ b/src/int/mod.rs @@ -0,0 +1,73 @@ + +pub mod mul; +pub mod sdiv; +pub mod shift; +pub mod udiv; + +/// Trait for some basic operations on integers +pub trait Int { + /// Returns the bitwidth of the int type + fn bits() -> u32; +} + +// TODO: Once i128/u128 support lands, we'll want to add impls for those as well +impl Int for u32 { + fn bits() -> u32 { + 32 + } +} +impl Int for i32 { + fn bits() -> u32 { + 32 + } +} +impl Int for u64 { + fn bits() -> u32 { + 64 + } +} +impl Int for i64 { + fn bits() -> u32 { + 64 + } +} + +/// Trait to convert an integer to/from smaller parts +pub trait LargeInt { + type LowHalf; + type HighHalf; + + fn low(self) -> Self::LowHalf; + fn high(self) -> Self::HighHalf; + fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self; +} + +// TODO: Once i128/u128 support lands, we'll want to add impls for those as well +impl LargeInt for u64 { + type LowHalf = u32; + type HighHalf = u32; + + fn low(self) -> u32 { + self as u32 + } + fn high(self) -> u32 { + (self >> 32) as u32 + } + fn from_parts(low: u32, high: u32) -> u64 { + low as u64 | ((high as u64) << 32) + } +} +impl LargeInt for i64 { + type LowHalf = u32; + type HighHalf = i32; + + fn low(self) -> u32 { + self as u32 + } + fn high(self) -> i32 { + (self >> 32) as i32 + } + fn from_parts(low: u32, high: i32) -> i64 { + low as i64 | ((high as i64) << 32) + } +} diff --git a/src/mul.rs b/src/int/mul.rs similarity index 99% rename from src/mul.rs rename to src/int/mul.rs index 2c4ce52..3d68444 100644 --- a/src/mul.rs +++ b/src/int/mul.rs @@ -1,4 +1,4 @@ -use {Int, LargeInt}; +use int::{Int, LargeInt}; macro_rules! mul { ($intrinsic:ident: $ty:ty) => { diff --git a/src/sdiv.rs b/src/int/sdiv.rs similarity index 99% rename from src/sdiv.rs rename to src/int/sdiv.rs index b123161..72ddbca 100644 --- a/src/sdiv.rs +++ b/src/int/sdiv.rs @@ -1,4 +1,4 @@ -use Int; +use int::Int; macro_rules! div { ($intrinsic:ident: $ty:ty, $uty:ty) => { diff --git a/src/shift.rs b/src/int/shift.rs similarity index 99% rename from src/shift.rs rename to src/int/shift.rs index ccbfe13..96ae6db 100644 --- a/src/shift.rs +++ b/src/int/shift.rs @@ -1,4 +1,4 @@ -use {Int, LargeInt}; +use int::{Int, LargeInt}; macro_rules! ashl { ($intrinsic:ident: $ty:ty) => { diff --git a/src/udiv.rs b/src/int/udiv.rs similarity index 99% rename from src/udiv.rs rename to src/int/udiv.rs index 82932e3..f3ec720 100644 --- a/src/udiv.rs +++ b/src/int/udiv.rs @@ -1,5 +1,5 @@ use core::mem; -use {Int, LargeInt}; +use int::{Int, LargeInt}; /// Returns `n / d` #[cfg_attr(not(test), no_mangle)] diff --git a/src/lib.rs b/src/lib.rs index 1351c38..b817d74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,83 +20,14 @@ extern crate core; #[cfg(all(not(windows), not(target_os = "macos")))] extern crate rlibc; +pub mod int; + #[cfg(target_arch = "arm")] pub mod arm; #[cfg(target_arch = "x86_64")] pub mod x86_64; -pub mod udiv; -pub mod sdiv; -pub mod mul; -pub mod shift; - #[cfg(test)] mod qc; -/// Trait for some basic operations on integers -trait Int { - fn bits() -> u32; -} - -// TODO: Once i128/u128 support lands, we'll want to add impls for those as well -impl Int for u32 { - fn bits() -> u32 { - 32 - } -} -impl Int for i32 { - fn bits() -> u32 { - 32 - } -} -impl Int for u64 { - fn bits() -> u32 { - 64 - } -} -impl Int for i64 { - fn bits() -> u32 { - 64 - } -} - -/// Trait to convert an integer to/from smaller parts -trait LargeInt { - type LowHalf; - type HighHalf; - - fn low(self) -> Self::LowHalf; - fn high(self) -> Self::HighHalf; - fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self; -} - -// TODO: Once i128/u128 support lands, we'll want to add impls for those as well -impl LargeInt for u64 { - type LowHalf = u32; - type HighHalf = u32; - - fn low(self) -> u32 { - self as u32 - } - fn high(self) -> u32 { - (self >> 32) as u32 - } - fn from_parts(low: u32, high: u32) -> u64 { - low as u64 | ((high as u64) << 32) - } -} -impl LargeInt for i64 { - type LowHalf = u32; - type HighHalf = i32; - - fn low(self) -> u32 { - self as u32 - } - fn high(self) -> i32 { - (self >> 32) as i32 - } - fn from_parts(low: u32, high: i32) -> i64 { - low as i64 | ((high as i64) << 32) - } -} diff --git a/src/qc.rs b/src/qc.rs index 6b37cce..eccde76 100644 --- a/src/qc.rs +++ b/src/qc.rs @@ -8,7 +8,7 @@ use std::fmt; use quickcheck::{Arbitrary, Gen}; -use LargeInt; +use int::LargeInt; // Generates values in the full range of the integer type macro_rules! arbitrary {