Skip to content

Commit 127542f

Browse files
committed
Create optionally-available __int128 typedefs and use them for ARM64 definitions.
This adds the following types to fixed_width_ints behind appropriate platform cfgs: * __int128 * __int128_t * __uint128 * __uint128_t and user_fpsimd_struct to arm64 android and linux.
1 parent 1708299 commit 127542f

File tree

8 files changed

+116
-1
lines changed

8 files changed

+116
-1
lines changed

build.rs

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ fn main() {
6060
println!("cargo:rustc-cfg=libc_align");
6161
}
6262

63+
// Rust >= 1.26 supports i128 and u128:
64+
if rustc_minor_ver >= 26 || rustc_dep_of_std {
65+
println!("cargo:rustc-cfg=libc_int128");
66+
}
67+
6368
// Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
6469
// Otherwise, it defines an incompatible type to retaining
6570
// backwards-compatibility.
@@ -82,6 +87,11 @@ fn main() {
8287
println!("cargo:rustc-cfg=libc_ptr_addr_of");
8388
}
8489

90+
// Rust >= 1.57.0 allows assert! (and panics in general) in constants.
91+
if rustc_minor_ver >= 57 || rustc_dep_of_std {
92+
println!("cargo:rustc-cfg=libc_const_assert");
93+
}
94+
8595
// #[thread_local] is currently unstable
8696
if rustc_dep_of_std {
8797
println!("cargo:rustc-cfg=libc_thread_local");

libc-test/semver/android-aarch64.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ SYS_syscalls
1212
SYS_fcntl
1313
__system_property_wait
1414
user_regs_struct
15+
user_fpsimd_struct

libc-test/semver/linux-aarch64.txt

+1
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ max_align_t
9191
mcontext_t
9292
ucontext_t
9393
user_regs_struct
94+
user_fpsimd_struct

src/fixed_width_ints.rs

+75
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,78 @@ pub type uint16_t = u16;
1818
pub type uint32_t = u32;
1919
#[deprecated(since = "0.2.55", note = "Use u64 instead.")]
2020
pub type uint64_t = u64;
21+
22+
cfg_if! {
23+
if #[cfg(all(libc_int128, target_arch = "aarch64", not(target_os = "windows")))] {
24+
// This introduces partial support for FFI with __int128 and
25+
// equivalent types on platforms where Rust's definition is validated
26+
// to match the standard C ABI of that platform.
27+
//
28+
// Rust does not guarantee u128/i128 are sound for FFI, and its
29+
// definitions are in fact known to be incompatible. [0]
30+
//
31+
// However these problems aren't fundamental, and are just platform
32+
// inconsistencies. Specifically at the time of this writing:
33+
//
34+
// * For x64 SysV ABIs (everything but Windows), the types are underaligned.
35+
// * For all Windows ABIs, Microsoft doesn't actually officially define __int128,
36+
// and as a result different implementations don't actually agree on its ABI.
37+
//
38+
// But on the other major aarch64 platforms (android, linux, ios, macos) we have
39+
// validated that rustc has the right ABI for these types. This is important because
40+
// aarch64 uses these types in some fundamental OS types like user_fpsimd_struct,
41+
// which represents saved simd registers.
42+
//
43+
// Any API which uses these types will need to `#[ignore(improper_ctypes)]`
44+
// until the upstream rust issue is resolved, but this at least lets us make
45+
// progress on platforms where this type is important.
46+
//
47+
// The supported architectures and OSes is intentionally very restricted,
48+
// as careful work needs to be done to verify that a particular platform
49+
// has a conformant ABI.
50+
//
51+
// [0]: https://github.com/rust-lang/rust/issues/54341
52+
53+
/// C `__int128` (a GCC extension that's part of many ABIs)
54+
pub type __int128 = i128;
55+
/// C `unsigned __int128` (a GCC extension that's part of many ABIs)
56+
pub type __uint128 = u128;
57+
/// C __int128_t (alternate name for [__int128][])
58+
pub type __int128_t = i128;
59+
/// C __uint128_t (alternate name for [__uint128][])
60+
pub type __uint128_t = u128;
61+
62+
cfg_if! {
63+
if #[cfg(libc_const_assert)] {
64+
// NOTE: if you add more platforms to here, you may need to cfg
65+
// these consts. They should always match the platform's values
66+
// for `sizeof(__int128)` and `_Alignof(__int128)`.
67+
const _SIZE_128: usize = 16;
68+
const _ALIGN_128: usize = 16;
69+
70+
/// Since Rust doesn't officially guarantee that these types
71+
/// have compatible ABIs, we const assert that these values have the
72+
/// known size/align of the target platform's libc. If rustc ever
73+
/// tries to regress things, it will cause a compilation error.
74+
///
75+
/// This isn't a bullet-proof solution because e.g. it doesn't
76+
/// catch the fact that llvm and gcc disagree on how x64 __int128
77+
/// is actually *passed* on the stack (clang underaligns it for
78+
/// the same reason that rustc *never* properly aligns it).
79+
const _ASSERT_128_COMPAT: () = {
80+
assert!(core::mem::size_of::<__int128>() == _SIZE_128);
81+
assert!(core::mem::align_of::<__int128>() == _ALIGN_128);
82+
83+
assert!(core::mem::size_of::<__uint128>() == _SIZE_128);
84+
assert!(core::mem::align_of::<__uint128>() == _ALIGN_128);
85+
86+
assert!(core::mem::size_of::<__int128_t>() == _SIZE_128);
87+
assert!(core::mem::align_of::<__int128_t>() == _ALIGN_128);
88+
89+
assert!(core::mem::size_of::<__uint128_t>() == _SIZE_128);
90+
assert!(core::mem::align_of::<__uint128_t>() == _ALIGN_128);
91+
};
92+
}
93+
}
94+
}
95+
}

src/unix/bsd/apple/b64/aarch64/align.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ s! {
3939
pub __pad: u32,
4040
}
4141

42-
#[repr(align(16))]
42+
// This type natively uses a uint128, but for a while we hacked
43+
// it in with repr(align) and `[u64; 2]`. uint128 isn't available
44+
// all the way back to our earliest supported versions so we
45+
// preserver the old shim.
46+
#[cfg_attr(not(libc_int128), repr(align(16)))]
4347
pub struct __darwin_arm_neon_state64 {
48+
#[cfg(libc_int128)]
49+
pub __v: [::__uint128_t; 32],
50+
#[cfg(not(libc_int128))]
4451
pub __v: [[u64; 2]; 32],
4552
pub __fpsr: u32,
4653
pub __fpcr: u32,

src/unix/linux_like/android/b64/aarch64/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ s! {
5555
pub pc: u64,
5656
pub pstate: u64,
5757
}
58+
59+
#[cfg(libc_int128)]
60+
pub struct user_fpsimd_struct {
61+
pub vregs: [::__uint128_t; 32],
62+
pub fpsr: u32,
63+
pub fpcr: u32,
64+
}
5865
}
5966

6067
pub const O_DIRECT: ::c_int = 0x10000;

src/unix/linux_like/linux/gnu/b64/aarch64/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ s! {
150150
pub pstate: ::c_ulonglong,
151151
}
152152

153+
#[cfg(libc_int128)]
154+
pub struct user_fpsimd_struct {
155+
pub vregs: [::__uint128_t; 32],
156+
pub fpsr: ::c_uint,
157+
pub fpcr: ::c_uint,
158+
}
159+
153160
pub struct ipc_perm {
154161
pub __key: ::key_t,
155162
pub uid: ::uid_t,

src/unix/linux_like/linux/musl/b64/aarch64/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ s! {
5656
pub pstate: ::c_ulonglong,
5757
}
5858

59+
#[cfg(libc_int128)]
60+
pub struct user_fpsimd_struct {
61+
pub vregs: [::__uint128_t; 32],
62+
pub fpsr: ::c_uint,
63+
pub fpcr: ::c_uint,
64+
}
65+
5966
pub struct ipc_perm {
6067
pub __ipc_perm_key: ::key_t,
6168
pub uid: ::uid_t,

0 commit comments

Comments
 (0)