Skip to content

Commit c4142ed

Browse files
author
Jorge Aparicio
authored
Merge pull request #26 from Amanieu/int_trait
Add traits for integer operations
2 parents 1c9c2b4 + 8a94b69 commit c4142ed

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

src/lib.rs

+59-4
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,76 @@
99
#[cfg(test)]
1010
extern crate core;
1111

12-
use core::mem;
13-
1412
#[cfg(target_arch = "arm")]
1513
pub mod arm;
1614

1715
#[cfg(test)]
1816
mod test;
1917

20-
const CHAR_BITS: usize = 8;
18+
/// Trait for some basic operations on integers
19+
trait Int {
20+
fn bits() -> usize;
21+
}
22+
23+
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
24+
impl Int for u32 {
25+
fn bits() -> usize { 32 }
26+
}
27+
impl Int for i32 {
28+
fn bits() -> usize { 32 }
29+
}
30+
impl Int for u64 {
31+
fn bits() -> usize { 64 }
32+
}
33+
impl Int for i64 {
34+
fn bits() -> usize { 64 }
35+
}
36+
37+
/// Trait to convert an integer to/from smaller parts
38+
trait LargeInt {
39+
type LowHalf;
40+
type HighHalf;
41+
42+
fn low(self) -> Self::LowHalf;
43+
fn high(self) -> Self::HighHalf;
44+
fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self;
45+
}
46+
47+
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
48+
impl LargeInt for u64 {
49+
type LowHalf = u32;
50+
type HighHalf = u32;
51+
52+
fn low(self) -> u32 {
53+
self as u32
54+
}
55+
fn high(self) -> u32 {
56+
(self >> 32) as u32
57+
}
58+
fn from_parts(low: u32, high: u32) -> u64 {
59+
low as u64 | ((high as u64) << 32)
60+
}
61+
}
62+
impl LargeInt for i64 {
63+
type LowHalf = u32;
64+
type HighHalf = i32;
65+
66+
fn low(self) -> u32 {
67+
self as u32
68+
}
69+
fn high(self) -> i32 {
70+
(self >> 32) as i32
71+
}
72+
fn from_parts(low: u32, high: i32) -> i64 {
73+
low as i64 | ((high as i64) << 32)
74+
}
75+
}
2176

2277
macro_rules! absv_i2 {
2378
($intrinsic:ident : $ty:ty) => {
2479
#[no_mangle]
2580
pub extern "C" fn $intrinsic(x: $ty) -> $ty {
26-
let n = mem::size_of::<$ty>() * CHAR_BITS;
81+
let n = <$ty>::bits();
2782
if x == 1 << (n - 1) {
2883
panic!();
2984
}

0 commit comments

Comments
 (0)