Skip to content

Commit

Permalink
Add implementations for f128 addition and subtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed May 11, 2024
1 parent 798d46f commit 34db533
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ These builtins are needed to support 128-bit integers.

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

- [ ] addtf3.c
- [x] addtf3.c
- [ ] comparetf2.c
- [ ] divtf3.c
- [x] extenddftf2.c
Expand All @@ -255,7 +255,7 @@ These builtins are needed to support `f16` and `f128`, which are in the process
- [ ] ppc/fixunstfdi.c
- [ ] ppc/floatditf.c
- [ ] ppc/floatunditf.c
- [ ] subtf3.c
- [x] subtf3.c
- [x] truncdfhf2.c
- [x] truncsfhf2.c
- [x] trunctfdf2.c
Expand Down
6 changes: 0 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,7 @@ mod c {
("__floatsitf", "floatsitf.c"),
("__floatunditf", "floatunditf.c"),
("__floatunsitf", "floatunsitf.c"),
("__addtf3", "addtf3.c"),
("__multf3", "multf3.c"),
("__subtf3", "subtf3.c"),
("__divtf3", "divtf3.c"),
("__powitf2", "powitf2.c"),
("__fe_getround", "fp_mode.c"),
Expand All @@ -500,9 +498,7 @@ mod c {
if target_arch == "mips64" {
sources.extend(&[
("__netf2", "comparetf2.c"),
("__addtf3", "addtf3.c"),
("__multf3", "multf3.c"),
("__subtf3", "subtf3.c"),
("__fixtfsi", "fixtfsi.c"),
("__floatsitf", "floatsitf.c"),
("__fixunstfsi", "fixunstfsi.c"),
Expand All @@ -515,9 +511,7 @@ mod c {
if target_arch == "loongarch64" {
sources.extend(&[
("__netf2", "comparetf2.c"),
("__addtf3", "addtf3.c"),
("__multf3", "multf3.c"),
("__subtf3", "subtf3.c"),
("__fixtfsi", "fixtfsi.c"),
("__floatsitf", "floatsitf.c"),
("__fixunstfsi", "fixunstfsi.c"),
Expand Down
5 changes: 5 additions & 0 deletions src/float/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ intrinsics! {
add(a, b)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __addtf3(a: f128, b: f128) -> f128 {
add(a, b)
}

#[cfg(target_arch = "arm")]
pub extern "C" fn __addsf3vfp(a: f32, b: f32) -> f32 {
a + b
Expand Down
6 changes: 6 additions & 0 deletions src/float/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ intrinsics! {
__adddf3(a, f64::from_repr(b.repr() ^ f64::SIGN_MASK))
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __subtf3(a: f128, b: f128) -> f128 {
use crate::float::add::__addtf3;
__addtf3(a, f128::from_repr(b.repr() ^ f128::SIGN_MASK))
}

#[cfg(target_arch = "arm")]
pub extern "C" fn __subsf3vfp(a: f32, b: f32) -> f32 {
a - b
Expand Down
2 changes: 2 additions & 0 deletions testcrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ no-asm = ["compiler_builtins/no-asm"]
no-f16-f128 = ["compiler_builtins/no-f16-f128"]
mem = ["compiler_builtins/mem"]
mangled-names = ["compiler_builtins/mangled-names"]
# Skip tests that rely on f128 symbols being available on the system
no-sys-f128 = []
18 changes: 18 additions & 0 deletions testcrate/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;

fn main() {
let target = env::var("TARGET").unwrap();

// These platforms do not have f128 symbols available in their system libraries, so
// skip related tests.
if target.starts_with("arm-")
|| target.contains("apple-darwin")
|| target.contains("windows-msvc")
// FIXME(llvm): There is an ABI incompatibility between GCC and Clang on 32-bit x86.
// See <https://github.com/llvm/llvm-project/issues/77401>.
|| target.starts_with("i686")
{
println!("cargo:warning=using apfloat fallback for f128");
println!("cargo:rustc-cfg=feature=\"no-sys-f128\"");
}
}
11 changes: 11 additions & 0 deletions testcrate/tests/addsub.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![allow(unused_macros)]
#![feature(f128)]
#![feature(f16)]

use core::ops::{Add, Sub};
use testcrate::*;
Expand Down Expand Up @@ -109,6 +111,15 @@ fn float_addsub() {
f32, __addsf3, __subsf3, Single, all();
f64, __adddf3, __subdf3, Double, all();
);

#[cfg(not(feature = "no-f16-f128"))]
{
use compiler_builtins::float::{add::__addtf3, sub::__subtf3, Float};

float_sum!(
f128, __addtf3, __subtf3, Quad, not(feature = "no-sys-f128");
);
}
}

#[cfg(target_arch = "arm")]
Expand Down

0 comments on commit 34db533

Please sign in to comment.