Explorar el Código

Move integer functions to separate module

Matt Ickstadt hace 8 años
padre
commit
6f0d50ebea
Se han modificado 8 ficheros con 82 adiciones y 78 borrados
  1. 2 2
      src/arm.rs
  2. 73 0
      src/int/mod.rs
  3. 1 1
      src/int/mul.rs
  4. 1 1
      src/int/sdiv.rs
  5. 1 1
      src/int/shift.rs
  6. 1 1
      src/int/udiv.rs
  7. 2 71
      src/lib.rs
  8. 1 1
      src/qc.rs

+ 2 - 2
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" {

+ 73 - 0
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)
+    }
+}

+ 1 - 1
src/mul.rs → src/int/mul.rs

@@ -1,4 +1,4 @@
-use {Int, LargeInt};
+use int::{Int, LargeInt};
 
 macro_rules! mul {
     ($intrinsic:ident: $ty:ty) => {

+ 1 - 1
src/sdiv.rs → src/int/sdiv.rs

@@ -1,4 +1,4 @@
-use Int;
+use int::Int;
 
 macro_rules! div {
     ($intrinsic:ident: $ty:ty, $uty:ty) => {

+ 1 - 1
src/shift.rs → src/int/shift.rs

@@ -1,4 +1,4 @@
-use {Int, LargeInt};
+use int::{Int, LargeInt};
 
 macro_rules! ashl {
     ($intrinsic:ident: $ty:ty) => {

+ 1 - 1
src/udiv.rs → 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)]

+ 2 - 71
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)
-    }
-}

+ 1 - 1
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 {