Skip to content

std: correct rounding in parse_hex_float.zig #10743

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
Feb 1, 2022
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
20 changes: 11 additions & 9 deletions lib/std/fmt/parse_hex_float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,19 @@ pub fn parseHexFloat(comptime T: type, s: []const u8) !T {
exponent += 1;
}

// There are two cases to handle:
// - We've truncated more than 0.5ULP (R=S=1), increase the mantissa.
// - We've truncated exactly 0.5ULP (R=1 S=0), increase the mantissa if the
// result is odd (G=1).
// The two checks can be neatly folded as follows.
mantissa |= @boolToInt(mantissa & 0b100 != 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Changing this line to:
mantissa += @boolToInt(mantissa & 0b100 != 0);

seems to fix the issue without needing to check both sides of the truncation.

Copy link
Contributor

Choose a reason for hiding this comment

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

and then removing the next line: mantissa += 1

Copy link
Member

@andrewrk andrewrk Feb 1, 2022

Choose a reason for hiding this comment

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

I did a cleanup commit + merge. If you have an additional improvement you would like to make, would you mind submitting it as a follow-up PR?

mantissa += 1;

// Whenever the guard bit is one (G=1) and:
// - we've truncated more than 0.5ULP (R=S=1)
// - we've truncated exactly 0.5ULP (R=1 S=0)
// Were are going to increase the mantissa (round up)
const guard_bit_and_half_or_more = (mantissa & 0b110) == 0b110;
mantissa >>= 2;
exponent += 2;

if (mantissa & (1 << (mantissa_bits + 1)) != 0) {
if (guard_bit_and_half_or_more) {
mantissa += 1;
}

if (mantissa == (1 << (mantissa_bits + 1))) {
// Renormalize, if the exponent overflows we'll catch that below.
mantissa >>= 1;
exponent += 1;
Expand Down Expand Up @@ -338,6 +339,7 @@ test "f128" {
// // Min denormalized value.
.{ .s = "0x1p-16494", .v = math.f128_true_min },
.{ .s = "-0x1p-16494", .v = -math.f128_true_min },
.{ .s = "0x1.edcb34a235253948765432134674fp-1", .v = 0x1.edcb34a235253948765432134674fp-1 },
};

for (cases) |case| {
Expand Down
5 changes: 0 additions & 5 deletions test/behavior/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,6 @@ test "allow signed integer division/remainder when values are comptime known and
}

test "quad hex float literal parsing accurate" {
if (builtin.zig_backend != .stage1) {
// TODO https://github.com/ziglang/zig/issues/10737
return error.SkipZigTest;
}

const a: f128 = 0x1.1111222233334444555566667777p+0;

// implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing.
Expand Down