font/sfnt: simpler and more efficient FixedType conversions

This commit is contained in:
Qwerasd
2025-07-04 09:28:13 -06:00
parent 5553f7bf68
commit 1a8a048136

View File

@ -76,24 +76,22 @@ fn FixedPoint(comptime T: type, int_bits: u64, frac_bits: u64) type {
));
const half = @as(T, 1) << @intCast(frac_bits - 1);
frac: std.meta.Int(.unsigned, frac_bits),
int: std.meta.Int(type_info.signedness, int_bits),
const Frac = std.meta.Int(.unsigned, frac_bits);
const Int = std.meta.Int(type_info.signedness, int_bits);
frac: Frac,
int: Int,
pub fn to(self: Self, comptime FloatType: type) FloatType {
const i: FloatType = @floatFromInt(self.int);
const f: FloatType = @floatFromInt(self.frac);
return i + f / frac_factor;
return @as(FloatType, @floatFromInt(
@as(T, @bitCast(self)),
)) / frac_factor;
}
pub fn from(float: anytype) Self {
const int = @floor(float);
const frac = @abs(float - int);
return .{
.int = @intFromFloat(int),
.frac = @intFromFloat(@round(frac * frac_factor)),
};
return @bitCast(
@as(T, @intFromFloat(@round(float * frac_factor))),
);
}
/// Round to the nearest integer, .5 rounds away from 0.