A custom N-bit floating point type, representing f * 2^e.
e is biased, so it be directly shifted into the exponent bits.
Negative exponent indicates an invalid result.
pub fn BiasedFp(comptime T: type) type
pub fn BiasedFp(comptime T: type) type {
const MantissaT = mantissaType(T);
return struct {
const Self = @This();
/// The significant digits.
f: MantissaT,
/// The biased, binary exponent.
e: i32,
pub fn zero() Self {
return .{ .f = 0, .e = 0 };
}
pub fn zeroPow2(e: i32) Self {
return .{ .f = 0, .e = e };
}
pub fn inf(comptime FloatT: type) Self {
const e = (1 << std.math.floatExponentBits(FloatT)) - 1;
return switch (FloatT) {
f80 => .{ .f = 0x8000000000000000, .e = e },
else => .{ .f = 0, .e = e },
};
}
pub fn eql(self: Self, other: Self) bool {
return self.f == other.f and self.e == other.e;
}
pub fn toFloat(self: Self, comptime FloatT: type, negative: bool) FloatT {
var word = self.f;
word |= @as(MantissaT, @intCast(self.e)) << std.math.floatMantissaBits(FloatT);
var f = floatFromUnsigned(FloatT, MantissaT, word);
if (negative) f = -f;
return f;
}
};
}