Skip to content

Add __addhf3, __subhf3, __mulhf3, and hf comparisons #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions builtins-test/tests/addsub.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unused_macros)]
#![cfg_attr(f16_enabled, feature(f16))]
#![cfg_attr(f128_enabled, feature(f128))]

use builtins_test::*;
Expand Down Expand Up @@ -115,28 +116,25 @@ macro_rules! float_sum {
mod float_addsub {
use super::*;

#[cfg(f16_enabled)]
float_sum! {
f16, __addhf3, __subhf3, Half, all();
}

float_sum! {
f32, __addsf3, __subsf3, Single, all();
f64, __adddf3, __subdf3, Double, all();
}
}

#[cfg(f128_enabled)]
#[cfg(not(x86_no_sse))]
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
mod float_addsub_f128 {
use super::*;

#[cfg(f128_enabled)]
#[cfg(not(x86_no_sse))]
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
float_sum! {
f128, __addtf3, __subtf3, Quad, not(feature = "no-sys-f128");
}
}

#[cfg(f128_enabled)]
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
mod float_addsub_f128_ppc {
use super::*;

#[cfg(f128_enabled)]
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
float_sum! {
f128, __addkf3, __subkf3, Quad, not(feature = "no-sys-f128");
}
Expand Down
21 changes: 21 additions & 0 deletions builtins-test/tests/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(unused_macros)]
#![allow(unreachable_code)]
#![cfg_attr(f16_enabled, feature(f16))]
#![cfg_attr(f128_enabled, feature(f128))]

use builtins_test::*;
Expand Down Expand Up @@ -51,6 +52,26 @@ mod float_comparisons {
};
}

#[test]
#[cfg(f16_enabled)]
fn cmp_f16() {
use compiler_builtins::float::cmp::{
__eqhf2, __gehf2, __gthf2, __lehf2, __lthf2, __nehf2, __unordhf2,
};

fuzz_float_2(N, |x: f16, y: f16| {
assert_eq!(__unordhf2(x, y) != 0, x.is_nan() || y.is_nan());
cmp!(f16, x, y, Half, all(),
1, __lthf2;
1, __lehf2;
1, __eqhf2;
-1, __gehf2;
-1, __gthf2;
1, __nehf2;
);
});
}

#[test]
fn cmp_f32() {
use compiler_builtins::float::cmp::{
Expand Down
8 changes: 7 additions & 1 deletion builtins-test/tests/mul.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(unused_macros)]
#![cfg_attr(f16_enabled, feature(f16))]
#![cfg_attr(f128_enabled, feature(f128))]
#![allow(unused_macros)]

use builtins_test::*;

Expand Down Expand Up @@ -117,6 +118,11 @@ macro_rules! float_mul {
mod float_mul {
use super::*;

#[cfg(f16_enabled)]
float_mul! {
f16, __mulhf3, Half, all();
}

// FIXME(#616): Stop ignoring arches that don't have native support once fix for builtins is in
// nightly.
float_mul! {
Expand Down
5 changes: 5 additions & 0 deletions compiler-builtins/src/float/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ where
}

intrinsics! {
#[cfg(f16_enabled)]
pub extern "C" fn __addhf3(a: f16, b: f16) -> f16 {
add(a, b)
}

#[aapcs_on_arm]
#[arm_aeabi_alias = __aeabi_fadd]
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {
Expand Down
31 changes: 31 additions & 0 deletions compiler-builtins/src/float/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,37 @@ fn unord<F: Float>(a: F, b: F) -> bool {
a_abs > inf_rep || b_abs > inf_rep
}

#[cfg(f16_enabled)]
intrinsics! {
pub extern "C" fn __lehf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
cmp(a, b).to_le_abi()
}

pub extern "C" fn __gehf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
cmp(a, b).to_ge_abi()
}

pub extern "C" fn __unordhf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
unord(a, b) as crate::float::cmp::CmpResult
}

pub extern "C" fn __eqhf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
cmp(a, b).to_le_abi()
}

pub extern "C" fn __lthf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
cmp(a, b).to_le_abi()
}

pub extern "C" fn __nehf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
cmp(a, b).to_le_abi()
}

pub extern "C" fn __gthf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
cmp(a, b).to_ge_abi()
}
}

intrinsics! {
pub extern "C" fn __lesf2(a: f32, b: f32) -> crate::float::cmp::CmpResult {
cmp(a, b).to_le_abi()
Expand Down
5 changes: 5 additions & 0 deletions compiler-builtins/src/float/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ where
}

intrinsics! {
#[cfg(f16_enabled)]
pub extern "C" fn __mulhf3(a: f16, b: f16) -> f16 {
mul(a, b)
}

#[aapcs_on_arm]
#[arm_aeabi_alias = __aeabi_fmul]
pub extern "C" fn __mulsf3(a: f32, b: f32) -> f32 {
Expand Down
5 changes: 5 additions & 0 deletions compiler-builtins/src/float/sub.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use crate::float::Float;

intrinsics! {
#[cfg(f16_enabled)]
pub extern "C" fn __subhf3(a: f16, b: f16) -> f16 {
crate::float::add::__addhf3(a, f16::from_bits(b.to_bits() ^ f16::SIGN_MASK))
}

#[arm_aeabi_alias = __aeabi_fsub]
pub extern "C" fn __subsf3(a: f32, b: f32) -> f32 {
crate::float::add::__addsf3(a, f32::from_bits(b.to_bits() ^ f32::SIGN_MASK))
Expand Down
2 changes: 1 addition & 1 deletion crates/symbol-check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn verify_no_duplicates(archive: &Archive) {

// Windows has symbols for literal numeric constants, string literals, and MinGW pseudo-
// relocations. These are allowed to have repeated definitions.
let win_allowed_dup_pfx = ["__real@", "__xmm@", "??_C@_", ".refptr"];
let win_allowed_dup_pfx = ["__real@", "__xmm@", "__ymm@", "??_C@_", ".refptr"];
if win_allowed_dup_pfx
.iter()
.any(|pfx| sym.name.starts_with(pfx))
Expand Down