Skip to content

Commit af2f694

Browse files
committed
Add c_longdouble
1 parent 25cdf1f commit af2f694

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Equivalent to C's `long double` type.
2+
3+
This type is [`f64`] on Apple and Windows targets, [`f128`] on most 64-bits unix
4+
targets, x87_f80 on x86, a randomly chosen type on powerpc, and usually the same
5+
as [`c_double`] on 32-bit targets.
6+
7+
[`float`]: c_float

library/core/src/ffi/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub use self::va_list::{VaList, VaListImpl};
3838
pub mod va_list;
3939

4040
mod primitives;
41+
#[unstable(feature = "c_longdouble", issue = "none")]
42+
pub use self::primitives::c_longdouble;
4143
#[stable(feature = "core_ffi_c", since = "1.64.0")]
4244
pub use self::primitives::{
4345
c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,

library/core/src/ffi/primitives.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type_alias! { "c_ulonglong.md", c_ulonglong = u64; }
3333

3434
type_alias! { "c_float.md", c_float = f32; }
3535
type_alias! { "c_double.md", c_double = f64; }
36+
type_alias! { "c_longdouble.md", c_longdouble = c_longdouble_definition::c_longdouble; #[doc(cfg(all()))] }
3637

3738
mod c_char_definition {
3839
crate::cfg_match! {
@@ -183,3 +184,57 @@ mod c_int_definition {
183184
}
184185
}
185186
}
187+
188+
mod c_longdouble_definition {
189+
crate::cfg_match! {
190+
any(
191+
// Windows and Apple use f64 across the board
192+
target_family = "windows",
193+
target_vendor = "apple",
194+
// FreeBSD-like operating systems use f64 on mips
195+
all(
196+
target_arch = "mips64",
197+
any(target_os = "freebsd", target_os = "dragonfly")
198+
),
199+
) => {
200+
pub(super) type c_longdouble = f64;
201+
}
202+
any(target_arch = "powerpc", target_arch = "powerpc64") => {
203+
// double-double or f128 based on config
204+
compile_error!("who knows");
205+
}
206+
any(
207+
target_arch = "aarch64",
208+
// Not yet implemented
209+
// target_arch = "loongarch32",
210+
target_arch = "loongarch64",
211+
target_arch = "mips64",
212+
target_arch = "riscv32",
213+
target_arch = "riscv64",
214+
target_arch = "s390x",
215+
target_arch = "wasm32",
216+
target_arch = "wasm64",
217+
// Note: mips32 with the N32 ABI is also f128 (?)
218+
219+
// Android and OpenHarmony use f128 on x86
220+
all(
221+
any(target_arch = "x86", target_arch = "x86_64"),
222+
any(target_os = "android", target_env = "ohos")
223+
),
224+
// Default to f128 for other 64-bit targets
225+
all(not(target_arch = "x86_64"), target_pointer_width = "64")
226+
) => {
227+
pub(super) type c_longdouble = f128;
228+
}
229+
// Most other operating systems use the x87 80-bit extended precision float
230+
any(target_arch = "x86", target_arch = "x86_64") => {
231+
// todo: obviously this isn't right
232+
pub(super) type c_longdouble = ();
233+
}
234+
// Most 16- and 32-bit targets use the same size as `c_double`, usually `f64` but
235+
// possibly `f32` on e.g. AVR.
236+
_ => {
237+
pub(super) type c_longdouble = c_double;
238+
}
239+
}
240+
}

library/std/src/ffi/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@
164164
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
165165
pub mod c_str;
166166

167+
#[unstable(feature = "c_size_", issue = "none")]
168+
pub use core::ffi::c_longdouble;
167169
#[stable(feature = "core_c_void", since = "1.30.0")]
168170
pub use core::ffi::c_void;
169171
#[unstable(

0 commit comments

Comments
 (0)