Skip to content

Commit 2b3edd7

Browse files
committed
Implement f128 addition and subtraction
1 parent b041cc0 commit 2b3edd7

File tree

7 files changed

+69
-9
lines changed

7 files changed

+69
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ These builtins are needed to support 128-bit integers.
232232

233233
These builtins are needed to support `f16` and `f128`, which are in the process of being added to Rust.
234234

235-
- [ ] addtf3.c
235+
- [x] addtf3.c
236236
- [ ] comparetf2.c
237237
- [ ] divtf3.c
238238
- [x] extenddftf2.c
@@ -255,7 +255,7 @@ These builtins are needed to support `f16` and `f128`, which are in the process
255255
- [ ] ppc/fixunstfdi.c
256256
- [ ] ppc/floatditf.c
257257
- [ ] ppc/floatunditf.c
258-
- [ ] subtf3.c
258+
- [x] subtf3.c
259259
- [x] truncdfhf2.c
260260
- [x] truncsfhf2.c
261261
- [x] trunctfdf2.c

build.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,7 @@ mod c {
543543
("__floatsitf", "floatsitf.c"),
544544
("__floatunditf", "floatunditf.c"),
545545
("__floatunsitf", "floatunsitf.c"),
546-
("__addtf3", "addtf3.c"),
547546
("__multf3", "multf3.c"),
548-
("__subtf3", "subtf3.c"),
549547
("__divtf3", "divtf3.c"),
550548
("__powitf2", "powitf2.c"),
551549
("__fe_getround", "fp_mode.c"),
@@ -564,9 +562,7 @@ mod c {
564562
if target_arch == "mips64" {
565563
sources.extend(&[
566564
("__netf2", "comparetf2.c"),
567-
("__addtf3", "addtf3.c"),
568565
("__multf3", "multf3.c"),
569-
("__subtf3", "subtf3.c"),
570566
("__fixtfsi", "fixtfsi.c"),
571567
("__floatsitf", "floatsitf.c"),
572568
("__fixunstfsi", "fixunstfsi.c"),
@@ -579,9 +575,7 @@ mod c {
579575
if target_arch == "loongarch64" {
580576
sources.extend(&[
581577
("__netf2", "comparetf2.c"),
582-
("__addtf3", "addtf3.c"),
583578
("__multf3", "multf3.c"),
584-
("__subtf3", "subtf3.c"),
585579
("__fixtfsi", "fixtfsi.c"),
586580
("__floatsitf", "floatsitf.c"),
587581
("__fixunstfsi", "fixunstfsi.c"),

src/float/add.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ intrinsics! {
203203
add(a, b)
204204
}
205205

206+
#[cfg(not(any(feature = "no-f16-f128", target_arch = "powerpc", target_arch = "powerpc64")))]
207+
pub extern "C" fn __addtf3(a: f128, b: f128) -> f128 {
208+
add(a, b)
209+
}
210+
211+
#[cfg(all(not(feature = "no-f16-f128"), any(target_arch = "powerpc", target_arch = "powerpc64")))]
212+
pub extern "C" fn __addkf3(a: f128, b: f128) -> f128 {
213+
add(a, b)
214+
}
215+
206216
#[cfg(target_arch = "arm")]
207217
pub extern "C" fn __addsf3vfp(a: f32, b: f32) -> f32 {
208218
a + b

src/float/sub.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ intrinsics! {
1515
__adddf3(a, f64::from_repr(b.repr() ^ f64::SIGN_MASK))
1616
}
1717

18+
#[cfg(not(any(feature = "no-f16-f128", target_arch = "powerpc", target_arch = "powerpc64")))]
19+
pub extern "C" fn __subtf3(a: f128, b: f128) -> f128 {
20+
use crate::float::add::__addtf3;
21+
__addtf3(a, f128::from_repr(b.repr() ^ f128::SIGN_MASK))
22+
}
23+
24+
#[cfg(all(not(feature = "no-f16-f128"), any(target_arch = "powerpc", target_arch = "powerpc64")))]
25+
pub extern "C" fn __subkf3(a: f128, b: f128) -> f128 {
26+
use crate::float::add::__addkf3;
27+
__addkf3(a, f128::from_repr(b.repr() ^ f128::SIGN_MASK))
28+
}
29+
1830
#[cfg(target_arch = "arm")]
1931
pub extern "C" fn __subsf3vfp(a: f32, b: f32) -> f32 {
2032
a - b

testcrate/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ no-asm = ["compiler_builtins/no-asm"]
3333
no-f16-f128 = ["compiler_builtins/no-f16-f128"]
3434
mem = ["compiler_builtins/mem"]
3535
mangled-names = ["compiler_builtins/mangled-names"]
36+
# Skip tests that rely on f128 symbols being available on the system
37+
no-sys-f128 = []

testcrate/build.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::env;
2+
3+
fn main() {
4+
let target = env::var("TARGET").unwrap();
5+
6+
// These platforms do not have f128 symbols available in their system libraries, so
7+
// skip related tests.
8+
if target.starts_with("arm-")
9+
|| target.contains("apple-darwin")
10+
|| target.contains("windows-msvc")
11+
// GCC and LLVM disagree on the ABI of `f16` and `f128` with MinGW. See
12+
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>.
13+
|| target.contains("windows-gnu")
14+
// FIXME(llvm): There is an ABI incompatibility between GCC and Clang on 32-bit x86.
15+
// See <https://github.com/llvm/llvm-project/issues/77401>.
16+
|| target.starts_with("i686")
17+
// 32-bit PowerPC gets code generated that Qemu cannot handle. See
18+
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105635926>.
19+
|| target.starts_with("powerpc-")
20+
// FIXME: We get different results from the builtin functions. See
21+
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105657287>.
22+
|| target.starts_with("powerpc64-")
23+
{
24+
println!("cargo:warning=using apfloat fallback for f128");
25+
println!("cargo:rustc-cfg=feature=\"no-sys-f128\"");
26+
}
27+
}

testcrate/tests/addsub.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(unused_macros)]
2+
#![feature(f128)]
3+
#![feature(f16)]
24

3-
use core::ops::{Add, Sub};
45
use testcrate::*;
56

67
macro_rules! sum {
@@ -104,11 +105,24 @@ fn float_addsub() {
104105
sub::{__subdf3, __subsf3},
105106
Float,
106107
};
108+
use core::ops::{Add, Sub};
107109

108110
float_sum!(
109111
f32, __addsf3, __subsf3, Single, all();
110112
f64, __adddf3, __subdf3, Double, all();
111113
);
114+
115+
#[cfg(not(feature = "no-f16-f128"))]
116+
{
117+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
118+
use compiler_builtins::float::{add::__addkf3 as __addtf3, sub::__subkf3 as __subtf3};
119+
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
120+
use compiler_builtins::float::{add::__addtf3, sub::__subtf3};
121+
122+
float_sum!(
123+
f128, __addtf3, __subtf3, Quad, not(feature = "no-sys-f128");
124+
);
125+
}
112126
}
113127

114128
#[cfg(target_arch = "arm")]
@@ -119,6 +133,7 @@ fn float_addsub_arm() {
119133
sub::{__subdf3vfp, __subsf3vfp},
120134
Float,
121135
};
136+
use core::ops::{Add, Sub};
122137

123138
float_sum!(
124139
f32, __addsf3vfp, __subsf3vfp, Single, all();

0 commit comments

Comments
 (0)