Skip to content

Fix fptrunc Float64 -> Float16 rounding through Float32 #57809

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

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/runtime_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1629,8 +1629,8 @@ cvt_iintrinsic(LLVMFPtoUI, fptoui)
#define fintrinsic_read_float32(p) *(float *)p
#define fintrinsic_read_float64(p) *(double *)p

#define fintrinsic_write_float16(p, x) *(uint16_t *)p = float_to_half(x)
#define fintrinsic_write_bfloat16(p, x) *(uint16_t *)p = float_to_bfloat(x)
#define fintrinsic_write_float16(p, x) *(uint16_t *)p = double_to_half(x)
#define fintrinsic_write_bfloat16(p, x) *(uint16_t *)p = double_to_bfloat(x)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the same logic applies to bfloat16. Since bfloat16 is just a truncated float32, shouldn't it always be legal to go through Float32? If not, it'd be good to have a test.

Copy link
Member

Choose a reason for hiding this comment

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

According to the double_to_bfloat code, the same correction to the rounding direction is required whenever the float32 (after truncation from float64) is exactly halfway between 2 bfloat16 values, but wasn't required for the subnormal case (as was demonstrated in the original issue for float16)

#define fintrinsic_write_float32(p, x) *(float *)p = x
#define fintrinsic_write_float64(p, x) *(double *)p = x

Expand Down
11 changes: 11 additions & 0 deletions test/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ end
@test_intrinsic Core.Intrinsics.fptrunc Float16 Float32(3.3) Float16(3.3)
@test_intrinsic Core.Intrinsics.fptrunc Float16 Float64(3.3) Float16(3.3)

# #57805 - cases where rounding Float64 -> Float32 -> Float16 would fail
# 2^-25 * 0b1.0000000000000000000000000000000000000001 binary
# 0 01111100110 0000000000000000000000000000000000000001000000000000
# 2^-25 * 0b1.0 binary
# 0 01100110 00000000000000000000000
# 2^-14 * 0b0.0000000001 (subnormal)
# 0 00000 0000000001 (correct)
# 0 00000 0000000000 (incorrect)
@test_intrinsic Core.Intrinsics.fptrunc Float16 0x1.0000000001p-25 Float16(6.0e-8)
@test_intrinsic Core.Intrinsics.fptrunc Float16 -0x1.0000000001p-25 Float16(-6.0e-8)

# float_to_half/bfloat_to_float special cases
@test_intrinsic Core.Intrinsics.fptrunc Float16 Inf32 Inf16
@test_intrinsic Core.Intrinsics.fptrunc Float16 -Inf32 -Inf16
Expand Down