Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

C interop: f80, f128 and c_longdouble

Metadata
Point of contactFolkert de Vries
StatusProposed
What and why
Tracking issue
Other tracking issuesN/A
Zulip channelN/A
Funding contactTrifecta Tech Foundation
Teamscompiler, lang, libs-api
Task ownersFolkert 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 on x86 and x86_64
  • core::arch::powerpc::ibm_f128, only available on powerpc, powerpc64 and powerpc64le

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

TaskOwner(s)Notes
implement core::arch::x86::x87_f80Folkert de Vries
implement core::arch::powerpc::ibm_f128Folkert de Vries
f128 ToString/FromStringFolkert 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/FromStringFolkert de Vries
add f128 assembly support for riscv in LLVM & rustcFolkert de Vries
fix the f128 windows ABI in LLVMFolkert de Vries, Trevor Grosswe will likely need support from the LLVM/Microsoft side here
stabilize core::arch::x86::x87_f80Folkert de Vries
stabilize core::arch::powerpc::ibm_f128Folkert de Vries
attempt to stabilize core::ffi::c_longdoubleFolkert de Vriesif the types must have (some) arithmetic operations, we might need more time

Team asks

TeamSupport levelNotes
compilerMediumThe changes are not complicated, but touch some core types so this might need some design/refactoring to get right
langSmallThese new types have a new ABI
libs-apiSmallJust a vibe check on the names and minimal APIs

Funding

PurposeCostFundedSponsor(s)
implementation work$36,000No

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_f80 and ibm_f128 on nightly
  • Month 3-9: improvements to f128 in rustc and LLVM
  • Month 9-12: stabilize x87_f80 and ibm_f128, prepare stabilization of c_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.)