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,217 @@
const std = @import("std");
const vm = @import("root");
pub const Vector4Int_x8 = struct {
x: i32x8,
y: i32x8,
z: i32x8,
w: i32x8,
pub const Array = [32]i32;
pub const zero = initScalarSingle(0);
pub const one = initScalarSingle(1);
pub const unit_x = initSingle(1, 0, 0, 0);
pub const unit_y = initSingle(0, 1, 0, 0);
pub const unit_z = initSingle(0, 0, 1, 0);
pub const unit_w = initSingle(0, 0, 0, 1);
pub const unit_nx = initSingle(-1, 0, 0, 0);
pub const unit_ny = initSingle(0, -1, 0, 0);
pub const unit_nz = initSingle(0, 0, -1, 0);
pub const unit_nw = initSingle(0, 0, 0, -1);
// --- INIT ----
pub inline fn init(x: i32x8, y: i32x8, z: i32x8, w: i32x8) Vector4Int_x8 {
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initSingle(x: i32, y: i32, z: i32, w: i32) Vector4Int_x8 {
return .{ .x = epi32(x), .y = epi32(y), .z = epi32(z), .w = epi32(w) };
}
pub inline fn initScalar(scalar: i32x8) Vector4Int_x8 {
return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar };
}
pub inline fn initScalarSingle(scalar: i32) Vector4Int_x8 {
return .{ .x = epi32(scalar), .y = epi32(scalar), .z = epi32(scalar), .w = epi32(scalar) };
}
pub inline fn initSplat(vector: Vector4Int) Vector4Int_x8 {
return .{ .x = epi32(vector.x), .y = epi32(vector.y), .z = epi32(vector.z), .w = epi32(vector.w) };
}
pub inline fn initArray(array: Array) Vector4Int_x8 {
const x: i32x8 = array[0..8].*;
const y: i32x8 = array[8..16].*;
const z: i32x8 = array[16..24].*;
const w: i32x8 = array[24..32].*;
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initArrayTranspose(array: Array) Vector4Int_x8 {
const vector: @Vector(32, i32) = array;
const x: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 });
const y: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 });
const z: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 });
const w: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 });
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initArrayOfVectors(vectors: [8]Vector4Int) Vector4Int_x8 {
return initArrayTranspose(@bitCast(vectors));
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector4Int_x8) Array {
const x: [8]i32 = self.x;
const y: [8]i32 = self.y;
const z: [8]i32 = self.z;
const w: [8]i32 = self.w;
return x ++ y ++ z ++ w;
}
pub inline fn asArrayTranspose(self: Vector4Int_x8) Array {
const vector: @Vector(32, i32) = self.asArray();
const transposed: @Vector(32, i32) = @shuffle(i32, vector, undefined, [_]i32{
0, 8, 16, 24,
1, 9, 17, 25,
2, 10, 18, 26,
3, 11, 19, 27,
4, 12, 20, 28,
5, 13, 21, 29,
6, 14, 22, 30,
7, 15, 23, 31,
});
return transposed;
}
pub inline fn asArrayOfVectors(self: Vector4Int_x8) [8]Vector4Int {
return @bitCast(self.asArrayTranspose());
}
pub inline fn unpack(self: Vector4Int_x8) [4]i32x8 {
return .{ self.x, self.y, self.z, self.w };
}
// --- LOAD AND STORE ---
pub inline fn loadArray(self: *Vector4Int_x8, array: *const Array) void {
self.x = array[0..8].*;
self.y = array[8..16].*;
self.z = array[16..24].*;
self.w = array[24..32].*;
}
pub inline fn loadArrayTranspose(self: *Vector4Int_x8, array: *const Array) void {
const vector: @Vector(24, i32) = array;
self.x = @shuffle(i32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 });
self.y = @shuffle(i32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 });
self.z = @shuffle(i32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 });
self.w = @shuffle(i32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 });
}
pub inline fn loadArrayOfVectors(self: *Vector4Int_x8, vectors: *const [8]Vector4Int) void {
self.loadArrayTranspose(@ptrCast(vectors));
}
pub inline fn storeArray(self: *const Vector4Int_x8, array: *Array) void {
array[0..8].* = self.x;
array[8..16].* = self.y;
array[16..24].* = self.z;
array[24..32].* = self.w;
}
pub inline fn storeArrayTranspose(self: *const Vector4Int_x8, array: *Array) void {
const vector: @Vector(32, i32) = self.asArray();
const transposed: @Vector(32, i32) = @shuffle(i32, vector, undefined, [_]i32{
0, 8, 16, 24,
1, 9, 17, 25,
2, 10, 18, 26,
3, 11, 19, 27,
4, 12, 20, 28,
5, 13, 21, 29,
6, 14, 22, 30,
7, 15, 23, 31,
});
array.* = transposed;
}
pub inline fn storeArrayOfVectors(self: *const Vector4Int_x8, vectors: *[8]Vector4Int) void {
self.storeArrayTranspose(@ptrCast(vectors));
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z, .w = self.w + other.w };
}
pub inline fn sub(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z, .w = self.w - other.w };
}
pub inline fn mul(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z, .w = self.w * other.w };
}
pub inline fn mulScalar(self: Vector4Int_x8, scalar: i32x8) Vector4Int_x8 {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar };
}
pub inline fn mulScalarSingle(self: Vector4Int_x8, scalar: i32) Vector4Int_x8 {
return .{ .x = self.x * epi32(scalar), .y = self.y * epi32(scalar), .z = self.z * epi32(scalar), .w = self.w * epi32(scalar) };
}
pub inline fn div(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z), .w = @divFloor(self.w, other.w) };
}
pub inline fn divScalar(self: Vector4Int_x8, scalar: i32x8) Vector4Int_x8 {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar), .w = @divFloor(self.w, scalar) };
}
pub inline fn divScalarSingle(self: Vector4Int_x8, scalar: i32) Vector4Int_x8 {
return .{ .x = @divFloor(self.x, epi32(scalar)), .y = @divFloor(self.y, epi32(scalar)), .z = @divFloor(self.z, epi32(scalar)), .w = @divFloor(self.w, epi32(scalar)) };
}
pub inline fn mod(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z), .w = @mod(self.w, other.w) };
}
pub inline fn modScalar(self: Vector4Int_x8, scalar: i32x8) Vector4Int_x8 {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar), .w = @mod(self.w, scalar) };
}
pub inline fn modScalarSingle(self: Vector4Int_x8, scalar: i32) Vector4Int_x8 {
return .{ .x = @mod(self.x, epi32(scalar)), .y = @mod(self.y, epi32(scalar)), .z = @mod(self.z, epi32(scalar)), .w = @mod(self.w, epi32(scalar)) };
}
pub inline fn negate(self: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w };
}
pub inline fn abs(self: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)), .w = @intCast(@abs(self.w)) };
}
pub inline fn min(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z), .w = @min(self.w, other.w) };
}
pub inline fn max(self: Vector4Int_x8, other: Vector4Int_x8) Vector4Int_x8 {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z), .w = @max(self.w, other.w) };
}
// --- OTHER ---
pub inline fn lenSquared(self: Vector4Int_x8) i32x8 {
return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w;
}
pub inline fn dot(self: Vector4Int_x8, other: Vector4Int_x8) i32x8 {
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w;
}
};