Passed some Scales stuff to comptime

This commit is contained in:
adrien 2026-04-21 18:18:12 +02:00
parent aa09054c4d
commit bbab41008f

View File

@ -3,7 +3,7 @@ const hlp = @import("helper.zig");
const Dimensions = @import("Dimensions.zig"); const Dimensions = @import("Dimensions.zig");
const Dimension = @import("Dimensions.zig").Dimension; const Dimension = @import("Dimensions.zig").Dimension;
pub const UnitScale = enum(i32) { pub const UnitScale = enum(isize) {
P = 15, P = 15,
T = 12, T = 12,
G = 9, G = 9,
@ -28,7 +28,7 @@ pub const UnitScale = enum(i32) {
// Undefined // Undefined
_, _,
pub fn str(self: @This()) []const u8 { pub inline fn str(self: @This()) []const u8 {
var buf: [16]u8 = undefined; var buf: [16]u8 = undefined;
return switch (self) { return switch (self) {
inline .none => "", inline .none => "",
@ -38,7 +38,7 @@ pub const UnitScale = enum(i32) {
} }
/// Helper to get the actual scaling factor /// Helper to get the actual scaling factor
pub fn getFactor(self: @This()) f64 { pub inline fn getFactor(self: @This()) comptime_float {
return switch (self) { return switch (self) {
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))), inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))),
inline else => @floatFromInt(@intFromEnum(self)), inline else => @floatFromInt(@intFromEnum(self)),
@ -46,10 +46,10 @@ pub const UnitScale = enum(i32) {
} }
/// Helper to get the actual scaling factor in i32 /// Helper to get the actual scaling factor in i32
pub fn getFactorInt(self: @This()) i32 { pub inline fn getFactorInt(self: @This()) comptime_int {
return switch (self) { return switch (self) {
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.powi(i32, 10.0, @intFromEnum(self)) catch 0, inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => comptime std.math.powi(i32, 10.0, @intFromEnum(self)) catch 0,
inline else => @intFromEnum(self), inline else => comptime @intFromEnum(self),
}; };
} }
}; };
@ -89,7 +89,7 @@ pub fn min(comptime s1: Scales, comptime s2: Scales) Scales {
return out; return out;
} }
pub fn getFactor(comptime s: Scales, comptime d: Dimensions) f64 { pub inline fn getFactor(comptime s: Scales, comptime d: Dimensions) comptime_float {
comptime var factor: f64 = 1.0; comptime var factor: f64 = 1.0;
inline for (std.enums.values(Dimension)) |dim| { inline for (std.enums.values(Dimension)) |dim| {
const power = d.get(dim); const power = d.get(dim);