Compare commits

...

4 Commits

Author SHA1 Message Date
31651dc96a Integer lerp and unlerp 2026-01-07 13:54:32 +01:00
34f0b1fb89 Vector int-float conversion methods 2026-01-06 21:14:12 +01:00
7963813034 Nothing 2026-01-06 21:13:16 +01:00
49cf6e4237 Move test 2026-01-06 14:41:15 +01:00
15 changed files with 104 additions and 30 deletions

View File

@@ -599,7 +599,7 @@ const import = struct {
pub const Allocator = struct {
const alignment_bytes = 16;
const alignment: std.mem.Alignment = .fromByteUnits(alignment_bytes);
const alignment = std.mem.Alignment.fromByteUnits(alignment_bytes);
const AllocationPtr = *align(alignment_bytes) anyopaque;
const AllocationSlice = []align(alignment_bytes) u8;

View File

@@ -63,6 +63,32 @@ pub const Color = extern struct {
@compileError("Invalid color literal: " ++ literal);
}
test l {
const i = Color.l("#012");
try std.testing.expectEqual(0x00, i.r);
try std.testing.expectEqual(0x11, i.g);
try std.testing.expectEqual(0x22, i.b);
try std.testing.expectEqual(0xFF, i.a);
const j = Color.l("#3456");
try std.testing.expectEqual(0x33, j.r);
try std.testing.expectEqual(0x44, j.g);
try std.testing.expectEqual(0x55, j.b);
try std.testing.expectEqual(0x66, j.a);
const k = Color.l("#F08040");
try std.testing.expectEqual(0xF0, k.r);
try std.testing.expectEqual(0x80, k.g);
try std.testing.expectEqual(0x40, k.b);
try std.testing.expectEqual(0xFF, k.a);
const m = Color.l("#20304050");
try std.testing.expectEqual(0x20, m.r);
try std.testing.expectEqual(0x30, m.g);
try std.testing.expectEqual(0x40, m.b);
try std.testing.expectEqual(0x50, m.a);
}
pub inline fn asArray(self: Color) Array {
return @bitCast(self);
}
@@ -71,29 +97,3 @@ pub const Color = extern struct {
try w.print("#{X:0>2}{X:0>2}{X:0>2}{X:0>2}", .{ self.r, self.g, self.b, self.a });
}
};
test "l" {
const i: Color = .l("#012");
try std.testing.expectEqual(0x00, i.r);
try std.testing.expectEqual(0x11, i.g);
try std.testing.expectEqual(0x22, i.b);
try std.testing.expectEqual(0xFF, i.a);
const j: Color = .l("#3456");
try std.testing.expectEqual(0x33, j.r);
try std.testing.expectEqual(0x44, j.g);
try std.testing.expectEqual(0x55, j.b);
try std.testing.expectEqual(0x66, j.a);
const k: Color = .l("#F08040");
try std.testing.expectEqual(0xF0, k.r);
try std.testing.expectEqual(0x80, k.g);
try std.testing.expectEqual(0x40, k.b);
try std.testing.expectEqual(0xFF, k.a);
const l: Color = .l("#20304050");
try std.testing.expectEqual(0x20, l.r);
try std.testing.expectEqual(0x30, l.g);
try std.testing.expectEqual(0x40, l.b);
try std.testing.expectEqual(0x50, l.a);
}

View File

@@ -78,8 +78,34 @@ pub const cossin_x8 = trig.cossin_x8;
// -----------------------------------------------------------------------------
pub inline fn lerp(comptime T: type, a: T, b: T, t: T) T {
return @mulAdd(T, t, b, @mulAdd(T, -t, a, a));
pub inline fn lerp(a: f32, b: f32, t: f32) f32 {
return @mulAdd(f32, t, b, @mulAdd(f32, -t, a, a));
}
pub inline fn lerpInt(a: i32, b: i32, t: f32) i32 {
const ab = b - a;
const ab_float: f32 = @floatFromInt(ab);
const d: i32 = @intFromFloat(@round(t * ab_float));
return a + d;
}
pub inline fn lerpInt64(a: i64, b: i64, t: f32) i64 {
const ab = b - a;
const ab_float: f32 = @floatFromInt(ab);
const d: i64 = @intFromFloat(@round(t * ab_float));
return a + d;
}
pub inline fn unlerp(a: f32, b: f32, x: f32) f32 {
return (x - a) / (b - a);
}
pub inline fn unlerpInt(a: i32, b: i32, x: i32) f32 {
return @as(f32, @floatFromInt(x - a)) / @as(f32, @floatFromInt(b - a));
}
pub inline fn unlerpInt64(a: i64, b: i64, x: i64) f32 {
return @as(f32, @floatFromInt(x - a)) / @as(f32, @floatFromInt(b - a));
}
test "refAllDecls" {

View File

@@ -34,6 +34,10 @@ pub const Vector2 = extern struct {
return @bitCast(self);
}
pub inline fn asInt(self: Vector2) vm.Vector2Int {
return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y) };
}
pub inline fn withZ(self: Vector2, z: f32) vm.Vector3 {
return .{ .x = self.x, .y = self.y, .z = z };
}

View File

@@ -34,6 +34,10 @@ pub const Vector2Int = extern struct {
return @bitCast(self);
}
pub inline fn asFloat(self: Vector2Int) vm.Vector2 {
return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y) };
}
pub inline fn withZ(self: Vector2Int, z: i32) vm.Vector3Int {
return .{ .x = self.x, .y = self.y, .z = z };
}

View File

@@ -44,6 +44,10 @@ pub const Vector2Int_x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asFloat(self: Vector2Int_x8) vm.Vector2x8 {
return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y) };
}
pub inline fn withZ(self: Vector2Int_x8, z: vm.i32x8) vm.Vector3Int_x8 {
return .{ .x = self.x, .y = self.y, .z = z };
}

View File

@@ -44,6 +44,10 @@ pub const Vector2x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asInt(self: Vector2x8) vm.Vector2Int_x8 {
return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y) };
}
pub inline fn withZ(self: Vector2x8, z: vm.f32x8) vm.Vector3x8 {
return .{ .x = self.x, .y = self.y, .z = z };
}

View File

@@ -37,6 +37,10 @@ pub const Vector3 = extern struct {
return @bitCast(self);
}
pub inline fn asInt(self: Vector3) vm.Vector3Int {
return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z) };
}
pub inline fn dropZ(self: Vector3) vm.Vector2 {
return .{ .x = self.x, .y = self.y };
}
@@ -145,7 +149,7 @@ pub const Vector3 = extern struct {
pub inline fn rotate_x8(self: Vector3, quaternion: vm.Quaternion_x8) vm.Vector3x8 {
const w = quaternion.getScalar();
const xyz = quaternion.getVector();
const self_x8: vm.Vector3x8 = .splat(self);
const self_x8 = vm.Vector3x8.splat(self);
return .add(
self_x8,

View File

@@ -37,6 +37,10 @@ pub const Vector3Int = extern struct {
return @bitCast(self);
}
pub inline fn asFloat(self: Vector3Int) vm.Vector3 {
return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z) };
}
pub inline fn dropZ(self: Vector3Int) vm.Vector2Int {
return .{ .x = self.x, .y = self.y };
}

View File

@@ -48,6 +48,10 @@ pub const Vector3Int_x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asFloat(self: Vector3Int_x8) vm.Vector3x8 {
return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z) };
}
pub inline fn dropZ(self: Vector3Int_x8) vm.Vector2Int_x8 {
return .{ .x = self.x, .y = self.y };
}

View File

@@ -48,6 +48,10 @@ pub const Vector3x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asInt(self: Vector3x8) vm.Vector3Int_x8 {
return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z) };
}
pub inline fn dropZ(self: Vector3x8) vm.Vector2x8 {
return .{ .x = self.x, .y = self.y };
}

View File

@@ -36,6 +36,10 @@ pub const Vector4 = extern struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asInt(self: Vector4) vm.Vector4Int {
return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z), .w = @intFromFloat(self.w) };
}
pub inline fn asArray(self: Vector4) Array {
return @bitCast(self);
}

View File

@@ -40,6 +40,10 @@ pub const Vector4Int = extern struct {
return @bitCast(self);
}
pub inline fn asFloat(self: Vector4Int) vm.Vector4 {
return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z), .w = @floatFromInt(self.w) };
}
pub inline fn dropW(self: Vector4Int) vm.Vector3Int {
return .{ .x = self.x, .y = self.y, .z = self.z };
}

View File

@@ -52,6 +52,10 @@ pub const Vector4Int_x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asFloat(self: Vector4Int_x8) vm.Vector4x8 {
return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z), .w = @floatFromInt(self.w) };
}
pub inline fn dropW(self: Vector4Int_x8) vm.Vector3Int_x8 {
return .{ .x = self.x, .y = self.y, .z = self.z };
}

View File

@@ -52,6 +52,10 @@ pub const Vector4x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn asInt(self: Vector4x8) vm.Vector4Int_x8 {
return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z), .w = @intFromFloat(self.w) };
}
pub inline fn dropW(self: Vector4x8) vm.Vector3x8 {
return .{ .x = self.x, .y = self.y, .z = self.z };
}