vecmath file split mayhem

This commit is contained in:
2026-01-02 00:58:21 +01:00
parent fd16e5a2b0
commit b09200b7ab
24 changed files with 2844 additions and 2632 deletions

View File

@@ -0,0 +1,186 @@
const std = @import("std");
const vm = @import("root");
pub const Vector2Int_x8 = struct {
x: i32x8,
y: i32x8,
pub const Array = [16]i32;
pub const zero = initScalarSingle(0);
pub const one = initScalarSingle(1);
pub const unit_x = initSingle(1, 0);
pub const unit_y = initSingle(0, 1);
pub const unit_nx = initSingle(-1, 0);
pub const unit_ny = initSingle(0, -1);
// --- INIT ----
pub inline fn init(x: i32x8, y: i32x8) Vector2Int_x8 {
return .{ .x = x, .y = y };
}
pub inline fn initSingle(x: i32, y: i32) Vector2Int_x8 {
return .{ .x = epi32(x), .y = epi32(y) };
}
pub inline fn initScalar(scalar: i32x8) Vector2Int_x8 {
return .{ .x = scalar, .y = scalar };
}
pub inline fn initScalarSingle(scalar: i32) Vector2Int_x8 {
return .{ .x = epi32(scalar), .y = epi32(scalar) };
}
pub inline fn initSplat(vector: Vector2Int) Vector2Int_x8 {
return .{ .x = epi32(vector.x), .y = epi32(vector.y) };
}
pub inline fn initArray(array: Array) Vector2Int_x8 {
const x: i32x8 = array[0..8].*;
const y: i32x8 = array[8..16].*;
return .{ .x = x, .y = y };
}
pub inline fn initArrayTranspose(array: Array) Vector2Int_x8 {
const a: i32x8 = array[0..8].*;
const b: i32x8 = array[8..16].*;
const x: i32x8 = @shuffle(i32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) });
const y: i32x8 = @shuffle(i32, a, b, [_]i32{ 1, 3, 5, 7, ~@as(i32, 1), ~@as(i32, 3), ~@as(i32, 5), ~@as(i32, 7) });
return .{ .x = x, .y = y };
}
pub inline fn initArrayOfVectors(vectors: [8]Vector2Int) Vector2Int_x8 {
return initArrayTranspose(@bitCast(vectors));
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector2Int_x8) Array {
const x: [8]i32 = self.x;
const y: [8]i32 = self.y;
return x ++ y;
}
pub inline fn asArrayTranspose(self: Vector2Int_x8) Array {
const a = @shuffle(i32, self.x, self.y, [_]i32{ 0, ~@as(i32, 0), 1, ~@as(i32, 1), 2, ~@as(i32, 2), 3, ~@as(i32, 3) });
const b = @shuffle(i32, self.x, self.y, [_]i32{ 4, ~@as(i32, 4), 5, ~@as(i32, 5), 6, ~@as(i32, 6), 7, ~@as(i32, 7) });
return a ++ b;
}
pub inline fn asArrayOfVectors(self: Vector2Int_x8) [8]Vector2Int {
return @bitCast(self.asArrayTranspose());
}
pub inline fn unpack(self: Vector2Int_x8) [2]i32x8 {
return .{ self.x, self.y };
}
// --- LOAD AND STORE ---
pub inline fn loadArray(self: *Vector2Int_x8, array: *const Array) void {
self.x = array[0..8].*;
self.y = array[8..16].*;
}
pub inline fn loadArrayTranspose(self: *Vector2Int_x8, array: *const Array) void {
const a: i32x8 = array[0..8].*;
const b: i32x8 = array[8..16].*;
self.x = @shuffle(i32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) });
self.y = @shuffle(i32, a, b, [_]i32{ 1, 3, 5, 7, ~@as(i32, 1), ~@as(i32, 3), ~@as(i32, 5), ~@as(i32, 7) });
}
pub inline fn loadArrayOfVectors(self: *Vector2Int_x8, vectors: *const [8]Vector2Int) void {
self.loadArrayTranspose(@ptrCast(vectors));
}
pub inline fn storeArray(self: *const Vector2Int_x8, array: *Array) void {
array[0..8].* = self.x;
array[8..16].* = self.y;
}
pub inline fn storeArrayTranspose(self: *const Vector2Int_x8, array: *Array) void {
array[0..8].* = @shuffle(i32, self.x, self.y, [_]i32{ 0, ~@as(i32, 0), 1, ~@as(i32, 1), 2, ~@as(i32, 2), 3, ~@as(i32, 3) });
array[8..16].* = @shuffle(i32, self.x, self.y, [_]i32{ 4, ~@as(i32, 4), 5, ~@as(i32, 5), 6, ~@as(i32, 6), 7, ~@as(i32, 7) });
}
pub inline fn storeArrayOfVectors(self: *const Vector2Int_x8, vectors: *[8]Vector2Int) void {
self.storeArrayTranspose(@ptrCast(vectors));
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = self.x + other.x, .y = self.y + other.y };
}
pub inline fn sub(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = self.x - other.x, .y = self.y - other.y };
}
pub inline fn mul(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = self.x * other.x, .y = self.y * other.y };
}
pub inline fn mulScalar(self: Vector2Int_x8, scalar: i32x8) Vector2Int_x8 {
return .{ .x = self.x * scalar, .y = self.y * scalar };
}
pub inline fn mulScalarSingle(self: Vector2Int_x8, scalar: i32) Vector2Int_x8 {
return .{ .x = self.x * epi32(scalar), .y = self.y * epi32(scalar) };
}
pub inline fn div(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y) };
}
pub inline fn divScalar(self: Vector2Int_x8, scalar: i32x8) Vector2Int_x8 {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar) };
}
pub inline fn divScalarSingle(self: Vector2Int_x8, scalar: i32) Vector2Int_x8 {
return .{ .x = @divFloor(self.x, epi32(scalar)), .y = @divFloor(self.y, epi32(scalar)) };
}
pub inline fn mod(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y) };
}
pub inline fn modScalar(self: Vector2Int_x8, scalar: i32x8) Vector2Int_x8 {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar) };
}
pub inline fn modScalarSingle(self: Vector2Int_x8, scalar: i32) Vector2Int_x8 {
return .{ .x = @mod(self.x, epi32(scalar)), .y = @mod(self.y, epi32(scalar)) };
}
pub inline fn negate(self: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = -self.x, .y = -self.y };
}
pub inline fn abs(self: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)) };
}
pub inline fn min(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) };
}
pub inline fn max(self: Vector2Int_x8, other: Vector2Int_x8) Vector2Int_x8 {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) };
}
// --- OTHER ---
pub inline fn lenSquared(self: Vector2Int_x8) i32x8 {
return self.x * self.x + self.y * self.y;
}
pub inline fn dot(self: Vector2Int_x8, other: Vector2Int_x8) i32x8 {
return self.x * other.x + self.y * other.y;
}
pub inline fn cross(self: Vector2Int_x8, other: Vector2Int_x8) i32x8 {
return self.x * other.y - self.y * other.x;
}
};