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

163 lines
5.7 KiB
Zig

const std = @import("std");
const vm = @import("../root.zig");
pub const Vector2Int_x8 = struct {
x: vm.i32x8,
y: vm.i32x8,
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: vm.i32x8, y: vm.i32x8) Vector2Int_x8 {
return .{ .x = x, .y = y };
}
pub inline fn initSingle(x: i32, y: i32) Vector2Int_x8 {
return .{ .x = vm.epi32(x), .y = vm.epi32(y) };
}
pub inline fn initScalar(scalar: vm.i32x8) Vector2Int_x8 {
return .{ .x = scalar, .y = scalar };
}
pub inline fn initScalarSingle(scalar: i32) Vector2Int_x8 {
return .{ .x = vm.epi32(scalar), .y = vm.epi32(scalar) };
}
pub inline fn initArrayOfVectors(vectors: [8]vm.Vector2Int) Vector2Int_x8 {
const vector: @Vector(16, i32) = @as([16]i32, @bitCast(vectors));
return .{
.x = @shuffle(i32, vector, undefined, [_]i32{ 0, 2, 4, 6, 8, 10, 12, 14 }),
.y = @shuffle(i32, vector, undefined, [_]i32{ 1, 3, 5, 7, 9, 11, 13, 15 }),
};
}
pub inline fn splat(vector: vm.Vector2Int) Vector2Int_x8 {
return .{ .x = vm.epi32(vector.x), .y = vm.epi32(vector.y) };
}
// --- CONVERSION ----------------------------------------------------------
pub inline fn asArrayOfVectors(self: Vector2Int_x8) [8]vm.Vector2Int {
const vector: @Vector(16, i32) = self.x ++ self.y;
return @bitCast(@as([16]i32, @shuffle(i32, vector, undefined, [_]i32{
0, 8,
1, 9,
2, 10,
3, 11,
4, 12,
5, 13,
6, 14,
7, 15,
})));
}
pub inline fn unpack(self: Vector2Int_x8) [2]vm.i32x8 {
return .{ self.x, self.y };
}
// --- LOAD AND STORE ------------------------------------------------------
pub inline fn loadArrayOfVectors(self: *Vector2Int_x8, array: *const [8]vm.Vector2Int) void {
const vector: @Vector(16, i32) = @as(*const [16]i32, @ptrCast(array)).*;
self.x = @shuffle(i32, vector, undefined, [_]i32{ 0, 2, 4, 6, 8, 10, 12, 14 });
self.y = @shuffle(i32, vector, undefined, [_]i32{ 1, 3, 5, 7, 9, 11, 13, 15 });
}
pub inline fn storeArrayOfVectors(self: *const Vector2Int_x8, array: *[8]vm.Vector2Int) void {
const vector: @Vector(16, i32) = self.x ++ self.y;
@as(*[16]i32, @ptrCast(array)).* = @shuffle(i32, vector, undefined, [_]i32{
0, 8,
1, 9,
2, 10,
3, 11,
4, 12,
5, 13,
6, 14,
7, 15,
});
}
// --- 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: vm.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 * vm.epi32(scalar), .y = self.y * vm.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: vm.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, vm.epi32(scalar)), .y = @divFloor(self.y, vm.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: vm.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, vm.epi32(scalar)), .y = @mod(self.y, vm.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) vm.i32x8 {
return self.x * self.x + self.y * self.y;
}
pub inline fn dot(self: Vector2Int_x8, other: Vector2Int_x8) vm.i32x8 {
return self.x * other.x + self.y * other.y;
}
pub inline fn cross(self: Vector2Int_x8, other: Vector2Int_x8) vm.i32x8 {
return self.x * other.y - self.y * other.x;
}
};