Skip to content

Commit 28f6d4a

Browse files
committed
Add test for i128 ffi usage
1 parent 1fefa3c commit 28f6d4a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/rt/rust_test_helpers.c

+19
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

+45
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+
}

0 commit comments

Comments
 (0)