Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ee0c03f

Browse files
committedOct 20, 2023
Support float16
1 parent e41f9dc commit ee0c03f

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed
 

‎bindgen-tests/tests/expectations/tests/float16.rs

+66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindgen-tests/tests/headers/float16.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq
2+
3+
static __fp16 global;
4+
5+
struct Test__Float16
6+
{
7+
__fp16 f;
8+
};
9+
10+
struct Test__Float16Ref
11+
{
12+
__fp16 *f;
13+
};
14+
15+
/*
16+
// This options are currently supported only on specific targets (eg. x86 with sse2)
17+
_Float16 returns_f16();
18+
19+
void gets_f16(_Float16 arg);
20+
21+
struct Test__Float16_Complex
22+
{
23+
_Float16 _Complex mMember;
24+
};
25+
26+
struct Test__Float16_ComplexPtr
27+
{
28+
_Float16 _Complex *mMember;
29+
};
30+
31+
_Float16 _Complex globalValueHalf;
32+
33+
_Float16 _Complex returns_f16_complex();
34+
35+
void gets_f16_complex(_Float16 _Complex arg);
36+
*/

‎bindgen/codegen/helpers.rs

+9
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ pub(crate) mod ast_ty {
232232
//
233233
// Also, maybe this one shouldn't be the default?
234234
match (fk, ctx.options().convert_floats) {
235+
(FloatKind::Float16, _) => {
236+
// TODO: do f16 when rust lands it
237+
ctx.generated_bindgen_float16();
238+
if ctx.options().enable_cxx_namespaces {
239+
syn::parse_quote! { root::__BindgenFloat16 }
240+
} else {
241+
syn::parse_quote! { __BindgenFloat16 }
242+
}
243+
}
235244
(FloatKind::Float, true) => syn::parse_quote! { f32 },
236245
(FloatKind::Double, true) => syn::parse_quote! { f64 },
237246
(FloatKind::Float, false) => raw_type(ctx, "c_float"),

‎bindgen/codegen/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ impl CodeGenerator for Module {
578578
if result.saw_incomplete_array {
579579
utils::prepend_incomplete_array_types(ctx, &mut *result);
580580
}
581+
if ctx.need_bindgen_float16_type() {
582+
utils::prepend_float16_type(&mut *result);
583+
}
581584
if ctx.need_bindgen_complex_type() {
582585
utils::prepend_complex_type(&mut *result);
583586
}
@@ -5136,6 +5139,20 @@ pub(crate) mod utils {
51365139
result.extend(old_items);
51375140
}
51385141

5142+
pub(crate) fn prepend_float16_type(
5143+
result: &mut Vec<proc_macro2::TokenStream>,
5144+
) {
5145+
let float16_type = quote! {
5146+
#[derive(PartialEq, Copy, Clone, Hash, Debug, Default)]
5147+
#[repr(transparent)]
5148+
pub struct __BindgenFloat16(pub u16);
5149+
};
5150+
5151+
let items = vec![float16_type];
5152+
let old_items = mem::replace(result, items);
5153+
result.extend(old_items);
5154+
}
5155+
51395156
pub(crate) fn prepend_complex_type(
51405157
result: &mut Vec<proc_macro2::TokenStream>,
51415158
) {

‎bindgen/codegen/serialize.rs

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl<'a> CSerialize<'a> for Type {
270270
write!(writer, "const ")?;
271271
}
272272
match float_kind {
273+
FloatKind::Float16 => write!(writer, "_Float16")?,
273274
FloatKind::Float => write!(writer, "float")?,
274275
FloatKind::Double => write!(writer, "double")?,
275276
FloatKind::LongDouble => write!(writer, "long double")?,
@@ -281,6 +282,7 @@ impl<'a> CSerialize<'a> for Type {
281282
write!(writer, "const ")?;
282283
}
283284
match float_kind {
285+
FloatKind::Float16 => write!(writer, "_Float16 complex")?,
284286
FloatKind::Float => write!(writer, "float complex")?,
285287
FloatKind::Double => write!(writer, "double complex")?,
286288
FloatKind::LongDouble => {

‎bindgen/ir/context.rs

+16
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ pub(crate) struct BindgenContext {
386386
/// Whether a bindgen complex was generated
387387
generated_bindgen_complex: Cell<bool>,
388388

389+
/// Whether a bindgen float16 was generated
390+
generated_bindgen_float16: Cell<bool>,
391+
389392
/// The set of `ItemId`s that are allowlisted. This the very first thing
390393
/// computed after parsing our IR, and before running any of our analyses.
391394
allowlisted: Option<ItemSet>,
@@ -585,6 +588,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
585588
target_info,
586589
options,
587590
generated_bindgen_complex: Cell::new(false),
591+
generated_bindgen_float16: Cell::new(false),
588592
allowlisted: None,
589593
blocklisted_types_implement_traits: Default::default(),
590594
codegen_items: None,
@@ -2005,6 +2009,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
20052009
CXType_ULongLong => TypeKind::Int(IntKind::ULongLong),
20062010
CXType_Int128 => TypeKind::Int(IntKind::I128),
20072011
CXType_UInt128 => TypeKind::Int(IntKind::U128),
2012+
CXType_Float16 | CXType_Half => TypeKind::Float(FloatKind::Float16),
20082013
CXType_Float => TypeKind::Float(FloatKind::Float),
20092014
CXType_Double => TypeKind::Float(FloatKind::Double),
20102015
CXType_LongDouble => TypeKind::Float(FloatKind::LongDouble),
@@ -2013,6 +2018,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
20132018
let float_type =
20142019
ty.elem_type().expect("Not able to resolve complex type?");
20152020
let float_kind = match float_type.kind() {
2021+
CXType_Float16 | CXType_Half => FloatKind::Float16,
20162022
CXType_Float => FloatKind::Float,
20172023
CXType_Double => FloatKind::Double,
20182024
CXType_LongDouble => FloatKind::LongDouble,
@@ -2528,6 +2534,16 @@ If you encounter an error missing from this list, please file an issue or a PR!"
25282534
self.generated_bindgen_complex.get()
25292535
}
25302536

2537+
/// Call if a bindgen float16 is generated
2538+
pub(crate) fn generated_bindgen_float16(&self) {
2539+
self.generated_bindgen_float16.set(true)
2540+
}
2541+
2542+
/// Whether we need to generate the bindgen float16 type
2543+
pub(crate) fn need_bindgen_float16_type(&self) -> bool {
2544+
self.generated_bindgen_float16.get()
2545+
}
2546+
25312547
/// Compute which `enum`s have an associated `typedef` definition.
25322548
fn compute_enum_typedef_combos(&mut self) {
25332549
let _t = self.timer("compute_enum_typedef_combos");

‎bindgen/ir/ty.rs

+2
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ impl TemplateParameters for TypeKind {
558558
/// The kind of float this type represents.
559559
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
560560
pub(crate) enum FloatKind {
561+
/// A half (`_Float16` or `__fp16`)
562+
Float16,
561563
/// A `float`.
562564
Float,
563565
/// A `double`.

0 commit comments

Comments
 (0)
Please sign in to comment.