Skip to content

Commit 25eafd7

Browse files
committed
Teach the float code about u128.
1 parent 7455e3b commit 25eafd7

File tree

5 files changed

+85
-17
lines changed

5 files changed

+85
-17
lines changed

src/codegen/helpers.rs

+33-10
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
122122
pub mod ast_ty {
123123
use ir::context::BindgenContext;
124124
use ir::function::FunctionSig;
125+
use ir::layout::Layout;
125126
use ir::ty::FloatKind;
126127
use quote;
127128
use proc_macro2;
@@ -144,22 +145,44 @@ pub mod ast_ty {
144145
pub fn float_kind_rust_type(
145146
ctx: &BindgenContext,
146147
fk: FloatKind,
148+
layout: Option<Layout>,
147149
) -> quote::Tokens {
148-
// TODO: we probably should just take the type layout into
149-
// account?
150+
// TODO: we probably should take the type layout into account more
151+
// often?
150152
//
151153
// Also, maybe this one shouldn't be the default?
152-
//
153-
// FIXME: `c_longdouble` doesn't seem to be defined in some
154-
// systems, so we use `c_double` directly.
155154
match (fk, ctx.options().convert_floats) {
156155
(FloatKind::Float, true) => quote! { f32 },
157-
(FloatKind::Double, true) |
158-
(FloatKind::LongDouble, true) => quote! { f64 },
156+
(FloatKind::Double, true) => quote! { f64 },
159157
(FloatKind::Float, false) => raw_type(ctx, "c_float"),
160-
(FloatKind::Double, false) |
161-
(FloatKind::LongDouble, false) => raw_type(ctx, "c_double"),
162-
(FloatKind::Float128, _) => quote! { [u8; 16] },
158+
(FloatKind::Double, false) => raw_type(ctx, "c_double"),
159+
(FloatKind::LongDouble, _) => {
160+
match layout {
161+
Some(layout) => {
162+
match layout.size {
163+
4 => quote! { f32 },
164+
8 => quote! { f64 },
165+
// TODO(emilio): If rust ever gains f128 we should
166+
// use it here and below.
167+
_ => super::integer_type(ctx, layout).unwrap_or(quote! { f64 }),
168+
}
169+
}
170+
None => {
171+
debug_assert!(
172+
false,
173+
"How didn't we know the layout for a primitive type?"
174+
);
175+
quote! { f64 }
176+
}
177+
}
178+
}
179+
(FloatKind::Float128, _) => {
180+
if ctx.options().rust_features.i128_and_u128 {
181+
quote! { u128 }
182+
} else {
183+
quote! { [u64; 2] }
184+
}
185+
}
163186
}
164187
}
165188

src/codegen/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3077,9 +3077,9 @@ impl TryToRustTy for Type {
30773077
}
30783078
}
30793079
}
3080-
TypeKind::Float(fk) => Ok(float_kind_rust_type(ctx, fk)),
3080+
TypeKind::Float(fk) => Ok(float_kind_rust_type(ctx, fk, self.layout(ctx))),
30813081
TypeKind::Complex(fk) => {
3082-
let float_path = float_kind_rust_type(ctx, fk);
3082+
let float_path = float_kind_rust_type(ctx, fk, self.layout(ctx));
30833083

30843084
ctx.generated_bindgen_complex();
30853085
Ok(if ctx.options().enable_cxx_namespaces {

tests/expectations/tests/convert-floats.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
4-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5-
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
69

710
#[derive(PartialEq, Copy, Clone, Hash, Debug, Default)]
811
#[repr(C)]
@@ -16,7 +19,7 @@ pub struct foo {
1619
pub bar: ::std::os::raw::c_float,
1720
pub baz: ::std::os::raw::c_float,
1821
pub bazz: ::std::os::raw::c_double,
19-
pub bazzz: *mut ::std::os::raw::c_double,
22+
pub bazzz: *mut f64,
2023
pub complexFloat: __BindgenComplex<::std::os::raw::c_float>,
2124
pub complexDouble: __BindgenComplex<::std::os::raw::c_double>,
2225
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy, Clone)]
12+
pub struct foo {
13+
pub bar: u128,
14+
}
15+
#[test]
16+
fn bindgen_test_layout_foo() {
17+
assert_eq!(
18+
::std::mem::size_of::<foo>(),
19+
16usize,
20+
concat!("Size of: ", stringify!(foo))
21+
);
22+
assert_eq!(
23+
::std::mem::align_of::<foo>(),
24+
16usize,
25+
concat!("Alignment of ", stringify!(foo))
26+
);
27+
assert_eq!(
28+
unsafe { &(*(::std::ptr::null::<foo>())).bar as *const _ as usize },
29+
0usize,
30+
concat!("Offset of field: ", stringify!(foo), "::", stringify!(bar))
31+
);
32+
}

tests/expectations/tests/union_bitfield_1_0.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(C)]
611
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
@@ -279,3 +284,8 @@ impl Default for HasBigBitfield {
279284
unsafe { ::std::mem::zeroed() }
280285
}
281286
}
287+
impl ::std::cmp::PartialEq for HasBigBitfield {
288+
fn eq(&self, other: &HasBigBitfield) -> bool {
289+
&self.bindgen_union_field[..] == &other.bindgen_union_field[..]
290+
}
291+
}

0 commit comments

Comments
 (0)