JUMBO vecmath completion update
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
const std = @import("std");
|
||||
const vm = @import("root");
|
||||
const vm = @import("../root.zig");
|
||||
|
||||
pub const Vector2x8 = struct {
|
||||
x: f32x8,
|
||||
y: f32x8,
|
||||
|
||||
pub const Array = [16]f32;
|
||||
x: vm.f32x8,
|
||||
y: vm.f32x8,
|
||||
|
||||
pub const zero = initScalarSingle(0);
|
||||
pub const one = initScalarSingle(1);
|
||||
@@ -14,101 +12,79 @@ pub const Vector2x8 = struct {
|
||||
pub const unit_nx = initSingle(-1, 0);
|
||||
pub const unit_ny = initSingle(0, -1);
|
||||
|
||||
// --- INIT ----
|
||||
// --- INIT ----------------------------------------------------------------
|
||||
|
||||
pub inline fn init(x: f32x8, y: f32x8) Vector2x8 {
|
||||
pub inline fn init(x: vm.f32x8, y: vm.f32x8) Vector2x8 {
|
||||
return .{ .x = x, .y = y };
|
||||
}
|
||||
|
||||
pub inline fn initSingle(x: f32, y: f32) Vector2x8 {
|
||||
return .{ .x = ps(x), .y = ps(y) };
|
||||
return .{ .x = vm.ps(x), .y = vm.ps(y) };
|
||||
}
|
||||
|
||||
pub inline fn initScalar(scalar: f32x8) Vector2x8 {
|
||||
pub inline fn initScalar(scalar: vm.f32x8) Vector2x8 {
|
||||
return .{ .x = scalar, .y = scalar };
|
||||
}
|
||||
|
||||
pub inline fn initScalarSingle(scalar: f32) Vector2x8 {
|
||||
return .{ .x = ps(scalar), .y = ps(scalar) };
|
||||
return .{ .x = vm.ps(scalar), .y = vm.ps(scalar) };
|
||||
}
|
||||
|
||||
pub inline fn initSplat(vector: Vector2) Vector2x8 {
|
||||
return .{ .x = ps(vector.x), .y = ps(vector.y) };
|
||||
pub inline fn initArrayOfVectors(vectors: [8]vm.Vector2) Vector2x8 {
|
||||
const vector: @Vector(16, f32) = @as([16]f32, @bitCast(vectors));
|
||||
return .{
|
||||
.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 2, 4, 6, 8, 10, 12, 14 }),
|
||||
.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 3, 5, 7, 9, 11, 13, 15 }),
|
||||
};
|
||||
}
|
||||
|
||||
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 splat(vector: vm.Vector2) Vector2x8 {
|
||||
return .{ .x = vm.ps(vector.x), .y = vm.ps(vector.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 };
|
||||
// --- CONVERSION ----------------------------------------------------------
|
||||
|
||||
pub inline fn asArrayOfVectors(self: Vector2x8) [8]vm.Vector2 {
|
||||
const vector: @Vector(16, f32) = self.x ++ self.y;
|
||||
return @bitCast(@as([16]f32, @shuffle(f32, vector, undefined, [_]i32{
|
||||
0, 8,
|
||||
1, 9,
|
||||
2, 10,
|
||||
3, 11,
|
||||
4, 12,
|
||||
5, 13,
|
||||
6, 14,
|
||||
7, 15,
|
||||
})));
|
||||
}
|
||||
|
||||
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 {
|
||||
pub inline fn unpack(self: Vector2x8) [2]vm.f32x8 {
|
||||
return .{ self.x, self.y };
|
||||
}
|
||||
|
||||
// --- LOAD AND STORE ---
|
||||
// --- 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 loadArrayOfVectors(self: *Vector2x8, array: *const [8]vm.Vector2) void {
|
||||
const vector: @Vector(16, f32) = @as(*const [16]f32, @ptrCast(array)).*;
|
||||
self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 2, 4, 6, 8, 10, 12, 14 });
|
||||
self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 3, 5, 7, 9, 11, 13, 15 });
|
||||
}
|
||||
|
||||
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 storeArrayOfVectors(self: *const Vector2x8, array: *[8]vm.Vector2) void {
|
||||
const vector: @Vector(16, f32) = self.x ++ self.y;
|
||||
@as(*[16]f32, @ptrCast(array)).* = @shuffle(f32, vector, undefined, [_]i32{
|
||||
0, 8,
|
||||
1, 9,
|
||||
2, 10,
|
||||
3, 11,
|
||||
4, 12,
|
||||
5, 13,
|
||||
6, 14,
|
||||
7, 15,
|
||||
});
|
||||
}
|
||||
|
||||
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 ---
|
||||
// --- COMPONENT-WISE ------------------------------------------------------
|
||||
|
||||
pub inline fn add(self: Vector2x8, other: Vector2x8) Vector2x8 {
|
||||
return .{ .x = self.x + other.x, .y = self.y + other.y };
|
||||
@@ -122,24 +98,24 @@ pub const Vector2x8 = struct {
|
||||
return .{ .x = self.x * other.x, .y = self.y * other.y };
|
||||
}
|
||||
|
||||
pub inline fn mulScalar(self: Vector2x8, scalar: f32x8) Vector2x8 {
|
||||
pub inline fn mulScalar(self: Vector2x8, scalar: vm.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) };
|
||||
return .{ .x = self.x * vm.ps(scalar), .y = self.y * vm.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 {
|
||||
pub inline fn divScalar(self: Vector2x8, scalar: vm.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) };
|
||||
return .{ .x = self.x / vm.ps(scalar), .y = self.y / vm.ps(scalar) };
|
||||
}
|
||||
|
||||
pub inline fn negate(self: Vector2x8) Vector2x8 {
|
||||
@@ -170,36 +146,49 @@ pub const Vector2x8 = struct {
|
||||
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) };
|
||||
}
|
||||
|
||||
// --- OTHER ---
|
||||
// --- OTHER ---------------------------------------------------------------
|
||||
|
||||
pub inline fn len(self: Vector2x8) f32x8 {
|
||||
pub inline fn len(self: Vector2x8) vm.f32x8 {
|
||||
return @sqrt(self.x * self.x + self.y * self.y);
|
||||
}
|
||||
|
||||
pub inline fn lenSquared(self: Vector2x8) f32x8 {
|
||||
pub inline fn lenSquared(self: Vector2x8) vm.f32x8 {
|
||||
return self.x * self.x + self.y * self.y;
|
||||
}
|
||||
|
||||
pub inline fn dot(self: Vector2x8, other: Vector2x8) f32x8 {
|
||||
pub inline fn dot(self: Vector2x8, other: Vector2x8) vm.f32x8 {
|
||||
return self.x * other.x + self.y * other.y;
|
||||
}
|
||||
|
||||
pub inline fn cross(self: Vector2x8, other: Vector2x8) f32x8 {
|
||||
pub inline fn cross(self: Vector2x8, other: Vector2x8) vm.f32x8 {
|
||||
return self.x * other.y - self.y * other.x;
|
||||
}
|
||||
|
||||
pub inline fn lerp(a: Vector2x8, b: Vector2x8, t: f32x8) Vector2x8 {
|
||||
pub inline fn lerp(a: Vector2x8, b: Vector2x8, t: vm.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)),
|
||||
.x = @mulAdd(vm.f32x8, t, b.x, @mulAdd(vm.f32x8, -t, a.x, a.x)),
|
||||
.y = @mulAdd(vm.f32x8, t, b.y, @mulAdd(vm.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();
|
||||
pub inline fn lerpSingle(a: Vector2x8, b: Vector2x8, t: f32) Vector2x8 {
|
||||
return .{
|
||||
.x = self.x * c - self.y * s,
|
||||
.y = self.x * s + self.y * c,
|
||||
.x = @mulAdd(vm.f32x8, vm.ps(t), b.x, @mulAdd(vm.f32x8, -vm.ps(t), a.x, a.x)),
|
||||
.y = @mulAdd(vm.f32x8, vm.ps(t), b.y, @mulAdd(vm.f32x8, -vm.ps(t), a.y, a.y)),
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn rotate(self: Vector2x8, complex: vm.Complex_x8) Vector2x8 {
|
||||
return .{
|
||||
.x = self.x * complex.re - self.y * complex.im,
|
||||
.y = self.x * complex.im + self.y * complex.re,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn rotateSingle(self: Vector2x8, complex: vm.Complex) Vector2x8 {
|
||||
return .{
|
||||
.x = self.x * vm.ps(complex.re) - self.y * vm.ps(complex.im),
|
||||
.y = self.x * vm.ps(complex.im) + self.y * vm.ps(complex.re),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user