Skip to content

Commit a113c7c

Browse files
beetreestgross35
authored andcommitted
Fix ABI for f16 builtins on Intel Apple targets
1 parent d78cc8e commit a113c7c

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/float/extend.rs

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ intrinsics! {
8686
intrinsics! {
8787
#[avr_skip]
8888
#[aapcs_on_arm]
89+
#[apple_f16_arg_abi]
8990
#[arm_aeabi_alias = __aeabi_h2f]
9091
#[cfg(f16_enabled)]
9192
pub extern "C" fn __extendhfsf2(a: f16) -> f32 {
@@ -94,6 +95,7 @@ intrinsics! {
9495

9596
#[avr_skip]
9697
#[aapcs_on_arm]
98+
#[apple_f16_arg_abi]
9799
#[cfg(f16_enabled)]
98100
pub extern "C" fn __gnu_h2f_ieee(a: f16) -> f32 {
99101
extend(a)

src/float/trunc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ intrinsics! {
134134
intrinsics! {
135135
#[avr_skip]
136136
#[aapcs_on_arm]
137+
#[apple_f16_ret_abi]
137138
#[arm_aeabi_alias = __aeabi_f2h]
138139
#[cfg(f16_enabled)]
139140
pub extern "C" fn __truncsfhf2(a: f32) -> f16 {
@@ -142,13 +143,15 @@ intrinsics! {
142143

143144
#[avr_skip]
144145
#[aapcs_on_arm]
146+
#[apple_f16_ret_abi]
145147
#[cfg(f16_enabled)]
146148
pub extern "C" fn __gnu_f2h_ieee(a: f32) -> f16 {
147149
trunc(a)
148150
}
149151

150152
#[avr_skip]
151153
#[aapcs_on_arm]
154+
#[apple_f16_ret_abi]
152155
#[arm_aeabi_alias = __aeabi_d2h]
153156
#[cfg(f16_enabled)]
154157
pub extern "C" fn __truncdfhf2(a: f64) -> f16 {

src/macros.rs

+100
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,106 @@ macro_rules! intrinsics {
276276
intrinsics!($($rest)*);
277277
);
278278

279+
// `arm_aeabi_alias` would conflict with `f16_apple_{arg,ret}_abi` not handled here. Avoid macro ambiguity by combining in a
280+
// single `#[]`.
281+
(
282+
#[apple_f16_arg_abi]
283+
#[arm_aeabi_alias = $alias:ident]
284+
$($t:tt)*
285+
) => {
286+
intrinsics! {
287+
#[apple_f16_arg_abi, arm_aeabi_alias = $alias]
288+
$($t)*
289+
}
290+
};
291+
(
292+
#[apple_f16_ret_abi]
293+
#[arm_aeabi_alias = $alias:ident]
294+
$($t:tt)*
295+
) => {
296+
intrinsics! {
297+
#[apple_f16_ret_abi, arm_aeabi_alias = $alias]
298+
$($t)*
299+
}
300+
};
301+
302+
// On x86 (32-bit and 64-bit) Apple platforms, `f16` is passed and returned like a `u16` unless
303+
// the builtin involves `f128`.
304+
(
305+
// `arm_aeabi_alias` would conflict if not handled here. Avoid macro ambiguity by combining
306+
// in a single `#[]`.
307+
#[apple_f16_arg_abi $(, arm_aeabi_alias = $alias:ident)?]
308+
$(#[$($attr:tt)*])*
309+
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
310+
$($body:tt)*
311+
}
312+
313+
$($rest:tt)*
314+
) => (
315+
#[cfg(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64")))]
316+
$(#[$($attr)*])*
317+
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
318+
$($body)*
319+
}
320+
321+
#[cfg(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64"), not(feature = "mangled-names")))]
322+
mod $name {
323+
#[no_mangle]
324+
#[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")]
325+
$(#[$($attr)*])*
326+
extern $abi fn $name( $($argname: u16),* ) $(-> $ret)? {
327+
super::$name($(f16::from_bits($argname)),*)
328+
}
329+
}
330+
331+
#[cfg(not(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64"))))]
332+
intrinsics! {
333+
$(#[arm_aeabi_alias = $alias])?
334+
$(#[$($attr)*])*
335+
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
336+
$($body)*
337+
}
338+
}
339+
340+
intrinsics!($($rest)*);
341+
);
342+
(
343+
#[apple_f16_ret_abi $(, arm_aeabi_alias = $alias:ident)?]
344+
$(#[$($attr:tt)*])*
345+
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
346+
$($body:tt)*
347+
}
348+
349+
$($rest:tt)*
350+
) => (
351+
#[cfg(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64")))]
352+
$(#[$($attr)*])*
353+
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
354+
$($body)*
355+
}
356+
357+
#[cfg(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64"), not(feature = "mangled-names")))]
358+
mod $name {
359+
#[no_mangle]
360+
#[cfg_attr(not(all(windows, target_env = "gnu")), linkage = "weak")]
361+
$(#[$($attr)*])*
362+
extern $abi fn $name( $($argname: $ty),* ) -> u16 {
363+
super::$name($($argname),*).to_bits()
364+
}
365+
}
366+
367+
#[cfg(not(all(target_vendor = "apple", any(target_arch = "x86", target_arch = "x86_64"))))]
368+
intrinsics! {
369+
$(#[arm_aeabi_alias = $alias])?
370+
$(#[$($attr)*])*
371+
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
372+
$($body)*
373+
}
374+
}
375+
376+
intrinsics!($($rest)*);
377+
);
378+
279379
// A bunch of intrinsics on ARM are aliased in the standard compiler-rt
280380
// build under `__aeabi_*` aliases, and LLVM will call these instead of the
281381
// original function. The aliasing here is used to generate these symbols in

0 commit comments

Comments
 (0)