Skip to content
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

Extend bf16 #1902

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

chihminchao
Copy link
Contributor

No description provided.

also add  fxx_neg helper functions

Signed-off-by: Chih-Min Chao <[email protected]>
return signF32UI(a.v);
}

bool f64_neg( float64_t a )
Copy link
Collaborator

@aswaterman aswaterman Jan 24, 2025

Choose a reason for hiding this comment

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

neg is a confusing name for these functions; it sounds like a function that negates (i.e. returns -a), especially since RISC-V has pseudoinstructions named fneg that perform the negation operation. sign is a better name.


bfloat16_t i32_to_bf16( int32_t a )
{
return f32_to_bf16(i32_to_f32(a));
Copy link
Collaborator

@aswaterman aswaterman Jan 24, 2025

Choose a reason for hiding this comment

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

I would like @nibrunieAtSi5's confirmation, but I believe this implementation can incorrectly double-round. You need to perform the i32 to f32 conversion under round-to-odd, then restore the rounding mode before converting to bf16.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds about right,
for example converting 0b1_0000_0001_0000_0000_0001_0000 (a.k.a. 0x1010011) from integer to BF16 going through FP32 will result in an error in RNE mode:

  • 1st conversion will result in 0b1_0000_0001_0000_0000_000 (since the input is a mid-point for FP32, it is rounded to the even mantissa)
  • 2nd conversion from FP32 to BF16 will round the mantissa to 0b1_0000_000 (same reason)

but in fact 0b1_0000_001 was expected but the sticky information encompassed in the round bit of the first conversion was lost

case 0x004: // -subnormal
case 0x020: //+ sub
sub = true;
default: // +- normal
Copy link
Collaborator

Choose a reason for hiding this comment

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

@nibrunieAtSi5 should review this one, since IIRC there is a special case for BF16 subnormals.


float16_t ui32_to_bf16( uint32_t a )
{
return f32_to_bf16(ui32_to_f32(a));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

@aswaterman aswaterman left a comment

Choose a reason for hiding this comment

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

Looks like it's on the right track, but please address the few issues I raised, possibly in consultation with @nibrunieAtSi5.

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.

3 participants