From 998404d82aafa7d1c446ec0f10e2845fd3b1b47f Mon Sep 17 00:00:00 2001 From: adrien Date: Tue, 21 Apr 2026 17:31:22 +0200 Subject: [PATCH] Switched EVERYTHING in Scales and Dimensions to comptime --- src/Dimensions.zig | 16 ++++++++-------- src/Quantity.zig | 14 ++++++-------- src/QuantityVec.zig | 3 +-- src/Scales.zig | 30 +++++++++++++++--------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/Dimensions.zig b/src/Dimensions.zig index 65abf7b..886f06c 100644 --- a/src/Dimensions.zig +++ b/src/Dimensions.zig @@ -33,24 +33,24 @@ pub const Dimension = enum { const Self = @This(); -data: std.EnumArray(Dimension, i8), +data: std.EnumArray(Dimension, comptime_int), pub fn init(comptime init_val: anytype) Self { - var s = Self{ .data = std.EnumArray(Dimension, i8).initFill(0) }; + var s = Self{ .data = std.EnumArray(Dimension, comptime_int).initFill(0) }; inline for (std.meta.fields(@TypeOf(init_val))) |f| s.data.set(@field(Dimension, f.name), @field(init_val, f.name)); return s; } -pub fn initFill(val: i8) Self { - return .{ .data = std.EnumArray(Dimension, i8).initFill(val) }; +pub fn initFill(comptime val: comptime_int) Self { + return .{ .data = std.EnumArray(Dimension, comptime_int).initFill(val) }; } -pub fn get(self: Self, key: Dimension) i8 { +pub fn get(comptime self: Self, comptime key: Dimension) comptime_int { return self.data.get(key); } -pub fn set(self: *Self, key: Dimension, val: i8) void { +pub fn set(comptime self: *Self, comptime key: Dimension, comptime val: i8) void { self.data.set(key, val); } @@ -64,13 +64,13 @@ pub fn add(comptime a: Self, comptime b: Self) Self { pub fn sub(comptime a: Self, comptime b: Self) Self { @setEvalBranchQuota(10_000); var result = Self.initFill(0); - for (std.enums.values(Dimension)) |d| + inline for (std.enums.values(Dimension)) |d| result.set(d, a.get(d) - b.get(d)); return result; } pub fn eql(comptime a: Self, comptime b: Self) bool { - for (std.enums.values(Dimension)) |d| + inline for (std.enums.values(Dimension)) |d| if (a.get(d) != b.get(d)) return false; return true; } diff --git a/src/Quantity.zig b/src/Quantity.zig index f514a7e..a477166 100644 --- a/src/Quantity.zig +++ b/src/Quantity.zig @@ -127,11 +127,10 @@ pub fn Quantity(T: type, d: Dimensions, s: Scales) type { writer: *std.Io.Writer, ) !void { try writer.print("{d}", .{self.value}); - var iter = std.EnumSet(Dimension).initFull().iterator(); var first = true; - while (iter.next()) |bu| { + inline for (std.enums.values(Dimension)) |bu| { const v = dims.get(bu); - if (v == 0) continue; + if (comptime v == 0) continue; if (!first) try writer.writeAll("."); @@ -592,9 +591,8 @@ test "Overhead Analysis: Quantity vs Native" { a + b else if (comptime std.mem.eql(u8, op_name, "mulBy")) a * b - else - if (comptime @typeInfo(T) == .int) @divTrunc(a, b) else a / b; - + else if (comptime @typeInfo(T) == .int) @divTrunc(a, b) else a / b; + if (comptime @typeInfo(T) == .float) n_sink += r else n_sink ^= r; } const n_end = getTime(io); @@ -607,7 +605,7 @@ test "Overhead Analysis: Quantity vs Native" { for (0..ITERS) |i| { const qa = M{ .value = getValT(T, i) }; const qb = if (comptime std.mem.eql(u8, op_name, "divBy")) S{ .value = getValT(T, 2) } else M{ .value = getValT(T, 2) }; - + const r = if (comptime std.mem.eql(u8, op_name, "add")) qa.add(qb) else if (comptime std.mem.eql(u8, op_name, "mulBy")) @@ -630,7 +628,7 @@ test "Overhead Analysis: Quantity vs Native" { op_name, TNames[tidx], avg_n, avg_q, slowdown, }); } - if (j != Ops.len - 1) std.debug.print("├───────────┼──────┼───────────┼───────────┼───────────┤\n", .{}); + if (j != Ops.len - 1) std.debug.print("├───────────┼──────┼───────────┼───────────┼───────────┤\n", .{}); } std.debug.print("└───────────┴──────┴───────────┴───────────┴───────────┘\n", .{}); diff --git a/src/QuantityVec.zig b/src/QuantityVec.zig index 3691714..ac67df0 100644 --- a/src/QuantityVec.zig +++ b/src/QuantityVec.zig @@ -154,9 +154,8 @@ pub fn QuantityVec(comptime len: usize, comptime Q: type) type { try writer.print("{d:.2}", .{v}); } try writer.writeAll(")"); - var iter = std.EnumSet(Dimension).initFull().iterator(); var first = true; - while (iter.next()) |bu| { + inline for (std.enums.values(Dimension)) |bu| { const v = dims.get(bu); if (v == 0) continue; if (!first) try writer.writeAll("."); diff --git a/src/Scales.zig b/src/Scales.zig index 194fbe8..d51c4cb 100644 --- a/src/Scales.zig +++ b/src/Scales.zig @@ -31,9 +31,9 @@ pub const UnitScale = enum(i32) { pub fn str(self: @This()) []const u8 { var buf: [16]u8 = undefined; return switch (self) { - .none => "", + inline .none => "", inline .P, .T, .G, .M, .k, .h, .da, .d, .c, .m, .u, .n, .p, .f, .min, .hour, .year => @tagName(self), - else => std.fmt.bufPrint(&buf, "[{d}]", .{@intFromEnum(self)}) catch "[]", + else => std.fmt.bufPrint(&buf, "[{d}]", .{@intFromEnum(self)}) catch "[]", // This cannot be inline because of non exhaustive enum, but that's ok, it is just str, not calculation }; } @@ -41,7 +41,7 @@ pub const UnitScale = enum(i32) { pub fn getFactor(self: @This()) f64 { 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))), - else => @floatFromInt(@intFromEnum(self)), + inline else => @floatFromInt(@intFromEnum(self)), }; } @@ -49,7 +49,7 @@ pub const UnitScale = enum(i32) { pub fn getFactorInt(self: @This()) i32 { 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, - else => @intFromEnum(self), + inline else => @intFromEnum(self), }; } }; @@ -59,7 +59,7 @@ const Scales = @This(); data: std.EnumArray(Dimension, UnitScale), pub fn init(comptime init_val: anytype) Scales { - var s = Scales{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) }; + comptime var s = Scales{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) }; inline for (std.meta.fields(@TypeOf(init_val))) |f| { if (comptime hlp.isInt(@TypeOf(@field(init_val, f.name)))) s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name))) @@ -69,30 +69,30 @@ pub fn init(comptime init_val: anytype) Scales { return s; } -pub fn initFill(val: UnitScale) Scales { - return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) }; +pub fn initFill(comptime val: UnitScale) Scales { + return comptime .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) }; } -pub fn get(self: Scales, key: Dimension) UnitScale { - return self.data.get(key); +pub fn get(comptime self: Scales, comptime key: Dimension) UnitScale { + return comptime self.data.get(key); } -pub fn set(self: *Scales, key: Dimension, val: UnitScale) void { - self.data.set(key, val); +pub fn set(comptime self: *Scales, comptime key: Dimension, comptime val: UnitScale) void { + comptime self.data.set(key, val); } pub fn min(comptime s1: Scales, comptime s2: Scales) Scales { @setEvalBranchQuota(10_000); - var out = Scales.initFill(.none); - for (std.enums.values(Dimension)) |dim| + comptime var out = Scales.initFill(.none); + inline for (std.enums.values(Dimension)) |dim| out.set(dim, if (s1.get(dim).getFactorInt() > s2.get(dim).getFactorInt()) s2.get(dim) else s1.get(dim)); return out; } pub fn getFactor(comptime s: Scales, comptime d: Dimensions) f64 { - var factor: f64 = 1.0; - for (std.enums.values(Dimension)) |dim| { + comptime var factor: f64 = 1.0; + inline for (std.enums.values(Dimension)) |dim| { const power = d.get(dim); if (power == 0) continue;