Skip to content

Commit 6f0d50e

Browse files
committed
Move integer functions to separate module
1 parent 8603e64 commit 6f0d50e

File tree

8 files changed

+82
-78
lines changed

8 files changed

+82
-78
lines changed

src/arm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ pub unsafe fn __aeabi_ldivmod() {
6060
// TODO: These two functions should be defined as aliases
6161
#[cfg_attr(not(test), no_mangle)]
6262
pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
63-
::udiv::__udivsi3(a, b)
63+
::int::udiv::__udivsi3(a, b)
6464
}
6565

6666
#[cfg_attr(not(test), no_mangle)]
6767
pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
68-
::sdiv::__divsi3(a, b)
68+
::int::sdiv::__divsi3(a, b)
6969
}
7070

7171
extern "C" {

src/int/mod.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
pub mod mul;
3+
pub mod sdiv;
4+
pub mod shift;
5+
pub mod udiv;
6+
7+
/// Trait for some basic operations on integers
8+
pub trait Int {
9+
/// Returns the bitwidth of the int type
10+
fn bits() -> u32;
11+
}
12+
13+
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
14+
impl Int for u32 {
15+
fn bits() -> u32 {
16+
32
17+
}
18+
}
19+
impl Int for i32 {
20+
fn bits() -> u32 {
21+
32
22+
}
23+
}
24+
impl Int for u64 {
25+
fn bits() -> u32 {
26+
64
27+
}
28+
}
29+
impl Int for i64 {
30+
fn bits() -> u32 {
31+
64
32+
}
33+
}
34+
35+
/// Trait to convert an integer to/from smaller parts
36+
pub trait LargeInt {
37+
type LowHalf;
38+
type HighHalf;
39+
40+
fn low(self) -> Self::LowHalf;
41+
fn high(self) -> Self::HighHalf;
42+
fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self;
43+
}
44+
45+
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
46+
impl LargeInt for u64 {
47+
type LowHalf = u32;
48+
type HighHalf = u32;
49+
50+
fn low(self) -> u32 {
51+
self as u32
52+
}
53+
fn high(self) -> u32 {
54+
(self >> 32) as u32
55+
}
56+
fn from_parts(low: u32, high: u32) -> u64 {
57+
low as u64 | ((high as u64) << 32)
58+
}
59+
}
60+
impl LargeInt for i64 {
61+
type LowHalf = u32;
62+
type HighHalf = i32;
63+
64+
fn low(self) -> u32 {
65+
self as u32
66+
}
67+
fn high(self) -> i32 {
68+
(self >> 32) as i32
69+
}
70+
fn from_parts(low: u32, high: i32) -> i64 {
71+
low as i64 | ((high as i64) << 32)
72+
}
73+
}

src/mul.rs renamed to src/int/mul.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use {Int, LargeInt};
1+
use int::{Int, LargeInt};
22

33
macro_rules! mul {
44
($intrinsic:ident: $ty:ty) => {

src/sdiv.rs renamed to src/int/sdiv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Int;
1+
use int::Int;
22

33
macro_rules! div {
44
($intrinsic:ident: $ty:ty, $uty:ty) => {

src/shift.rs renamed to src/int/shift.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use {Int, LargeInt};
1+
use int::{Int, LargeInt};
22

33
macro_rules! ashl {
44
($intrinsic:ident: $ty:ty) => {

src/udiv.rs renamed to src/int/udiv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::mem;
2-
use {Int, LargeInt};
2+
use int::{Int, LargeInt};
33

44
/// Returns `n / d`
55
#[cfg_attr(not(test), no_mangle)]

src/lib.rs

+2-71
Original file line numberDiff line numberDiff line change
@@ -20,83 +20,14 @@ extern crate core;
2020
#[cfg(all(not(windows), not(target_os = "macos")))]
2121
extern crate rlibc;
2222

23+
pub mod int;
24+
2325
#[cfg(target_arch = "arm")]
2426
pub mod arm;
2527

2628
#[cfg(target_arch = "x86_64")]
2729
pub mod x86_64;
2830

29-
pub mod udiv;
30-
pub mod sdiv;
31-
pub mod mul;
32-
pub mod shift;
33-
3431
#[cfg(test)]
3532
mod qc;
3633

37-
/// Trait for some basic operations on integers
38-
trait Int {
39-
fn bits() -> u32;
40-
}
41-
42-
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
43-
impl Int for u32 {
44-
fn bits() -> u32 {
45-
32
46-
}
47-
}
48-
impl Int for i32 {
49-
fn bits() -> u32 {
50-
32
51-
}
52-
}
53-
impl Int for u64 {
54-
fn bits() -> u32 {
55-
64
56-
}
57-
}
58-
impl Int for i64 {
59-
fn bits() -> u32 {
60-
64
61-
}
62-
}
63-
64-
/// Trait to convert an integer to/from smaller parts
65-
trait LargeInt {
66-
type LowHalf;
67-
type HighHalf;
68-
69-
fn low(self) -> Self::LowHalf;
70-
fn high(self) -> Self::HighHalf;
71-
fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self;
72-
}
73-
74-
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
75-
impl LargeInt for u64 {
76-
type LowHalf = u32;
77-
type HighHalf = u32;
78-
79-
fn low(self) -> u32 {
80-
self as u32
81-
}
82-
fn high(self) -> u32 {
83-
(self >> 32) as u32
84-
}
85-
fn from_parts(low: u32, high: u32) -> u64 {
86-
low as u64 | ((high as u64) << 32)
87-
}
88-
}
89-
impl LargeInt for i64 {
90-
type LowHalf = u32;
91-
type HighHalf = i32;
92-
93-
fn low(self) -> u32 {
94-
self as u32
95-
}
96-
fn high(self) -> i32 {
97-
(self >> 32) as i32
98-
}
99-
fn from_parts(low: u32, high: i32) -> i64 {
100-
low as i64 | ((high as i64) << 32)
101-
}
102-
}

src/qc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt;
88

99
use quickcheck::{Arbitrary, Gen};
1010

11-
use LargeInt;
11+
use int::LargeInt;
1212

1313
// Generates values in the full range of the integer type
1414
macro_rules! arbitrary {

0 commit comments

Comments
 (0)