authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:06:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-25 09:04:04-07:00
log55c5da1b205dc5eddd2749381caacf28bea6f90e
tree785c19473b5910b3419b91745500df9971d8e0fe
parentb3cd38ea4a7520fabbb05d3d2e74351c7c8effdb

fmt: fix f80 hex formatting

These ifs were missing a case for f80 which should have shifted by one, but we can just compute the correct value instead. Also, we want the fractional bits to be a multiple of four, not the mantissa bits, since the mantissa could have a leading one which we want to be separated.

1 files changed, 9 insertions(+), 4 deletions(-)

lib/std/fmt.zig+9-4
...@@ -1207,10 +1207,9 @@ pub fn formatFloatHexadecimal(...@@ -1207,10 +1207,9 @@ pub fn formatFloatHexadecimal(
1207 mantissa |= 1 << fractional_bits; // Add the implicit integer bit.1207 mantissa |= 1 << fractional_bits; // Add the implicit integer bit.
1208 }1208 }
12091209
1210 // Fill in zeroes to round the mantissa width to a multiple of 4.
1211 if (T == f16) mantissa <<= 2 else if (T == f32) mantissa <<= 1;
1212
1213 const mantissa_digits = (fractional_bits + 3) / 4;1210 const mantissa_digits = (fractional_bits + 3) / 4;
1211 // Fill in zeroes to round the fraction width to a multiple of 4.
1212 mantissa <<= mantissa_digits * 4 - fractional_bits;
12141213
1215 if (options.precision) |precision| {1214 if (options.precision) |precision| {
1216 // Round if needed.1215 // Round if needed.
...@@ -2317,21 +2316,25 @@ test "float.hexadecimal" {...@@ -2317,21 +2316,25 @@ test "float.hexadecimal" {
2317 try expectFmt("f16: 0x1.554p-2", "f16: {x}", .{@as(f16, 1.0 / 3.0)});2316 try expectFmt("f16: 0x1.554p-2", "f16: {x}", .{@as(f16, 1.0 / 3.0)});
2318 try expectFmt("f32: 0x1.555556p-2", "f32: {x}", .{@as(f32, 1.0 / 3.0)});2317 try expectFmt("f32: 0x1.555556p-2", "f32: {x}", .{@as(f32, 1.0 / 3.0)});
2319 try expectFmt("f64: 0x1.5555555555555p-2", "f64: {x}", .{@as(f64, 1.0 / 3.0)});2318 try expectFmt("f64: 0x1.5555555555555p-2", "f64: {x}", .{@as(f64, 1.0 / 3.0)});
2319 try expectFmt("f80: 0x1.5555555555555556p-2", "f80: {x}", .{@as(f80, 1.0 / 3.0)});
2320 try expectFmt("f128: 0x1.5555555555555555555555555555p-2", "f128: {x}", .{@as(f128, 1.0 / 3.0)});2320 try expectFmt("f128: 0x1.5555555555555555555555555555p-2", "f128: {x}", .{@as(f128, 1.0 / 3.0)});
23212321
2322 try expectFmt("f16: 0x1p-14", "f16: {x}", .{math.floatMin(f16)});2322 try expectFmt("f16: 0x1p-14", "f16: {x}", .{math.floatMin(f16)});
2323 try expectFmt("f32: 0x1p-126", "f32: {x}", .{math.floatMin(f32)});2323 try expectFmt("f32: 0x1p-126", "f32: {x}", .{math.floatMin(f32)});
2324 try expectFmt("f64: 0x1p-1022", "f64: {x}", .{math.floatMin(f64)});2324 try expectFmt("f64: 0x1p-1022", "f64: {x}", .{math.floatMin(f64)});
2325 try expectFmt("f80: 0x1p-16382", "f80: {x}", .{math.floatMin(f80)});
2325 try expectFmt("f128: 0x1p-16382", "f128: {x}", .{math.floatMin(f128)});2326 try expectFmt("f128: 0x1p-16382", "f128: {x}", .{math.floatMin(f128)});
23262327
2327 try expectFmt("f16: 0x0.004p-14", "f16: {x}", .{math.floatTrueMin(f16)});2328 try expectFmt("f16: 0x0.004p-14", "f16: {x}", .{math.floatTrueMin(f16)});
2328 try expectFmt("f32: 0x0.000002p-126", "f32: {x}", .{math.floatTrueMin(f32)});2329 try expectFmt("f32: 0x0.000002p-126", "f32: {x}", .{math.floatTrueMin(f32)});
2329 try expectFmt("f64: 0x0.0000000000001p-1022", "f64: {x}", .{math.floatTrueMin(f64)});2330 try expectFmt("f64: 0x0.0000000000001p-1022", "f64: {x}", .{math.floatTrueMin(f64)});
2331 try expectFmt("f80: 0x0.0000000000000002p-16382", "f80: {x}", .{math.floatTrueMin(f80)});
2330 try expectFmt("f128: 0x0.0000000000000000000000000001p-16382", "f128: {x}", .{math.floatTrueMin(f128)});2332 try expectFmt("f128: 0x0.0000000000000000000000000001p-16382", "f128: {x}", .{math.floatTrueMin(f128)});
23312333
2332 try expectFmt("f16: 0x1.ffcp15", "f16: {x}", .{math.floatMax(f16)});2334 try expectFmt("f16: 0x1.ffcp15", "f16: {x}", .{math.floatMax(f16)});
2333 try expectFmt("f32: 0x1.fffffep127", "f32: {x}", .{math.floatMax(f32)});2335 try expectFmt("f32: 0x1.fffffep127", "f32: {x}", .{math.floatMax(f32)});
2334 try expectFmt("f64: 0x1.fffffffffffffp1023", "f64: {x}", .{math.floatMax(f64)});2336 try expectFmt("f64: 0x1.fffffffffffffp1023", "f64: {x}", .{math.floatMax(f64)});
2337 try expectFmt("f80: 0x1.fffffffffffffffep16383", "f80: {x}", .{math.floatMax(f80)});
2335 try expectFmt("f128: 0x1.ffffffffffffffffffffffffffffp16383", "f128: {x}", .{math.floatMax(f128)});2338 try expectFmt("f128: 0x1.ffffffffffffffffffffffffffffp16383", "f128: {x}", .{math.floatMax(f128)});
2336}2339}
23372340
...@@ -2339,11 +2342,13 @@ test "float.hexadecimal.precision" {...@@ -2339,11 +2342,13 @@ test "float.hexadecimal.precision" {
2339 try expectFmt("f16: 0x1.5p-2", "f16: {x:.1}", .{@as(f16, 1.0 / 3.0)});2342 try expectFmt("f16: 0x1.5p-2", "f16: {x:.1}", .{@as(f16, 1.0 / 3.0)});
2340 try expectFmt("f32: 0x1.555p-2", "f32: {x:.3}", .{@as(f32, 1.0 / 3.0)});2343 try expectFmt("f32: 0x1.555p-2", "f32: {x:.3}", .{@as(f32, 1.0 / 3.0)});
2341 try expectFmt("f64: 0x1.55555p-2", "f64: {x:.5}", .{@as(f64, 1.0 / 3.0)});2344 try expectFmt("f64: 0x1.55555p-2", "f64: {x:.5}", .{@as(f64, 1.0 / 3.0)});
2342 try expectFmt("f128: 0x1.5555555p-2", "f128: {x:.7}", .{@as(f128, 1.0 / 3.0)});2345 try expectFmt("f80: 0x1.5555555p-2", "f80: {x:.7}", .{@as(f80, 1.0 / 3.0)});
2346 try expectFmt("f128: 0x1.555555555p-2", "f128: {x:.9}", .{@as(f128, 1.0 / 3.0)});
23432347
2344 try expectFmt("f16: 0x1.00000p0", "f16: {x:.5}", .{@as(f16, 1.0)});2348 try expectFmt("f16: 0x1.00000p0", "f16: {x:.5}", .{@as(f16, 1.0)});
2345 try expectFmt("f32: 0x1.00000p0", "f32: {x:.5}", .{@as(f32, 1.0)});2349 try expectFmt("f32: 0x1.00000p0", "f32: {x:.5}", .{@as(f32, 1.0)});
2346 try expectFmt("f64: 0x1.00000p0", "f64: {x:.5}", .{@as(f64, 1.0)});2350 try expectFmt("f64: 0x1.00000p0", "f64: {x:.5}", .{@as(f64, 1.0)});
2351 try expectFmt("f80: 0x1.00000p0", "f80: {x:.5}", .{@as(f80, 1.0)});
2347 try expectFmt("f128: 0x1.00000p0", "f128: {x:.5}", .{@as(f128, 1.0)});2352 try expectFmt("f128: 0x1.00000p0", "f128: {x:.5}", .{@as(f128, 1.0)});
2348}2353}
23492354