167 lines
4.9 KiB
Zig
167 lines
4.9 KiB
Zig
const std = @import("std");
|
|
const math = @import("../math.zig");
|
|
|
|
const Vector2 = @import("Vector2.zig").Vector2;
|
|
|
|
const f32x8 = math.f32x8;
|
|
const ps = math.ps;
|
|
|
|
pub const Vector2x8 = extern struct {
|
|
x: f32x8,
|
|
y: f32x8,
|
|
|
|
pub const zero = Vector2x8.init(ps(0), ps(0));
|
|
pub const one = Vector2x8.init(ps(1), ps(1));
|
|
pub const unit_x = Vector2x8.init(ps(1), ps(0));
|
|
pub const unit_y = Vector2x8.init(ps(0), ps(1));
|
|
pub const unit_nx = Vector2x8.init(ps(-1), ps(0));
|
|
pub const unit_ny = Vector2x8.init(ps(0), ps(-1));
|
|
|
|
// --- INIT ---
|
|
|
|
pub inline fn initScalar(x: f32, y: f32) Vector2x8 {
|
|
return .{ .x = ps(x), .y = ps(y) };
|
|
}
|
|
|
|
pub inline fn initScalars(x: f32x8, y: f32x8) Vector2x8 {
|
|
return .{ .x = x, .y = y };
|
|
}
|
|
|
|
pub inline fn initVector(vector: Vector2) Vector2x8 {
|
|
return .{ .x = ps(vector.getX()), .y = ps(vector.getY()) };
|
|
}
|
|
|
|
pub inline fn initVectors(vectors: [8]Vector2) Vector2x8 {
|
|
const v0: f32x8 = @as([8]f32, @bitCast(vectors[0..4].*));
|
|
const v1: f32x8 = @as([8]f32, @bitCast(vectors[4..8].*));
|
|
const x: f32x8 = @shuffle(f32, v0, v1, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) });
|
|
const y: f32x8 = @shuffle(f32, v0, v1, [_]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 asVectors(self: Vector2x8) [8]Vector2 {
|
|
const v0: f32x8 = @shuffle(f32, self.x, self.y, [_]i32{ 0, ~@as(i32, 0), 1, ~@as(i32, 1), 2, ~@as(i32, 2), 3, ~@as(i32, 3) });
|
|
const v1: f32x8 = @shuffle(f32, self.x, self.y, [_]i32{ 4, ~@as(i32, 4), 5, ~@as(i32, 5), 6, ~@as(i32, 6), 7, ~@as(i32, 7) });
|
|
return @as([4]Vector2, @bitCast(@as([8]f32, v0))) ++ @as([4]Vector2, @bitCast(@as([8]f32, v1)));
|
|
}
|
|
|
|
// --- 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 div(self: Vector2x8, other: Vector2x8) Vector2x8 {
|
|
return .{
|
|
.x = self.x / other.x,
|
|
.y = self.y / other.y,
|
|
};
|
|
}
|
|
|
|
pub inline fn negate(self: Vector2x8) Vector2x8 {
|
|
return .{
|
|
.x = -self.x,
|
|
.y = -self.y,
|
|
};
|
|
}
|
|
|
|
pub inline fn mulScalar(self: Vector2x8, scalar: f32) Vector2x8 {
|
|
return .{
|
|
.x = self.x * ps(scalar),
|
|
.y = self.y * ps(scalar),
|
|
};
|
|
}
|
|
|
|
pub inline fn mulScalars(self: Vector2x8, scalar: f32x8) Vector2x8 {
|
|
return .{
|
|
.x = self.x * scalar,
|
|
.y = self.y * scalar,
|
|
};
|
|
}
|
|
|
|
pub inline fn divScalar(self: Vector2x8, scalar: f32) Vector2x8 {
|
|
return .{
|
|
.x = self.x / ps(scalar),
|
|
.y = self.y / ps(scalar),
|
|
};
|
|
}
|
|
|
|
pub inline fn divScalars(self: Vector2x8, scalar: f32x8) Vector2x8 {
|
|
return .{
|
|
.x = self.x / scalar,
|
|
.y = self.y / scalar,
|
|
};
|
|
}
|
|
|
|
// --- 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 normalize(self: Vector2x8) Vector2x8 {
|
|
const len_vector: f32x8 = @sqrt(self.x * self.x + self.y * self.y);
|
|
return .{
|
|
.x = self.x / len_vector,
|
|
.y = self.y / len_vector,
|
|
};
|
|
}
|
|
|
|
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(self: Vector2x8, other: Vector2x8, t: f32x8) Vector2x8 {
|
|
const s: f32x8 = ps(1.0) - t;
|
|
return .{
|
|
.x = self.x * s + other.x * t,
|
|
.y = self.y * s + other.y * t,
|
|
};
|
|
}
|
|
|
|
pub inline fn rotate(self: Vector2x8, angle_rad: f32) Vector2x8 {
|
|
const c = @cos(angle_rad);
|
|
const s = @sin(angle_rad);
|
|
return .{
|
|
.x = self.x * ps(c) - self.y * ps(s),
|
|
.y = self.x * ps(s) + self.y * ps(c),
|
|
};
|
|
}
|
|
|
|
pub inline fn rotatev(self: Vector2x8, angle_rad: f32x8) Vector2x8 {
|
|
const c = @cos(angle_rad);
|
|
const s = @sin(angle_rad);
|
|
return .{
|
|
.x = self.x * c - self.y * s,
|
|
.y = self.x * s + self.y * c,
|
|
};
|
|
}
|
|
};
|