Skip to content

Commit b967dca

Browse files
committed
Add __addhf3, __subhf3, __mulhf3, and hf comparisons
LLVM does not currently emit these, but it is being discussed as an option on platforms where `f32` is not hardware supported. These three intrinsics are trivial, as are comparisons, so add them here.
1 parent dbe26bd commit b967dca

File tree

7 files changed

+84
-14
lines changed

7 files changed

+84
-14
lines changed

builtins-test/tests/addsub.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(unused_macros)]
2+
#![cfg_attr(f16_enabled, feature(f16))]
23
#![cfg_attr(f128_enabled, feature(f128))]
34

45
use builtins_test::*;
@@ -115,28 +116,25 @@ macro_rules! float_sum {
115116
mod float_addsub {
116117
use super::*;
117118

119+
#[cfg(f16_enabled)]
120+
float_sum! {
121+
f16, __addhf3, __subhf3, Half, all();
122+
}
123+
118124
float_sum! {
119125
f32, __addsf3, __subsf3, Single, all();
120126
f64, __adddf3, __subdf3, Double, all();
121127
}
122-
}
123-
124-
#[cfg(f128_enabled)]
125-
#[cfg(not(x86_no_sse))]
126-
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
127-
mod float_addsub_f128 {
128-
use super::*;
129128

129+
#[cfg(f128_enabled)]
130+
#[cfg(not(x86_no_sse))]
131+
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
130132
float_sum! {
131133
f128, __addtf3, __subtf3, Quad, not(feature = "no-sys-f128");
132134
}
133-
}
134-
135-
#[cfg(f128_enabled)]
136-
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
137-
mod float_addsub_f128_ppc {
138-
use super::*;
139135

136+
#[cfg(f128_enabled)]
137+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
140138
float_sum! {
141139
f128, __addkf3, __subkf3, Quad, not(feature = "no-sys-f128");
142140
}

builtins-test/tests/cmp.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(unused_macros)]
22
#![allow(unreachable_code)]
3+
#![cfg_attr(f16_enabled, feature(f16))]
34
#![cfg_attr(f128_enabled, feature(f128))]
45

56
use builtins_test::*;
@@ -51,6 +52,25 @@ mod float_comparisons {
5152
};
5253
}
5354

55+
#[test]
56+
fn cmp_f16() {
57+
use compiler_builtins::float::cmp::{
58+
__eqhf2, __gehf2, __gthf2, __lehf2, __lthf2, __nehf2, __unordhf2,
59+
};
60+
61+
fuzz_float_2(N, |x: f16, y: f16| {
62+
assert_eq!(__unordhf2(x, y) != 0, x.is_nan() || y.is_nan());
63+
cmp!(f16, x, y, Half, all(),
64+
1, __lthf2;
65+
1, __lehf2;
66+
1, __eqhf2;
67+
-1, __gehf2;
68+
-1, __gthf2;
69+
1, __nehf2;
70+
);
71+
});
72+
}
73+
5474
#[test]
5575
fn cmp_f32() {
5676
use compiler_builtins::float::cmp::{

builtins-test/tests/mul.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![allow(unused_macros)]
1+
#![cfg_attr(f16_enabled, feature(f16))]
22
#![cfg_attr(f128_enabled, feature(f128))]
3+
#![allow(unused_macros)]
34

45
use builtins_test::*;
56

@@ -117,6 +118,11 @@ macro_rules! float_mul {
117118
mod float_mul {
118119
use super::*;
119120

121+
#[cfg(f16_enabled)]
122+
float_mul! {
123+
f16, __mulhf3, Half, all();
124+
}
125+
120126
// FIXME(#616): Stop ignoring arches that don't have native support once fix for builtins is in
121127
// nightly.
122128
float_mul! {

compiler-builtins/src/float/add.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ where
191191
}
192192

193193
intrinsics! {
194+
#[cfg(f16_enabled)]
195+
pub extern "C" fn __addhf3(a: f16, b: f16) -> f16 {
196+
add(a, b)
197+
}
198+
194199
#[aapcs_on_arm]
195200
#[arm_aeabi_alias = __aeabi_fadd]
196201
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {

compiler-builtins/src/float/cmp.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,37 @@ fn unord<F: Float>(a: F, b: F) -> bool {
115115
a_abs > inf_rep || b_abs > inf_rep
116116
}
117117

118+
#[cfg(f16_enabled)]
119+
intrinsics! {
120+
pub extern "C" fn __lehf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
121+
cmp(a, b).to_le_abi()
122+
}
123+
124+
pub extern "C" fn __gehf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
125+
cmp(a, b).to_ge_abi()
126+
}
127+
128+
pub extern "C" fn __unordhf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
129+
unord(a, b) as crate::float::cmp::CmpResult
130+
}
131+
132+
pub extern "C" fn __eqhf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
133+
cmp(a, b).to_le_abi()
134+
}
135+
136+
pub extern "C" fn __lthf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
137+
cmp(a, b).to_le_abi()
138+
}
139+
140+
pub extern "C" fn __nehf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
141+
cmp(a, b).to_le_abi()
142+
}
143+
144+
pub extern "C" fn __gthf2(a: f16, b: f16) -> crate::float::cmp::CmpResult {
145+
cmp(a, b).to_ge_abi()
146+
}
147+
}
148+
118149
intrinsics! {
119150
pub extern "C" fn __lesf2(a: f32, b: f32) -> crate::float::cmp::CmpResult {
120151
cmp(a, b).to_le_abi()

compiler-builtins/src/float/mul.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ where
180180
}
181181

182182
intrinsics! {
183+
#[cfg(f16_enabled)]
184+
pub extern "C" fn __mulhf3(a: f16, b: f16) -> f16 {
185+
mul(a, b)
186+
}
187+
183188
#[aapcs_on_arm]
184189
#[arm_aeabi_alias = __aeabi_fmul]
185190
pub extern "C" fn __mulsf3(a: f32, b: f32) -> f32 {

compiler-builtins/src/float/sub.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use crate::float::Float;
22

33
intrinsics! {
4+
#[cfg(f16_enabled)]
5+
pub extern "C" fn __subhf3(a: f16, b: f16) -> f16 {
6+
crate::float::add::__addhf3(a, f16::from_bits(b.to_bits() ^ f16::SIGN_MASK))
7+
}
8+
49
#[arm_aeabi_alias = __aeabi_fsub]
510
pub extern "C" fn __subsf3(a: f32, b: f32) -> f32 {
611
crate::float::add::__addsf3(a, f32::from_bits(b.to_bits() ^ f32::SIGN_MASK))

0 commit comments

Comments
 (0)