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(...@@ -1124,6 +1124,7 @@ pub fn formatFloatHexadecimal(
1124 const TU = std.meta.Int(.unsigned, std.meta.bitCount(T));1124 const TU = std.meta.Int(.unsigned, std.meta.bitCount(T));
11251125
1126 const mantissa_bits = math.floatMantissaBits(T);1126 const mantissa_bits = math.floatMantissaBits(T);
1127 const fractional_bits = math.floatFractionalBits(T);
1127 const exponent_bits = math.floatExponentBits(T);1128 const exponent_bits = math.floatExponentBits(T);
1128 const mantissa_mask = (1 << mantissa_bits) - 1;1129 const mantissa_mask = (1 << mantissa_bits) - 1;
1129 const exponent_mask = (1 << exponent_bits) - 1;1130 const exponent_mask = (1 << exponent_bits) - 1;
...@@ -1155,14 +1156,14 @@ pub fn formatFloatHexadecimal(...@@ -1155,14 +1156,14 @@ pub fn formatFloatHexadecimal(
1155 // Adjust the exponent for printing.1156 // Adjust the exponent for printing.
1156 exponent += 1;1157 exponent += 1;
1157 } else {1158 } else {
1158 // Add the implicit 1.1159 if (fractional_bits == mantissa_bits)
1159 mantissa |= 1 << mantissa_bits;1160 mantissa |= 1 << fractional_bits; // Add the implicit integer bit.
1160 }1161 }
11611162
1162 // Fill in zeroes to round the mantissa width to a multiple of 4.1163 // Fill in zeroes to round the mantissa width to a multiple of 4.
1163 if (T == f16) mantissa <<= 2 else if (T == f32) mantissa <<= 1;1164 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
1167 if (options.precision) |precision| {1168 if (options.precision) |precision| {
1168 // Round if needed.1169 // Round if needed.
lib/std/math/signbit.zig+6
...@@ -9,6 +9,7 @@ pub fn signbit(x: anytype) bool {...@@ -9,6 +9,7 @@ pub fn signbit(x: anytype) bool {
9 f16 => signbit16(x),9 f16 => signbit16(x),
10 f32 => signbit32(x),10 f32 => signbit32(x),
11 f64 => signbit64(x),11 f64 => signbit64(x),
12 f80 => signbit80(x),
12 f128 => signbit128(x),13 f128 => signbit128(x),
13 else => @compileError("signbit not implemented for " ++ @typeName(T)),14 else => @compileError("signbit not implemented for " ++ @typeName(T)),
14 };15 };
...@@ -29,6 +30,11 @@ fn signbit64(x: f64) bool {...@@ -29,6 +30,11 @@ fn signbit64(x: f64) bool {
29 return bits >> 63 != 0;30 return bits >> 63 != 0;
30}31}
3132
33fn signbit80(x: f80) bool {
34 const bits = @bitCast(u80, x);
35 return bits >> 79 != 0;
36}
37
32fn signbit128(x: f128) bool {38fn signbit128(x: f128) bool {
33 const bits = @bitCast(u128, x);39 const bits = @bitCast(u128, x);
34 return bits >> 127 != 0;40 return bits >> 127 != 0;