Skip to content

Commit 26e2ee0

Browse files
committed
Auto merge of #38767 - est31:master, r=eddyb
Some i128 tests * Add some FFI tests for i128 on architectures where we have sort of working "C" FFI support. On all other architectures we ignore the test. * enhance the u128 overflow tests
2 parents 5dd07b6 + 28f6d4a commit 26e2ee0

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

src/rt/rust_test_helpers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,22 @@ LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
268268
li.QuadPart += 1;
269269
return li;
270270
}
271+
272+
#define DO_INT128_TEST !(defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && \
273+
defined(__amd64__)
274+
275+
#if DO_INT128_TEST
276+
277+
unsigned __int128 identity(unsigned __int128 a) {
278+
return a;
279+
}
280+
281+
__int128 square(__int128 a) {
282+
return a * a;
283+
}
284+
285+
__int128 sub(__int128 a, __int128 b) {
286+
return a - b;
287+
}
288+
289+
#endif

src/test/run-pass/i128-ffi.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-stage0
12+
// ignore-stage1
13+
14+
// MSVC doesn't support 128 bit integers, and other Windows
15+
// C compilers have very inconsistent views on how the ABI
16+
// should look like.
17+
18+
// ignore-windows
19+
20+
// Ignore 32 bit targets:
21+
// ignore-x86, ignore-arm
22+
23+
#![feature(i128_type)]
24+
25+
#[link(name = "rust_test_helpers", kind = "static")]
26+
extern "C" {
27+
fn identity(f: u128) -> u128;
28+
fn square(f: i128) -> i128;
29+
fn sub(f: i128, f: i128) -> i128;
30+
}
31+
32+
fn main() {
33+
unsafe {
34+
let a = 0x734C_C2F2_A521;
35+
let b = 0x33EE_0E2A_54E2_59DA_A0E7_8E41;
36+
let b_out = identity(b);
37+
assert_eq!(b, b_out);
38+
let a_square = square(a);
39+
assert_eq!(b, a_square as u128);
40+
let k = 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210;
41+
let k_d = 0x2468_ACF1_3579_BDFF_DB97_530E_CA86_420;
42+
let k_out = sub(k_d, k);
43+
assert_eq!(k, k_out);
44+
}
45+
}

src/test/run-pass/i128.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,12 @@ fn main() {
9696
assert_eq!((-z).checked_mul(-z), Some(0x734C_C2F2_A521));
9797
assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
9898
assert_eq!((k).checked_mul(k), None);
99+
let l: i128 = b(i128::min_value());
100+
let o: i128 = b(17);
101+
assert_eq!(l.checked_sub(b(2)), None);
102+
assert_eq!(l.checked_add(l), None);
103+
assert_eq!((-(l + 1)).checked_add(2), None);
104+
assert_eq!(l.checked_sub(l), Some(0));
105+
assert_eq!(b(1u128).checked_shl(b(127)), Some(1 << 127));
106+
assert_eq!(o.checked_shl(b(128)), None);
99107
}

src/test/run-pass/u128.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
// ignore-stage0
1212
// ignore-stage1
13-
#![feature(i128_type)]
13+
#![feature(i128_type, test)]
14+
15+
extern crate test;
16+
use test::black_box as b;
1417

1518
fn main() {
1619
let x: u128 = 0xFFFF_FFFF_FFFF_FFFF__FFFF_FFFF_FFFF_FFFF;
@@ -63,5 +66,15 @@ fn main() {
6366
format!("{}", u128::max_value()));
6467
assert_eq!("147573952589676412928", format!("{:?}", j));
6568
// common traits
66-
x.clone();
69+
assert_eq!(x, b(x.clone()));
70+
// overflow checks
71+
assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
72+
assert_eq!((k).checked_mul(k), None);
73+
let l: u128 = b(u128::max_value() - 10);
74+
let o: u128 = b(17);
75+
assert_eq!(l.checked_add(b(11)), None);
76+
assert_eq!(l.checked_sub(l), Some(0));
77+
assert_eq!(o.checked_sub(b(18)), None);
78+
assert_eq!(b(1u128).checked_shl(b(127)), Some(1 << 127));
79+
assert_eq!(o.checked_shl(b(128)), None);
6780
}

0 commit comments

Comments
 (0)