Skip to content

Commit 35d7e66

Browse files
authored
Add i128/u128 inc/dec and i64 Ptr add/sub (#466)
1 parent be4c5d0 commit 35d7e66

9 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎libcc2rs/src/dec.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ macro_rules! prefix_wrap_dec_impl {
5959
};
6060
}
6161

62-
postfix_wrap_dec_impl!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
62+
postfix_wrap_dec_impl!(
63+
i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize
64+
);
6365
postfix_nowrap_dec_impl!(f32, f64);
6466

65-
prefix_wrap_dec_impl!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
67+
prefix_wrap_dec_impl!(
68+
i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize
69+
);
6670
prefix_nowrap_dec_impl!(f32, f64);
6771

6872
pub trait UnsafePostfixDec {

‎libcc2rs/src/inc.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ macro_rules! prefix_wrap_inc_impl {
5959
};
6060
}
6161

62-
prefix_wrap_inc_impl!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
62+
prefix_wrap_inc_impl!(
63+
i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize
64+
);
6365
prefix_nowrap_inc_impl!(f32, f64);
6466

65-
postfix_wrap_inc_impl!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
67+
postfix_wrap_inc_impl!(
68+
i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize
69+
);
6670
postfix_nowrap_inc_impl!(f32, f64);
6771

6872
pub trait UnsafePostfixInc {

‎libcc2rs/src/rc.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ macro_rules! impl_ptr_add_sub {
925925
}
926926
)+ }
927927
}
928-
impl_ptr_add_sub!(i32, u32, u64, isize, usize);
928+
impl_ptr_add_sub!(i32, u32, i64, u64, isize, usize);
929929

930930
impl<T> PostfixInc for Ptr<T> {
931931
#[inline]

‎tests/unit/out/refcount/pointer_offset.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ fn main_0() -> i32 {
4545
i.prefix_inc();
4646
}
4747
assert!((out == 51));
48+
let mut step: i64 = 2_i64;
49+
let mut offset: Ptr<i32> = (arr.as_pointer() as Ptr<i32>).offset((step) as isize);
50+
assert!(({ (offset.read()) } == { (*arr.borrow())[(2) as usize] }));
51+
offset = { offset.offset(-((step) as isize)) };
52+
assert!(({ (offset).clone() } == { ((arr.as_pointer() as Ptr<i32>).offset(0)) }));
4853
return 0;
4954
}
5055
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/refcount/unary.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ fn main_0() -> i32 {
3131
assert!((d == 1.5E+0));
3232
assert!(((n + n) == (2 * x2)));
3333
assert!((a[(0) as usize] == a[(0) as usize]));
34+
let mut wide: i128 = 5_i128;
35+
let mut w1: i128 = wide.postfix_inc();
36+
let mut w2: i128 = wide.prefix_inc();
37+
let mut w3: i128 = wide.postfix_dec();
38+
let mut w4: i128 = wide.prefix_dec();
39+
assert!((((w1 == 5_i128) && (w2 == 7_i128)) && (w3 == 7_i128)) && (w4 == 5_i128));
40+
let mut uwide: u128 = 0_u128;
41+
uwide.prefix_dec();
42+
uwide.postfix_inc();
43+
assert!((uwide == 0_u128));
3444
return 0;
3545
}
3646
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/pointer_offset.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ unsafe fn main_0() -> i32 {
4747
i.prefix_inc();
4848
}
4949
assert!(((out) == (51)));
50+
let mut step: i64 = 2_i64;
51+
let mut offset: *mut i32 = arr.as_mut_ptr().offset((step) as isize);
52+
assert!(((*offset) == (arr[(2) as usize])));
53+
offset = offset.offset(-((step) as isize));
54+
assert!(((offset) == (&mut arr[(0) as usize] as *mut i32)));
5055
return 0;
5156
}
5257
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/unary.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ unsafe fn main_0() -> i32 {
3333
assert!(((d) == (1.5E+0)));
3434
assert!((((n) + (n)) == ((2) * (x2))));
3535
assert!(((a[(0) as usize]) == (a[(0) as usize])));
36+
let mut wide: i128 = 5_i128;
37+
let mut w1: i128 = wide.postfix_inc();
38+
let mut w2: i128 = wide.prefix_inc();
39+
let mut w3: i128 = wide.postfix_dec();
40+
let mut w4: i128 = wide.prefix_dec();
41+
assert!(
42+
((((w1) == (5_i128)) && ((w2) == (7_i128))) && ((w3) == (7_i128))) && ((w4) == (5_i128))
43+
);
44+
let mut uwide: u128 = 0_u128;
45+
uwide.prefix_dec();
46+
uwide.postfix_inc();
47+
assert!(((uwide) == (0_u128)));
3648
return 0;
3749
}
3850
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/pointer_offset.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ int main() {
1717
for (int i = 0; i < 5; ++i)
1818
out += ptr[i];
1919
assert(out == 51);
20+
21+
long step = 2;
22+
int *offset = arr + step;
23+
assert(*offset == arr[2]);
24+
offset = offset - step;
25+
assert(offset == &arr[0]);
2026
return 0;
2127
}

‎tests/unit/unary.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,16 @@ int main() {
1919
assert(d == 1.5);
2020
assert(+n + +n == 2 * x2);
2121
assert(+a[0] == a[0]);
22+
23+
__int128 wide = 5;
24+
__int128 w1 = wide++;
25+
__int128 w2 = ++wide;
26+
__int128 w3 = wide--;
27+
__int128 w4 = --wide;
28+
assert(w1 == 5 && w2 == 7 && w3 == 7 && w4 == 5);
29+
unsigned __int128 uwide = 0;
30+
--uwide;
31+
uwide++;
32+
assert(uwide == 0);
2233
return 0;
2334
}

0 commit comments

Comments
 (0)