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,238 @@
const std = @import("std");
const vm = @import("root");
pub const Vector3x8 = struct {
x: f32x8,
y: f32x8,
z: f32x8,
pub const Array = [24]f32;
pub const zero = initScalarSingle(0);
pub const one = initScalarSingle(1);
pub const unit_x = initSingle(1, 0, 0);
pub const unit_y = initSingle(0, 1, 0);
pub const unit_z = initSingle(0, 0, 1);
pub const unit_nx = initSingle(-1, 0, 0);
pub const unit_ny = initSingle(0, -1, 0);
pub const unit_nz = initSingle(0, 0, -1);
// --- INIT ----
pub inline fn init(x: f32x8, y: f32x8, z: f32x8) Vector3x8 {
return .{ .x = x, .y = y, .z = z };
}
pub inline fn initSingle(x: f32, y: f32, z: f32) Vector3x8 {
return .{ .x = ps(x), .y = ps(y), .z = ps(z) };
}
pub inline fn initScalar(scalar: f32x8) Vector3x8 {
return .{ .x = scalar, .y = scalar, .z = scalar };
}
pub inline fn initScalarSingle(scalar: f32) Vector3x8 {
return .{ .x = ps(scalar), .y = ps(scalar), .z = ps(scalar) };
}
pub inline fn initSplat(vector: Vector3) Vector3x8 {
return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z) };
}
pub inline fn initArray(array: Array) Vector3x8 {
const x: f32x8 = array[0..8].*;
const y: f32x8 = array[8..16].*;
const z: f32x8 = array[16..24].*;
return .{ .x = x, .y = y, .z = z };
}
pub inline fn initArrayTranspose(array: Array) Vector3x8 {
const vector: @Vector(24, f32) = array;
const x: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 });
const y: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 });
const z: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 });
return .{ .x = x, .y = y, .z = z };
}
pub inline fn initArrayOfVectors(vectors: [8]Vector3) Vector3x8 {
return initArrayTranspose(@bitCast(vectors));
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector3x8) Array {
const x: [8]f32 = self.x;
const y: [8]f32 = self.y;
const z: [8]f32 = self.z;
return x ++ y ++ z;
}
pub inline fn asArrayTranspose(self: Vector3x8) Array {
const vector: @Vector(24, f32) = self.asArray();
const transposed: @Vector(24, f32) = @shuffle(f32, vector, undefined, [_]i32{
0, 8, 16,
1, 9, 17,
2, 10, 18,
3, 11, 19,
4, 12, 20,
5, 13, 21,
6, 14, 22,
7, 15, 23,
});
return transposed;
}
pub inline fn asArrayOfVectors(self: Vector3x8) [8]Vector3 {
return @bitCast(self.asArrayTranspose());
}
pub inline fn unpack(self: Vector3x8) [3]f32x8 {
return .{ self.x, self.y, self.z };
}
// --- LOAD AND STORE ---
pub inline fn loadArray(self: *Vector3x8, array: *const Array) void {
self.x = array[0..8].*;
self.y = array[8..16].*;
self.z = array[16..24].*;
}
pub inline fn loadArrayTranspose(self: *Vector3x8, array: *const Array) void {
const vector: @Vector(24, f32) = array;
self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 });
self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 });
self.z = @shuffle(f32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 });
}
pub inline fn loadArrayOfVectors(self: *Vector3x8, vectors: *const [8]Vector3) void {
self.loadArrayTranspose(@ptrCast(vectors));
}
pub inline fn storeArray(self: *const Vector3x8, array: *Array) void {
array[0..8].* = self.x;
array[8..16].* = self.y;
array[16..24].* = self.z;
}
pub inline fn storeArrayTranspose(self: *const Vector3x8, array: *Array) void {
const vector: @Vector(24, f32) = self.asArray();
const transposed: @Vector(24, f32) = @shuffle(f32, vector, undefined, [_]i32{
0, 8, 16,
1, 9, 17,
2, 10, 18,
3, 11, 19,
4, 12, 20,
5, 13, 21,
6, 14, 22,
7, 15, 23,
});
array.* = transposed;
}
pub inline fn storeArrayOfVectors(self: *const Vector3x8, vectors: *[8]Vector3) void {
self.storeArrayTranspose(@ptrCast(vectors));
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z };
}
pub inline fn sub(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z };
}
pub inline fn mul(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z };
}
pub inline fn mulScalar(self: Vector3x8, scalar: f32x8) Vector3x8 {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
}
pub inline fn mulScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 {
return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar), .z = self.z * ps(scalar) };
}
pub inline fn div(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z };
}
pub inline fn divScalar(self: Vector3x8, scalar: f32x8) Vector3x8 {
return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar };
}
pub inline fn divScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 {
return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar), .z = self.z / ps(scalar) };
}
pub inline fn negate(self: Vector3x8) Vector3x8 {
return .{ .x = -self.x, .y = -self.y, .z = -self.z };
}
pub inline fn abs(self: Vector3x8) Vector3x8 {
return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) };
}
pub inline fn floor(self: Vector3x8) Vector3x8 {
return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) };
}
pub inline fn ceil(self: Vector3x8) Vector3x8 {
return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) };
}
pub inline fn round(self: Vector3x8) Vector3x8 {
return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) };
}
pub inline fn min(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) };
}
pub inline fn max(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) };
}
// --- OTHER ---
pub inline fn len(self: Vector3x8) f32x8 {
return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
}
pub inline fn lenSquared(self: Vector3x8) f32x8 {
return self.x * self.x + self.y * self.y + self.z * self.z;
}
pub inline fn dot(self: Vector3x8, other: Vector3x8) f32x8 {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
pub inline fn cross(self: Vector3x8, other: Vector3x8) Vector3x8 {
return .{
.x = self.y * other.z - self.z * other.y,
.y = self.z * other.x - self.x * other.z,
.z = self.x * other.y - self.y * other.x,
};
}
pub inline fn lerp(a: Vector3x8, b: Vector3x8, t: f32x8) Vector3x8 {
return .{
.x = @mulAdd(f32x8, t, b.x, @mulAdd(f32x8, -t, a.x, a.x)),
.y = @mulAdd(f32x8, t, b.y, @mulAdd(f32x8, -t, a.y, a.y)),
.z = @mulAdd(f32x8, t, b.z, @mulAdd(f32x8, -t, a.z, a.z)),
};
}
pub inline fn rotate(self: Vector3x8, quaternion: Quaternion_x8) Vector3x8 {
const quaternion_scalar = quaternion.getScalar();
const quaternion_vector = quaternion.getVector();
return .add(self, .cross(
.add(quaternion_vector, quaternion_vector),
.add(.cross(quaternion_vector, self), .mulScalar(self, quaternion_scalar)),
));
}
};