Skip to content

Commit 9debde0

Browse files
committed
Auto merge of #133 - est31:i128, r=japaric
i128 intrinsics Adds i128 intrinsics. Note that this PR doesn't do float intrinsics, due to the missing presence of macros, those are however still required in order for rustc to switch to this crate.
2 parents 0d0f22a + 37d3490 commit 9debde0

File tree

12 files changed

+547
-213
lines changed

12 files changed

+547
-213
lines changed

Diff for: README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ features = ["c"]
193193

194194
These builtins are needed to support 128-bit integers, which are in the process of being added to Rust.
195195

196-
- [ ] ashlti3.c
197-
- [ ] ashrti3.c
198-
- [ ] divti3.c
196+
- [x] ashlti3.c
197+
- [x] ashrti3.c
198+
- [x] divti3.c
199199
- [ ] fixdfti.c
200200
- [ ] fixsfti.c
201201
- [ ] fixunsdfti.c
@@ -204,13 +204,13 @@ These builtins are needed to support 128-bit integers, which are in the process
204204
- [ ] floattisf.c
205205
- [ ] floatuntidf.c
206206
- [ ] floatuntisf.c
207-
- [ ] lshrti3.c
208-
- [ ] modti3.c
209-
- [ ] muloti4.c
210-
- [ ] multi3.c
211-
- [ ] udivmodti4.c
212-
- [ ] udivti3.c
213-
- [ ] umodti3.c
207+
- [x] lshrti3.c
208+
- [x] modti3.c
209+
- [x] muloti4.c
210+
- [x] multi3.c
211+
- [x] udivmodti4.c
212+
- [x] udivti3.c
213+
- [x] umodti3.c
214214

215215
## Unimplemented functions
216216

Diff for: build.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ fn main() {
150150
"int_util.c",
151151
"muldc3.c",
152152
"muldf3.c",
153-
"muloti4.c",
154153
"mulsc3.c",
155154
"mulsf3.c",
156155
"mulvdi3.c",
@@ -179,13 +178,10 @@ fn main() {
179178
sources.extend(&["absvti2.c",
180179
"addtf3.c",
181180
"addvti3.c",
182-
"ashlti3.c",
183-
"ashrti3.c",
184181
"clzti2.c",
185182
"cmpti2.c",
186183
"ctzti2.c",
187184
"divtf3.c",
188-
"divti3.c",
189185
"ffsti2.c",
190186
"fixdfti.c",
191187
"fixsfti.c",
@@ -199,10 +195,7 @@ fn main() {
199195
"floatuntidf.c",
200196
"floatuntisf.c",
201197
"floatuntixf.c",
202-
"lshrti3.c",
203-
"modti3.c",
204198
"multf3.c",
205-
"multi3.c",
206199
"mulvti3.c",
207200
"negti2.c",
208201
"negvti2.c",
@@ -212,10 +205,7 @@ fn main() {
212205
"subtf3.c",
213206
"subvti3.c",
214207
"trampoline_setup.c",
215-
"ucmpti2.c",
216-
"udivmodti4.c",
217-
"udivti3.c",
218-
"umodti3.c"]);
208+
"ucmpti2.c"]);
219209
}
220210

221211
if target_vendor == "apple" {

Diff for: compiler-rt/compiler-rt-cdylib/build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ fn main() {
6060
"addsf3.c",
6161
"powidf2.c",
6262
"powisf2.c",
63+
// 128 bit integers
64+
"lshrti3.c",
65+
"modti3.c",
66+
"muloti4.c",
67+
"multi3.c",
68+
"udivmodti4.c",
69+
"udivti3.c",
70+
"umodti3.c",
71+
"ashlti3.c",
72+
"ashrti3.c",
73+
"divti3.c",
6374
]);
6475

6576
let builtins_dir = Path::new("compiler-rt/lib/builtins");

Diff for: compiler-rt/compiler-rt-cdylib/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ declare!(___adddf3, __adddf3);
5858
declare!(___powisf2, __powisf2);
5959
declare!(___powidf2, __powidf2);
6060

61+
#[cfg(all(not(windows),
62+
not(target_arch = "mips64"),
63+
not(target_arch = "mips64el"),
64+
target_pointer_width="64"))]
65+
pub mod int_128 {
66+
extern {
67+
fn __lshrti3();
68+
fn __modti3();
69+
fn __muloti4();
70+
fn __multi3();
71+
fn __udivmodti4();
72+
fn __udivti3();
73+
fn __umodti3();
74+
fn __ashlti3();
75+
fn __ashrti3();
76+
fn __divti3();
77+
}
78+
79+
declare!(___lshrti3, __lshrti3);
80+
declare!(___modti3, __modti3);
81+
declare!(___muloti4, __muloti4);
82+
declare!(___multi3, __multi3);
83+
declare!(___udivmodti4, __udivmodti4);
84+
declare!(___udivti3, __udivti3);
85+
declare!(___umodti3, __umodti3);
86+
declare!(___ashlti3, __ashlti3);
87+
declare!(___ashrti3, __ashrti3);
88+
declare!(___divti3, __divti3);
89+
}
90+
6191
#[lang = "eh_personality"]
6292
fn eh_personality() {}
6393
#[lang = "panic_fmt"]

Diff for: src/bin/intrinsics.rs

+46
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(lang_items)]
1313
#![feature(libc)]
1414
#![feature(start)]
15+
#![feature(i128_type)]
1516
#![no_std]
1617

1718
#[cfg(not(thumb))]
@@ -300,6 +301,42 @@ mod intrinsics {
300301
pub fn umoddi3(a: u64, b: u64) -> u64 {
301302
a % b
302303
}
304+
305+
pub fn muloti4(a: u128, b: u128) -> Option<u128> {
306+
a.checked_mul(b)
307+
}
308+
309+
pub fn multi3(a: u128, b: u128) -> u128 {
310+
a.wrapping_mul(b)
311+
}
312+
313+
pub fn ashlti3(a: u128, b: usize) -> u128 {
314+
a >> b
315+
}
316+
317+
pub fn ashrti3(a: u128, b: usize) -> u128 {
318+
a << b
319+
}
320+
321+
pub fn lshrti3(a: i128, b: usize) -> i128 {
322+
a >> b
323+
}
324+
325+
pub fn udivti3(a: u128, b: u128) -> u128 {
326+
a / b
327+
}
328+
329+
pub fn umodti3(a: u128, b: u128) -> u128 {
330+
a % b
331+
}
332+
333+
pub fn divti3(a: i128, b: i128) -> i128 {
334+
a / b
335+
}
336+
337+
pub fn modti3(a: i128, b: i128) -> i128 {
338+
a % b
339+
}
303340
}
304341

305342
#[cfg(feature = "c")]
@@ -356,6 +393,15 @@ fn run() {
356393
bb(powidf2(bb(2.), bb(3)));
357394
bb(powisf2(bb(2.), bb(3)));
358395
bb(umoddi3(bb(2), bb(3)));
396+
bb(muloti4(bb(2), bb(2)));
397+
bb(multi3(bb(2), bb(2)));
398+
bb(ashlti3(bb(2), bb(2)));
399+
bb(ashrti3(bb(2), bb(2)));
400+
bb(lshrti3(bb(2), bb(2)));
401+
bb(udivti3(bb(2), bb(2)));
402+
bb(umodti3(bb(2), bb(2)));
403+
bb(divti3(bb(2), bb(2)));
404+
bb(modti3(bb(2), bb(2)));
359405
}
360406

361407
#[cfg(all(feature = "c", not(thumb)))]

Diff for: src/int/mod.rs

+50-44
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
macro_rules! hty {
2+
($ty:ty) => {
3+
<$ty as LargeInt>::HighHalf
4+
}
5+
}
6+
7+
macro_rules! os_ty {
8+
($ty:ty) => {
9+
<$ty as Int>::OtherSign
10+
}
11+
}
112

213
pub mod mul;
314
pub mod sdiv;
@@ -6,32 +17,33 @@ pub mod udiv;
617

718
/// Trait for some basic operations on integers
819
pub trait Int {
20+
/// Type with the same width but other signedness
21+
type OtherSign;
922
/// Returns the bitwidth of the int type
1023
fn bits() -> u32;
1124
}
1225

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
26+
macro_rules! int_impl {
27+
($ity:ty, $sty:ty, $bits:expr) => {
28+
impl Int for $ity {
29+
type OtherSign = $sty;
30+
fn bits() -> u32 {
31+
$bits
32+
}
33+
}
34+
impl Int for $sty {
35+
type OtherSign = $ity;
36+
fn bits() -> u32 {
37+
$bits
38+
}
39+
}
3240
}
3341
}
3442

43+
int_impl!(i32, u32, 32);
44+
int_impl!(i64, u64, 64);
45+
int_impl!(i128, u128, 128);
46+
3547
/// Trait to convert an integer to/from smaller parts
3648
pub trait LargeInt {
3749
type LowHalf;
@@ -42,32 +54,26 @@ pub trait LargeInt {
4254
fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self;
4355
}
4456

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;
57+
macro_rules! large_int {
58+
($ty:ty, $tylow:ty, $tyhigh:ty, $halfbits:expr) => {
59+
impl LargeInt for $ty {
60+
type LowHalf = $tylow;
61+
type HighHalf = $tyhigh;
4962

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)
63+
fn low(self) -> $tylow {
64+
self as $tylow
65+
}
66+
fn high(self) -> $tyhigh {
67+
(self >> $halfbits) as $tyhigh
68+
}
69+
fn from_parts(low: $tylow, high: $tyhigh) -> $ty {
70+
low as $ty | ((high as $ty) << $halfbits)
71+
}
72+
}
5873
}
5974
}
60-
impl LargeInt for i64 {
61-
type LowHalf = u32;
62-
type HighHalf = i32;
6375

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-
}
76+
large_int!(u64, u32, u32, 32);
77+
large_int!(i64, u32, i32, 32);
78+
large_int!(u128, u64, u64, 64);
79+
large_int!(i128, u64, i64, 64);

0 commit comments

Comments
 (0)