File tree 1 file changed +59
-4
lines changed
1 file changed +59
-4
lines changed Original file line number Diff line number Diff line change 9
9
#[ cfg( test) ]
10
10
extern crate core;
11
11
12
- use core:: mem;
13
-
14
12
#[ cfg( target_arch = "arm" ) ]
15
13
pub mod arm;
16
14
17
15
#[ cfg( test) ]
18
16
mod test;
19
17
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
+ }
21
76
22
77
macro_rules! absv_i2 {
23
78
( $intrinsic: ident : $ty: ty) => {
24
79
#[ no_mangle]
25
80
pub extern "C" fn $intrinsic( x: $ty) -> $ty {
26
- let n = mem :: size_of :: <$ty>( ) * CHAR_BITS ;
81
+ let n = <$ty>:: bits ( ) ;
27
82
if x == 1 << ( n - 1 ) {
28
83
panic!( ) ;
29
84
}
You can’t perform that action at this time.
0 commit comments