Move integer functions to separate module

This commit is contained in:
Matt Ickstadt 2016-08-17 15:50:24 -05:00
parent 8603e64554
commit 6f0d50ebea
8 changed files with 82 additions and 78 deletions

View File

@ -60,12 +60,12 @@ pub unsafe fn __aeabi_ldivmod() {
// TODO: These two functions should be defined as aliases // TODO: These two functions should be defined as aliases
#[cfg_attr(not(test), no_mangle)] #[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 { 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)] #[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 { pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
::sdiv::__divsi3(a, b) ::int::sdiv::__divsi3(a, b)
} }
extern "C" { extern "C" {

73
src/int/mod.rs Normal file
View File

@ -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)
}
}

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
use core::mem; use core::mem;
use {Int, LargeInt}; use int::{Int, LargeInt};
/// Returns `n / d` /// Returns `n / d`
#[cfg_attr(not(test), no_mangle)] #[cfg_attr(not(test), no_mangle)]

View File

@ -20,83 +20,14 @@ extern crate core;
#[cfg(all(not(windows), not(target_os = "macos")))] #[cfg(all(not(windows), not(target_os = "macos")))]
extern crate rlibc; extern crate rlibc;
pub mod int;
#[cfg(target_arch = "arm")] #[cfg(target_arch = "arm")]
pub mod arm; pub mod arm;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub mod x86_64; pub mod x86_64;
pub mod udiv;
pub mod sdiv;
pub mod mul;
pub mod shift;
#[cfg(test)] #[cfg(test)]
mod qc; 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)
}
}

View File

@ -8,7 +8,7 @@ use std::fmt;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use LargeInt; use int::LargeInt;
// Generates values in the full range of the integer type // Generates values in the full range of the integer type
macro_rules! arbitrary { macro_rules! arbitrary {