authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-31 22:59:49-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-31 22:59:49-05:00
logd3e40b0d8085ec82e6e5b33f2010de6cb9779ce5
tree998df07db358b117250000eb539a08a021af3873
parent627cf6ce482349c172150d058660f7a1646c2aac
parentc46f7588cea98c48e6c7f637e13536378027e260
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10743 from m-radomski/master

std: correct rounding in parse_hex_float.zig

2 files changed, 11 insertions(+), 14 deletions(-)

lib/std/fmt/parse_hex_float.zig+11-9
......@@ -202,18 +202,19 @@ pub fn parseHexFloat(comptime T: type, s: []const u8) !T {
202202 exponent += 1;
203203 }
204204
205 // There are two cases to handle:
206 // - We've truncated more than 0.5ULP (R=S=1), increase the mantissa.
207 // - We've truncated exactly 0.5ULP (R=1 S=0), increase the mantissa if the
208 // result is odd (G=1).
209 // The two checks can be neatly folded as follows.
210 mantissa |= @boolToInt(mantissa & 0b100 != 0);
211 mantissa += 1;
212
205 // Whenever the guard bit is one (G=1) and:
206 // - we've truncated more than 0.5ULP (R=S=1)
207 // - we've truncated exactly 0.5ULP (R=1 S=0)
208 // Were are going to increase the mantissa (round up)
209 const guard_bit_and_half_or_more = (mantissa & 0b110) == 0b110;
213210 mantissa >>= 2;
214211 exponent += 2;
215212
216 if (mantissa & (1 << (mantissa_bits + 1)) != 0) {
213 if (guard_bit_and_half_or_more) {
214 mantissa += 1;
215 }
216
217 if (mantissa == (1 << (mantissa_bits + 1))) {
217218 // Renormalize, if the exponent overflows we'll catch that below.
218219 mantissa >>= 1;
219220 exponent += 1;
......@@ -338,6 +339,7 @@ test "f128" {
338339 // // Min denormalized value.
339340 .{ .s = "0x1p-16494", .v = math.f128_true_min },
340341 .{ .s = "-0x1p-16494", .v = -math.f128_true_min },
342 .{ .s = "0x1.edcb34a235253948765432134674fp-1", .v = 0x1.edcb34a235253948765432134674fp-1 },
341343 };
342344
343345 for (cases) |case| {
test/behavior/math.zig-5
......@@ -632,11 +632,6 @@ test "allow signed integer division/remainder when values are comptime known and
632632}
633633
634634test "quad hex float literal parsing accurate" {
635 if (builtin.zig_backend != .stage1) {
636 // TODO https://github.com/ziglang/zig/issues/10737
637 return error.SkipZigTest;
638 }
639
640635 const a: f128 = 0x1.1111222233334444555566667777p+0;
641636
642637 // implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing.