authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-25 16:32:42-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-25 17:21:09-07:00
logf5540778b56636cffd89f794c6b1a9266d7d36f8
tree08ddd7e6b30ea5bda531bc7fc57a3f6236927213
parentd930e015c75d60e22d0a116a1f4fa0df4d68b6b6

stdlib: Fix hex-float printing for f80


2 files changed, 10 insertions(+), 3 deletions(-)

lib/std/fmt.zig+4-3
......@@ -1124,6 +1124,7 @@ pub fn formatFloatHexadecimal(
11241124 const TU = std.meta.Int(.unsigned, std.meta.bitCount(T));
11251125
11261126 const mantissa_bits = math.floatMantissaBits(T);
1127 const fractional_bits = math.floatFractionalBits(T);
11271128 const exponent_bits = math.floatExponentBits(T);
11281129 const mantissa_mask = (1 << mantissa_bits) - 1;
11291130 const exponent_mask = (1 << exponent_bits) - 1;
......@@ -1155,14 +1156,14 @@ pub fn formatFloatHexadecimal(
11551156 // Adjust the exponent for printing.
11561157 exponent += 1;
11571158 } else {
1158 // Add the implicit 1.
1159 mantissa |= 1 << mantissa_bits;
1159 if (fractional_bits == mantissa_bits)
1160 mantissa |= 1 << fractional_bits; // Add the implicit integer bit.
11601161 }
11611162
11621163 // Fill in zeroes to round the mantissa width to a multiple of 4.
11631164 if (T == f16) mantissa <<= 2 else if (T == f32) mantissa <<= 1;
11641165
1165 const mantissa_digits = (mantissa_bits + 3) / 4;
1166 const mantissa_digits = (fractional_bits + 3) / 4;
11661167
11671168 if (options.precision) |precision| {
11681169 // Round if needed.
lib/std/math/signbit.zig+6
......@@ -9,6 +9,7 @@ pub fn signbit(x: anytype) bool {
99 f16 => signbit16(x),
1010 f32 => signbit32(x),
1111 f64 => signbit64(x),
12 f80 => signbit80(x),
1213 f128 => signbit128(x),
1314 else => @compileError("signbit not implemented for " ++ @typeName(T)),
1415 };
......@@ -29,6 +30,11 @@ fn signbit64(x: f64) bool {
2930 return bits >> 63 != 0;
3031}
3132
33fn signbit80(x: f80) bool {
34 const bits = @bitCast(u80, x);
35 return bits >> 79 != 0;
36}
37
3238fn signbit128(x: f128) bool {
3339 const bits = @bitCast(u128, x);
3440 return bits >> 127 != 0;