vecmath file split mayhem
This commit is contained in:
205
packages/vecmath/src/vectors/Vector2x8.zig
Normal file
205
packages/vecmath/src/vectors/Vector2x8.zig
Normal 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,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user