Implement f128 addition and subtraction

This commit is contained in:
Trevor Gross 2024-05-10 19:22:03 -05:00
parent 4509315d2f
commit 2755f457f8
7 changed files with 69 additions and 9 deletions

View file

@ -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
@ -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

View file

@ -543,9 +543,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"),
@ -564,9 +562,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"),
@ -579,9 +575,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"),

View file

@ -203,6 +203,16 @@ intrinsics! {
add(a, b)
}
#[cfg(not(any(feature = "no-f16-f128", target_arch = "powerpc", target_arch = "powerpc64")))]
pub extern "C" fn __addtf3(a: f128, b: f128) -> f128 {
add(a, b)
}
#[cfg(all(not(feature = "no-f16-f128"), any(target_arch = "powerpc", target_arch = "powerpc64")))]
pub extern "C" fn __addkf3(a: f128, b: f128) -> f128 {
add(a, b)
}
#[cfg(target_arch = "arm")]
pub extern "C" fn __addsf3vfp(a: f32, b: f32) -> f32 {
a + b

View file

@ -15,6 +15,18 @@ intrinsics! {
__adddf3(a, f64::from_repr(b.repr() ^ f64::SIGN_MASK))
}
#[cfg(not(any(feature = "no-f16-f128", target_arch = "powerpc", target_arch = "powerpc64")))]
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(all(not(feature = "no-f16-f128"), any(target_arch = "powerpc", target_arch = "powerpc64")))]
pub extern "C" fn __subkf3(a: f128, b: f128) -> f128 {
use crate::float::add::__addkf3;
__addkf3(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

View file

@ -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 = []

View file

@ -0,0 +1,27 @@
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")
// GCC and LLVM disagree on the ABI of `f16` and `f128` with MinGW. See
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>.
|| target.contains("windows-gnu")
// 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")
// 32-bit PowerPC gets code generated that Qemu cannot handle. See
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105635926>.
|| target.starts_with("powerpc-")
// FIXME: We get different results from the builtin functions. See
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105657287>.
|| target.starts_with("powerpc64-")
{
println!("cargo:warning=using apfloat fallback for f128");
println!("cargo:rustc-cfg=feature=\"no-sys-f128\"");
}
}

View file

@ -1,6 +1,7 @@
#![allow(unused_macros)]
#![feature(f128)]
#![feature(f16)]
use core::ops::{Add, Sub};
use testcrate::*;
macro_rules! sum {
@ -104,11 +105,24 @@ fn float_addsub() {
sub::{__subdf3, __subsf3},
Float,
};
use core::ops::{Add, Sub};
float_sum!(
f32, __addsf3, __subsf3, Single, all();
f64, __adddf3, __subdf3, Double, all();
);
#[cfg(not(feature = "no-f16-f128"))]
{
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
use compiler_builtins::float::{add::__addkf3 as __addtf3, sub::__subkf3 as __subtf3};
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
use compiler_builtins::float::{add::__addtf3, sub::__subtf3};
float_sum!(
f128, __addtf3, __subtf3, Quad, not(feature = "no-sys-f128");
);
}
}
#[cfg(target_arch = "arm")]
@ -119,6 +133,7 @@ fn float_addsub_arm() {
sub::{__subdf3vfp, __subsf3vfp},
Float,
};
use core::ops::{Add, Sub};
float_sum!(
f32, __addsf3vfp, __subsf3vfp, Single, all();