C interop: f80, f128 and c_longdouble
| Metadata | |
|---|---|
| Point of contact | Folkert de Vries |
| Status | Proposed |
| What and why | |
| Tracking issue | |
| Other tracking issues | N/A |
| Zulip channel | N/A |
| Funding contact | Trifecta Tech Foundation |
| Teams | compiler, lang, libs-api |
| Task owners | Folkert de Vries, Trevor Gross |
Summary
Rust should be able to define an ABI-compatible counterpart to every signature that C can define. This is important for C interop and translation of code from C to rust, using manual FFI, and tooling like c-bindgen and c2rust. This goal closes a long-standing gap in rust’s ability to match C.
Motivation
The status quo
Today there are still a number of cases where rust cannot, in a portable way, express type signatures that C can define.
The C long double type is one such missing piece. Normally rust provides the C-compatible types as type aliases (e.g. type c_int = i32), but c_longdouble requires additional types: on many platforms it maps to f64, but on some it maps instead to an 80-bit or 128-bit float, two types that are not currently available in stable rust.
We will add the types (with no or very limited arithmetic operations) for interop purposes.
What we propose to do about it
We will add two new storage-only float types to core::arch to facilitate with interop:
core::arch::x86::x87_f80, only available onx86andx86_64core::arch::powerpc::ibm_f128, only available onpowerpc,powerpc64andpowerpc64le
The API will be minimal, and will not include any arithmetic operations. Those operations can be added later.
We will also continue to push f128 forward, both on the LLVM and rustc side.
Work items over the next year
| Task | Owner(s) | Notes |
|---|---|---|
implement core::arch::x86::x87_f80 | Folkert de Vries | |
implement core::arch::powerpc::ibm_f128 | Folkert de Vries | |
f128 ToString/FromString | Folkert de Vries` | There is some active work on these algorithms, we’ll try to help out where we can |
core::arch::powerpc::ibm_f128 ToString/FromString | Folkert de Vries | |
add f128 assembly support for riscv in LLVM & rustc | Folkert de Vries | |
fix the f128 windows ABI in LLVM | Folkert de Vries, Trevor Gross | we will likely need support from the LLVM/Microsoft side here |
stabilize core::arch::x86::x87_f80 | Folkert de Vries | |
stabilize core::arch::powerpc::ibm_f128 | Folkert de Vries | |
attempt to stabilize core::ffi::c_longdouble | Folkert de Vries | if the types must have (some) arithmetic operations, we might need more time |
Team asks
| Team | Support level | Notes |
|---|---|---|
| compiler | Medium | The changes are not complicated, but touch some core types so this might need some design/refactoring to get right |
| lang | Small | These new types have a new ABI |
| libs-api | Small | Just a vibe check on the names and minimal APIs |
Funding
| Purpose | Cost | Funded | Sponsor(s) |
|---|---|---|---|
| implementation work | $36,000 | No |
Target timeline
The duration of the project is 12 months. Starting from the agreed start date (“Month 1”), the timeline we’re targeting is:
- Month 1-3: unstable
c_longdouble,x87_f80andibm_f128on nightly - Month 3-9: improvements to
f128in rustc and LLVM - Month 9-12: stabilize
x87_f80andibm_f128, prepare stabilization ofc_longdouble
The expected effort for the work is 3 person-months, the long timeline is really due to LLVM release cycles.
Notes
There is a stalled RFC for some of these types at https://github.com/rust-lang/rfcs/pull/3456.
Blockers for f128 support are tracked at cfg.has_reliable_f128.
There is a draft PR https://github.com/rust-lang/rust/pull/140417 for c_longdouble that already lists the right type for c_longdouble for many targets.
Further discussion is at #t-libs > `f80`, `f128` and `c_longdouble`.
Frequently asked questions
(None.)