Files
castle/packages/vecmath/src/vectors/Vector4Int_x8.zig

177 lines
7.7 KiB
Zig

const std = @import("std");
const vm = @import("../root.zig");
pub const Vector4Int_x8 = struct {
x: vm.i32x8,
y: vm.i32x8,
z: vm.i32x8,
w: vm.i32x8,
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: vm.i32x8, y: vm.i32x8, z: vm.i32x8, w: vm.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 = vm.epi32(x), .y = vm.epi32(y), .z = vm.epi32(z), .w = vm.epi32(w) };
}
pub inline fn initScalar(scalar: vm.i32x8) Vector4Int_x8 {
return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar };
}
pub inline fn initScalarSingle(scalar: i32) Vector4Int_x8 {
return .{ .x = vm.epi32(scalar), .y = vm.epi32(scalar), .z = vm.epi32(scalar), .w = vm.epi32(scalar) };
}
pub inline fn initArrayOfVectors(vectors: [8]vm.Vector4Int) Vector4Int_x8 {
const vector: @Vector(32, i32) = @as([32]i32, @bitCast(vectors));
return .{
.x = @shuffle(i32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }),
.y = @shuffle(i32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }),
.z = @shuffle(i32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }),
.w = @shuffle(i32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }),
};
}
pub inline fn initSplat(vector: vm.Vector4Int) Vector4Int_x8 {
return .{ .x = vm.epi32(vector.x), .y = vm.epi32(vector.y), .z = vm.epi32(vector.z), .w = vm.epi32(vector.w) };
}
// --- CONVERSION ----------------------------------------------------------
pub inline fn dropW(self: Vector4Int_x8) vm.Vector3Int_x8 {
return .{ .x = self.x, .y = self.y, .z = self.z };
}
pub inline fn dropZW(self: Vector4Int_x8) vm.Vector2Int_x8 {
return .{ .x = self.x, .y = self.y };
}
pub inline fn asArrayOfVectors(self: Vector4Int_x8) [8]vm.Vector4 {
const vector: @Vector(32, i32) = self.x ++ self.y ++ self.z ++ self.w;
return @bitCast(@as([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,
})));
}
pub inline fn unpack(self: Vector4Int_x8) [4]vm.i32x8 {
return .{ self.x, self.y, self.z, self.w };
}
// --- LOAD AND STORE ------------------------------------------------------
pub inline fn loadArrayOfVectors(self: *Vector4Int_x8, array: *const [8]vm.Vector4Int) void {
const vector: @Vector(32, i32) = @as(*const [32]i32, @ptrCast(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 storeArrayOfVectors(self: *const Vector4Int_x8, array: *[8]vm.Vector4Int) void {
const vector: @Vector(32, i32) = self.x ++ self.y ++ self.z ++ self.w;
@as(*[32]i32, @ptrCast(array)).* = @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,
});
}
// --- 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: vm.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 * vm.epi32(scalar), .y = self.y * vm.epi32(scalar), .z = self.z * vm.epi32(scalar), .w = self.w * vm.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: vm.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, vm.epi32(scalar)), .y = @divFloor(self.y, vm.epi32(scalar)), .z = @divFloor(self.z, vm.epi32(scalar)), .w = @divFloor(self.w, vm.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: vm.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, vm.epi32(scalar)), .y = @mod(self.y, vm.epi32(scalar)), .z = @mod(self.z, vm.epi32(scalar)), .w = @mod(self.w, vm.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) vm.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) vm.i32x8 {
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w;
}
};