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,131 @@
const std = @import("std");
const vm = @import("root");
pub const Vector2 = extern struct {
x: f32,
y: f32,
pub const Array = [2]f32;
pub const zero = initScalar(0);
pub const one = initScalar(1);
pub const unit_x = init(1, 0);
pub const unit_y = init(0, 1);
pub const unit_nx = init(-1, 0);
pub const unit_ny = init(0, -1);
// --- INIT ----
pub inline fn init(x: f32, y: f32) Vector2 {
return .{ .x = x, .y = y };
}
pub inline fn initScalar(scalar: f32) Vector2 {
return .{ .x = scalar, .y = scalar };
}
pub inline fn initArray(array: Array) Vector2 {
return @bitCast(array);
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector2) Array {
return @bitCast(self);
}
pub inline fn asArrayPtr(self: *Vector2) *Array {
return @ptrCast(self);
}
pub inline fn asArrayConstPtr(self: *const Vector2) *const Array {
return @ptrCast(self);
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector2, other: Vector2) Vector2 {
return .{ .x = self.x + other.x, .y = self.y + other.y };
}
pub inline fn sub(self: Vector2, other: Vector2) Vector2 {
return .{ .x = self.x - other.x, .y = self.y - other.y };
}
pub inline fn mul(self: Vector2, other: Vector2) Vector2 {
return .{ .x = self.x * other.x, .y = self.y * other.y };
}
pub inline fn mulScalar(self: Vector2, scalar: f32) Vector2 {
return .{ .x = self.x * scalar, .y = self.y * scalar };
}
pub inline fn div(self: Vector2, other: Vector2) Vector2 {
return .{ .x = self.x / other.x, .y = self.y / other.y };
}
pub inline fn divScalar(self: Vector2, scalar: f32) Vector2 {
return .{ .x = self.x / scalar, .y = self.y / scalar };
}
pub inline fn negate(self: Vector2) Vector2 {
return .{ .x = -self.x, .y = -self.y };
}
pub inline fn abs(self: Vector2) Vector2 {
return .{ .x = @abs(self.x), .y = @abs(self.y) };
}
pub inline fn floor(self: Vector2) Vector2 {
return .{ .x = @floor(self.x), .y = @floor(self.y) };
}
pub inline fn ceil(self: Vector2) Vector2 {
return .{ .x = @ceil(self.x), .y = @ceil(self.y) };
}
pub inline fn round(self: Vector2) Vector2 {
return .{ .x = @round(self.x), .y = @round(self.y) };
}
pub inline fn min(self: Vector2, other: Vector2) Vector2 {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) };
}
pub inline fn max(self: Vector2, other: Vector2) Vector2 {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) };
}
// --- OTHER ---
pub inline fn len(self: Vector2) f32 {
return @sqrt(self.x * self.x + self.y * self.y);
}
pub inline fn lenSquared(self: Vector2) f32 {
return self.x * self.x + self.y * self.y;
}
pub inline fn dot(self: Vector2, other: Vector2) f32 {
return self.x * other.x + self.y * other.y;
}
pub inline fn cross(self: Vector2, other: Vector2) f32 {
return self.x * other.y - self.y * other.x;
}
pub inline fn lerp(a: Vector2, b: Vector2, t: f32) Vector2 {
return .{
.x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)),
.y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)),
};
}
pub inline fn rotate(self: Vector2, angle_turns: f32) Vector2 {
const c, const s = cossin(angle_turns).asArray();
return .{
.x = self.x * c - self.y * s,
.y = self.x * s + self.y * c,
};
}
};

View File

@@ -0,0 +1,108 @@
const std = @import("std");
const vm = @import("root");
pub const Vector2Int = extern struct {
x: i32,
y: i32,
pub const Array = [2]i32;
pub const zero = initScalar(0);
pub const one = initScalar(1);
pub const unit_x = init(1, 0);
pub const unit_y = init(0, 1);
pub const unit_nx = init(-1, 0);
pub const unit_ny = init(0, -1);
// --- INIT ----
pub inline fn init(x: i32, y: i32) Vector2Int {
return .{ .x = x, .y = y };
}
pub inline fn initScalar(scalar: i32) Vector2Int {
return .{ .x = scalar, .y = scalar };
}
pub inline fn initArray(array: Array) Vector2Int {
return @bitCast(array);
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector2Int) Array {
return @bitCast(self);
}
pub inline fn asArrayPtr(self: *Vector2Int) *Array {
return @ptrCast(self);
}
pub inline fn asArrayConstPtr(self: *const Vector2Int) *const Array {
return @ptrCast(self);
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = self.x + other.x, .y = self.y + other.y };
}
pub inline fn sub(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = self.x - other.x, .y = self.y - other.y };
}
pub inline fn mul(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = self.x * other.x, .y = self.y * other.y };
}
pub inline fn mulScalar(self: Vector2Int, scalar: f32) Vector2Int {
return .{ .x = self.x * scalar, .y = self.y * scalar };
}
pub inline fn div(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y) };
}
pub inline fn divScalar(self: Vector2Int, scalar: f32) Vector2Int {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar) };
}
pub inline fn mod(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y) };
}
pub inline fn modScalar(self: Vector2Int, scalar: f32) Vector2Int {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar) };
}
pub inline fn negate(self: Vector2Int) Vector2Int {
return .{ .x = -self.x, .y = -self.y };
}
pub inline fn abs(self: Vector2Int) Vector2Int {
return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)) };
}
pub inline fn min(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) };
}
pub inline fn max(self: Vector2Int, other: Vector2Int) Vector2Int {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) };
}
// --- OTHER ---
pub inline fn lenSquared(self: Vector2Int) i32 {
return self.x * self.x + self.y * self.y;
}
pub inline fn dot(self: Vector2Int, other: Vector2Int) i32 {
return self.x * other.x + self.y * other.y;
}
pub inline fn cross(self: Vector2Int, other: Vector2Int) i32 {
return self.x * other.y - self.y * other.x;
}
};

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;
}
};

View File

@@ -0,0 +1,205 @@
const std = @import("std");
const vm = @import("root");
pub const Vector2x8 = struct {
x: f32x8,
y: f32x8,
pub const Array = [16]f32;
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: f32x8, y: f32x8) Vector2x8 {
return .{ .x = x, .y = y };
}
pub inline fn initSingle(x: f32, y: f32) Vector2x8 {
return .{ .x = ps(x), .y = ps(y) };
}
pub inline fn initScalar(scalar: f32x8) Vector2x8 {
return .{ .x = scalar, .y = scalar };
}
pub inline fn initScalarSingle(scalar: f32) Vector2x8 {
return .{ .x = ps(scalar), .y = ps(scalar) };
}
pub inline fn initSplat(vector: Vector2) Vector2x8 {
return .{ .x = ps(vector.x), .y = ps(vector.y) };
}
pub inline fn initArray(array: Array) Vector2x8 {
const x: f32x8 = array[0..8].*;
const y: f32x8 = array[8..16].*;
return .{ .x = x, .y = y };
}
pub inline fn initArrayTranspose(array: Array) Vector2x8 {
const a: f32x8 = array[0..8].*;
const b: f32x8 = array[8..16].*;
const x: f32x8 = @shuffle(f32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) });
const y: f32x8 = @shuffle(f32, 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]Vector2) Vector2x8 {
return initArrayTranspose(@bitCast(vectors));
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector2x8) Array {
const x: [8]f32 = self.x;
const y: [8]f32 = self.y;
return x ++ y;
}
pub inline fn asArrayTranspose(self: Vector2x8) Array {
const a = @shuffle(f32, self.x, self.y, [_]i32{ 0, ~@as(i32, 0), 1, ~@as(i32, 1), 2, ~@as(i32, 2), 3, ~@as(i32, 3) });
const b = @shuffle(f32, 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: Vector2x8) [8]Vector2 {
return @bitCast(self.asArrayTranspose());
}
pub inline fn unpack(self: Vector2x8) [2]f32x8 {
return .{ self.x, self.y };
}
// --- LOAD AND STORE ---
pub inline fn loadArray(self: *Vector2x8, array: *const Array) void {
self.x = array[0..8].*;
self.y = array[8..16].*;
}
pub inline fn loadArrayTranspose(self: *Vector2x8, array: *const Array) void {
const a: f32x8 = array[0..8].*;
const b: f32x8 = array[8..16].*;
self.x = @shuffle(f32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) });
self.y = @shuffle(f32, a, b, [_]i32{ 1, 3, 5, 7, ~@as(i32, 1), ~@as(i32, 3), ~@as(i32, 5), ~@as(i32, 7) });
}
pub inline fn loadArrayOfVectors(self: *Vector2x8, vectors: *const [8]Vector2) void {
self.loadArrayTranspose(@ptrCast(vectors));
}
pub inline fn storeArray(self: *const Vector2x8, array: *Array) void {
array[0..8].* = self.x;
array[8..16].* = self.y;
}
pub inline fn storeArrayTranspose(self: *const Vector2x8, array: *Array) void {
array[0..8].* = @shuffle(f32, 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(f32, 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 Vector2x8, vectors: *[8]Vector2) void {
self.storeArrayTranspose(@ptrCast(vectors));
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector2x8, other: Vector2x8) Vector2x8 {
return .{ .x = self.x + other.x, .y = self.y + other.y };
}
pub inline fn sub(self: Vector2x8, other: Vector2x8) Vector2x8 {
return .{ .x = self.x - other.x, .y = self.y - other.y };
}
pub inline fn mul(self: Vector2x8, other: Vector2x8) Vector2x8 {
return .{ .x = self.x * other.x, .y = self.y * other.y };
}
pub inline fn mulScalar(self: Vector2x8, scalar: f32x8) Vector2x8 {
return .{ .x = self.x * scalar, .y = self.y * scalar };
}
pub inline fn mulScalarSingle(self: Vector2x8, scalar: f32) Vector2x8 {
return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar) };
}
pub inline fn div(self: Vector2x8, other: Vector2x8) Vector2x8 {
return .{ .x = self.x / other.x, .y = self.y / other.y };
}
pub inline fn divScalar(self: Vector2x8, scalar: f32x8) Vector2x8 {
return .{ .x = self.x / scalar, .y = self.y / scalar };
}
pub inline fn divScalarSingle(self: Vector2x8, scalar: f32) Vector2x8 {
return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar) };
}
pub inline fn negate(self: Vector2x8) Vector2x8 {
return .{ .x = -self.x, .y = -self.y };
}
pub inline fn abs(self: Vector2x8) Vector2x8 {
return .{ .x = @abs(self.x), .y = @abs(self.y) };
}
pub inline fn floor(self: Vector2x8) Vector2x8 {
return .{ .x = @floor(self.x), .y = @floor(self.y) };
}
pub inline fn ceil(self: Vector2x8) Vector2x8 {
return .{ .x = @ceil(self.x), .y = @ceil(self.y) };
}
pub inline fn round(self: Vector2x8) Vector2x8 {
return .{ .x = @round(self.x), .y = @round(self.y) };
}
pub inline fn min(self: Vector2x8, other: Vector2x8) Vector2x8 {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) };
}
pub inline fn max(self: Vector2x8, other: Vector2x8) Vector2x8 {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) };
}
// --- OTHER ---
pub inline fn len(self: Vector2x8) f32x8 {
return @sqrt(self.x * self.x + self.y * self.y);
}
pub inline fn lenSquared(self: Vector2x8) f32x8 {
return self.x * self.x + self.y * self.y;
}
pub inline fn dot(self: Vector2x8, other: Vector2x8) f32x8 {
return self.x * other.x + self.y * other.y;
}
pub inline fn cross(self: Vector2x8, other: Vector2x8) f32x8 {
return self.x * other.y - self.y * other.x;
}
pub inline fn lerp(a: Vector2x8, b: Vector2x8, t: f32x8) Vector2x8 {
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)),
};
}
pub inline fn rotate(self: Vector2x8, angle_turns: f32x8) Vector2x8 {
const c, const s = cossin_x8(angle_turns).unpack();
return .{
.x = self.x * c - self.y * s,
.y = self.x * s + self.y * c,
};
}
};

View File

@@ -0,0 +1,141 @@
const std = @import("std");
const vm = @import("root");
pub const Vector3 = extern struct {
x: f32,
y: f32,
z: f32,
pub const Array = [3]f32;
pub const zero = initScalar(0);
pub const one = initScalar(1);
pub const unit_x = init(1, 0, 0);
pub const unit_y = init(0, 1, 0);
pub const unit_z = init(0, 0, 1);
pub const unit_nx = init(-1, 0, 0);
pub const unit_ny = init(0, -1, 0);
pub const unit_nz = init(0, 0, -1);
// --- INIT ----
pub inline fn init(x: f32, y: f32, z: f32) Vector3 {
return .{ .x = x, .y = y, .z = z };
}
pub inline fn initScalar(scalar: f32) Vector3 {
return .{ .x = scalar, .y = scalar, .z = scalar };
}
pub inline fn initArray(array: Array) Vector3 {
return @bitCast(array);
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector3) Array {
return @bitCast(self);
}
pub inline fn asArrayPtr(self: *Vector3) *Array {
return @ptrCast(self);
}
pub inline fn asArrayConstPtr(self: *const Vector3) *const Array {
return @ptrCast(self);
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector3, other: Vector3) Vector3 {
return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z };
}
pub inline fn sub(self: Vector3, other: Vector3) Vector3 {
return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z };
}
pub inline fn mul(self: Vector3, other: Vector3) Vector3 {
return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z };
}
pub inline fn mulScalar(self: Vector3, scalar: f32) Vector3 {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
}
pub inline fn div(self: Vector3, other: Vector3) Vector3 {
return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z };
}
pub inline fn divScalar(self: Vector3, scalar: f32) Vector3 {
return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar };
}
pub inline fn negate(self: Vector3) Vector3 {
return .{ .x = -self.x, .y = -self.y, .z = -self.z };
}
pub inline fn abs(self: Vector3) Vector3 {
return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) };
}
pub inline fn floor(self: Vector3) Vector3 {
return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) };
}
pub inline fn ceil(self: Vector3) Vector3 {
return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) };
}
pub inline fn round(self: Vector3) Vector3 {
return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) };
}
pub inline fn min(self: Vector3, other: Vector3) Vector3 {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) };
}
pub inline fn max(self: Vector3, other: Vector3) Vector3 {
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: Vector3) f32 {
return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
}
pub inline fn lenSquared(self: Vector3) f32 {
return self.x * self.x + self.y * self.y + self.z * self.z;
}
pub inline fn dot(self: Vector3, other: Vector3) f32 {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
pub inline fn cross(self: Vector3, other: Vector3) Vector3 {
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: Vector3, b: Vector3, t: f32) Vector3 {
return .{
.x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)),
.y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)),
.z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)),
};
}
pub inline fn rotate(self: Vector3, quaternion: Quaternion) Vector3 {
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)),
));
}
};

View File

@@ -0,0 +1,115 @@
const std = @import("std");
const vm = @import("root");
pub const Vector3Int = extern struct {
x: i32,
y: i32,
z: i32,
pub const Array = [3]i32;
pub const zero = initScalar(0);
pub const one = initScalar(1);
pub const unit_x = init(1, 0, 0);
pub const unit_y = init(0, 1, 0);
pub const unit_z = init(0, 0, 1);
pub const unit_nx = init(-1, 0, 0);
pub const unit_ny = init(0, -1, 0);
pub const unit_nz = init(0, 0, -1);
// --- INIT ----
pub inline fn init(x: i32, y: i32, z: i32) Vector3Int {
return .{ .x = x, .y = y, .z = z };
}
pub inline fn initScalar(scalar: i32) Vector3Int {
return .{ .x = scalar, .y = scalar, .z = scalar };
}
pub inline fn initArray(array: Array) Vector3Int {
return @bitCast(array);
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector3Int) Array {
return @bitCast(self);
}
pub inline fn asArrayPtr(self: *Vector3Int) *Array {
return @ptrCast(self);
}
pub inline fn asArrayConstPtr(self: *const Vector3Int) *const Array {
return @ptrCast(self);
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z };
}
pub inline fn sub(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z };
}
pub inline fn mul(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z };
}
pub inline fn mulScalar(self: Vector3Int, scalar: f32) Vector3Int {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
}
pub inline fn div(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z) };
}
pub inline fn divScalar(self: Vector3Int, scalar: f32) Vector3Int {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) };
}
pub inline fn mod(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z) };
}
pub inline fn modScalar(self: Vector3Int, scalar: f32) Vector3Int {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) };
}
pub inline fn negate(self: Vector3Int) Vector3Int {
return .{ .x = -self.x, .y = -self.y, .z = -self.z };
}
pub inline fn abs(self: Vector3Int) Vector3Int {
return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)) };
}
pub inline fn min(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) };
}
pub inline fn max(self: Vector3Int, other: Vector3Int) Vector3Int {
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) };
}
// --- OTHER ---
pub inline fn lenSquared(self: Vector3Int) i32 {
return self.x * self.x + self.y * self.y + self.z * self.z;
}
pub inline fn dot(self: Vector3Int, other: Vector3Int) i32 {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
pub inline fn cross(self: Vector3Int, other: Vector3Int) Vector3Int {
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,
};
}
};

View File

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

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)),
));
}
};

View File

@@ -0,0 +1,127 @@
const std = @import("std");
const vm = @import("root");
pub const Vector4 = extern struct {
x: f32,
y: f32,
z: f32,
w: f32,
pub const Array = [4]f32;
pub const zero = initScalar(0);
pub const one = initScalar(1);
pub const unit_x = init(1, 0, 0, 0);
pub const unit_y = init(0, 1, 0, 0);
pub const unit_z = init(0, 0, 1, 0);
pub const unit_w = init(0, 0, 0, 1);
pub const unit_nx = init(-1, 0, 0, 0);
pub const unit_ny = init(0, -1, 0, 0);
pub const unit_nz = init(0, 0, -1, 0);
pub const unit_nw = init(0, 0, 0, -1);
// --- INIT ----
pub inline fn init(x: f32, y: f32, z: f32, w: f32) Vector4 {
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initScalar(scalar: f32) Vector4 {
return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar };
}
pub inline fn initArray(array: Array) Vector4 {
return @bitCast(array);
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector4) Array {
return @bitCast(self);
}
pub inline fn asArrayPtr(self: *Vector4) *Array {
return @ptrCast(self);
}
pub inline fn asArrayConstPtr(self: *const Vector4) *const Array {
return @ptrCast(self);
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector4, other: Vector4) Vector4 {
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: Vector4, other: Vector4) Vector4 {
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: Vector4, other: Vector4) Vector4 {
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: Vector4, scalar: f32) Vector4 {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar };
}
pub inline fn div(self: Vector4, other: Vector4) Vector4 {
return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w };
}
pub inline fn divScalar(self: Vector4, scalar: f32) Vector4 {
return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar };
}
pub inline fn negate(self: Vector4) Vector4 {
return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w };
}
pub inline fn abs(self: Vector4) Vector4 {
return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) };
}
pub inline fn floor(self: Vector4) Vector4 {
return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) };
}
pub inline fn ceil(self: Vector4) Vector4 {
return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) };
}
pub inline fn round(self: Vector4) Vector4 {
return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) };
}
pub inline fn min(self: Vector4, other: Vector4) Vector4 {
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: Vector4, other: Vector4) Vector4 {
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 len(self: Vector4) f32 {
return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w);
}
pub inline fn lenSquared(self: Vector4) f32 {
return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w;
}
pub inline fn dot(self: Vector4, other: Vector4) f32 {
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w;
}
pub inline fn lerp(a: Vector4, b: Vector4, t: f32) Vector4 {
return .{
.x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)),
.y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)),
.z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)),
.w = @mulAdd(f32, t, b.w, @mulAdd(f32, -t, a.w, a.w)),
};
}
};

View File

@@ -0,0 +1,110 @@
const std = @import("std");
const vm = @import("root");
pub const Vector4Int = extern struct {
x: i32,
y: i32,
z: i32,
w: i32,
pub const Array = [4]i32;
pub const zero = initScalar(0);
pub const one = initScalar(1);
pub const unit_x = init(1, 0, 0, 0);
pub const unit_y = init(0, 1, 0, 0);
pub const unit_z = init(0, 0, 1, 0);
pub const unit_w = init(0, 0, 0, 1);
pub const unit_nx = init(-1, 0, 0, 0);
pub const unit_ny = init(0, -1, 0, 0);
pub const unit_nz = init(0, 0, -1, 0);
pub const unit_nw = init(0, 0, 0, -1);
// --- INIT ----
pub inline fn init(x: i32, y: i32, z: i32, w: i32) Vector4Int {
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initScalar(scalar: i32) Vector4Int {
return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar };
}
pub inline fn initArray(array: Array) Vector4Int {
return @bitCast(array);
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector4Int) Array {
return @bitCast(self);
}
pub inline fn asArrayPtr(self: *Vector4Int) *Array {
return @ptrCast(self);
}
pub inline fn asArrayConstPtr(self: *const Vector4Int) *const Array {
return @ptrCast(self);
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector4Int, other: Vector4Int) Vector4Int {
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, other: Vector4Int) Vector4Int {
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, other: Vector4Int) Vector4Int {
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, scalar: f32) Vector4Int {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar };
}
pub inline fn div(self: Vector4Int, other: Vector4Int) Vector4Int {
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, scalar: f32) Vector4Int {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar), .w = @divFloor(self.w, scalar) };
}
pub inline fn mod(self: Vector4Int, other: Vector4Int) Vector4Int {
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, scalar: f32) Vector4Int {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar), .w = @mod(self.w, scalar) };
}
pub inline fn negate(self: Vector4Int) Vector4Int {
return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w };
}
pub inline fn abs(self: Vector4Int) Vector4Int {
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, other: Vector4Int) Vector4Int {
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, other: Vector4Int) Vector4Int {
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) i32 {
return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w;
}
pub inline fn dot(self: Vector4Int, other: Vector4Int) i32 {
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w;
}
};

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;
}
};

View File

@@ -0,0 +1,230 @@
const std = @import("std");
const vm = @import("root");
pub const Vector4x8 = struct {
x: f32x8,
y: f32x8,
z: f32x8,
w: f32x8,
pub const Array = [32]f32;
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: f32x8, y: f32x8, z: f32x8, w: f32x8) Vector4x8 {
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initSingle(x: f32, y: f32, z: f32, w: f32) Vector4x8 {
return .{ .x = ps(x), .y = ps(y), .z = ps(z), .w = ps(w) };
}
pub inline fn initScalar(scalar: f32x8) Vector4x8 {
return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar };
}
pub inline fn initScalarSingle(scalar: f32) Vector4x8 {
return .{ .x = ps(scalar), .y = ps(scalar), .z = ps(scalar), .w = ps(scalar) };
}
pub inline fn initSplat(vector: Vector4) Vector4x8 {
return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z), .w = ps(vector.w) };
}
pub inline fn initArray(array: Array) Vector4x8 {
const x: f32x8 = array[0..8].*;
const y: f32x8 = array[8..16].*;
const z: f32x8 = array[16..24].*;
const w: f32x8 = array[24..32].*;
return .{ .x = x, .y = y, .z = z, .w = w };
}
pub inline fn initArrayTranspose(array: Array) Vector4x8 {
const vector: @Vector(32, f32) = array;
const x: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 });
const y: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 });
const z: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 });
const w: f32x8 = @shuffle(f32, 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]Vector4) Vector4x8 {
return initArrayTranspose(@bitCast(vectors));
}
// --- CONVERSION ---
pub inline fn asArray(self: Vector4x8) Array {
const x: [8]f32 = self.x;
const y: [8]f32 = self.y;
const z: [8]f32 = self.z;
const w: [8]f32 = self.w;
return x ++ y ++ z ++ w;
}
pub inline fn asArrayTranspose(self: Vector4x8) Array {
const vector: @Vector(32, f32) = self.asArray();
const transposed: @Vector(32, f32) = @shuffle(f32, 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: Vector4x8) [8]Vector4 {
return @bitCast(self.asArrayTranspose());
}
pub inline fn unpack(self: Vector4x8) [4]f32x8 {
return .{ self.x, self.y, self.z, self.w };
}
// --- LOAD AND STORE ---
pub inline fn loadArray(self: *Vector4x8, 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: *Vector4x8, array: *const Array) void {
const vector: @Vector(32, f32) = array;
self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 });
self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 });
self.z = @shuffle(f32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 });
self.w = @shuffle(f32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 });
}
pub inline fn loadArrayOfVectors(self: *Vector4x8, vectors: *const [8]Vector4) void {
self.loadArrayTranspose(@ptrCast(vectors));
}
pub inline fn storeArray(self: *const Vector4x8, 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 Vector4x8, array: *Array) void {
const vector: @Vector(32, f32) = self.asArray();
const transposed: @Vector(32, f32) = @shuffle(f32, 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 Vector4x8, vectors: *[8]Vector4) void {
self.storeArrayTranspose(@ptrCast(vectors));
}
// --- COMPONENT-WISE ---
pub inline fn add(self: Vector4x8, other: Vector4x8) Vector4x8 {
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: Vector4x8, other: Vector4x8) Vector4x8 {
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: Vector4x8, other: Vector4x8) Vector4x8 {
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: Vector4x8, scalar: f32x8) Vector4x8 {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar };
}
pub inline fn mulScalarSingle(self: Vector4x8, scalar: f32) Vector4x8 {
return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar), .z = self.z * ps(scalar), .w = self.w * ps(scalar) };
}
pub inline fn div(self: Vector4x8, other: Vector4x8) Vector4x8 {
return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w };
}
pub inline fn divScalar(self: Vector4x8, scalar: f32x8) Vector4x8 {
return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar };
}
pub inline fn divScalarSingle(self: Vector4x8, scalar: f32) Vector4x8 {
return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar), .z = self.z / ps(scalar), .w = self.w / ps(scalar) };
}
pub inline fn negate(self: Vector4x8) Vector4x8 {
return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w };
}
pub inline fn abs(self: Vector4x8) Vector4x8 {
return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) };
}
pub inline fn floor(self: Vector4x8) Vector4x8 {
return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) };
}
pub inline fn ceil(self: Vector4x8) Vector4x8 {
return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) };
}
pub inline fn round(self: Vector4x8) Vector4x8 {
return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) };
}
pub inline fn min(self: Vector4x8, other: Vector4x8) Vector4x8 {
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: Vector4x8, other: Vector4x8) Vector4x8 {
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 len(self: Vector4x8) f32x8 {
return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w);
}
pub inline fn lenSquared(self: Vector4x8) f32x8 {
return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w;
}
pub inline fn dot(self: Vector4x8, other: Vector4x8) f32x8 {
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w;
}
pub inline fn lerp(a: Vector4x8, b: Vector4x8, t: f32x8) Vector4x8 {
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)),
.w = @mulAdd(f32x8, t, b.w, @mulAdd(f32x8, -t, a.w, a.w)),
};
}
};