Skip to content

Add fmaf16 and fmaf128 #390

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions crates/compiler-builtins-smoke-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![no_std]

#[allow(dead_code)]
#[allow(unused_imports)]
#[allow(clippy::all)] // We don't get `libm`'s list of `allow`s, so just ignore Clippy.
#[path = "../../../src/math/mod.rs"]
pub mod libm;
14 changes: 14 additions & 0 deletions crates/libm-macros/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
None,
&["copysignf128"],
),
(
// `(f16, f16, f16) -> f16`
FloatTy::F16,
Signature { args: &[Ty::F16, Ty::F16, Ty::F16], returns: &[Ty::F16] },
None,
&["fmaf16"],
),
(
// `(f32, f32, f32) -> f32`
FloatTy::F32,
Expand All @@ -106,6 +113,13 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
None,
&["fma"],
),
(
// `(f128, f128, f128) -> f128`
FloatTy::F128,
Signature { args: &[Ty::F128, Ty::F128, Ty::F128], returns: &[Ty::F128] },
None,
&["fmaf128"],
),
(
// `(f32) -> i32`
FloatTy::F32,
Expand Down
3 changes: 2 additions & 1 deletion crates/libm-test/benches/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ libm_macros::for_each_function! {
exp10 | exp10f | exp2 | exp2f => (true, Some(musl_math_sys::MACRO_FN_NAME)),

// Musl does not provide `f16` and `f128` functions
copysignf16 | copysignf128 | fabsf16 | fabsf128 => (false, None),
copysignf16 | copysignf128 | fabsf16 | fabsf128 |
fmaf16 | fmaf128 => (false, None),

// By default we never skip (false) and always have a musl function available
_ => (false, Some(musl_math_sys::MACRO_FN_NAME))
Expand Down
5 changes: 2 additions & 3 deletions crates/libm-test/src/f8_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub struct f8(u8);
impl Float for f8 {
type Int = u8;
type SignedInt = i8;
type ExpInt = i8;

const ZERO: Self = Self(0b0_0000_000);
const NEG_ZERO: Self = Self(0b1_0000_000);
Expand Down Expand Up @@ -62,8 +61,8 @@ impl Float for f8 {
self.0 & Self::SIGN_MASK != 0
}

fn exp(self) -> Self::ExpInt {
unimplemented!()
fn exp(self) -> i32 {
((self.to_bits() & Self::EXP_MASK) >> Self::SIG_BITS) as i32
}

fn from_bits(a: Self::Int) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/libm-test/src/mpfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ libm_macros::for_each_function! {
expm1 | expm1f => exp_m1,
fabs | fabsf => abs,
fdim | fdimf => positive_diff,
fma | fmaf => mul_add,
fma | fmaf | fmaf16 | fmaf128 => mul_add,
fmax | fmaxf => max,
fmin | fminf => min,
lgamma | lgammaf => ln_gamma,
Expand Down
5 changes: 5 additions & 0 deletions crates/libm-test/src/precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,12 @@ fn bessel_prec_dropoff<F: Float>(
None
}

#[cfg(f16_enabled)]
impl MaybeOverride<(f16, f16, f16)> for SpecialCase {}
impl MaybeOverride<(f32, f32, f32)> for SpecialCase {}
impl MaybeOverride<(f64, f64, f64)> for SpecialCase {}
#[cfg(f128_enabled)]
impl MaybeOverride<(f128, f128, f128)> for SpecialCase {}

impl MaybeOverride<(f32, i32)> for SpecialCase {}
impl MaybeOverride<(f64, i32)> for SpecialCase {}
2 changes: 1 addition & 1 deletion crates/libm-test/tests/compare_built_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
libm_macros::for_each_function! {
callback: musl_rand_tests,
// Musl does not support `f16` and `f128` on all platforms.
skip: [copysignf16, copysignf128, fabsf16, fabsf128],
skip: [copysignf16, copysignf128, fabsf16, fabsf128, fmaf16, fmaf128],
attributes: [
#[cfg_attr(x86_no_sse, ignore)] // FIXME(correctness): wrong result on i586
[exp10, exp10f, exp2, exp2f, rint]
Expand Down
2 changes: 2 additions & 0 deletions crates/libm-test/tests/multiprecision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ libm_macros::for_each_function! {
fdimf,
fma,
fmaf,
fmaf16,
fmaf128,
fmax,
fmaxf,
fmin,
Expand Down
20 changes: 18 additions & 2 deletions etc/function-definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,32 @@
"fma": {
"sources": [
"src/libm_helper.rs",
"src/math/fma.rs"
"src/math/fma.rs",
"src/math/generic/fma.rs"
],
"type": "f64"
},
"fmaf": {
"sources": [
"src/math/fmaf.rs"
"src/math/fmaf.rs",
"src/math/generic/fma.rs"
],
"type": "f32"
},
"fmaf128": {
"sources": [
"src/math/fmaf128.rs",
"src/math/generic/fma.rs"
],
"type": "f128"
},
"fmaf16": {
"sources": [
"src/math/fmaf16.rs",
"src/math/generic/fma.rs"
],
"type": "f16"
},
"fmax": {
"sources": [
"src/libm_helper.rs",
Expand Down
2 changes: 2 additions & 0 deletions etc/function-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ floor
floorf
fma
fmaf
fmaf128
fmaf16
fmax
fmaxf
fmin
Expand Down
4 changes: 4 additions & 0 deletions src/math/fma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ fn mul(x: u64, y: u64) -> (u64, u64) {
/// according to the rounding mode characterized by the value of FLT_ROUNDS.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fma(x: f64, y: f64, z: f64) -> f64 {
if true {
return super::generic::fma(x, y, z, scalbn);
}

let x1p63: f64 = f64::from_bits(0x43e0000000000000); // 0x1p63 === 2 ^ 63
let x0_ffffff8p_63 = f64::from_bits(0x3bfffffff0000000); // 0x0.ffffff8p-63

Expand Down
4 changes: 4 additions & 0 deletions src/math/fmaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ use super::fenv::{
/// according to the rounding mode characterized by the value of FLT_ROUNDS.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmaf(x: f32, y: f32, mut z: f32) -> f32 {
if true {
return super::generic::fma_big::<f32, f64>(x, y, z);
}

let xy: f64;
let mut result: f64;
let mut ui: u64;
Expand Down
6 changes: 6 additions & 0 deletions src/math/fmaf128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[expect(unused)]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmaf128(x: f128, y: f128, z: f128) -> f128 {
// super::generic::fma(x, y, z)
todo!()
}
4 changes: 4 additions & 0 deletions src/math/fmaf16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmaf16(x: f16, y: f16, z: f16) -> f16 {
super::generic::fma_big::<f16, f32>(x, y, z)
}
Loading
Loading