Skip to content

Add frexpf16, frexpf128, ilogbf16, and ilogbf128 #862

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
46 changes: 46 additions & 0 deletions crates/libm-macros/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
None,
&["fmaf128"],
),
(
// `(f16) -> i32`
FloatTy::F16,
Signature {
args: &[Ty::F16],
returns: &[Ty::I32],
},
None,
&["ilogbf16"],
),
(
// `(f32) -> i32`
FloatTy::F32,
Expand All @@ -278,6 +288,16 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
None,
&["ilogb"],
),
(
// `(f128) -> i32`
FloatTy::F128,
Signature {
args: &[Ty::F128],
returns: &[Ty::I32],
},
None,
&["ilogbf128"],
),
(
// `(i32, f32) -> f32`
FloatTy::F32,
Expand Down Expand Up @@ -364,6 +384,19 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
}),
&["modf"],
),
(
// `(f16, &mut c_int) -> f16` as `(f16) -> (f16, i32)`
FloatTy::F16,
Signature {
args: &[Ty::F16],
returns: &[Ty::F16, Ty::I32],
},
Some(Signature {
args: &[Ty::F16, Ty::MutCInt],
returns: &[Ty::F16],
}),
&["frexpf16"],
),
(
// `(f32, &mut c_int) -> f32` as `(f32) -> (f32, i32)`
FloatTy::F32,
Expand All @@ -390,6 +423,19 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
}),
&["frexp", "lgamma_r"],
),
(
// `(f128, &mut c_int) -> f128` as `(f128) -> (f128, i32)`
FloatTy::F128,
Signature {
args: &[Ty::F128],
returns: &[Ty::F128, Ty::I32],
},
Some(Signature {
args: &[Ty::F128, Ty::MutCInt],
returns: &[Ty::F128],
}),
&["frexpf128"],
),
(
// `(f32, f32, &mut c_int) -> f32` as `(f32, f32) -> (f32, i32)`
FloatTy::F32,
Expand Down
4 changes: 4 additions & 0 deletions crates/util/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
| fminimumf16
| fmodf128
| fmodf16
| frexpf128
| frexpf16
| ilogbf128
| ilogbf16
| ldexpf128
| ldexpf16
| rintf128
Expand Down
4 changes: 4 additions & 0 deletions libm-test/benches/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ libm_macros::for_each_function! {
| fminimumf16
| fmodf128
| fmodf16
| frexpf128
| frexpf16
| ilogbf128
| ilogbf16
| ldexpf128
| ldexpf16
| rintf128
Expand Down
24 changes: 22 additions & 2 deletions libm-test/src/generate/case_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,24 @@ fn fmodf16_cases() -> Vec<TestCase<op::fmodf16::Routine>> {
vec![]
}

fn frexp_cases() -> Vec<TestCase<op::frexp::Routine>> {
#[cfg(f16_enabled)]
fn frexpf16_cases() -> Vec<TestCase<op::frexpf16::Routine>> {
vec![]
}

fn frexpf_cases() -> Vec<TestCase<op::frexpf::Routine>> {
vec![]
}

fn frexp_cases() -> Vec<TestCase<op::frexp::Routine>> {
vec![]
}

#[cfg(f128_enabled)]
fn frexpf128_cases() -> Vec<TestCase<op::frexpf128::Routine>> {
vec![]
}

fn hypot_cases() -> Vec<TestCase<op::hypot::Routine>> {
vec![]
}
Expand All @@ -465,14 +475,24 @@ fn hypotf_cases() -> Vec<TestCase<op::hypotf::Routine>> {
vec![]
}

fn ilogb_cases() -> Vec<TestCase<op::ilogb::Routine>> {
#[cfg(f16_enabled)]
fn ilogbf16_cases() -> Vec<TestCase<op::ilogbf16::Routine>> {
vec![]
}

fn ilogbf_cases() -> Vec<TestCase<op::ilogbf::Routine>> {
vec![]
}

fn ilogb_cases() -> Vec<TestCase<op::ilogb::Routine>> {
vec![]
}

#[cfg(f128_enabled)]
fn ilogbf128_cases() -> Vec<TestCase<op::ilogbf128::Routine>> {
vec![]
}

fn j0_cases() -> Vec<TestCase<op::j0::Routine>> {
vec![]
}
Expand Down
78 changes: 41 additions & 37 deletions libm-test/src/mpfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ libm_macros::for_each_function! {
fmodf16,
frexp,
frexpf,
frexpf128,
frexpf16,
ilogb,
ilogbf,
ilogbf128,
ilogbf16,
jn,
jnf,
ldexp,
Expand Down Expand Up @@ -323,43 +327,6 @@ macro_rules! impl_op_for_ty {
}
}

impl MpOp for crate::op::[<frexp $suffix>]::Routine {
type MpTy = MpFloat;

fn new_mp() -> Self::MpTy {
new_mpfloat::<Self::FTy>()
}

fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
this.assign(input.0);
let exp = this.frexp_mut();
(prep_retval::<Self::FTy>(this, Ordering::Equal), exp)
}
}

impl MpOp for crate::op::[<ilogb $suffix>]::Routine {
type MpTy = MpFloat;

fn new_mp() -> Self::MpTy {
new_mpfloat::<Self::FTy>()
}

fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
this.assign(input.0);

// `get_exp` follows `frexp` for `0.5 <= |m| < 1.0`. Adjust the exponent by
// one to scale the significand to `1.0 <= |m| < 2.0`.
this.get_exp().map(|v| v - 1).unwrap_or_else(|| {
if this.is_infinite() {
i32::MAX
} else {
// Zero or NaN
i32::MIN
}
})
}
}

impl MpOp for crate::op::[<jn $suffix>]::Routine {
type MpTy = MpFloat;

Expand Down Expand Up @@ -504,6 +471,43 @@ macro_rules! impl_op_for_ty_all {
}
}

impl MpOp for crate::op::[<frexp $suffix>]::Routine {
type MpTy = MpFloat;

fn new_mp() -> Self::MpTy {
new_mpfloat::<Self::FTy>()
}

fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
this.assign(input.0);
let exp = this.frexp_mut();
(prep_retval::<Self::FTy>(this, Ordering::Equal), exp)
}
}

impl MpOp for crate::op::[<ilogb $suffix>]::Routine {
type MpTy = MpFloat;

fn new_mp() -> Self::MpTy {
new_mpfloat::<Self::FTy>()
}

fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
this.assign(input.0);

// `get_exp` follows `frexp` for `0.5 <= |m| < 1.0`. Adjust the exponent by
// one to scale the significand to `1.0 <= |m| < 2.0`.
this.get_exp().map(|v| v - 1).unwrap_or_else(|| {
if this.is_infinite() {
i32::MAX
} else {
// Zero or NaN
i32::MIN
}
})
}
}

// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
// methods.
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
Expand Down
12 changes: 12 additions & 0 deletions libm-test/src/test_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,15 @@ impl_tuples!(
(f32, f32);
(f64, f64);
);

#[cfg(f16_enabled)]
impl_tuples!(
(f16, i32);
(f16, f16);
);

#[cfg(f128_enabled)]
impl_tuples!(
(f128, i32);
(f128, f128);
);
4 changes: 4 additions & 0 deletions libm-test/tests/compare_built_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ libm_macros::for_each_function! {
fminimumf16,
fmodf128,
fmodf16,
frexpf128,
frexpf16,
ilogbf128,
ilogbf16,
ldexpf128,
ldexpf16,
rintf128,
Expand Down
46 changes: 29 additions & 17 deletions libm/src/math/frexp.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
/// Decompose a float into a normalized value within the range `[0.5, 1)`, and a power of 2.
///
/// That is, `x * 2^p` will represent the input value.
#[cfg(f16_enabled)]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn frexp(x: f64) -> (f64, i32) {
let mut y = x.to_bits();
let ee = ((y >> 52) & 0x7ff) as i32;
pub fn frexpf16(x: f16) -> (f16, i32) {
super::generic::frexp(x)
}

if ee == 0 {
if x != 0.0 {
let x1p64 = f64::from_bits(0x43f0000000000000);
let (x, e) = frexp(x * x1p64);
return (x, e - 64);
}
return (x, 0);
} else if ee == 0x7ff {
return (x, 0);
}
/// Decompose a float into a normalized value within the range `[0.5, 1)`, and a power of 2.
///
/// That is, `x * 2^p` will represent the input value.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn frexpf(x: f32) -> (f32, i32) {
super::generic::frexp(x)
}

let e = ee - 0x3fe;
y &= 0x800fffffffffffff;
y |= 0x3fe0000000000000;
return (f64::from_bits(y), e);
/// Decompose a float into a normalized value within the range `[0.5, 1)`, and a power of 2.
///
/// That is, `x * 2^p` will represent the input value.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn frexp(x: f64) -> (f64, i32) {
super::generic::frexp(x)
}

/// Decompose a float into a normalized value within the range `[0.5, 1)`, and a power of 2.
///
/// That is, `x * 2^p` will represent the input value.
#[cfg(f128_enabled)]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn frexpf128(x: f128) -> (f128, i32) {
super::generic::frexp(x)
}
22 changes: 0 additions & 22 deletions libm/src/math/frexpf.rs

This file was deleted.

25 changes: 25 additions & 0 deletions libm/src/math/generic/frexp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::super::{CastFrom, Float, MinInt};

pub fn frexp<F: Float>(x: F) -> (F, i32) {
let mut ix = x.to_bits();
let ee = x.ex();

if ee == 0 {
if x != F::ZERO {
// normalize via multiplication; 1p64 for `f64`
let magic = F::from_parts(false, F::EXP_BIAS + F::BITS, F::Int::ZERO);
magic.to_bits();

let (x, e) = frexp(x * magic);
return (x, e - F::BITS as i32);
}
return (x, 0);
} else if ee == F::EXP_SAT {
return (x, 0);
}

let e = ee as i32 - (F::EXP_BIAS as i32 - 1);
ix &= F::SIGN_MASK | F::SIG_MASK;
ix |= F::Int::cast_from(F::EXP_BIAS - 1) << F::SIG_BITS;
(F::from_bits(ix), e)
}
Loading