Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Add tests against MPFR for scalbn{f} and ldexp{f} #392

Merged
merged 1 commit into from
Jan 5, 2025

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Jan 3, 2025

No description provided.

Comment on lines 279 to 320
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
this.0.assign(input.0);
this.1.assign(2.0);
this.1.pow_assign(input.1);
let ord = this.0.mul_assign_round(&this.1, Nearest);
prep_retval::<Self::FTy>(&mut this.0, ord)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This discards the rounding of 2 ^ exp which should be okay since powers of two are representable if within range. Somebody should double check this though.

Copy link
Contributor Author

@tgross35 tgross35 Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this doesn't work because scalbn(INF, -HUGE) works out to INF * 0 = NaN, but the result should be infinity.

For reference, from the C spec:

  • scalbn(±0, n) returns ±0.
  • scalbn(x, 0) returns x.
  • scalbn(±∞, n) returns ±∞.

If the calculation does not overflow or underflow, the returned value is exact and independent of the current rounding direction mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after checking the docs, what you want is Float::shl, if you're not immediately converting back to a primitive float you'll probably also want to call subnormalize

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can look at subnormalize_ieee_round's source for an idea of how to use subnormalize, i'm assuming rust may gain support for non-ieee floats such as bf16 or f80.

Copy link
Contributor Author

@tgross35 tgross35 Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that does seem to work simpler than what I had. The call to prep_retval does call subnormalize_ieee_round before casting to the float type (link), I don't think bf16 uses libm (unlike f16, all bf16 ops may be done at f32 precision right?) but this will probably need to be adjusted once we add the x87 float type. Easiest to just cross that bridge later though, I'm just getting started with f16/f128 here.

@tgross35 tgross35 changed the title Add tests against MPFR for scalbn[f] and ldexp[f] Add tests against MPFR for scalbn{f} and ldexp{f} Jan 3, 2025
@tgross35 tgross35 force-pushed the tgross35/scalbn-tests branch 5 times, most recently from 02124db to 20cf129 Compare January 5, 2025 02:27
@tgross35 tgross35 force-pushed the tgross35/scalbn-tests branch from 20cf129 to 07b0681 Compare January 5, 2025 23:34
@tgross35 tgross35 enabled auto-merge (rebase) January 5, 2025 23:35
@tgross35 tgross35 merged commit 8d6f6a1 into master Jan 5, 2025
30 checks passed
@tgross35 tgross35 deleted the tgross35/scalbn-tests branch January 5, 2025 23:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants