Skip to content

libm: Add fmaf16#861

Draft
tgross35 wants to merge 2 commits intorust-lang:mainfrom
tgross35:f16-fma
Draft

libm: Add fmaf16#861
tgross35 wants to merge 2 commits intorust-lang:mainfrom
tgross35:f16-fma

Conversation

@tgross35
Copy link
Copy Markdown
Contributor

@tgross35 tgross35 commented Apr 20, 2025

Recreated from rust-lang/libm#419

Comment thread libm/src/math/fma_wide.rs Outdated
@@ -5,11 +5,10 @@ use super::support::{FpResult, IntTy, Round, Status};
use super::{CastFrom, CastInto, DFloat, Float, HFloat, MinInt};

// Placeholder so we can have `fmaf16` in the `Float` trait.
Copy link
Copy Markdown

@tmvkrpxl0 tmvkrpxl0 Dec 17, 2025

Choose a reason for hiding this comment

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

Is it really a placeholder if it (now) has implementation?

Copy link
Copy Markdown
Contributor Author

@tgross35 tgross35 Feb 12, 2026

Choose a reason for hiding this comment

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

Indeed, updated to a doc comment :)

@tgross35 tgross35 force-pushed the f16-fma branch 3 times, most recently from 40f4317 to 1c9cf79 Compare February 12, 2026 09:00
@rustbot

This comment has been minimized.

@tgross35 tgross35 force-pushed the f16-fma branch 5 times, most recently from 0501cfc to 11e2b85 Compare April 20, 2026 10:47
@tgross35
Copy link
Copy Markdown
Contributor Author

Baseline at 1909bf7:

icount::icount_bench_math_group::icount_bench_fmaf16 logspace:(setup_fmaf16())
  Baselines:                   arch_enabled|arch_enabled (old)
  Instructions:                       64543|N/A                  (*********)
  L1 Hits:                            71915|N/A                  (*********)
  LL Hits:                                3|N/A                  (*********)
  RAM Hits:                              18|N/A                  (*********)
  Total read+write:                   71936|N/A                  (*********)
  Estimated Cycles:                   72560|N/A                  (*********)

Comment thread libm/src/math/fmaf16.rs
} else {
rneg = mneg;
m64 + z64
};
Copy link
Copy Markdown
Contributor

@quaternic quaternic Apr 20, 2026

Choose a reason for hiding this comment

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

At this point we have the result as a sign-magnitude fixed-point value that just needs rounding. The rest of this function might be a useful to factor out, and test independently. Something like

/// Rounds the value `n * 2^s` to `f16`
fn f16_from_u64_with_scale(n: u64, s: i32) -> f16

And this could return

f16_from_u64_with_scale(r64, -40).copysign(/* from rneg */)

View changes since the review

Comment thread libm/src/math/fmaf16.rs
Comment on lines +177 to +192
// Apply rounding from LSB and GRS bits
if r & 0b01000 != 0 && r & 0b10111 != 0 {
r += 0b1000;
}

if r & (1 << 15) != 0 {
// rounding overflowed to the next power
r >>= 1;
rexp += 1;
println!("round rexp: {rexp}");
}
if r & (1 << 14) != 0 {
// ensure subnormals that rounded up get the exponent of 1
rexp = rexp.max(1);
println!("sub rexp: {rexp}");
}
Copy link
Copy Markdown
Contributor

@quaternic quaternic Apr 20, 2026

Choose a reason for hiding this comment

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

A handy trick for doing the rounding: The effect of rounding a finite magnitude up is always just +1 to the bits of the float. If the mantissa had its maximum value, it carries into the exponent field but that still gives the correct value because the mantissa is then all zeroes.

View changes since the review

@tgross35 tgross35 changed the title Add fmaf16 libm: Add fmaf16 Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants