From b09200b7abbee64758b0165bc570f2c5d6624d4b Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Fri, 2 Jan 2026 00:58:21 +0100 Subject: [PATCH] vecmath file split mayhem --- packages/vecmath/src/colors/Color.zig | 95 + packages/vecmath/src/matrices/Matrix3x2.zig | 82 + packages/vecmath/src/matrices/Matrix3x2x8.zig | 2 + packages/vecmath/src/matrices/Matrix4x4.zig | 123 + packages/vecmath/src/matrices/Matrix4x4x8.zig | 2 + packages/vecmath/src/root.zig | 2690 +---------------- packages/vecmath/src/rotors/Complex.zig | 45 + packages/vecmath/src/rotors/Complex_x8.zig | 55 + packages/vecmath/src/rotors/Quaternion.zig | 83 + packages/vecmath/src/rotors/Quaternion_x8.zig | 85 + packages/vecmath/src/simd.zig | 33 + packages/vecmath/src/trig.zig | 157 + packages/vecmath/src/vectors/Vector2.zig | 131 + packages/vecmath/src/vectors/Vector2Int.zig | 108 + .../vecmath/src/vectors/Vector2Int_x8.zig | 186 ++ packages/vecmath/src/vectors/Vector2x8.zig | 205 ++ packages/vecmath/src/vectors/Vector3.zig | 141 + packages/vecmath/src/vectors/Vector3Int.zig | 115 + .../vecmath/src/vectors/Vector3Int_x8.zig | 216 ++ packages/vecmath/src/vectors/Vector3x8.zig | 238 ++ packages/vecmath/src/vectors/Vector4.zig | 127 + packages/vecmath/src/vectors/Vector4Int.zig | 110 + .../vecmath/src/vectors/Vector4Int_x8.zig | 217 ++ packages/vecmath/src/vectors/Vector4x8.zig | 230 ++ 24 files changed, 2844 insertions(+), 2632 deletions(-) create mode 100644 packages/vecmath/src/colors/Color.zig create mode 100644 packages/vecmath/src/matrices/Matrix3x2.zig create mode 100644 packages/vecmath/src/matrices/Matrix3x2x8.zig create mode 100644 packages/vecmath/src/matrices/Matrix4x4.zig create mode 100644 packages/vecmath/src/matrices/Matrix4x4x8.zig create mode 100644 packages/vecmath/src/rotors/Complex.zig create mode 100644 packages/vecmath/src/rotors/Complex_x8.zig create mode 100644 packages/vecmath/src/rotors/Quaternion.zig create mode 100644 packages/vecmath/src/rotors/Quaternion_x8.zig create mode 100644 packages/vecmath/src/simd.zig create mode 100644 packages/vecmath/src/trig.zig create mode 100644 packages/vecmath/src/vectors/Vector2.zig create mode 100644 packages/vecmath/src/vectors/Vector2Int.zig create mode 100644 packages/vecmath/src/vectors/Vector2Int_x8.zig create mode 100644 packages/vecmath/src/vectors/Vector2x8.zig create mode 100644 packages/vecmath/src/vectors/Vector3.zig create mode 100644 packages/vecmath/src/vectors/Vector3Int.zig create mode 100644 packages/vecmath/src/vectors/Vector3Int_x8.zig create mode 100644 packages/vecmath/src/vectors/Vector3x8.zig create mode 100644 packages/vecmath/src/vectors/Vector4.zig create mode 100644 packages/vecmath/src/vectors/Vector4Int.zig create mode 100644 packages/vecmath/src/vectors/Vector4Int_x8.zig create mode 100644 packages/vecmath/src/vectors/Vector4x8.zig diff --git a/packages/vecmath/src/colors/Color.zig b/packages/vecmath/src/colors/Color.zig new file mode 100644 index 0000000..dc1f0d7 --- /dev/null +++ b/packages/vecmath/src/colors/Color.zig @@ -0,0 +1,95 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Color = extern struct { + r: u8, + g: u8, + b: u8, + a: u8, + + pub const Array = [4]u8; + + pub const clear = init(0, 0, 0, 0); + pub const black = initOpaque(0, 0, 0); + pub const red = initOpaque(255, 0, 0); + pub const green = initOpaque(0, 255, 0); + pub const blue = initOpaque(0, 0, 255); + pub const cyan = initOpaque(0, 255, 255); + pub const magenta = initOpaque(255, 0, 255); + pub const yellow = initOpaque(255, 255, 0); + pub const white = initOpaque(255, 255, 255); + + pub inline fn init(r: u8, g: u8, b: u8, a: u8) Color { + return .{ .r = r, .g = g, .b = b, .a = a }; + } + + pub inline fn initOpaque(r: u8, g: u8, b: u8) Color { + return .{ .r = r, .g = g, .b = b, .a = 255 }; + } + + pub inline fn initArray(array: Array) Color { + return @bitCast(array); + } + + pub inline fn l(comptime literal: []const u8) Color { + if (literal.len == 4 and literal[0] == '#') { // #RGB + return .initOpaque( + (std.fmt.parseUnsigned(u8, literal[1..2], 16) catch unreachable) * 0x11, + (std.fmt.parseUnsigned(u8, literal[2..3], 16) catch unreachable) * 0x11, + (std.fmt.parseUnsigned(u8, literal[3..4], 16) catch unreachable) * 0x11, + ); + } else if (literal.len == 5 and literal[0] == '#') { // #RGBA + return .init( + (std.fmt.parseUnsigned(u8, literal[1..2], 16) catch unreachable) * 0x11, + (std.fmt.parseUnsigned(u8, literal[2..3], 16) catch unreachable) * 0x11, + (std.fmt.parseUnsigned(u8, literal[3..4], 16) catch unreachable) * 0x11, + (std.fmt.parseUnsigned(u8, literal[4..5], 16) catch unreachable) * 0x11, + ); + } else if (literal.len == 7 and literal[0] == '#') { // #RRGGBB + return .initOpaque( + (std.fmt.parseUnsigned(u8, literal[1..3], 16) catch unreachable), + (std.fmt.parseUnsigned(u8, literal[3..5], 16) catch unreachable), + (std.fmt.parseUnsigned(u8, literal[5..7], 16) catch unreachable), + ); + } else if (literal.len == 9 and literal[0] == '#') { // #RRGGBBAA + return .init( + (std.fmt.parseUnsigned(u8, literal[1..3], 16) catch unreachable), + (std.fmt.parseUnsigned(u8, literal[3..5], 16) catch unreachable), + (std.fmt.parseUnsigned(u8, literal[5..7], 16) catch unreachable), + (std.fmt.parseUnsigned(u8, literal[7..9], 16) catch unreachable), + ); + } + + @compileError("Invalid color literal: " ++ literal); + } + + pub inline fn asArray(self: Color) Array { + return @bitCast(self); + } +}; + +test "l" { + const i: Color = .l("#012"); + try std.testing.expectEqual(0x00, i.r); + try std.testing.expectEqual(0x11, i.g); + try std.testing.expectEqual(0x22, i.b); + try std.testing.expectEqual(0xFF, i.a); + + const j: Color = .l("#3456"); + try std.testing.expectEqual(0x33, j.r); + try std.testing.expectEqual(0x44, j.g); + try std.testing.expectEqual(0x55, j.b); + try std.testing.expectEqual(0x66, j.a); + + const k: Color = .l("#F08040"); + try std.testing.expectEqual(0xF0, k.r); + try std.testing.expectEqual(0x80, k.g); + try std.testing.expectEqual(0x40, k.b); + try std.testing.expectEqual(0xFF, k.a); + + const l: Color = .l("#20304050"); + try std.testing.expectEqual(0x20, l.r); + try std.testing.expectEqual(0x30, l.g); + try std.testing.expectEqual(0x40, l.b); + try std.testing.expectEqual(0x50, l.a); +} diff --git a/packages/vecmath/src/matrices/Matrix3x2.zig b/packages/vecmath/src/matrices/Matrix3x2.zig new file mode 100644 index 0000000..f69fa54 --- /dev/null +++ b/packages/vecmath/src/matrices/Matrix3x2.zig @@ -0,0 +1,82 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Matrix3x2 = extern struct { + ix: f32, + iy: f32, + jx: f32, + jy: f32, + tx: f32, + ty: f32, + + pub const Array = [6]f32; + + pub const identity = init(1, 0, 0, 1, 0, 0); + + // --- INIT --- + + pub inline fn init(ix: f32, iy: f32, jx: f32, jy: f32, tx: f32, ty: f32) Matrix3x2 { + return .{ .ix = ix, .iy = iy, .jx = jx, .jy = jy, .tx = tx, .ty = ty }; + } + + pub inline fn initTranslation(t: Vector2) Matrix3x2 { + return .{ .ix = 1, .iy = 0, .jx = 0, .jy = 1, .tx = t.x, .ty = t.y }; + } + + pub inline fn initRotation(angle_turns: f32) Matrix3x2 { + const c, const s = cossin(angle_turns).asArray(); + return .{ .ix = c, .iy = s, .jx = -s, .jy = c, .tx = 0, .ty = 0 }; + } + + pub inline fn initScale(s: Vector2) Matrix3x2 { + return .{ .ix = s.x, .iy = 0, .jx = 0, .jy = s.y, .tx = 0, .ty = 0 }; + } + + pub inline fn initArray(array: Array) Matrix3x2 { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Matrix3x2) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Matrix3x2) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Matrix3x2) *const Array { + return @ptrCast(self); + } + + // --- TRANSFORM --- + + pub inline fn transformPoint(self: Matrix3x2, p: Vector2) Vector2 { + return .{ + .x = p.x * self.ix + p.y * self.jx + self.tx, + .y = p.x * self.iy + p.y * self.jy + self.ty, + }; + } + + pub inline fn transformPoint_x8(self: Matrix3x2, p: Vector2x8) Vector2x8 { + return .{ + .x = p.x * ps(self.ix) + p.y * ps(self.jx) + ps(self.tx), + .y = p.x * ps(self.iy) + p.y * ps(self.jy) + ps(self.ty), + }; + } + + pub inline fn transformVector(self: Matrix3x2, v: Vector2) Vector2 { + return .{ + .x = v.x * self.ix + v.y * self.jx, + .y = v.x * self.iy + v.y * self.jy, + }; + } + + pub inline fn transformVector_x8(self: Matrix3x2, v: Vector2x8) Vector2x8 { + return .{ + .x = v.x * ps(self.ix) + v.y * ps(self.jx), + .y = v.x * ps(self.iy) + v.y * ps(self.jy), + }; + } +}; diff --git a/packages/vecmath/src/matrices/Matrix3x2x8.zig b/packages/vecmath/src/matrices/Matrix3x2x8.zig new file mode 100644 index 0000000..6cb3c45 --- /dev/null +++ b/packages/vecmath/src/matrices/Matrix3x2x8.zig @@ -0,0 +1,2 @@ +const std = @import("std"); +const vm = @import("root"); diff --git a/packages/vecmath/src/matrices/Matrix4x4.zig b/packages/vecmath/src/matrices/Matrix4x4.zig new file mode 100644 index 0000000..19d1518 --- /dev/null +++ b/packages/vecmath/src/matrices/Matrix4x4.zig @@ -0,0 +1,123 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Matrix4x4 = extern struct { + ix: f32, + iy: f32, + iz: f32, + iw: f32, + jx: f32, + jy: f32, + jz: f32, + jw: f32, + kx: f32, + ky: f32, + kz: f32, + kw: f32, + tx: f32, + ty: f32, + tz: f32, + tw: f32, + + pub const Array = [16]f32; + + pub const identity = init(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); + + // --- INIT --- + + pub inline fn init(ix: f32, iy: f32, iz: f32, iw: f32, jx: f32, jy: f32, jz: f32, jw: f32, kx: f32, ky: f32, kz: f32, kw: f32, tx: f32, ty: f32, tz: f32, tw: f32) Matrix4x4 { + return .{ .ix = ix, .iy = iy, .iz = iz, .iw = iw, .jx = jx, .jy = jy, .jz = jz, .jw = jw, .kx = kx, .ky = ky, .kz = kz, .kw = kw, .tx = tx, .ty = ty, .tz = tz, .tw = tw }; + } + + pub inline fn initTranslation(t: Vector3) Matrix4x4 { + return .{ .ix = 1, .iy = 0, .iz = 0, .iw = 0, .jx = 0, .jy = 1, .jz = 0, .jw = 0, .kx = 0, .ky = 0, .kz = 1, .kw = 0, .tx = t.x, .ty = t.y, .tz = t.z, .tw = 1 }; + } + + pub inline fn initRotation(q: Quaternion) Matrix4x4 { + const xx = q.x * q.x; + const xy = q.x * q.y; + const xz = q.x * q.z; + const xw = q.x * q.w; + const yy = q.y * q.y; + const yz = q.y * q.z; + const yw = q.y * q.w; + const zz = q.z * q.z; + const zw = q.z * q.w; + + return .{ .ix = 1 - 2 * (yy + zz), .jx = 2 * (xy + zw), .kx = 2 * (xz - yw), .tx = 0, .iy = 2 * (xy - zw), .jy = 1 - 2 * (xx + zz), .ky = 2 * (yz + xw), .ty = 0, .iz = 2 * (xz + yw), .jz = 2 * (yz - xw), .kz = 1 - 2 * (xx + yy), .tz = 0, .iw = 0, .jw = 0, .kw = 0, .tw = 1 }; + } + + pub inline fn initScale(s: Vector3) Matrix4x4 { + return .{ .ix = s.x, .iy = 0, .iz = 0, .iw = 0, .jx = 0, .jy = s.y, .jz = 0, .jw = 0, .kx = 0, .ky = 0, .kz = s.z, .kw = 0, .tx = 0, .ty = 0, .tz = 0, .tw = 1 }; + } + + pub inline fn initArray(array: Array) Matrix4x4 { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Matrix4x4) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Matrix4x4) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Matrix4x4) *const Array { + return @ptrCast(self); + } + + // --- TRANSFORM --- + + pub inline fn transformPoint(self: Matrix4x4, p: Vector3) Vector3 { + return .{ + .x = p.x * self.ix + p.y * self.jx + p.z * self.kx + self.tx, + .y = p.x * self.iy + p.y * self.jy + p.z * self.ky + self.ty, + .z = p.x * self.iz + p.y * self.jz + p.z * self.kz + self.tz, + }; + } + + pub inline fn transformPoint_x8(self: Matrix4x4, p: Vector3x8) Vector3x8 { + return .{ + .x = p.x * ps(self.ix) + p.y * ps(self.jx) + p.z * ps(self.kx) + ps(self.tx), + .y = p.x * ps(self.iy) + p.y * ps(self.jy) + p.z * ps(self.ky) + ps(self.ty), + .z = p.x * ps(self.iz) + p.y * ps(self.jz) + p.z * ps(self.kz) + ps(self.tz), + }; + } + + pub inline fn transformVector(self: Matrix4x4, v: Vector3) Vector3 { + return .{ + .x = v.x * self.ix + v.y * self.jx + v.z * self.kx, + .y = v.x * self.iy + v.y * self.jy + v.z * self.ky, + .z = v.x * self.iz + v.y * self.jz + v.z * self.kz, + }; + } + + pub inline fn transformVector_x8(self: Matrix4x4, v: Vector3x8) Vector3x8 { + return .{ + .x = v.x * ps(self.ix) + v.y * ps(self.jx) + v.z * ps(self.kx), + .y = v.x * ps(self.iy) + v.y * ps(self.jy) + v.z * ps(self.ky), + .z = v.x * ps(self.iz) + v.y * ps(self.jz) + v.z * ps(self.kz), + }; + } + + pub inline fn transformHomogeneous(self: Matrix4x4, h: Vector4) Vector4 { + return .{ + .x = h.x * self.ix + h.y * self.jx + h.z * self.kx + h.w * self.tx, + .y = h.x * self.iy + h.y * self.jy + h.z * self.ky + h.w * self.ty, + .z = h.x * self.iz + h.y * self.jz + h.z * self.kz + h.w * self.tz, + .w = h.x * self.iw + h.y * self.jw + h.z * self.kw + h.w * self.tw, + }; + } + + pub inline fn transformHomogeneous_x8(self: Matrix4x4, h: Vector4x8) Vector4x8 { + return .{ + .x = h.x * ps(self.ix) + h.y * ps(self.jx) + h.z * ps(self.kx) + h.w * ps(self.tx), + .y = h.x * ps(self.iy) + h.y * ps(self.jy) + h.z * ps(self.ky) + h.w * ps(self.ty), + .z = h.x * ps(self.iz) + h.y * ps(self.jz) + h.z * ps(self.kz) + h.w * ps(self.tz), + .w = h.x * ps(self.iw) + h.y * ps(self.jw) + h.z * ps(self.kw) + h.w * ps(self.tw), + }; + } +}; diff --git a/packages/vecmath/src/matrices/Matrix4x4x8.zig b/packages/vecmath/src/matrices/Matrix4x4x8.zig new file mode 100644 index 0000000..6cb3c45 --- /dev/null +++ b/packages/vecmath/src/matrices/Matrix4x4x8.zig @@ -0,0 +1,2 @@ +const std = @import("std"); +const vm = @import("root"); diff --git a/packages/vecmath/src/root.zig b/packages/vecmath/src/root.zig index 93bc0b0..6856ddb 100644 --- a/packages/vecmath/src/root.zig +++ b/packages/vecmath/src/root.zig @@ -1,2651 +1,77 @@ const std = @import("std"); +// --- COLORS ------------------------------------------------------------------ + +pub const Color = @import("colors/Color.zig").Color; + +// --- MATRICES ---------------------------------------------------------------- + +pub const Matrix3x2 = @import("matrices/Matrix3x2.zig").Matrix3x2; +pub const Matrix3x2x8 = @import("matrices/Matrix3x2x8.zig").Matrix3x2x8; + +pub const Matrix4x4 = @import("matrices/Matrix4x4.zig").Matrix4x4; +pub const Matrix4x4x8 = @import("matrices/Matrix4x4x8.zig").Matrix4x4x8; + +// --- ROTORS ------------------------------------------------------------------ + +pub const Complex = @import("rotors/Complex.zig").Complex; +pub const Complex_x8 = @import("rotors/Complex_x8.zig").Complex_x8; + +pub const Quaternion = @import("rotors/Quaternion.zig").Quaternion; +pub const Quaternion_x8 = @import("rotors/Quaternion_x8.zig").Quaternion_x8; + +// --- VECTORS ----------------------------------------------------------------- + +pub const Vector2 = @import("vectors/Vector2.zig").Vector2; +pub const Vector2x8 = @import("vectors/Vector2x8.zig").Vector2x8; +pub const Vector2Int = @import("vectors/Vector2Int.zig").Vector2Int; +pub const Vector2Int_x8 = @import("vectors/Vector2Int_x8.zig").Vector2Int_x8; + +pub const Vector3 = @import("vectors/Vector3.zig").Vector3; +pub const Vector3x8 = @import("vectors/Vector3x8.zig").Vector3x8; +pub const Vector3Int = @import("vectors/Vector3Int.zig").Vector3Int; +pub const Vector3Int_x8 = @import("vectors/Vector3Int_x8.zig").Vector3Int_x8; + +pub const Vector4 = @import("vectors/Vector4.zig").Vector4; +pub const Vector4x8 = @import("vectors/Vector4x8.zig").Vector4x8; +pub const Vector4Int = @import("vectors/Vector4Int.zig").Vector4Int; +pub const Vector4Int_x8 = @import("vectors/Vector4Int_x8.zig").Vector4Int_x8; + // --- SIMD -------------------------------------------------------------------- -pub const f32x8 = @Vector(8, f32); -pub const i32x8 = @Vector(8, i32); -pub const u32x8 = @Vector(8, u32); -pub const f64x4 = @Vector(4, f64); -pub const i64x4 = @Vector(4, i64); -pub const u64x4 = @Vector(4, u64); +const simd = @import("simd.zig"); -pub inline fn ps(value: f32) f32x8 { - return @splat(value); -} +pub const f32x8 = simd.f32x8; +pub const i32x8 = simd.i32x8; +pub const u32x8 = simd.u32x8; +pub const f64x4 = simd.f64x4; +pub const i64x4 = simd.i64x4; +pub const u64x4 = simd.u64x4; -pub inline fn epi32(value: i32) i32x8 { - return @splat(value); -} - -pub inline fn epu32(value: u32) u32x8 { - return @splat(value); -} - -pub inline fn pd(value: f64) f64x4 { - return @splat(value); -} - -pub inline fn epi64(value: i64) i64x4 { - return @splat(value); -} - -pub inline fn epu64(value: u64) u64x4 { - return @splat(value); -} +pub const ps = simd.ps; +pub const epi32 = simd.epi32; +pub const epu32 = simd.epu32; +pub const pd = simd.pd; +pub const epi64 = simd.epi64; +pub const epu64 = simd.epu64; // --- TRIGONOMETRY ------------------------------------------------------------ -pub fn cos(angle_turns: f32) f32 { - return cossin(angle_turns).x; -} +const trig = @import("trig.zig"); -test "cos" { - try std.testing.expectEqual(1, cos(-1)); - try std.testing.expectEqual(0, cos(-0.75)); - try std.testing.expectEqual(-1, cos(-0.5)); - try std.testing.expectEqual(0, cos(-0.25)); - try std.testing.expectEqual(1, cos(0)); - try std.testing.expectEqual(0, cos(0.25)); - try std.testing.expectEqual(-1, cos(0.5)); - try std.testing.expectEqual(0, cos(0.75)); -} +pub const cos = trig.cos; +pub const cos_x8 = trig.cos_x8; +pub const sin = trig.sin; +pub const sin_x8 = trig.sin_x8; +pub const cossin = trig.cossin; +pub const cossin_x8 = trig.cossin_x8; -pub fn cos_x8(angle_turns: f32x8) f32x8 { - return cossin_x8(angle_turns).x; -} - -test "cos_x8" { - try std.testing.expectEqual( - .{ 1, 0, -1, 0, 1, 0, -1, 0 }, - cos_x8(.{ -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75 }), - ); -} - -pub fn sin(angle_turns: f32) f32 { - return cossin(angle_turns).y; -} - -test "sin" { - try std.testing.expectEqual(0, sin(-1)); - try std.testing.expectEqual(1, sin(-0.75)); - try std.testing.expectEqual(0, sin(-0.5)); - try std.testing.expectEqual(-1, sin(-0.25)); - try std.testing.expectEqual(0, sin(0)); - try std.testing.expectEqual(1, sin(0.25)); - try std.testing.expectEqual(0, sin(0.5)); - try std.testing.expectEqual(-1, sin(0.75)); -} - -pub fn sin_x8(angle_turns: f32x8) f32x8 { - return cossin_x8(angle_turns).y; -} - -test "sin_x8" { - try std.testing.expectEqual( - .{ 0, 1, 0, -1, 0, 1, 0, -1 }, - sin_x8(.{ -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75 }), - ); -} - -pub fn cossin(angle_turns: f32) Vector2 { - @setFloatMode(.optimized); - // Taylor series expansion for f(x)=cos(xπ/2) - const term_cos_0: f32 = 1.0; - const term_cos_2: f32 = -1.23370055; // -π²/8 - const term_cos_4: f32 = 0.253669508; // π⁴/384 - const term_cos_6: f32 = -0.020863481; // -π⁶/46080 - // Taylor series expansion for f(x)=sin(xπ/2) - const term_sin_1: f32 = 1.570796327; // π/2 - const term_sin_3: f32 = -0.645964098; // -π³/48 - const term_sin_5: f32 = 0.079692626; // π⁵/3840 - - const angle_01 = angle_turns - @floor(angle_turns); - const angle_04 = 4.0 * angle_01; - - const quadrant: u32 = @intFromFloat(angle_04); - - const x = angle_04 - @floor(angle_04); - const x2 = x * x; - - const c = ((term_cos_6 * x2 + term_cos_4) * x2 + term_cos_2) * x2 + term_cos_0; - const s = ((term_sin_5 * x2 + term_sin_3) * x2 + term_sin_1) * x; - - return switch (quadrant) { - 0 => .init(c, s), - 1 => .init(-s, c), - 2 => .init(-c, -s), - 3 => .init(s, -c), - else => unreachable, - }; -} - -test "cossin" { - try std.testing.expectEqual(Vector2.unit_x, cossin(-1)); - try std.testing.expectEqual(Vector2.unit_y, cossin(-0.75)); - try std.testing.expectEqual(Vector2.unit_nx, cossin(-0.5)); - try std.testing.expectEqual(Vector2.unit_ny, cossin(-0.25)); - try std.testing.expectEqual(Vector2.unit_x, cossin(0)); - try std.testing.expectEqual(Vector2.unit_y, cossin(0.25)); - try std.testing.expectEqual(Vector2.unit_nx, cossin(0.5)); - try std.testing.expectEqual(Vector2.unit_ny, cossin(0.75)); -} - -pub fn cossin_x8(angle_turns: f32x8) Vector2x8 { - @setFloatMode(.optimized); - // Taylor series expansion for f(x)=cos(xπ/2) - const term_cos_0 = ps(1.0); - const term_cos_2 = ps(-1.23370055); // -π²/8 - const term_cos_4 = ps(0.253669508); // π⁴/384 - const term_cos_6 = ps(-0.020863481); // -π⁶/46080 - // Taylor series expansion for f(x)=sin(xπ/2) - const term_sin_1 = ps(1.570796327); // π/2 - const term_sin_3 = ps(-0.645964098); // -π³/48 - const term_sin_5 = ps(0.079692626); // π⁵/3840 - - const angle_01 = angle_turns - @floor(angle_turns); - const angle_04 = ps(4.0) * angle_01; - - const quadrant: u32x8 = @intFromFloat(angle_04); - const quadrant_odd = (quadrant & epu32(1)) != epu32(0); - const sign_mask_cos = ((quadrant + epu32(1)) & epu32(0b10)) << @splat(30); - const sign_mask_sin = (quadrant & epu32(0b10)) << @splat(30); - - const x = angle_04 - @floor(angle_04); - const x2 = x * x; - - const c = ((term_cos_6 * x2 + term_cos_4) * x2 + term_cos_2) * x2 + term_cos_0; - const s = ((term_sin_5 * x2 + term_sin_3) * x2 + term_sin_1) * x; - - var result_cos = @select(f32, quadrant_odd, s, c); - var result_sin = @select(f32, quadrant_odd, c, s); - - result_cos = @bitCast(@as(u32x8, @bitCast(result_cos)) ^ sign_mask_cos); - result_sin = @bitCast(@as(u32x8, @bitCast(result_sin)) ^ sign_mask_sin); - - return .init(result_cos, result_sin); -} - -test "cossin_x8" { - try std.testing.expectEqual( - Vector2x8.initArrayOfVectors(.{ Vector2.unit_x, Vector2.unit_y, Vector2.unit_nx, Vector2.unit_ny, Vector2.unit_x, Vector2.unit_y, Vector2.unit_nx, Vector2.unit_ny }), - cossin_x8(.{ -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75 }), - ); -} - -// --- SCALARS ----------------------------------------------------------------- +// ----------------------------------------------------------------------------- pub inline fn lerp(a: f32, b: f32, t: f32) f32 { return @mulAdd(f32, t, b, @mulAdd(f32, -t, a, a)); } -// --- VECTORS ----------------------------------------------------------------- - -pub const Vector2 = extern struct { - x: f32, - y: f32, - - pub const Array = [2]f32; - - pub const zero = initScalar(0); - pub const one = initScalar(1); - pub const unit_x = init(1, 0); - pub const unit_y = init(0, 1); - pub const unit_nx = init(-1, 0); - pub const unit_ny = init(0, -1); - - // --- INIT ---- - - pub inline fn init(x: f32, y: f32) Vector2 { - return .{ .x = x, .y = y }; - } - - pub inline fn initScalar(scalar: f32) Vector2 { - return .{ .x = scalar, .y = scalar }; - } - - pub inline fn initArray(array: Array) Vector2 { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector2) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Vector2) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Vector2) *const Array { - return @ptrCast(self); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector2, other: Vector2) Vector2 { - return .{ .x = self.x + other.x, .y = self.y + other.y }; - } - - pub inline fn sub(self: Vector2, other: Vector2) Vector2 { - return .{ .x = self.x - other.x, .y = self.y - other.y }; - } - - pub inline fn mul(self: Vector2, other: Vector2) Vector2 { - return .{ .x = self.x * other.x, .y = self.y * other.y }; - } - - pub inline fn mulScalar(self: Vector2, scalar: f32) Vector2 { - return .{ .x = self.x * scalar, .y = self.y * scalar }; - } - - pub inline fn div(self: Vector2, other: Vector2) Vector2 { - return .{ .x = self.x / other.x, .y = self.y / other.y }; - } - - pub inline fn divScalar(self: Vector2, scalar: f32) Vector2 { - return .{ .x = self.x / scalar, .y = self.y / scalar }; - } - - pub inline fn negate(self: Vector2) Vector2 { - return .{ .x = -self.x, .y = -self.y }; - } - - pub inline fn abs(self: Vector2) Vector2 { - return .{ .x = @abs(self.x), .y = @abs(self.y) }; - } - - pub inline fn floor(self: Vector2) Vector2 { - return .{ .x = @floor(self.x), .y = @floor(self.y) }; - } - - pub inline fn ceil(self: Vector2) Vector2 { - return .{ .x = @ceil(self.x), .y = @ceil(self.y) }; - } - - pub inline fn round(self: Vector2) Vector2 { - return .{ .x = @round(self.x), .y = @round(self.y) }; - } - - pub inline fn min(self: Vector2, other: Vector2) Vector2 { - return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) }; - } - - pub inline fn max(self: Vector2, other: Vector2) Vector2 { - return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) }; - } - - // --- OTHER --- - - pub inline fn len(self: Vector2) f32 { - return @sqrt(self.x * self.x + self.y * self.y); - } - - pub inline fn lenSquared(self: Vector2) f32 { - return self.x * self.x + self.y * self.y; - } - - pub inline fn dot(self: Vector2, other: Vector2) f32 { - return self.x * other.x + self.y * other.y; - } - - pub inline fn cross(self: Vector2, other: Vector2) f32 { - return self.x * other.y - self.y * other.x; - } - - pub inline fn lerp(a: Vector2, b: Vector2, t: f32) Vector2 { - return .{ - .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), - .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), - }; - } - - pub inline fn rotate(self: Vector2, angle_turns: f32) Vector2 { - const c, const s = cossin(angle_turns).asArray(); - return .{ - .x = self.x * c - self.y * s, - .y = self.x * s + self.y * c, - }; - } -}; - -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, - }; - } -}; - -pub const Vector2Int = extern struct { - x: i32, - y: i32, - - pub const Array = [2]i32; - - pub const zero = initScalar(0); - pub const one = initScalar(1); - pub const unit_x = init(1, 0); - pub const unit_y = init(0, 1); - pub const unit_nx = init(-1, 0); - pub const unit_ny = init(0, -1); - - // --- INIT ---- - - pub inline fn init(x: i32, y: i32) Vector2Int { - return .{ .x = x, .y = y }; - } - - pub inline fn initScalar(scalar: i32) Vector2Int { - return .{ .x = scalar, .y = scalar }; - } - - pub inline fn initArray(array: Array) Vector2Int { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector2Int) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Vector2Int) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Vector2Int) *const Array { - return @ptrCast(self); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = self.x + other.x, .y = self.y + other.y }; - } - - pub inline fn sub(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = self.x - other.x, .y = self.y - other.y }; - } - - pub inline fn mul(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = self.x * other.x, .y = self.y * other.y }; - } - - pub inline fn mulScalar(self: Vector2Int, scalar: f32) Vector2Int { - return .{ .x = self.x * scalar, .y = self.y * scalar }; - } - - pub inline fn div(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y) }; - } - - pub inline fn divScalar(self: Vector2Int, scalar: f32) Vector2Int { - return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar) }; - } - - pub inline fn mod(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y) }; - } - - pub inline fn modScalar(self: Vector2Int, scalar: f32) Vector2Int { - return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar) }; - } - - pub inline fn negate(self: Vector2Int) Vector2Int { - return .{ .x = -self.x, .y = -self.y }; - } - - pub inline fn abs(self: Vector2Int) Vector2Int { - return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)) }; - } - - pub inline fn min(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) }; - } - - pub inline fn max(self: Vector2Int, other: Vector2Int) Vector2Int { - return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) }; - } - - // --- OTHER --- - - pub inline fn lenSquared(self: Vector2Int) i32 { - return self.x * self.x + self.y * self.y; - } - - pub inline fn dot(self: Vector2Int, other: Vector2Int) i32 { - return self.x * other.x + self.y * other.y; - } - - pub inline fn cross(self: Vector2Int, other: Vector2Int) i32 { - return self.x * other.y - self.y * other.x; - } -}; - -pub const Vector2Int_x8 = struct { - x: i32x8, - y: i32x8, - - pub const Array = [16]i32; - - 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: i32x8, y: i32x8) Vector2Int_x8 { - return .{ .x = x, .y = y }; - } - - pub inline fn initSingle(x: i32, y: i32) Vector2Int_x8 { - return .{ .x = epi32(x), .y = epi32(y) }; - } - - pub inline fn initScalar(scalar: i32x8) Vector2Int_x8 { - return .{ .x = scalar, .y = scalar }; - } - - pub inline fn initScalarSingle(scalar: i32) Vector2Int_x8 { - return .{ .x = epi32(scalar), .y = epi32(scalar) }; - } - - pub inline fn initSplat(vector: Vector2Int) Vector2Int_x8 { - return .{ .x = epi32(vector.x), .y = epi32(vector.y) }; - } - - pub inline fn initArray(array: Array) Vector2Int_x8 { - const x: i32x8 = array[0..8].*; - const y: i32x8 = array[8..16].*; - return .{ .x = x, .y = y }; - } - - pub inline fn initArrayTranspose(array: Array) Vector2Int_x8 { - const a: i32x8 = array[0..8].*; - const b: i32x8 = array[8..16].*; - const x: i32x8 = @shuffle(i32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) }); - const y: i32x8 = @shuffle(i32, 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]Vector2Int) Vector2Int_x8 { - return initArrayTranspose(@bitCast(vectors)); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector2Int_x8) Array { - const x: [8]i32 = self.x; - const y: [8]i32 = self.y; - return x ++ y; - } - - pub inline fn asArrayTranspose(self: Vector2Int_x8) Array { - const a = @shuffle(i32, self.x, self.y, [_]i32{ 0, ~@as(i32, 0), 1, ~@as(i32, 1), 2, ~@as(i32, 2), 3, ~@as(i32, 3) }); - const b = @shuffle(i32, 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: Vector2Int_x8) [8]Vector2Int { - return @bitCast(self.asArrayTranspose()); - } - - pub inline fn unpack(self: Vector2Int_x8) [2]i32x8 { - return .{ self.x, self.y }; - } - - // --- LOAD AND STORE --- - - pub inline fn loadArray(self: *Vector2Int_x8, array: *const Array) void { - self.x = array[0..8].*; - self.y = array[8..16].*; - } - - pub inline fn loadArrayTranspose(self: *Vector2Int_x8, array: *const Array) void { - const a: i32x8 = array[0..8].*; - const b: i32x8 = array[8..16].*; - self.x = @shuffle(i32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) }); - self.y = @shuffle(i32, a, b, [_]i32{ 1, 3, 5, 7, ~@as(i32, 1), ~@as(i32, 3), ~@as(i32, 5), ~@as(i32, 7) }); - } - - pub inline fn loadArrayOfVectors(self: *Vector2Int_x8, vectors: *const [8]Vector2Int) void { - self.loadArrayTranspose(@ptrCast(vectors)); - } - - pub inline fn storeArray(self: *const Vector2Int_x8, array: *Array) void { - array[0..8].* = self.x; - array[8..16].* = self.y; - } - - pub inline fn storeArrayTranspose(self: *const Vector2Int_x8, array: *Array) void { - array[0..8].* = @shuffle(i32, 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(i32, 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 Vector2Int_x8, vectors: *[8]Vector2Int) void { - self.storeArrayTranspose(@ptrCast(vectors)); - } - - // --- 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: 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 * epi32(scalar), .y = self.y * 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: 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, epi32(scalar)), .y = @divFloor(self.y, 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: 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, epi32(scalar)), .y = @mod(self.y, 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) i32x8 { - return self.x * self.x + self.y * self.y; - } - - pub inline fn dot(self: Vector2Int_x8, other: Vector2Int_x8) i32x8 { - return self.x * other.x + self.y * other.y; - } - - pub inline fn cross(self: Vector2Int_x8, other: Vector2Int_x8) i32x8 { - return self.x * other.y - self.y * other.x; - } -}; - -pub const Vector3 = extern struct { - x: f32, - y: f32, - z: f32, - - pub const Array = [3]f32; - - pub const zero = initScalar(0); - pub const one = initScalar(1); - pub const unit_x = init(1, 0, 0); - pub const unit_y = init(0, 1, 0); - pub const unit_z = init(0, 0, 1); - pub const unit_nx = init(-1, 0, 0); - pub const unit_ny = init(0, -1, 0); - pub const unit_nz = init(0, 0, -1); - - // --- INIT ---- - - pub inline fn init(x: f32, y: f32, z: f32) Vector3 { - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initScalar(scalar: f32) Vector3 { - return .{ .x = scalar, .y = scalar, .z = scalar }; - } - - pub inline fn initArray(array: Array) Vector3 { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector3) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Vector3) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Vector3) *const Array { - return @ptrCast(self); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector3, other: Vector3) Vector3 { - return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; - } - - pub inline fn sub(self: Vector3, other: Vector3) Vector3 { - return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; - } - - pub inline fn mul(self: Vector3, other: Vector3) Vector3 { - return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; - } - - pub inline fn mulScalar(self: Vector3, scalar: f32) Vector3 { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; - } - - pub inline fn div(self: Vector3, other: Vector3) Vector3 { - return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z }; - } - - pub inline fn divScalar(self: Vector3, scalar: f32) Vector3 { - return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar }; - } - - pub inline fn negate(self: Vector3) Vector3 { - return .{ .x = -self.x, .y = -self.y, .z = -self.z }; - } - - pub inline fn abs(self: Vector3) Vector3 { - return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) }; - } - - pub inline fn floor(self: Vector3) Vector3 { - return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) }; - } - - pub inline fn ceil(self: Vector3) Vector3 { - return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) }; - } - - pub inline fn round(self: Vector3) Vector3 { - return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) }; - } - - pub inline fn min(self: Vector3, other: Vector3) Vector3 { - return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; - } - - pub inline fn max(self: Vector3, other: Vector3) Vector3 { - return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; - } - - // --- OTHER --- - - pub inline fn len(self: Vector3) f32 { - return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z); - } - - pub inline fn lenSquared(self: Vector3) f32 { - return self.x * self.x + self.y * self.y + self.z * self.z; - } - - pub inline fn dot(self: Vector3, other: Vector3) f32 { - return self.x * other.x + self.y * other.y + self.z * other.z; - } - - pub inline fn cross(self: Vector3, other: Vector3) Vector3 { - return .{ - .x = self.y * other.z - self.z * other.y, - .y = self.z * other.x - self.x * other.z, - .z = self.x * other.y - self.y * other.x, - }; - } - - pub inline fn lerp(a: Vector3, b: Vector3, t: f32) Vector3 { - return .{ - .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), - .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), - .z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)), - }; - } - - pub inline fn rotate(self: Vector3, quaternion: Quaternion) Vector3 { - const quaternion_scalar = quaternion.getScalar(); - const quaternion_vector = quaternion.getVector(); - - return .add(self, .cross( - .add(quaternion_vector, quaternion_vector), - .add(.cross(quaternion_vector, self), .mulScalar(self, quaternion_scalar)), - )); - } -}; - -pub const Vector3x8 = struct { - x: f32x8, - y: f32x8, - z: f32x8, - - pub const Array = [24]f32; - - pub const zero = initScalarSingle(0); - pub const one = initScalarSingle(1); - pub const unit_x = initSingle(1, 0, 0); - pub const unit_y = initSingle(0, 1, 0); - pub const unit_z = initSingle(0, 0, 1); - pub const unit_nx = initSingle(-1, 0, 0); - pub const unit_ny = initSingle(0, -1, 0); - pub const unit_nz = initSingle(0, 0, -1); - - // --- INIT ---- - - pub inline fn init(x: f32x8, y: f32x8, z: f32x8) Vector3x8 { - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initSingle(x: f32, y: f32, z: f32) Vector3x8 { - return .{ .x = ps(x), .y = ps(y), .z = ps(z) }; - } - - pub inline fn initScalar(scalar: f32x8) Vector3x8 { - return .{ .x = scalar, .y = scalar, .z = scalar }; - } - - pub inline fn initScalarSingle(scalar: f32) Vector3x8 { - return .{ .x = ps(scalar), .y = ps(scalar), .z = ps(scalar) }; - } - - pub inline fn initSplat(vector: Vector3) Vector3x8 { - return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z) }; - } - - pub inline fn initArray(array: Array) Vector3x8 { - const x: f32x8 = array[0..8].*; - const y: f32x8 = array[8..16].*; - const z: f32x8 = array[16..24].*; - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initArrayTranspose(array: Array) Vector3x8 { - const vector: @Vector(24, f32) = array; - const x: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); - const y: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); - const z: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initArrayOfVectors(vectors: [8]Vector3) Vector3x8 { - return initArrayTranspose(@bitCast(vectors)); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector3x8) Array { - const x: [8]f32 = self.x; - const y: [8]f32 = self.y; - const z: [8]f32 = self.z; - return x ++ y ++ z; - } - - pub inline fn asArrayTranspose(self: Vector3x8) Array { - const vector: @Vector(24, f32) = self.asArray(); - const transposed: @Vector(24, f32) = @shuffle(f32, vector, undefined, [_]i32{ - 0, 8, 16, - 1, 9, 17, - 2, 10, 18, - 3, 11, 19, - 4, 12, 20, - 5, 13, 21, - 6, 14, 22, - 7, 15, 23, - }); - return transposed; - } - - pub inline fn asArrayOfVectors(self: Vector3x8) [8]Vector3 { - return @bitCast(self.asArrayTranspose()); - } - - pub inline fn unpack(self: Vector3x8) [3]f32x8 { - return .{ self.x, self.y, self.z }; - } - - // --- LOAD AND STORE --- - - pub inline fn loadArray(self: *Vector3x8, array: *const Array) void { - self.x = array[0..8].*; - self.y = array[8..16].*; - self.z = array[16..24].*; - } - - pub inline fn loadArrayTranspose(self: *Vector3x8, array: *const Array) void { - const vector: @Vector(24, f32) = array; - self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); - self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); - self.z = @shuffle(f32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); - } - - pub inline fn loadArrayOfVectors(self: *Vector3x8, vectors: *const [8]Vector3) void { - self.loadArrayTranspose(@ptrCast(vectors)); - } - - pub inline fn storeArray(self: *const Vector3x8, array: *Array) void { - array[0..8].* = self.x; - array[8..16].* = self.y; - array[16..24].* = self.z; - } - - pub inline fn storeArrayTranspose(self: *const Vector3x8, array: *Array) void { - const vector: @Vector(24, f32) = self.asArray(); - const transposed: @Vector(24, f32) = @shuffle(f32, vector, undefined, [_]i32{ - 0, 8, 16, - 1, 9, 17, - 2, 10, 18, - 3, 11, 19, - 4, 12, 20, - 5, 13, 21, - 6, 14, 22, - 7, 15, 23, - }); - array.* = transposed; - } - - pub inline fn storeArrayOfVectors(self: *const Vector3x8, vectors: *[8]Vector3) void { - self.storeArrayTranspose(@ptrCast(vectors)); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; - } - - pub inline fn sub(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; - } - - pub inline fn mul(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; - } - - pub inline fn mulScalar(self: Vector3x8, scalar: f32x8) Vector3x8 { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; - } - - pub inline fn mulScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 { - return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar), .z = self.z * ps(scalar) }; - } - - pub inline fn div(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z }; - } - - pub inline fn divScalar(self: Vector3x8, scalar: f32x8) Vector3x8 { - return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar }; - } - - pub inline fn divScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 { - return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar), .z = self.z / ps(scalar) }; - } - - pub inline fn negate(self: Vector3x8) Vector3x8 { - return .{ .x = -self.x, .y = -self.y, .z = -self.z }; - } - - pub inline fn abs(self: Vector3x8) Vector3x8 { - return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) }; - } - - pub inline fn floor(self: Vector3x8) Vector3x8 { - return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) }; - } - - pub inline fn ceil(self: Vector3x8) Vector3x8 { - return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) }; - } - - pub inline fn round(self: Vector3x8) Vector3x8 { - return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) }; - } - - pub inline fn min(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; - } - - pub inline fn max(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; - } - - // --- OTHER --- - - pub inline fn len(self: Vector3x8) f32x8 { - return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z); - } - - pub inline fn lenSquared(self: Vector3x8) f32x8 { - return self.x * self.x + self.y * self.y + self.z * self.z; - } - - pub inline fn dot(self: Vector3x8, other: Vector3x8) f32x8 { - return self.x * other.x + self.y * other.y + self.z * other.z; - } - - pub inline fn cross(self: Vector3x8, other: Vector3x8) Vector3x8 { - return .{ - .x = self.y * other.z - self.z * other.y, - .y = self.z * other.x - self.x * other.z, - .z = self.x * other.y - self.y * other.x, - }; - } - - pub inline fn lerp(a: Vector3x8, b: Vector3x8, t: f32x8) Vector3x8 { - 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)), - .z = @mulAdd(f32x8, t, b.z, @mulAdd(f32x8, -t, a.z, a.z)), - }; - } - - pub inline fn rotate(self: Vector3x8, quaternion: Quaternion_x8) Vector3x8 { - const quaternion_scalar = quaternion.getScalar(); - const quaternion_vector = quaternion.getVector(); - - return .add(self, .cross( - .add(quaternion_vector, quaternion_vector), - .add(.cross(quaternion_vector, self), .mulScalar(self, quaternion_scalar)), - )); - } -}; - -pub const Vector3Int = extern struct { - x: i32, - y: i32, - z: i32, - - pub const Array = [3]i32; - - pub const zero = initScalar(0); - pub const one = initScalar(1); - pub const unit_x = init(1, 0, 0); - pub const unit_y = init(0, 1, 0); - pub const unit_z = init(0, 0, 1); - pub const unit_nx = init(-1, 0, 0); - pub const unit_ny = init(0, -1, 0); - pub const unit_nz = init(0, 0, -1); - - // --- INIT ---- - - pub inline fn init(x: i32, y: i32, z: i32) Vector3Int { - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initScalar(scalar: i32) Vector3Int { - return .{ .x = scalar, .y = scalar, .z = scalar }; - } - - pub inline fn initArray(array: Array) Vector3Int { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector3Int) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Vector3Int) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Vector3Int) *const Array { - return @ptrCast(self); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; - } - - pub inline fn sub(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; - } - - pub inline fn mul(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; - } - - pub inline fn mulScalar(self: Vector3Int, scalar: f32) Vector3Int { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; - } - - pub inline fn div(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z) }; - } - - pub inline fn divScalar(self: Vector3Int, scalar: f32) Vector3Int { - return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) }; - } - - pub inline fn mod(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z) }; - } - - pub inline fn modScalar(self: Vector3Int, scalar: f32) Vector3Int { - return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) }; - } - - pub inline fn negate(self: Vector3Int) Vector3Int { - return .{ .x = -self.x, .y = -self.y, .z = -self.z }; - } - - pub inline fn abs(self: Vector3Int) Vector3Int { - return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)) }; - } - - pub inline fn min(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; - } - - pub inline fn max(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; - } - - // --- OTHER --- - - pub inline fn lenSquared(self: Vector3Int) i32 { - return self.x * self.x + self.y * self.y + self.z * self.z; - } - - pub inline fn dot(self: Vector3Int, other: Vector3Int) i32 { - return self.x * other.x + self.y * other.y + self.z * other.z; - } - - pub inline fn cross(self: Vector3Int, other: Vector3Int) Vector3Int { - return .{ - .x = self.y * other.z - self.z * other.y, - .y = self.z * other.x - self.x * other.z, - .z = self.x * other.y - self.y * other.x, - }; - } -}; - -pub const Vector3Int_x8 = struct { - x: i32x8, - y: i32x8, - z: i32x8, - - pub const Array = [24]i32; - - pub const zero = initScalarSingle(0); - pub const one = initScalarSingle(1); - pub const unit_x = initSingle(1, 0, 0); - pub const unit_y = initSingle(0, 1, 0); - pub const unit_z = initSingle(0, 0, 1); - pub const unit_nx = initSingle(-1, 0, 0); - pub const unit_ny = initSingle(0, -1, 0); - pub const unit_nz = initSingle(0, 0, -1); - - // --- INIT ---- - - pub inline fn init(x: i32x8, y: i32x8, z: i32x8) Vector3Int_x8 { - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initSingle(x: i32, y: i32, z: i32) Vector3Int_x8 { - return .{ .x = epi32(x), .y = epi32(y), .z = epi32(z) }; - } - - pub inline fn initScalar(scalar: i32x8) Vector3Int_x8 { - return .{ .x = scalar, .y = scalar, .z = scalar }; - } - - pub inline fn initScalarSingle(scalar: i32) Vector3Int_x8 { - return .{ .x = epi32(scalar), .y = epi32(scalar), .z = epi32(scalar) }; - } - - pub inline fn initSplat(vector: Vector3Int) Vector3Int_x8 { - return .{ .x = epi32(vector.x), .y = epi32(vector.y), .z = epi32(vector.z) }; - } - - pub inline fn initArray(array: Array) Vector3Int_x8 { - const x: i32x8 = array[0..8].*; - const y: i32x8 = array[8..16].*; - const z: i32x8 = array[16..24].*; - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initArrayTranspose(array: Array) Vector3Int_x8 { - const vector: @Vector(24, i32) = array; - const x: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); - const y: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); - const z: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); - return .{ .x = x, .y = y, .z = z }; - } - - pub inline fn initArrayOfVectors(vectors: [8]Vector3Int) Vector3Int_x8 { - return initArrayTranspose(@bitCast(vectors)); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector3Int_x8) Array { - const x: [8]i32 = self.x; - const y: [8]i32 = self.y; - const z: [8]i32 = self.z; - return x ++ y ++ z; - } - - pub inline fn asArrayTranspose(self: Vector3Int_x8) Array { - const vector: @Vector(24, i32) = self.asArray(); - const transposed: @Vector(24, i32) = @shuffle(i32, vector, undefined, [_]i32{ - 0, 8, 16, - 1, 9, 17, - 2, 10, 18, - 3, 11, 19, - 4, 12, 20, - 5, 13, 21, - 6, 14, 22, - 7, 15, 23, - }); - return transposed; - } - - pub inline fn asArrayOfVectors(self: Vector3Int_x8) [8]Vector3Int { - return @bitCast(self.asArrayTranspose()); - } - - pub inline fn unpack(self: Vector3Int_x8) [3]i32x8 { - return .{ self.x, self.y, self.z }; - } - - // --- LOAD AND STORE --- - - pub inline fn loadArray(self: *Vector3Int_x8, array: *const Array) void { - self.x = array[0..8].*; - self.y = array[8..16].*; - self.z = array[16..24].*; - } - - pub inline fn loadArrayTranspose(self: *Vector3Int_x8, array: *const Array) void { - const vector: @Vector(24, i32) = array; - self.x = @shuffle(i32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); - self.y = @shuffle(i32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); - self.z = @shuffle(i32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); - } - - pub inline fn loadArrayOfVectors(self: *Vector3Int_x8, vectors: *const [8]Vector3Int) void { - self.loadArrayTranspose(@ptrCast(vectors)); - } - - pub inline fn storeArray(self: *const Vector3Int_x8, array: *Array) void { - array[0..8].* = self.x; - array[8..16].* = self.y; - array[16..24].* = self.z; - } - - pub inline fn storeArrayTranspose(self: *const Vector3Int_x8, array: *Array) void { - const vector: @Vector(24, i32) = self.asArray(); - const transposed: @Vector(24, i32) = @shuffle(i32, vector, undefined, [_]i32{ - 0, 8, 16, - 1, 9, 17, - 2, 10, 18, - 3, 11, 19, - 4, 12, 20, - 5, 13, 21, - 6, 14, 22, - 7, 15, 23, - }); - array.* = transposed; - } - - pub inline fn storeArrayOfVectors(self: *const Vector3Int_x8, vectors: *[8]Vector3Int) void { - self.storeArrayTranspose(@ptrCast(vectors)); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; - } - - pub inline fn sub(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; - } - - pub inline fn mul(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; - } - - pub inline fn mulScalar(self: Vector3Int_x8, scalar: i32x8) Vector3Int_x8 { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; - } - - pub inline fn mulScalarSingle(self: Vector3Int_x8, scalar: i32) Vector3Int_x8 { - return .{ .x = self.x * epi32(scalar), .y = self.y * epi32(scalar), .z = self.z * epi32(scalar) }; - } - - pub inline fn div(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z) }; - } - - pub inline fn divScalar(self: Vector3Int_x8, scalar: i32x8) Vector3Int_x8 { - return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) }; - } - - pub inline fn divScalarSingle(self: Vector3Int_x8, scalar: i32) Vector3Int_x8 { - return .{ .x = @divFloor(self.x, epi32(scalar)), .y = @divFloor(self.y, epi32(scalar)), .z = @divFloor(self.z, epi32(scalar)) }; - } - - pub inline fn mod(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z) }; - } - - pub inline fn modScalar(self: Vector3Int_x8, scalar: i32x8) Vector3Int_x8 { - return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) }; - } - - pub inline fn modScalarSingle(self: Vector3Int_x8, scalar: i32) Vector3Int_x8 { - return .{ .x = @mod(self.x, epi32(scalar)), .y = @mod(self.y, epi32(scalar)), .z = @mod(self.z, epi32(scalar)) }; - } - - pub inline fn negate(self: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = -self.x, .y = -self.y, .z = -self.z }; - } - - pub inline fn abs(self: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)) }; - } - - pub inline fn min(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; - } - - pub inline fn max(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { - return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; - } - - // --- OTHER --- - - pub inline fn lenSquared(self: Vector3Int_x8) i32x8 { - return self.x * self.x + self.y * self.y + self.z * self.z; - } - - pub inline fn dot(self: Vector3Int_x8, other: Vector3Int_x8) i32x8 { - return self.x * other.x + self.y * other.y + self.z * other.z; - } - - pub inline fn cross(self: Vector3Int_x8, other: Vector3Int_x8) i32x8 { - return .{ - .x = self.y * other.z - self.z * other.y, - .y = self.z * other.x - self.x * other.z, - .z = self.x * other.y - self.y * other.x, - }; - } -}; - -pub const Vector4 = extern struct { - x: f32, - y: f32, - z: f32, - w: f32, - - pub const Array = [4]f32; - - pub const zero = initScalar(0); - pub const one = initScalar(1); - pub const unit_x = init(1, 0, 0, 0); - pub const unit_y = init(0, 1, 0, 0); - pub const unit_z = init(0, 0, 1, 0); - pub const unit_w = init(0, 0, 0, 1); - pub const unit_nx = init(-1, 0, 0, 0); - pub const unit_ny = init(0, -1, 0, 0); - pub const unit_nz = init(0, 0, -1, 0); - pub const unit_nw = init(0, 0, 0, -1); - - // --- INIT ---- - - pub inline fn init(x: f32, y: f32, z: f32, w: f32) Vector4 { - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initScalar(scalar: f32) Vector4 { - return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; - } - - pub inline fn initArray(array: Array) Vector4 { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector4) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Vector4) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Vector4) *const Array { - return @ptrCast(self); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector4, other: Vector4) Vector4 { - 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: Vector4, other: Vector4) Vector4 { - 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: Vector4, other: Vector4) Vector4 { - 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: Vector4, scalar: f32) Vector4 { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; - } - - pub inline fn div(self: Vector4, other: Vector4) Vector4 { - return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w }; - } - - pub inline fn divScalar(self: Vector4, scalar: f32) Vector4 { - return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar }; - } - - pub inline fn negate(self: Vector4) Vector4 { - return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; - } - - pub inline fn abs(self: Vector4) Vector4 { - return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) }; - } - - pub inline fn floor(self: Vector4) Vector4 { - return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) }; - } - - pub inline fn ceil(self: Vector4) Vector4 { - return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) }; - } - - pub inline fn round(self: Vector4) Vector4 { - return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) }; - } - - pub inline fn min(self: Vector4, other: Vector4) Vector4 { - 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: Vector4, other: Vector4) Vector4 { - 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 len(self: Vector4) f32 { - return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w); - } - - pub inline fn lenSquared(self: Vector4) f32 { - return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; - } - - pub inline fn dot(self: Vector4, other: Vector4) f32 { - return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; - } - - pub inline fn lerp(a: Vector4, b: Vector4, t: f32) Vector4 { - return .{ - .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), - .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), - .z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)), - .w = @mulAdd(f32, t, b.w, @mulAdd(f32, -t, a.w, a.w)), - }; - } -}; - -pub const Vector4x8 = struct { - x: f32x8, - y: f32x8, - z: f32x8, - w: f32x8, - - pub const Array = [32]f32; - - 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: f32x8, y: f32x8, z: f32x8, w: f32x8) Vector4x8 { - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initSingle(x: f32, y: f32, z: f32, w: f32) Vector4x8 { - return .{ .x = ps(x), .y = ps(y), .z = ps(z), .w = ps(w) }; - } - - pub inline fn initScalar(scalar: f32x8) Vector4x8 { - return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; - } - - pub inline fn initScalarSingle(scalar: f32) Vector4x8 { - return .{ .x = ps(scalar), .y = ps(scalar), .z = ps(scalar), .w = ps(scalar) }; - } - - pub inline fn initSplat(vector: Vector4) Vector4x8 { - return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z), .w = ps(vector.w) }; - } - - pub inline fn initArray(array: Array) Vector4x8 { - const x: f32x8 = array[0..8].*; - const y: f32x8 = array[8..16].*; - const z: f32x8 = array[16..24].*; - const w: f32x8 = array[24..32].*; - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initArrayTranspose(array: Array) Vector4x8 { - const vector: @Vector(32, f32) = array; - const x: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }); - const y: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }); - const z: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }); - const w: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }); - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initArrayOfVectors(vectors: [8]Vector4) Vector4x8 { - return initArrayTranspose(@bitCast(vectors)); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector4x8) Array { - const x: [8]f32 = self.x; - const y: [8]f32 = self.y; - const z: [8]f32 = self.z; - const w: [8]f32 = self.w; - return x ++ y ++ z ++ w; - } - - pub inline fn asArrayTranspose(self: Vector4x8) Array { - const vector: @Vector(32, f32) = self.asArray(); - const transposed: @Vector(32, f32) = @shuffle(f32, 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, - }); - return transposed; - } - - pub inline fn asArrayOfVectors(self: Vector4x8) [8]Vector4 { - return @bitCast(self.asArrayTranspose()); - } - - pub inline fn unpack(self: Vector4x8) [4]f32x8 { - return .{ self.x, self.y, self.z, self.w }; - } - - // --- LOAD AND STORE --- - - pub inline fn loadArray(self: *Vector4x8, array: *const Array) void { - self.x = array[0..8].*; - self.y = array[8..16].*; - self.z = array[16..24].*; - self.w = array[24..32].*; - } - - pub inline fn loadArrayTranspose(self: *Vector4x8, array: *const Array) void { - const vector: @Vector(32, f32) = array; - self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }); - self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }); - self.z = @shuffle(f32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }); - self.w = @shuffle(f32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }); - } - - pub inline fn loadArrayOfVectors(self: *Vector4x8, vectors: *const [8]Vector4) void { - self.loadArrayTranspose(@ptrCast(vectors)); - } - - pub inline fn storeArray(self: *const Vector4x8, array: *Array) void { - array[0..8].* = self.x; - array[8..16].* = self.y; - array[16..24].* = self.z; - array[24..32].* = self.w; - } - - pub inline fn storeArrayTranspose(self: *const Vector4x8, array: *Array) void { - const vector: @Vector(32, f32) = self.asArray(); - const transposed: @Vector(32, f32) = @shuffle(f32, 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, - }); - array.* = transposed; - } - - pub inline fn storeArrayOfVectors(self: *const Vector4x8, vectors: *[8]Vector4) void { - self.storeArrayTranspose(@ptrCast(vectors)); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector4x8, other: Vector4x8) Vector4x8 { - 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: Vector4x8, other: Vector4x8) Vector4x8 { - 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: Vector4x8, other: Vector4x8) Vector4x8 { - 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: Vector4x8, scalar: f32x8) Vector4x8 { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; - } - - pub inline fn mulScalarSingle(self: Vector4x8, scalar: f32) Vector4x8 { - return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar), .z = self.z * ps(scalar), .w = self.w * ps(scalar) }; - } - - pub inline fn div(self: Vector4x8, other: Vector4x8) Vector4x8 { - return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w }; - } - - pub inline fn divScalar(self: Vector4x8, scalar: f32x8) Vector4x8 { - return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar }; - } - - pub inline fn divScalarSingle(self: Vector4x8, scalar: f32) Vector4x8 { - return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar), .z = self.z / ps(scalar), .w = self.w / ps(scalar) }; - } - - pub inline fn negate(self: Vector4x8) Vector4x8 { - return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; - } - - pub inline fn abs(self: Vector4x8) Vector4x8 { - return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) }; - } - - pub inline fn floor(self: Vector4x8) Vector4x8 { - return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) }; - } - - pub inline fn ceil(self: Vector4x8) Vector4x8 { - return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) }; - } - - pub inline fn round(self: Vector4x8) Vector4x8 { - return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) }; - } - - pub inline fn min(self: Vector4x8, other: Vector4x8) Vector4x8 { - 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: Vector4x8, other: Vector4x8) Vector4x8 { - 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 len(self: Vector4x8) f32x8 { - return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w); - } - - pub inline fn lenSquared(self: Vector4x8) f32x8 { - return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; - } - - pub inline fn dot(self: Vector4x8, other: Vector4x8) f32x8 { - return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; - } - - pub inline fn lerp(a: Vector4x8, b: Vector4x8, t: f32x8) Vector4x8 { - 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)), - .z = @mulAdd(f32x8, t, b.z, @mulAdd(f32x8, -t, a.z, a.z)), - .w = @mulAdd(f32x8, t, b.w, @mulAdd(f32x8, -t, a.w, a.w)), - }; - } -}; - -pub const Vector4Int = extern struct { - x: i32, - y: i32, - z: i32, - w: i32, - - pub const Array = [4]i32; - - pub const zero = initScalar(0); - pub const one = initScalar(1); - pub const unit_x = init(1, 0, 0, 0); - pub const unit_y = init(0, 1, 0, 0); - pub const unit_z = init(0, 0, 1, 0); - pub const unit_w = init(0, 0, 0, 1); - pub const unit_nx = init(-1, 0, 0, 0); - pub const unit_ny = init(0, -1, 0, 0); - pub const unit_nz = init(0, 0, -1, 0); - pub const unit_nw = init(0, 0, 0, -1); - - // --- INIT ---- - - pub inline fn init(x: i32, y: i32, z: i32, w: i32) Vector4Int { - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initScalar(scalar: i32) Vector4Int { - return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; - } - - pub inline fn initArray(array: Array) Vector4Int { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector4Int) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Vector4Int) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Vector4Int) *const Array { - return @ptrCast(self); - } - - // --- COMPONENT-WISE --- - - pub inline fn add(self: Vector4Int, other: Vector4Int) Vector4Int { - 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, other: Vector4Int) Vector4Int { - 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, other: Vector4Int) Vector4Int { - 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, scalar: f32) Vector4Int { - return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; - } - - pub inline fn div(self: Vector4Int, other: Vector4Int) Vector4Int { - 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, scalar: f32) Vector4Int { - return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar), .w = @divFloor(self.w, scalar) }; - } - - pub inline fn mod(self: Vector4Int, other: Vector4Int) Vector4Int { - 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, scalar: f32) Vector4Int { - return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar), .w = @mod(self.w, scalar) }; - } - - pub inline fn negate(self: Vector4Int) Vector4Int { - return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; - } - - pub inline fn abs(self: Vector4Int) Vector4Int { - 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, other: Vector4Int) Vector4Int { - 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, other: Vector4Int) Vector4Int { - 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) i32 { - return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; - } - - pub inline fn dot(self: Vector4Int, other: Vector4Int) i32 { - return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; - } -}; - -pub const Vector4Int_x8 = struct { - x: i32x8, - y: i32x8, - z: i32x8, - w: i32x8, - - pub const Array = [32]i32; - - 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: i32x8, y: i32x8, z: i32x8, w: 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 = epi32(x), .y = epi32(y), .z = epi32(z), .w = epi32(w) }; - } - - pub inline fn initScalar(scalar: i32x8) Vector4Int_x8 { - return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; - } - - pub inline fn initScalarSingle(scalar: i32) Vector4Int_x8 { - return .{ .x = epi32(scalar), .y = epi32(scalar), .z = epi32(scalar), .w = epi32(scalar) }; - } - - pub inline fn initSplat(vector: Vector4Int) Vector4Int_x8 { - return .{ .x = epi32(vector.x), .y = epi32(vector.y), .z = epi32(vector.z), .w = epi32(vector.w) }; - } - - pub inline fn initArray(array: Array) Vector4Int_x8 { - const x: i32x8 = array[0..8].*; - const y: i32x8 = array[8..16].*; - const z: i32x8 = array[16..24].*; - const w: i32x8 = array[24..32].*; - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initArrayTranspose(array: Array) Vector4Int_x8 { - const vector: @Vector(32, i32) = array; - const x: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }); - const y: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }); - const z: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }); - const w: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }); - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initArrayOfVectors(vectors: [8]Vector4Int) Vector4Int_x8 { - return initArrayTranspose(@bitCast(vectors)); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Vector4Int_x8) Array { - const x: [8]i32 = self.x; - const y: [8]i32 = self.y; - const z: [8]i32 = self.z; - const w: [8]i32 = self.w; - return x ++ y ++ z ++ w; - } - - pub inline fn asArrayTranspose(self: Vector4Int_x8) Array { - const vector: @Vector(32, i32) = self.asArray(); - const transposed: @Vector(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, - }); - return transposed; - } - - pub inline fn asArrayOfVectors(self: Vector4Int_x8) [8]Vector4Int { - return @bitCast(self.asArrayTranspose()); - } - - pub inline fn unpack(self: Vector4Int_x8) [4]i32x8 { - return .{ self.x, self.y, self.z, self.w }; - } - - // --- LOAD AND STORE --- - - pub inline fn loadArray(self: *Vector4Int_x8, array: *const Array) void { - self.x = array[0..8].*; - self.y = array[8..16].*; - self.z = array[16..24].*; - self.w = array[24..32].*; - } - - pub inline fn loadArrayTranspose(self: *Vector4Int_x8, array: *const Array) void { - const vector: @Vector(24, i32) = 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 loadArrayOfVectors(self: *Vector4Int_x8, vectors: *const [8]Vector4Int) void { - self.loadArrayTranspose(@ptrCast(vectors)); - } - - pub inline fn storeArray(self: *const Vector4Int_x8, array: *Array) void { - array[0..8].* = self.x; - array[8..16].* = self.y; - array[16..24].* = self.z; - array[24..32].* = self.w; - } - - pub inline fn storeArrayTranspose(self: *const Vector4Int_x8, array: *Array) void { - const vector: @Vector(32, i32) = self.asArray(); - const transposed: @Vector(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, - }); - array.* = transposed; - } - - pub inline fn storeArrayOfVectors(self: *const Vector4Int_x8, vectors: *[8]Vector4Int) void { - self.storeArrayTranspose(@ptrCast(vectors)); - } - - // --- 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: 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 * epi32(scalar), .y = self.y * epi32(scalar), .z = self.z * epi32(scalar), .w = self.w * 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: 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, epi32(scalar)), .y = @divFloor(self.y, epi32(scalar)), .z = @divFloor(self.z, epi32(scalar)), .w = @divFloor(self.w, 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: 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, epi32(scalar)), .y = @mod(self.y, epi32(scalar)), .z = @mod(self.z, epi32(scalar)), .w = @mod(self.w, 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) 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) i32x8 { - return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; - } -}; - -// --- QUATERNION -------------------------------------------------------------- - -pub const Quaternion = extern struct { - x: f32, - y: f32, - z: f32, - w: f32, - - pub const Array = [4]f32; - - pub const identity = init(0, 0, 0, 1); - - // --- INIT --- - - pub inline fn init(x: f32, y: f32, z: f32, w: f32) Quaternion { - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initRotationXY(angle_turns: f32) Quaternion { - const half_angle_turns = 0.5 * angle_turns; - const c, const s = cossin(half_angle_turns).asArray(); - return .{ .x = 0, .y = 0, .z = s, .w = c }; - } - - pub inline fn initRotationXZ(angle_turns: f32) Quaternion { - const half_angle_turns = 0.5 * angle_turns; - const c, const s = cossin(half_angle_turns).asArray(); - return .{ .x = 0, .y = -s, .z = 0, .w = c }; - } - - pub inline fn initRotationYX(angle_turns: f32) Quaternion { - const half_angle_turns = 0.5 * angle_turns; - const c, const s = cossin(half_angle_turns).asArray(); - return .{ .x = 0, .y = 0, .z = -s, .w = c }; - } - - pub inline fn initRotationYZ(angle_turns: f32) Quaternion { - const half_angle_turns = 0.5 * angle_turns; - const c, const s = cossin(half_angle_turns).asArray(); - return .{ .x = s, .y = 0, .z = 0, .w = c }; - } - - pub inline fn initRotationZX(angle_turns: f32) Quaternion { - const half_angle_turns = 0.5 * angle_turns; - const c, const s = cossin(half_angle_turns).asArray(); - return .{ .x = 0, .y = s, .z = 0, .w = c }; - } - - pub inline fn initRotationZY(angle_turns: f32) Quaternion { - const half_angle_turns = 0.5 * angle_turns; - const c, const s = cossin(half_angle_turns).asArray(); - return .{ .x = -s, .y = 0, .z = 0, .w = c }; - } - - pub inline fn initArray(array: Array) Quaternion { - return @bitCast(array); - } - - pub inline fn initVector4(vector: Vector4) Quaternion { - return @bitCast(vector); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Quaternion) Array { - return @bitCast(self); - } - - pub inline fn asVector4(self: Quaternion) Vector4 { - return @bitCast(self); - } - - // --- ACCESSORS --- - - pub inline fn getVector(self: Quaternion) Vector3 { - return .{ .x = self.x, .y = self.y, .z = self.z }; - } - - pub inline fn getScalar(self: Quaternion) f32 { - return self.w; - } -}; - -pub const Quaternion_x8 = extern struct { - x: f32x8, - y: f32x8, - z: f32x8, - w: f32x8, - - pub const Array = [32]f32; - - pub const identity = initSingle(0, 0, 0, 1); - - // --- INIT --- - - pub inline fn init(x: f32x8, y: f32x8, z: f32x8, w: f32x8) Quaternion_x8 { - return .{ .x = x, .y = y, .z = z, .w = w }; - } - - pub inline fn initSingle(x: f32, y: f32, z: f32, w: f32) Quaternion_x8 { - return .{ .x = ps(x), .y = ps(y), .z = ps(z), .w = ps(w) }; - } - - pub inline fn initRotationXY(angle_turns: f32x8) Quaternion_x8 { - const half_angle_turns = ps(0.5) * angle_turns; - const c, const s = cossin_x8(half_angle_turns).unpack(); - return .{ .x = ps(0), .y = ps(0), .z = s, .w = c }; - } - - pub inline fn initRotationXZ(angle_turns: f32x8) Quaternion_x8 { - const half_angle_turns = ps(0.5) * angle_turns; - const c, const s = cossin_x8(half_angle_turns).unpack(); - return .{ .x = ps(0), .y = -s, .z = ps(0), .w = c }; - } - - pub inline fn initRotationYX(angle_turns: f32x8) Quaternion_x8 { - const half_angle_turns = ps(0.5) * angle_turns; - const c, const s = cossin_x8(half_angle_turns).unpack(); - return .{ .x = ps(0), .y = ps(0), .z = -s, .w = c }; - } - - pub inline fn initRotationYZ(angle_turns: f32x8) Quaternion_x8 { - const half_angle_turns = ps(0.5) * angle_turns; - const c, const s = cossin_x8(half_angle_turns).unpack(); - return .{ .x = s, .y = ps(0), .z = ps(0), .w = c }; - } - - pub inline fn initRotationZX(angle_turns: f32x8) Quaternion_x8 { - const half_angle_turns = ps(0.5) * angle_turns; - const c, const s = cossin_x8(half_angle_turns).unpack(); - return .{ .x = ps(0), .y = s, .z = ps(0), .w = c }; - } - - pub inline fn initRotationZY(angle_turns: f32x8) Quaternion_x8 { - const half_angle_turns = ps(0.5) * angle_turns; - const c, const s = cossin_x8(half_angle_turns).unpack(); - return .{ .x = -s, .y = ps(0), .z = ps(0), .w = c }; - } - - //pub inline fn initArray() Quaternion_x8 {} - - //pub inline fn initArrayTranspose() Quaternion_x8 {} - - //pub inline fn initArrayOfQuaternions() Quaternion_x8 {} - - //pub inline fn initVector4x8(vector: Vector4x8) Quaternion_x8 {} - - pub inline fn initSplat(quaternion: Quaternion) Quaternion_x8 { - return .{ .x = ps(quaternion.x), .y = ps(quaternion.y), .z = ps(quaternion.z), .w = ps(quaternion.w) }; - } - - pub inline fn initSplatVector4(vector: Vector4) Quaternion_x8 { - return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z), .w = ps(vector.w) }; - } - - // --- ACCESSORS --- - - pub inline fn getVector(self: Quaternion_x8) Vector3x8 { - return .{ .x = self.x, .y = self.y, .z = self.z }; - } - - pub inline fn getScalar(self: Quaternion_x8) f32x8 { - return self.w; - } -}; - -// --- MATRICES ---------------------------------------------------------------- - -pub const Matrix3x2 = extern struct { - ix: f32, - iy: f32, - jx: f32, - jy: f32, - tx: f32, - ty: f32, - - pub const Array = [6]f32; - - pub const identity = init(1, 0, 0, 1, 0, 0); - - // --- INIT --- - - pub inline fn init(ix: f32, iy: f32, jx: f32, jy: f32, tx: f32, ty: f32) Matrix3x2 { - return .{ .ix = ix, .iy = iy, .jx = jx, .jy = jy, .tx = tx, .ty = ty }; - } - - pub inline fn initTranslation(t: Vector2) Matrix3x2 { - return .{ .ix = 1, .iy = 0, .jx = 0, .jy = 1, .tx = t.x, .ty = t.y }; - } - - pub inline fn initRotation(angle_turns: f32) Matrix3x2 { - const c, const s = cossin(angle_turns).asArray(); - return .{ .ix = c, .iy = s, .jx = -s, .jy = c, .tx = 0, .ty = 0 }; - } - - pub inline fn initScale(s: Vector2) Matrix3x2 { - return .{ .ix = s.x, .iy = 0, .jx = 0, .jy = s.y, .tx = 0, .ty = 0 }; - } - - pub inline fn initArray(array: Array) Matrix3x2 { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Matrix3x2) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Matrix3x2) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Matrix3x2) *const Array { - return @ptrCast(self); - } - - // --- TRANSFORM --- - - pub inline fn transformPoint(self: Matrix3x2, p: Vector2) Vector2 { - return .{ - .x = p.x * self.ix + p.y * self.jx + self.tx, - .y = p.x * self.iy + p.y * self.jy + self.ty, - }; - } - - pub inline fn transformPoint_x8(self: Matrix3x2, p: Vector2x8) Vector2x8 { - return .{ - .x = p.x * ps(self.ix) + p.y * ps(self.jx) + ps(self.tx), - .y = p.x * ps(self.iy) + p.y * ps(self.jy) + ps(self.ty), - }; - } - - pub inline fn transformVector(self: Matrix3x2, v: Vector2) Vector2 { - return .{ - .x = v.x * self.ix + v.y * self.jx, - .y = v.x * self.iy + v.y * self.jy, - }; - } - - pub inline fn transformVector_x8(self: Matrix3x2, v: Vector2x8) Vector2x8 { - return .{ - .x = v.x * ps(self.ix) + v.y * ps(self.jx), - .y = v.x * ps(self.iy) + v.y * ps(self.jy), - }; - } -}; - -pub const Matrix4x4 = extern struct { - ix: f32, - iy: f32, - iz: f32, - iw: f32, - jx: f32, - jy: f32, - jz: f32, - jw: f32, - kx: f32, - ky: f32, - kz: f32, - kw: f32, - tx: f32, - ty: f32, - tz: f32, - tw: f32, - - pub const Array = [16]f32; - - pub const identity = init(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); - - // --- INIT --- - - pub inline fn init(ix: f32, iy: f32, iz: f32, iw: f32, jx: f32, jy: f32, jz: f32, jw: f32, kx: f32, ky: f32, kz: f32, kw: f32, tx: f32, ty: f32, tz: f32, tw: f32) Matrix4x4 { - return .{ .ix = ix, .iy = iy, .iz = iz, .iw = iw, .jx = jx, .jy = jy, .jz = jz, .jw = jw, .kx = kx, .ky = ky, .kz = kz, .kw = kw, .tx = tx, .ty = ty, .tz = tz, .tw = tw }; - } - - pub inline fn initTranslation(t: Vector3) Matrix4x4 { - return .{ .ix = 1, .iy = 0, .iz = 0, .iw = 0, .jx = 0, .jy = 1, .jz = 0, .jw = 0, .kx = 0, .ky = 0, .kz = 1, .kw = 0, .tx = t.x, .ty = t.y, .tz = t.z, .tw = 1 }; - } - - pub inline fn initRotation(q: Quaternion) Matrix4x4 { - const xx = q.x * q.x; - const xy = q.x * q.y; - const xz = q.x * q.z; - const xw = q.x * q.w; - const yy = q.y * q.y; - const yz = q.y * q.z; - const yw = q.y * q.w; - const zz = q.z * q.z; - const zw = q.z * q.w; - - return .{ .ix = 1 - 2 * (yy + zz), .jx = 2 * (xy + zw), .kx = 2 * (xz - yw), .tx = 0, .iy = 2 * (xy - zw), .jy = 1 - 2 * (xx + zz), .ky = 2 * (yz + xw), .ty = 0, .iz = 2 * (xz + yw), .jz = 2 * (yz - xw), .kz = 1 - 2 * (xx + yy), .tz = 0, .iw = 0, .jw = 0, .kw = 0, .tw = 1 }; - } - - pub inline fn initScale(s: Vector3) Matrix4x4 { - return .{ .ix = s.x, .iy = 0, .iz = 0, .iw = 0, .jx = 0, .jy = s.y, .jz = 0, .jw = 0, .kx = 0, .ky = 0, .kz = s.z, .kw = 0, .tx = 0, .ty = 0, .tz = 0, .tw = 1 }; - } - - pub inline fn initArray(array: Array) Matrix4x4 { - return @bitCast(array); - } - - // --- CONVERSION --- - - pub inline fn asArray(self: Matrix4x4) Array { - return @bitCast(self); - } - - pub inline fn asArrayPtr(self: *Matrix4x4) *Array { - return @ptrCast(self); - } - - pub inline fn asArrayConstPtr(self: *const Matrix4x4) *const Array { - return @ptrCast(self); - } - - // --- TRANSFORM --- - - pub inline fn transformPoint(self: Matrix4x4, p: Vector3) Vector3 { - return .{ - .x = p.x * self.ix + p.y * self.jx + p.z * self.kx + self.tx, - .y = p.x * self.iy + p.y * self.jy + p.z * self.ky + self.ty, - .z = p.x * self.iz + p.y * self.jz + p.z * self.kz + self.tz, - }; - } - - pub inline fn transformPoint_x8(self: Matrix4x4, p: Vector3x8) Vector3x8 { - return .{ - .x = p.x * ps(self.ix) + p.y * ps(self.jx) + p.z * ps(self.kx) + ps(self.tx), - .y = p.x * ps(self.iy) + p.y * ps(self.jy) + p.z * ps(self.ky) + ps(self.ty), - .z = p.x * ps(self.iz) + p.y * ps(self.jz) + p.z * ps(self.kz) + ps(self.tz), - }; - } - - pub inline fn transformVector(self: Matrix4x4, v: Vector3) Vector3 { - return .{ - .x = v.x * self.ix + v.y * self.jx + v.z * self.kx, - .y = v.x * self.iy + v.y * self.jy + v.z * self.ky, - .z = v.x * self.iz + v.y * self.jz + v.z * self.kz, - }; - } - - pub inline fn transformVector_x8(self: Matrix4x4, v: Vector3x8) Vector3x8 { - return .{ - .x = v.x * ps(self.ix) + v.y * ps(self.jx) + v.z * ps(self.kx), - .y = v.x * ps(self.iy) + v.y * ps(self.jy) + v.z * ps(self.ky), - .z = v.x * ps(self.iz) + v.y * ps(self.jz) + v.z * ps(self.kz), - }; - } - - pub inline fn transformHomogeneous(self: Matrix4x4, h: Vector4) Vector4 { - return .{ - .x = h.x * self.ix + h.y * self.jx + h.z * self.kx + h.w * self.tx, - .y = h.x * self.iy + h.y * self.jy + h.z * self.ky + h.w * self.ty, - .z = h.x * self.iz + h.y * self.jz + h.z * self.kz + h.w * self.tz, - .w = h.x * self.iw + h.y * self.jw + h.z * self.kw + h.w * self.tw, - }; - } - - pub inline fn transformHomogeneous_x8(self: Matrix4x4, h: Vector4x8) Vector4x8 { - return .{ - .x = h.x * ps(self.ix) + h.y * ps(self.jx) + h.z * ps(self.kx) + h.w * ps(self.tx), - .y = h.x * ps(self.iy) + h.y * ps(self.jy) + h.z * ps(self.ky) + h.w * ps(self.ty), - .z = h.x * ps(self.iz) + h.y * ps(self.jz) + h.z * ps(self.kz) + h.w * ps(self.tz), - .w = h.x * ps(self.iw) + h.y * ps(self.jw) + h.z * ps(self.kw) + h.w * ps(self.tw), - }; - } -}; - -// --- COLORS ------------------------------------------------------------------ - -pub const Color = extern struct { - r: u8, - g: u8, - b: u8, - a: u8, - - pub const Array = [4]u8; - - pub const clear = init(0, 0, 0, 0); - pub const black = initOpaque(0, 0, 0); - pub const red = initOpaque(255, 0, 0); - pub const green = initOpaque(0, 255, 0); - pub const blue = initOpaque(0, 0, 255); - pub const cyan = initOpaque(0, 255, 255); - pub const magenta = initOpaque(255, 0, 255); - pub const yellow = initOpaque(255, 255, 0); - pub const white = initOpaque(255, 255, 255); - - pub inline fn init(r: u8, g: u8, b: u8, a: u8) Color { - return .{ .r = r, .g = g, .b = b, .a = a }; - } - - pub inline fn initOpaque(r: u8, g: u8, b: u8) Color { - return .{ .r = r, .g = g, .b = b, .a = 255 }; - } - - pub inline fn initArray(array: Array) Color { - return @bitCast(array); - } - - pub inline fn l(comptime literal: []const u8) Color { - if (literal.len == 4 and literal[0] == '#') { // #RGB - return .initOpaque( - (std.fmt.parseUnsigned(u8, literal[1..2], 16) catch unreachable) * 0x11, - (std.fmt.parseUnsigned(u8, literal[2..3], 16) catch unreachable) * 0x11, - (std.fmt.parseUnsigned(u8, literal[3..4], 16) catch unreachable) * 0x11, - ); - } else if (literal.len == 5 and literal[0] == '#') { // #RGBA - return .init( - (std.fmt.parseUnsigned(u8, literal[1..2], 16) catch unreachable) * 0x11, - (std.fmt.parseUnsigned(u8, literal[2..3], 16) catch unreachable) * 0x11, - (std.fmt.parseUnsigned(u8, literal[3..4], 16) catch unreachable) * 0x11, - (std.fmt.parseUnsigned(u8, literal[4..5], 16) catch unreachable) * 0x11, - ); - } else if (literal.len == 7 and literal[0] == '#') { // #RRGGBB - return .initOpaque( - (std.fmt.parseUnsigned(u8, literal[1..3], 16) catch unreachable), - (std.fmt.parseUnsigned(u8, literal[3..5], 16) catch unreachable), - (std.fmt.parseUnsigned(u8, literal[5..7], 16) catch unreachable), - ); - } else if (literal.len == 9 and literal[0] == '#') { // #RRGGBBAA - return .init( - (std.fmt.parseUnsigned(u8, literal[1..3], 16) catch unreachable), - (std.fmt.parseUnsigned(u8, literal[3..5], 16) catch unreachable), - (std.fmt.parseUnsigned(u8, literal[5..7], 16) catch unreachable), - (std.fmt.parseUnsigned(u8, literal[7..9], 16) catch unreachable), - ); - } - - @compileError("Invalid color literal: " ++ literal); - } - - pub inline fn asArray(self: Color) Array { - return @bitCast(self); - } -}; - -test "Color.l" { - const i: Color = .l("#012"); - try std.testing.expectEqual(0x00, i.r); - try std.testing.expectEqual(0x11, i.g); - try std.testing.expectEqual(0x22, i.b); - try std.testing.expectEqual(0xFF, i.a); - - const j: Color = .l("#3456"); - try std.testing.expectEqual(0x33, j.r); - try std.testing.expectEqual(0x44, j.g); - try std.testing.expectEqual(0x55, j.b); - try std.testing.expectEqual(0x66, j.a); - - const k: Color = .l("#F08040"); - try std.testing.expectEqual(0xF0, k.r); - try std.testing.expectEqual(0x80, k.g); - try std.testing.expectEqual(0x40, k.b); - try std.testing.expectEqual(0xFF, k.a); - - const l: Color = .l("#20304050"); - try std.testing.expectEqual(0x20, l.r); - try std.testing.expectEqual(0x30, l.g); - try std.testing.expectEqual(0x40, l.b); - try std.testing.expectEqual(0x50, l.a); -} - -// ----------------------------------------------------------------------------- - test "refAllDecls" { std.testing.refAllDeclsRecursive(@This()); } diff --git a/packages/vecmath/src/rotors/Complex.zig b/packages/vecmath/src/rotors/Complex.zig new file mode 100644 index 0000000..11a1cc6 --- /dev/null +++ b/packages/vecmath/src/rotors/Complex.zig @@ -0,0 +1,45 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Complex = extern struct { + re: f32, + im: f32, + + pub const Array = [2]f32; + + pub const identity = init(1, 0); + + pub const @"0" = init(1, 0); + pub const @"90" = init(0, 1); + pub const @"180" = init(-1, 0); + pub const @"270" = init(0, -1); + + // --- INIT --- + + pub inline fn init(re: f32, im: f32) Complex { + return .{ .re = re, .im = im }; + } + + pub inline fn initRotation(angle_turns: f32) Complex { + const c, const s = vm.cossin(angle_turns).asArray(); + return .{ .re = c, .im = s }; + } + + pub inline fn initArray(array: Array) Complex { + return @bitCast(array); + } + + pub inline fn initVector2(vector: vm.Vector2) Complex { + return @bitCast(vector); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Complex) Array { + return @bitCast(self); + } + + pub inline fn asVector2(self: Complex) vm.Vector2 { + return @bitCast(self); + } +}; diff --git a/packages/vecmath/src/rotors/Complex_x8.zig b/packages/vecmath/src/rotors/Complex_x8.zig new file mode 100644 index 0000000..5e7f165 --- /dev/null +++ b/packages/vecmath/src/rotors/Complex_x8.zig @@ -0,0 +1,55 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Complex_x8 = struct { + re: vm.f32x8, + im: vm.f32x8, + + pub const Array = [16]f32; + + pub const identity = initSingle(1, 0); + + // --- INIT --- + + pub inline fn init(re: vm.f32x8, im: vm.f32x8) Complex_x8 { + return .{ .re = re, .im = im }; + } + + pub inline fn initSingle(re: f32, im: f32) Complex_x8 { + return .{ .re = vm.ps(re), .im = vm.ps(im) }; + } + + pub inline fn initRotation(angle_turns: vm.f32x8) Complex_x8 { + const c, const s = vm.cossin_x8(angle_turns).asArray(); + return .{ .re = c, .im = s }; + } + + pub inline fn initRotationSingle(angle_turns: f32) Complex_x8 { + const c, const s = vm.cossin(angle_turns).asArray(); + return .{ .re = vm.ps(c), .im = vm.ps(s) }; + } + + pub inline fn initSplat(complex: vm.Complex) Complex_x8 { + return .{ .re = vm.ps(complex.re), .im = vm.ps(complex.im) }; + } + + pub inline fn initArray(array: Array) Complex_x8 { + const re: vm.f32x8 = array[0..8].*; + const im: vm.f32x8 = array[8..16].*; + return .{ .re = re, .im = im }; + } + + pub inline fn initVector2(vector: vm.Vector2x8) Complex_x8 { + return @bitCast(vector); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Complex_x8) Array { + return @bitCast(self); + } + + pub inline fn asVector2(self: Complex_x8) vm.Vector2_x8 { + return @bitCast(self); + } +}; diff --git a/packages/vecmath/src/rotors/Quaternion.zig b/packages/vecmath/src/rotors/Quaternion.zig new file mode 100644 index 0000000..9210e86 --- /dev/null +++ b/packages/vecmath/src/rotors/Quaternion.zig @@ -0,0 +1,83 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Quaternion = extern struct { + x: f32, + y: f32, + z: f32, + w: f32, + + pub const Array = [4]f32; + + pub const identity = init(0, 0, 0, 1); + + // --- INIT --- + + pub inline fn init(x: f32, y: f32, z: f32, w: f32) Quaternion { + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initRotationXY(angle_turns: f32) Quaternion { + const half_angle_turns = 0.5 * angle_turns; + const c, const s = vm.cossin(half_angle_turns).asArray(); + return .{ .x = 0, .y = 0, .z = s, .w = c }; + } + + pub inline fn initRotationXZ(angle_turns: f32) Quaternion { + const half_angle_turns = 0.5 * angle_turns; + const c, const s = vm.cossin(half_angle_turns).asArray(); + return .{ .x = 0, .y = -s, .z = 0, .w = c }; + } + + pub inline fn initRotationYX(angle_turns: f32) Quaternion { + const half_angle_turns = 0.5 * angle_turns; + const c, const s = vm.cossin(half_angle_turns).asArray(); + return .{ .x = 0, .y = 0, .z = -s, .w = c }; + } + + pub inline fn initRotationYZ(angle_turns: f32) Quaternion { + const half_angle_turns = 0.5 * angle_turns; + const c, const s = vm.cossin(half_angle_turns).asArray(); + return .{ .x = s, .y = 0, .z = 0, .w = c }; + } + + pub inline fn initRotationZX(angle_turns: f32) Quaternion { + const half_angle_turns = 0.5 * angle_turns; + const c, const s = vm.cossin(half_angle_turns).asArray(); + return .{ .x = 0, .y = s, .z = 0, .w = c }; + } + + pub inline fn initRotationZY(angle_turns: f32) Quaternion { + const half_angle_turns = 0.5 * angle_turns; + const c, const s = vm.cossin(half_angle_turns).asArray(); + return .{ .x = -s, .y = 0, .z = 0, .w = c }; + } + + pub inline fn initArray(array: Array) Quaternion { + return @bitCast(array); + } + + pub inline fn initVector4(vector: vm.Vector4) Quaternion { + return @bitCast(vector); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Quaternion) Array { + return @bitCast(self); + } + + pub inline fn asVector4(self: Quaternion) vm.Vector4 { + return @bitCast(self); + } + + // --- ACCESSORS --- + + pub inline fn getVector(self: Quaternion) vm.Vector3 { + return .{ .x = self.x, .y = self.y, .z = self.z }; + } + + pub inline fn getScalar(self: Quaternion) f32 { + return self.w; + } +}; diff --git a/packages/vecmath/src/rotors/Quaternion_x8.zig b/packages/vecmath/src/rotors/Quaternion_x8.zig new file mode 100644 index 0000000..fb780c0 --- /dev/null +++ b/packages/vecmath/src/rotors/Quaternion_x8.zig @@ -0,0 +1,85 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Quaternion_x8 = extern struct { + x: vm.f32x8, + y: vm.f32x8, + z: vm.f32x8, + w: vm.f32x8, + + pub const Array = [32]f32; + + pub const identity = initSingle(0, 0, 0, 1); + + // --- INIT --- + + pub inline fn init(x: vm.f32x8, y: vm.f32x8, z: vm.f32x8, w: vm.f32x8) Quaternion_x8 { + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initSingle(x: f32, y: f32, z: f32, w: f32) Quaternion_x8 { + return .{ .x = vm.ps(x), .y = vm.ps(y), .z = vm.ps(z), .w = vm.ps(w) }; + } + + pub inline fn initRotationXY(angle_turns: vm.f32x8) Quaternion_x8 { + const half_angle_turns = vm.ps(0.5) * angle_turns; + const c, const s = vm.cossin_x8(half_angle_turns).unpack(); + return .{ .x = vm.ps(0), .y = vm.ps(0), .z = s, .w = c }; + } + + pub inline fn initRotationXZ(angle_turns: vm.f32x8) Quaternion_x8 { + const half_angle_turns = vm.ps(0.5) * angle_turns; + const c, const s = vm.cossin_x8(half_angle_turns).unpack(); + return .{ .x = vm.ps(0), .y = -s, .z = vm.ps(0), .w = c }; + } + + pub inline fn initRotationYX(angle_turns: vm.f32x8) Quaternion_x8 { + const half_angle_turns = vm.ps(0.5) * angle_turns; + const c, const s = vm.cossin_x8(half_angle_turns).unpack(); + return .{ .x = vm.ps(0), .y = vm.ps(0), .z = -s, .w = c }; + } + + pub inline fn initRotationYZ(angle_turns: vm.f32x8) Quaternion_x8 { + const half_angle_turns = vm.ps(0.5) * angle_turns; + const c, const s = vm.cossin_x8(half_angle_turns).unpack(); + return .{ .x = s, .y = vm.ps(0), .z = vm.ps(0), .w = c }; + } + + pub inline fn initRotationZX(angle_turns: vm.f32x8) Quaternion_x8 { + const half_angle_turns = vm.ps(0.5) * angle_turns; + const c, const s = vm.cossin_x8(half_angle_turns).unpack(); + return .{ .x = vm.ps(0), .y = s, .z = vm.ps(0), .w = c }; + } + + pub inline fn initRotationZY(angle_turns: vm.f32x8) Quaternion_x8 { + const half_angle_turns = vm.ps(0.5) * angle_turns; + const c, const s = vm.cossin_x8(half_angle_turns).unpack(); + return .{ .x = -s, .y = vm.ps(0), .z = vm.ps(0), .w = c }; + } + + //pub inline fn initArray() Quaternion_x8 {} + + //pub inline fn initArrayTranspose() Quaternion_x8 {} + + //pub inline fn initArrayOfQuaternions() Quaternion_x8 {} + + //pub inline fn initVector4x8(vector: Vector4x8) Quaternion_x8 {} + + pub inline fn initSplat(quaternion: vm.Quaternion) Quaternion_x8 { + return .{ .x = vm.ps(quaternion.x), .y = vm.ps(quaternion.y), .z = vm.ps(quaternion.z), .w = vm.ps(quaternion.w) }; + } + + pub inline fn initSplatVector4(vector: vm.Vector4) Quaternion_x8 { + return .{ .x = vm.ps(vector.x), .y = vm.ps(vector.y), .z = vm.ps(vector.z), .w = vm.ps(vector.w) }; + } + + // --- ACCESSORS --- + + pub inline fn getVector(self: Quaternion_x8) vm.Vector3x8 { + return .{ .x = self.x, .y = self.y, .z = self.z }; + } + + pub inline fn getScalar(self: Quaternion_x8) vm.f32x8 { + return self.w; + } +}; diff --git a/packages/vecmath/src/simd.zig b/packages/vecmath/src/simd.zig new file mode 100644 index 0000000..8b992ba --- /dev/null +++ b/packages/vecmath/src/simd.zig @@ -0,0 +1,33 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const f32x8 = @Vector(8, f32); +pub const i32x8 = @Vector(8, i32); +pub const u32x8 = @Vector(8, u32); +pub const f64x4 = @Vector(4, f64); +pub const i64x4 = @Vector(4, i64); +pub const u64x4 = @Vector(4, u64); + +pub inline fn ps(value: f32) f32x8 { + return @splat(value); +} + +pub inline fn epi32(value: i32) i32x8 { + return @splat(value); +} + +pub inline fn epu32(value: u32) u32x8 { + return @splat(value); +} + +pub inline fn pd(value: f64) f64x4 { + return @splat(value); +} + +pub inline fn epi64(value: i64) i64x4 { + return @splat(value); +} + +pub inline fn epu64(value: u64) u64x4 { + return @splat(value); +} diff --git a/packages/vecmath/src/trig.zig b/packages/vecmath/src/trig.zig new file mode 100644 index 0000000..d513b56 --- /dev/null +++ b/packages/vecmath/src/trig.zig @@ -0,0 +1,157 @@ +const std = @import("std"); +const vm = @import("root"); + +pub fn cos(angle_turns: f32) f32 { + return cossin(angle_turns).re; +} + +test "cos" { + try std.testing.expectEqual(1, cos(-1)); + try std.testing.expectEqual(0, cos(-0.75)); + try std.testing.expectEqual(-1, cos(-0.5)); + try std.testing.expectEqual(0, cos(-0.25)); + try std.testing.expectEqual(1, cos(0)); + try std.testing.expectEqual(0, cos(0.25)); + try std.testing.expectEqual(-1, cos(0.5)); + try std.testing.expectEqual(0, cos(0.75)); +} + +pub fn cos_x8(angle_turns: vm.f32x8) vm.f32x8 { + return cossin_x8(angle_turns).re; +} + +test "cos_x8" { + try std.testing.expectEqual( + .{ 1, 0, -1, 0, 1, 0, -1, 0 }, + cos_x8(.{ -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75 }), + ); +} + +pub fn sin(angle_turns: f32) f32 { + return cossin(angle_turns).im; +} + +test "sin" { + try std.testing.expectEqual(0, sin(-1)); + try std.testing.expectEqual(1, sin(-0.75)); + try std.testing.expectEqual(0, sin(-0.5)); + try std.testing.expectEqual(-1, sin(-0.25)); + try std.testing.expectEqual(0, sin(0)); + try std.testing.expectEqual(1, sin(0.25)); + try std.testing.expectEqual(0, sin(0.5)); + try std.testing.expectEqual(-1, sin(0.75)); +} + +pub fn sin_x8(angle_turns: vm.f32x8) vm.f32x8 { + return cossin_x8(angle_turns).im; +} + +test "sin_x8" { + try std.testing.expectEqual( + .{ 0, 1, 0, -1, 0, 1, 0, -1 }, + sin_x8(.{ -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75 }), + ); +} + +pub fn cossin(angle_turns: f32) vm.Complex { + @setFloatMode(.optimized); + // Taylor series expansion for f(x)=cos(xπ/2) + const term_cos_0: f32 = 1.0; + const term_cos_2: f32 = -1.23370055; // -π²/8 + const term_cos_4: f32 = 0.253669508; // π⁴/384 + const term_cos_6: f32 = -0.020863481; // -π⁶/46080 + // Taylor series expansion for f(x)=sin(xπ/2) + const term_sin_1: f32 = 1.570796327; // π/2 + const term_sin_3: f32 = -0.645964098; // -π³/48 + const term_sin_5: f32 = 0.079692626; // π⁵/3840 + + const angle_01 = angle_turns - @floor(angle_turns); + const angle_04 = 4.0 * angle_01; + + const quadrant: u32 = @intFromFloat(angle_04); + + const x = angle_04 - @floor(angle_04); + const x2 = x * x; + + const c = ((term_cos_6 * x2 + term_cos_4) * x2 + term_cos_2) * x2 + term_cos_0; + const s = ((term_sin_5 * x2 + term_sin_3) * x2 + term_sin_1) * x; + + return switch (quadrant) { + 0 => .init(c, s), + 1 => .init(-s, c), + 2 => .init(-c, -s), + 3 => .init(s, -c), + else => unreachable, + }; +} + +test "cossin" { + try std.testing.expectEqual(vm.Vector2.unit_x, cossin(-1)); + try std.testing.expectEqual(vm.Vector2.unit_y, cossin(-0.75)); + try std.testing.expectEqual(vm.Vector2.unit_nx, cossin(-0.5)); + try std.testing.expectEqual(vm.Vector2.unit_ny, cossin(-0.25)); + try std.testing.expectEqual(vm.Vector2.unit_x, cossin(0)); + try std.testing.expectEqual(vm.Vector2.unit_y, cossin(0.25)); + try std.testing.expectEqual(vm.Vector2.unit_nx, cossin(0.5)); + try std.testing.expectEqual(vm.Vector2.unit_ny, cossin(0.75)); +} + +pub fn cossin_x8(angle_turns: vm.f32x8) vm.Complex_x8 { + @setFloatMode(.optimized); + // Taylor series expansion for f(x)=cos(xπ/2) + const term_cos_0 = vm.ps(1.0); + const term_cos_2 = vm.ps(-1.23370055); // -π²/8 + const term_cos_4 = vm.ps(0.253669508); // π⁴/384 + const term_cos_6 = vm.ps(-0.020863481); // -π⁶/46080 + // Taylor series expansion for f(x)=sin(xπ/2) + const term_sin_1 = vm.ps(1.570796327); // π/2 + const term_sin_3 = vm.ps(-0.645964098); // -π³/48 + const term_sin_5 = vm.ps(0.079692626); // π⁵/3840 + + const angle_01 = angle_turns - @floor(angle_turns); + const angle_04 = vm.ps(4.0) * angle_01; + + const quadrant: vm.u32x8 = @intFromFloat(angle_04); + const quadrant_odd = (quadrant & vm.epu32(1)) != vm.epu32(0); + const sign_mask_cos = ((quadrant + vm.epu32(1)) & vm.epu32(0b10)) << @splat(30); + const sign_mask_sin = (quadrant & vm.epu32(0b10)) << @splat(30); + + const x = angle_04 - @floor(angle_04); + const x2 = x * x; + + const c = ((term_cos_6 * x2 + term_cos_4) * x2 + term_cos_2) * x2 + term_cos_0; + const s = ((term_sin_5 * x2 + term_sin_3) * x2 + term_sin_1) * x; + + var result_cos = @select(f32, quadrant_odd, s, c); + var result_sin = @select(f32, quadrant_odd, c, s); + + result_cos = @bitCast(@as(vm.u32x8, @bitCast(result_cos)) ^ sign_mask_cos); + result_sin = @bitCast(@as(vm.u32x8, @bitCast(result_sin)) ^ sign_mask_sin); + + return .init(result_cos, result_sin); +} + +test "cossin_x8" { + try std.testing.expectEqual( + vm.Vector2x8.initArrayOfVectors(.{ + vm.Vector2.unit_x, + vm.Vector2.unit_y, + vm.Vector2.unit_nx, + vm.Vector2.unit_ny, + vm.Vector2.unit_x, + vm.Vector2.unit_y, + vm.Vector2.unit_nx, + vm.Vector2.unit_ny, + }), + cossin_x8(.{ + -1, + -0.75, + -0.5, + -0.25, + 0, + 0.25, + 0.5, + 0.75, + }), + ); +} diff --git a/packages/vecmath/src/vectors/Vector2.zig b/packages/vecmath/src/vectors/Vector2.zig new file mode 100644 index 0000000..945295c --- /dev/null +++ b/packages/vecmath/src/vectors/Vector2.zig @@ -0,0 +1,131 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector2 = extern struct { + x: f32, + y: f32, + + pub const Array = [2]f32; + + pub const zero = initScalar(0); + pub const one = initScalar(1); + pub const unit_x = init(1, 0); + pub const unit_y = init(0, 1); + pub const unit_nx = init(-1, 0); + pub const unit_ny = init(0, -1); + + // --- INIT ---- + + pub inline fn init(x: f32, y: f32) Vector2 { + return .{ .x = x, .y = y }; + } + + pub inline fn initScalar(scalar: f32) Vector2 { + return .{ .x = scalar, .y = scalar }; + } + + pub inline fn initArray(array: Array) Vector2 { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector2) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Vector2) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Vector2) *const Array { + return @ptrCast(self); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector2, other: Vector2) Vector2 { + return .{ .x = self.x + other.x, .y = self.y + other.y }; + } + + pub inline fn sub(self: Vector2, other: Vector2) Vector2 { + return .{ .x = self.x - other.x, .y = self.y - other.y }; + } + + pub inline fn mul(self: Vector2, other: Vector2) Vector2 { + return .{ .x = self.x * other.x, .y = self.y * other.y }; + } + + pub inline fn mulScalar(self: Vector2, scalar: f32) Vector2 { + return .{ .x = self.x * scalar, .y = self.y * scalar }; + } + + pub inline fn div(self: Vector2, other: Vector2) Vector2 { + return .{ .x = self.x / other.x, .y = self.y / other.y }; + } + + pub inline fn divScalar(self: Vector2, scalar: f32) Vector2 { + return .{ .x = self.x / scalar, .y = self.y / scalar }; + } + + pub inline fn negate(self: Vector2) Vector2 { + return .{ .x = -self.x, .y = -self.y }; + } + + pub inline fn abs(self: Vector2) Vector2 { + return .{ .x = @abs(self.x), .y = @abs(self.y) }; + } + + pub inline fn floor(self: Vector2) Vector2 { + return .{ .x = @floor(self.x), .y = @floor(self.y) }; + } + + pub inline fn ceil(self: Vector2) Vector2 { + return .{ .x = @ceil(self.x), .y = @ceil(self.y) }; + } + + pub inline fn round(self: Vector2) Vector2 { + return .{ .x = @round(self.x), .y = @round(self.y) }; + } + + pub inline fn min(self: Vector2, other: Vector2) Vector2 { + return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) }; + } + + pub inline fn max(self: Vector2, other: Vector2) Vector2 { + return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) }; + } + + // --- OTHER --- + + pub inline fn len(self: Vector2) f32 { + return @sqrt(self.x * self.x + self.y * self.y); + } + + pub inline fn lenSquared(self: Vector2) f32 { + return self.x * self.x + self.y * self.y; + } + + pub inline fn dot(self: Vector2, other: Vector2) f32 { + return self.x * other.x + self.y * other.y; + } + + pub inline fn cross(self: Vector2, other: Vector2) f32 { + return self.x * other.y - self.y * other.x; + } + + pub inline fn lerp(a: Vector2, b: Vector2, t: f32) Vector2 { + return .{ + .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), + .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), + }; + } + + pub inline fn rotate(self: Vector2, angle_turns: f32) Vector2 { + const c, const s = cossin(angle_turns).asArray(); + return .{ + .x = self.x * c - self.y * s, + .y = self.x * s + self.y * c, + }; + } +}; diff --git a/packages/vecmath/src/vectors/Vector2Int.zig b/packages/vecmath/src/vectors/Vector2Int.zig new file mode 100644 index 0000000..2b60b98 --- /dev/null +++ b/packages/vecmath/src/vectors/Vector2Int.zig @@ -0,0 +1,108 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector2Int = extern struct { + x: i32, + y: i32, + + pub const Array = [2]i32; + + pub const zero = initScalar(0); + pub const one = initScalar(1); + pub const unit_x = init(1, 0); + pub const unit_y = init(0, 1); + pub const unit_nx = init(-1, 0); + pub const unit_ny = init(0, -1); + + // --- INIT ---- + + pub inline fn init(x: i32, y: i32) Vector2Int { + return .{ .x = x, .y = y }; + } + + pub inline fn initScalar(scalar: i32) Vector2Int { + return .{ .x = scalar, .y = scalar }; + } + + pub inline fn initArray(array: Array) Vector2Int { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector2Int) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Vector2Int) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Vector2Int) *const Array { + return @ptrCast(self); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = self.x + other.x, .y = self.y + other.y }; + } + + pub inline fn sub(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = self.x - other.x, .y = self.y - other.y }; + } + + pub inline fn mul(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = self.x * other.x, .y = self.y * other.y }; + } + + pub inline fn mulScalar(self: Vector2Int, scalar: f32) Vector2Int { + return .{ .x = self.x * scalar, .y = self.y * scalar }; + } + + pub inline fn div(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y) }; + } + + pub inline fn divScalar(self: Vector2Int, scalar: f32) Vector2Int { + return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar) }; + } + + pub inline fn mod(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y) }; + } + + pub inline fn modScalar(self: Vector2Int, scalar: f32) Vector2Int { + return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar) }; + } + + pub inline fn negate(self: Vector2Int) Vector2Int { + return .{ .x = -self.x, .y = -self.y }; + } + + pub inline fn abs(self: Vector2Int) Vector2Int { + return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)) }; + } + + pub inline fn min(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y) }; + } + + pub inline fn max(self: Vector2Int, other: Vector2Int) Vector2Int { + return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y) }; + } + + // --- OTHER --- + + pub inline fn lenSquared(self: Vector2Int) i32 { + return self.x * self.x + self.y * self.y; + } + + pub inline fn dot(self: Vector2Int, other: Vector2Int) i32 { + return self.x * other.x + self.y * other.y; + } + + pub inline fn cross(self: Vector2Int, other: Vector2Int) i32 { + return self.x * other.y - self.y * other.x; + } +}; diff --git a/packages/vecmath/src/vectors/Vector2Int_x8.zig b/packages/vecmath/src/vectors/Vector2Int_x8.zig new file mode 100644 index 0000000..9894cd7 --- /dev/null +++ b/packages/vecmath/src/vectors/Vector2Int_x8.zig @@ -0,0 +1,186 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector2Int_x8 = struct { + x: i32x8, + y: i32x8, + + pub const Array = [16]i32; + + 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: i32x8, y: i32x8) Vector2Int_x8 { + return .{ .x = x, .y = y }; + } + + pub inline fn initSingle(x: i32, y: i32) Vector2Int_x8 { + return .{ .x = epi32(x), .y = epi32(y) }; + } + + pub inline fn initScalar(scalar: i32x8) Vector2Int_x8 { + return .{ .x = scalar, .y = scalar }; + } + + pub inline fn initScalarSingle(scalar: i32) Vector2Int_x8 { + return .{ .x = epi32(scalar), .y = epi32(scalar) }; + } + + pub inline fn initSplat(vector: Vector2Int) Vector2Int_x8 { + return .{ .x = epi32(vector.x), .y = epi32(vector.y) }; + } + + pub inline fn initArray(array: Array) Vector2Int_x8 { + const x: i32x8 = array[0..8].*; + const y: i32x8 = array[8..16].*; + return .{ .x = x, .y = y }; + } + + pub inline fn initArrayTranspose(array: Array) Vector2Int_x8 { + const a: i32x8 = array[0..8].*; + const b: i32x8 = array[8..16].*; + const x: i32x8 = @shuffle(i32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) }); + const y: i32x8 = @shuffle(i32, 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]Vector2Int) Vector2Int_x8 { + return initArrayTranspose(@bitCast(vectors)); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector2Int_x8) Array { + const x: [8]i32 = self.x; + const y: [8]i32 = self.y; + return x ++ y; + } + + pub inline fn asArrayTranspose(self: Vector2Int_x8) Array { + const a = @shuffle(i32, self.x, self.y, [_]i32{ 0, ~@as(i32, 0), 1, ~@as(i32, 1), 2, ~@as(i32, 2), 3, ~@as(i32, 3) }); + const b = @shuffle(i32, 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: Vector2Int_x8) [8]Vector2Int { + return @bitCast(self.asArrayTranspose()); + } + + pub inline fn unpack(self: Vector2Int_x8) [2]i32x8 { + return .{ self.x, self.y }; + } + + // --- LOAD AND STORE --- + + pub inline fn loadArray(self: *Vector2Int_x8, array: *const Array) void { + self.x = array[0..8].*; + self.y = array[8..16].*; + } + + pub inline fn loadArrayTranspose(self: *Vector2Int_x8, array: *const Array) void { + const a: i32x8 = array[0..8].*; + const b: i32x8 = array[8..16].*; + self.x = @shuffle(i32, a, b, [_]i32{ 0, 2, 4, 6, ~@as(i32, 0), ~@as(i32, 2), ~@as(i32, 4), ~@as(i32, 6) }); + self.y = @shuffle(i32, a, b, [_]i32{ 1, 3, 5, 7, ~@as(i32, 1), ~@as(i32, 3), ~@as(i32, 5), ~@as(i32, 7) }); + } + + pub inline fn loadArrayOfVectors(self: *Vector2Int_x8, vectors: *const [8]Vector2Int) void { + self.loadArrayTranspose(@ptrCast(vectors)); + } + + pub inline fn storeArray(self: *const Vector2Int_x8, array: *Array) void { + array[0..8].* = self.x; + array[8..16].* = self.y; + } + + pub inline fn storeArrayTranspose(self: *const Vector2Int_x8, array: *Array) void { + array[0..8].* = @shuffle(i32, 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(i32, 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 Vector2Int_x8, vectors: *[8]Vector2Int) void { + self.storeArrayTranspose(@ptrCast(vectors)); + } + + // --- 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: 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 * epi32(scalar), .y = self.y * 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: 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, epi32(scalar)), .y = @divFloor(self.y, 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: 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, epi32(scalar)), .y = @mod(self.y, 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) i32x8 { + return self.x * self.x + self.y * self.y; + } + + pub inline fn dot(self: Vector2Int_x8, other: Vector2Int_x8) i32x8 { + return self.x * other.x + self.y * other.y; + } + + pub inline fn cross(self: Vector2Int_x8, other: Vector2Int_x8) i32x8 { + return self.x * other.y - self.y * other.x; + } +}; diff --git a/packages/vecmath/src/vectors/Vector2x8.zig b/packages/vecmath/src/vectors/Vector2x8.zig new file mode 100644 index 0000000..7bc6f5d --- /dev/null +++ b/packages/vecmath/src/vectors/Vector2x8.zig @@ -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, + }; + } +}; diff --git a/packages/vecmath/src/vectors/Vector3.zig b/packages/vecmath/src/vectors/Vector3.zig new file mode 100644 index 0000000..1d656ee --- /dev/null +++ b/packages/vecmath/src/vectors/Vector3.zig @@ -0,0 +1,141 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector3 = extern struct { + x: f32, + y: f32, + z: f32, + + pub const Array = [3]f32; + + pub const zero = initScalar(0); + pub const one = initScalar(1); + pub const unit_x = init(1, 0, 0); + pub const unit_y = init(0, 1, 0); + pub const unit_z = init(0, 0, 1); + pub const unit_nx = init(-1, 0, 0); + pub const unit_ny = init(0, -1, 0); + pub const unit_nz = init(0, 0, -1); + + // --- INIT ---- + + pub inline fn init(x: f32, y: f32, z: f32) Vector3 { + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initScalar(scalar: f32) Vector3 { + return .{ .x = scalar, .y = scalar, .z = scalar }; + } + + pub inline fn initArray(array: Array) Vector3 { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector3) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Vector3) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Vector3) *const Array { + return @ptrCast(self); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector3, other: Vector3) Vector3 { + return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; + } + + pub inline fn sub(self: Vector3, other: Vector3) Vector3 { + return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; + } + + pub inline fn mul(self: Vector3, other: Vector3) Vector3 { + return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; + } + + pub inline fn mulScalar(self: Vector3, scalar: f32) Vector3 { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; + } + + pub inline fn div(self: Vector3, other: Vector3) Vector3 { + return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z }; + } + + pub inline fn divScalar(self: Vector3, scalar: f32) Vector3 { + return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar }; + } + + pub inline fn negate(self: Vector3) Vector3 { + return .{ .x = -self.x, .y = -self.y, .z = -self.z }; + } + + pub inline fn abs(self: Vector3) Vector3 { + return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) }; + } + + pub inline fn floor(self: Vector3) Vector3 { + return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) }; + } + + pub inline fn ceil(self: Vector3) Vector3 { + return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) }; + } + + pub inline fn round(self: Vector3) Vector3 { + return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) }; + } + + pub inline fn min(self: Vector3, other: Vector3) Vector3 { + return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; + } + + pub inline fn max(self: Vector3, other: Vector3) Vector3 { + return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; + } + + // --- OTHER --- + + pub inline fn len(self: Vector3) f32 { + return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z); + } + + pub inline fn lenSquared(self: Vector3) f32 { + return self.x * self.x + self.y * self.y + self.z * self.z; + } + + pub inline fn dot(self: Vector3, other: Vector3) f32 { + return self.x * other.x + self.y * other.y + self.z * other.z; + } + + pub inline fn cross(self: Vector3, other: Vector3) Vector3 { + return .{ + .x = self.y * other.z - self.z * other.y, + .y = self.z * other.x - self.x * other.z, + .z = self.x * other.y - self.y * other.x, + }; + } + + pub inline fn lerp(a: Vector3, b: Vector3, t: f32) Vector3 { + return .{ + .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), + .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), + .z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)), + }; + } + + pub inline fn rotate(self: Vector3, quaternion: Quaternion) Vector3 { + const quaternion_scalar = quaternion.getScalar(); + const quaternion_vector = quaternion.getVector(); + + return .add(self, .cross( + .add(quaternion_vector, quaternion_vector), + .add(.cross(quaternion_vector, self), .mulScalar(self, quaternion_scalar)), + )); + } +}; diff --git a/packages/vecmath/src/vectors/Vector3Int.zig b/packages/vecmath/src/vectors/Vector3Int.zig new file mode 100644 index 0000000..fd015c1 --- /dev/null +++ b/packages/vecmath/src/vectors/Vector3Int.zig @@ -0,0 +1,115 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector3Int = extern struct { + x: i32, + y: i32, + z: i32, + + pub const Array = [3]i32; + + pub const zero = initScalar(0); + pub const one = initScalar(1); + pub const unit_x = init(1, 0, 0); + pub const unit_y = init(0, 1, 0); + pub const unit_z = init(0, 0, 1); + pub const unit_nx = init(-1, 0, 0); + pub const unit_ny = init(0, -1, 0); + pub const unit_nz = init(0, 0, -1); + + // --- INIT ---- + + pub inline fn init(x: i32, y: i32, z: i32) Vector3Int { + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initScalar(scalar: i32) Vector3Int { + return .{ .x = scalar, .y = scalar, .z = scalar }; + } + + pub inline fn initArray(array: Array) Vector3Int { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector3Int) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Vector3Int) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Vector3Int) *const Array { + return @ptrCast(self); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; + } + + pub inline fn sub(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; + } + + pub inline fn mul(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; + } + + pub inline fn mulScalar(self: Vector3Int, scalar: f32) Vector3Int { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; + } + + pub inline fn div(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z) }; + } + + pub inline fn divScalar(self: Vector3Int, scalar: f32) Vector3Int { + return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) }; + } + + pub inline fn mod(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z) }; + } + + pub inline fn modScalar(self: Vector3Int, scalar: f32) Vector3Int { + return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) }; + } + + pub inline fn negate(self: Vector3Int) Vector3Int { + return .{ .x = -self.x, .y = -self.y, .z = -self.z }; + } + + pub inline fn abs(self: Vector3Int) Vector3Int { + return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)) }; + } + + pub inline fn min(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; + } + + pub inline fn max(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; + } + + // --- OTHER --- + + pub inline fn lenSquared(self: Vector3Int) i32 { + return self.x * self.x + self.y * self.y + self.z * self.z; + } + + pub inline fn dot(self: Vector3Int, other: Vector3Int) i32 { + return self.x * other.x + self.y * other.y + self.z * other.z; + } + + pub inline fn cross(self: Vector3Int, other: Vector3Int) Vector3Int { + return .{ + .x = self.y * other.z - self.z * other.y, + .y = self.z * other.x - self.x * other.z, + .z = self.x * other.y - self.y * other.x, + }; + } +}; diff --git a/packages/vecmath/src/vectors/Vector3Int_x8.zig b/packages/vecmath/src/vectors/Vector3Int_x8.zig new file mode 100644 index 0000000..4e5d69b --- /dev/null +++ b/packages/vecmath/src/vectors/Vector3Int_x8.zig @@ -0,0 +1,216 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector3Int_x8 = struct { + x: i32x8, + y: i32x8, + z: i32x8, + + pub const Array = [24]i32; + + pub const zero = initScalarSingle(0); + pub const one = initScalarSingle(1); + pub const unit_x = initSingle(1, 0, 0); + pub const unit_y = initSingle(0, 1, 0); + pub const unit_z = initSingle(0, 0, 1); + pub const unit_nx = initSingle(-1, 0, 0); + pub const unit_ny = initSingle(0, -1, 0); + pub const unit_nz = initSingle(0, 0, -1); + + // --- INIT ---- + + pub inline fn init(x: i32x8, y: i32x8, z: i32x8) Vector3Int_x8 { + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initSingle(x: i32, y: i32, z: i32) Vector3Int_x8 { + return .{ .x = epi32(x), .y = epi32(y), .z = epi32(z) }; + } + + pub inline fn initScalar(scalar: i32x8) Vector3Int_x8 { + return .{ .x = scalar, .y = scalar, .z = scalar }; + } + + pub inline fn initScalarSingle(scalar: i32) Vector3Int_x8 { + return .{ .x = epi32(scalar), .y = epi32(scalar), .z = epi32(scalar) }; + } + + pub inline fn initSplat(vector: Vector3Int) Vector3Int_x8 { + return .{ .x = epi32(vector.x), .y = epi32(vector.y), .z = epi32(vector.z) }; + } + + pub inline fn initArray(array: Array) Vector3Int_x8 { + const x: i32x8 = array[0..8].*; + const y: i32x8 = array[8..16].*; + const z: i32x8 = array[16..24].*; + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initArrayTranspose(array: Array) Vector3Int_x8 { + const vector: @Vector(24, i32) = array; + const x: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); + const y: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); + const z: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initArrayOfVectors(vectors: [8]Vector3Int) Vector3Int_x8 { + return initArrayTranspose(@bitCast(vectors)); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector3Int_x8) Array { + const x: [8]i32 = self.x; + const y: [8]i32 = self.y; + const z: [8]i32 = self.z; + return x ++ y ++ z; + } + + pub inline fn asArrayTranspose(self: Vector3Int_x8) Array { + const vector: @Vector(24, i32) = self.asArray(); + const transposed: @Vector(24, i32) = @shuffle(i32, vector, undefined, [_]i32{ + 0, 8, 16, + 1, 9, 17, + 2, 10, 18, + 3, 11, 19, + 4, 12, 20, + 5, 13, 21, + 6, 14, 22, + 7, 15, 23, + }); + return transposed; + } + + pub inline fn asArrayOfVectors(self: Vector3Int_x8) [8]Vector3Int { + return @bitCast(self.asArrayTranspose()); + } + + pub inline fn unpack(self: Vector3Int_x8) [3]i32x8 { + return .{ self.x, self.y, self.z }; + } + + // --- LOAD AND STORE --- + + pub inline fn loadArray(self: *Vector3Int_x8, array: *const Array) void { + self.x = array[0..8].*; + self.y = array[8..16].*; + self.z = array[16..24].*; + } + + pub inline fn loadArrayTranspose(self: *Vector3Int_x8, array: *const Array) void { + const vector: @Vector(24, i32) = array; + self.x = @shuffle(i32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); + self.y = @shuffle(i32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); + self.z = @shuffle(i32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); + } + + pub inline fn loadArrayOfVectors(self: *Vector3Int_x8, vectors: *const [8]Vector3Int) void { + self.loadArrayTranspose(@ptrCast(vectors)); + } + + pub inline fn storeArray(self: *const Vector3Int_x8, array: *Array) void { + array[0..8].* = self.x; + array[8..16].* = self.y; + array[16..24].* = self.z; + } + + pub inline fn storeArrayTranspose(self: *const Vector3Int_x8, array: *Array) void { + const vector: @Vector(24, i32) = self.asArray(); + const transposed: @Vector(24, i32) = @shuffle(i32, vector, undefined, [_]i32{ + 0, 8, 16, + 1, 9, 17, + 2, 10, 18, + 3, 11, 19, + 4, 12, 20, + 5, 13, 21, + 6, 14, 22, + 7, 15, 23, + }); + array.* = transposed; + } + + pub inline fn storeArrayOfVectors(self: *const Vector3Int_x8, vectors: *[8]Vector3Int) void { + self.storeArrayTranspose(@ptrCast(vectors)); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; + } + + pub inline fn sub(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; + } + + pub inline fn mul(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; + } + + pub inline fn mulScalar(self: Vector3Int_x8, scalar: i32x8) Vector3Int_x8 { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; + } + + pub inline fn mulScalarSingle(self: Vector3Int_x8, scalar: i32) Vector3Int_x8 { + return .{ .x = self.x * epi32(scalar), .y = self.y * epi32(scalar), .z = self.z * epi32(scalar) }; + } + + pub inline fn div(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z) }; + } + + pub inline fn divScalar(self: Vector3Int_x8, scalar: i32x8) Vector3Int_x8 { + return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) }; + } + + pub inline fn divScalarSingle(self: Vector3Int_x8, scalar: i32) Vector3Int_x8 { + return .{ .x = @divFloor(self.x, epi32(scalar)), .y = @divFloor(self.y, epi32(scalar)), .z = @divFloor(self.z, epi32(scalar)) }; + } + + pub inline fn mod(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z) }; + } + + pub inline fn modScalar(self: Vector3Int_x8, scalar: i32x8) Vector3Int_x8 { + return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) }; + } + + pub inline fn modScalarSingle(self: Vector3Int_x8, scalar: i32) Vector3Int_x8 { + return .{ .x = @mod(self.x, epi32(scalar)), .y = @mod(self.y, epi32(scalar)), .z = @mod(self.z, epi32(scalar)) }; + } + + pub inline fn negate(self: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = -self.x, .y = -self.y, .z = -self.z }; + } + + pub inline fn abs(self: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)) }; + } + + pub inline fn min(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; + } + + pub inline fn max(self: Vector3Int_x8, other: Vector3Int_x8) Vector3Int_x8 { + return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; + } + + // --- OTHER --- + + pub inline fn lenSquared(self: Vector3Int_x8) i32x8 { + return self.x * self.x + self.y * self.y + self.z * self.z; + } + + pub inline fn dot(self: Vector3Int_x8, other: Vector3Int_x8) i32x8 { + return self.x * other.x + self.y * other.y + self.z * other.z; + } + + pub inline fn cross(self: Vector3Int_x8, other: Vector3Int_x8) i32x8 { + return .{ + .x = self.y * other.z - self.z * other.y, + .y = self.z * other.x - self.x * other.z, + .z = self.x * other.y - self.y * other.x, + }; + } +}; diff --git a/packages/vecmath/src/vectors/Vector3x8.zig b/packages/vecmath/src/vectors/Vector3x8.zig new file mode 100644 index 0000000..8ff6942 --- /dev/null +++ b/packages/vecmath/src/vectors/Vector3x8.zig @@ -0,0 +1,238 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector3x8 = struct { + x: f32x8, + y: f32x8, + z: f32x8, + + pub const Array = [24]f32; + + pub const zero = initScalarSingle(0); + pub const one = initScalarSingle(1); + pub const unit_x = initSingle(1, 0, 0); + pub const unit_y = initSingle(0, 1, 0); + pub const unit_z = initSingle(0, 0, 1); + pub const unit_nx = initSingle(-1, 0, 0); + pub const unit_ny = initSingle(0, -1, 0); + pub const unit_nz = initSingle(0, 0, -1); + + // --- INIT ---- + + pub inline fn init(x: f32x8, y: f32x8, z: f32x8) Vector3x8 { + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initSingle(x: f32, y: f32, z: f32) Vector3x8 { + return .{ .x = ps(x), .y = ps(y), .z = ps(z) }; + } + + pub inline fn initScalar(scalar: f32x8) Vector3x8 { + return .{ .x = scalar, .y = scalar, .z = scalar }; + } + + pub inline fn initScalarSingle(scalar: f32) Vector3x8 { + return .{ .x = ps(scalar), .y = ps(scalar), .z = ps(scalar) }; + } + + pub inline fn initSplat(vector: Vector3) Vector3x8 { + return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z) }; + } + + pub inline fn initArray(array: Array) Vector3x8 { + const x: f32x8 = array[0..8].*; + const y: f32x8 = array[8..16].*; + const z: f32x8 = array[16..24].*; + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initArrayTranspose(array: Array) Vector3x8 { + const vector: @Vector(24, f32) = array; + const x: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); + const y: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); + const z: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); + return .{ .x = x, .y = y, .z = z }; + } + + pub inline fn initArrayOfVectors(vectors: [8]Vector3) Vector3x8 { + return initArrayTranspose(@bitCast(vectors)); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector3x8) Array { + const x: [8]f32 = self.x; + const y: [8]f32 = self.y; + const z: [8]f32 = self.z; + return x ++ y ++ z; + } + + pub inline fn asArrayTranspose(self: Vector3x8) Array { + const vector: @Vector(24, f32) = self.asArray(); + const transposed: @Vector(24, f32) = @shuffle(f32, vector, undefined, [_]i32{ + 0, 8, 16, + 1, 9, 17, + 2, 10, 18, + 3, 11, 19, + 4, 12, 20, + 5, 13, 21, + 6, 14, 22, + 7, 15, 23, + }); + return transposed; + } + + pub inline fn asArrayOfVectors(self: Vector3x8) [8]Vector3 { + return @bitCast(self.asArrayTranspose()); + } + + pub inline fn unpack(self: Vector3x8) [3]f32x8 { + return .{ self.x, self.y, self.z }; + } + + // --- LOAD AND STORE --- + + pub inline fn loadArray(self: *Vector3x8, array: *const Array) void { + self.x = array[0..8].*; + self.y = array[8..16].*; + self.z = array[16..24].*; + } + + pub inline fn loadArrayTranspose(self: *Vector3x8, array: *const Array) void { + const vector: @Vector(24, f32) = array; + self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 3, 6, 9, 12, 15, 18, 21 }); + self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 4, 7, 10, 13, 16, 19, 22 }); + self.z = @shuffle(f32, vector, undefined, [_]i32{ 2, 5, 8, 11, 14, 17, 20, 23 }); + } + + pub inline fn loadArrayOfVectors(self: *Vector3x8, vectors: *const [8]Vector3) void { + self.loadArrayTranspose(@ptrCast(vectors)); + } + + pub inline fn storeArray(self: *const Vector3x8, array: *Array) void { + array[0..8].* = self.x; + array[8..16].* = self.y; + array[16..24].* = self.z; + } + + pub inline fn storeArrayTranspose(self: *const Vector3x8, array: *Array) void { + const vector: @Vector(24, f32) = self.asArray(); + const transposed: @Vector(24, f32) = @shuffle(f32, vector, undefined, [_]i32{ + 0, 8, 16, + 1, 9, 17, + 2, 10, 18, + 3, 11, 19, + 4, 12, 20, + 5, 13, 21, + 6, 14, 22, + 7, 15, 23, + }); + array.* = transposed; + } + + pub inline fn storeArrayOfVectors(self: *const Vector3x8, vectors: *[8]Vector3) void { + self.storeArrayTranspose(@ptrCast(vectors)); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z }; + } + + pub inline fn sub(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z }; + } + + pub inline fn mul(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; + } + + pub inline fn mulScalar(self: Vector3x8, scalar: f32x8) Vector3x8 { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; + } + + pub inline fn mulScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 { + return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar), .z = self.z * ps(scalar) }; + } + + pub inline fn div(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z }; + } + + pub inline fn divScalar(self: Vector3x8, scalar: f32x8) Vector3x8 { + return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar }; + } + + pub inline fn divScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 { + return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar), .z = self.z / ps(scalar) }; + } + + pub inline fn negate(self: Vector3x8) Vector3x8 { + return .{ .x = -self.x, .y = -self.y, .z = -self.z }; + } + + pub inline fn abs(self: Vector3x8) Vector3x8 { + return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) }; + } + + pub inline fn floor(self: Vector3x8) Vector3x8 { + return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) }; + } + + pub inline fn ceil(self: Vector3x8) Vector3x8 { + return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) }; + } + + pub inline fn round(self: Vector3x8) Vector3x8 { + return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) }; + } + + pub inline fn min(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) }; + } + + pub inline fn max(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) }; + } + + // --- OTHER --- + + pub inline fn len(self: Vector3x8) f32x8 { + return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z); + } + + pub inline fn lenSquared(self: Vector3x8) f32x8 { + return self.x * self.x + self.y * self.y + self.z * self.z; + } + + pub inline fn dot(self: Vector3x8, other: Vector3x8) f32x8 { + return self.x * other.x + self.y * other.y + self.z * other.z; + } + + pub inline fn cross(self: Vector3x8, other: Vector3x8) Vector3x8 { + return .{ + .x = self.y * other.z - self.z * other.y, + .y = self.z * other.x - self.x * other.z, + .z = self.x * other.y - self.y * other.x, + }; + } + + pub inline fn lerp(a: Vector3x8, b: Vector3x8, t: f32x8) Vector3x8 { + 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)), + .z = @mulAdd(f32x8, t, b.z, @mulAdd(f32x8, -t, a.z, a.z)), + }; + } + + pub inline fn rotate(self: Vector3x8, quaternion: Quaternion_x8) Vector3x8 { + const quaternion_scalar = quaternion.getScalar(); + const quaternion_vector = quaternion.getVector(); + + return .add(self, .cross( + .add(quaternion_vector, quaternion_vector), + .add(.cross(quaternion_vector, self), .mulScalar(self, quaternion_scalar)), + )); + } +}; diff --git a/packages/vecmath/src/vectors/Vector4.zig b/packages/vecmath/src/vectors/Vector4.zig new file mode 100644 index 0000000..f3506b3 --- /dev/null +++ b/packages/vecmath/src/vectors/Vector4.zig @@ -0,0 +1,127 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector4 = extern struct { + x: f32, + y: f32, + z: f32, + w: f32, + + pub const Array = [4]f32; + + pub const zero = initScalar(0); + pub const one = initScalar(1); + pub const unit_x = init(1, 0, 0, 0); + pub const unit_y = init(0, 1, 0, 0); + pub const unit_z = init(0, 0, 1, 0); + pub const unit_w = init(0, 0, 0, 1); + pub const unit_nx = init(-1, 0, 0, 0); + pub const unit_ny = init(0, -1, 0, 0); + pub const unit_nz = init(0, 0, -1, 0); + pub const unit_nw = init(0, 0, 0, -1); + + // --- INIT ---- + + pub inline fn init(x: f32, y: f32, z: f32, w: f32) Vector4 { + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initScalar(scalar: f32) Vector4 { + return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; + } + + pub inline fn initArray(array: Array) Vector4 { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector4) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Vector4) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Vector4) *const Array { + return @ptrCast(self); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector4, other: Vector4) Vector4 { + 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: Vector4, other: Vector4) Vector4 { + 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: Vector4, other: Vector4) Vector4 { + 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: Vector4, scalar: f32) Vector4 { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; + } + + pub inline fn div(self: Vector4, other: Vector4) Vector4 { + return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w }; + } + + pub inline fn divScalar(self: Vector4, scalar: f32) Vector4 { + return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar }; + } + + pub inline fn negate(self: Vector4) Vector4 { + return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; + } + + pub inline fn abs(self: Vector4) Vector4 { + return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) }; + } + + pub inline fn floor(self: Vector4) Vector4 { + return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) }; + } + + pub inline fn ceil(self: Vector4) Vector4 { + return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) }; + } + + pub inline fn round(self: Vector4) Vector4 { + return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) }; + } + + pub inline fn min(self: Vector4, other: Vector4) Vector4 { + 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: Vector4, other: Vector4) Vector4 { + 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 len(self: Vector4) f32 { + return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w); + } + + pub inline fn lenSquared(self: Vector4) f32 { + return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; + } + + pub inline fn dot(self: Vector4, other: Vector4) f32 { + return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; + } + + pub inline fn lerp(a: Vector4, b: Vector4, t: f32) Vector4 { + return .{ + .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), + .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), + .z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)), + .w = @mulAdd(f32, t, b.w, @mulAdd(f32, -t, a.w, a.w)), + }; + } +}; diff --git a/packages/vecmath/src/vectors/Vector4Int.zig b/packages/vecmath/src/vectors/Vector4Int.zig new file mode 100644 index 0000000..6a2f86d --- /dev/null +++ b/packages/vecmath/src/vectors/Vector4Int.zig @@ -0,0 +1,110 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector4Int = extern struct { + x: i32, + y: i32, + z: i32, + w: i32, + + pub const Array = [4]i32; + + pub const zero = initScalar(0); + pub const one = initScalar(1); + pub const unit_x = init(1, 0, 0, 0); + pub const unit_y = init(0, 1, 0, 0); + pub const unit_z = init(0, 0, 1, 0); + pub const unit_w = init(0, 0, 0, 1); + pub const unit_nx = init(-1, 0, 0, 0); + pub const unit_ny = init(0, -1, 0, 0); + pub const unit_nz = init(0, 0, -1, 0); + pub const unit_nw = init(0, 0, 0, -1); + + // --- INIT ---- + + pub inline fn init(x: i32, y: i32, z: i32, w: i32) Vector4Int { + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initScalar(scalar: i32) Vector4Int { + return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; + } + + pub inline fn initArray(array: Array) Vector4Int { + return @bitCast(array); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector4Int) Array { + return @bitCast(self); + } + + pub inline fn asArrayPtr(self: *Vector4Int) *Array { + return @ptrCast(self); + } + + pub inline fn asArrayConstPtr(self: *const Vector4Int) *const Array { + return @ptrCast(self); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector4Int, other: Vector4Int) Vector4Int { + 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, other: Vector4Int) Vector4Int { + 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, other: Vector4Int) Vector4Int { + 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, scalar: f32) Vector4Int { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; + } + + pub inline fn div(self: Vector4Int, other: Vector4Int) Vector4Int { + 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, scalar: f32) Vector4Int { + return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar), .w = @divFloor(self.w, scalar) }; + } + + pub inline fn mod(self: Vector4Int, other: Vector4Int) Vector4Int { + 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, scalar: f32) Vector4Int { + return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar), .w = @mod(self.w, scalar) }; + } + + pub inline fn negate(self: Vector4Int) Vector4Int { + return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; + } + + pub inline fn abs(self: Vector4Int) Vector4Int { + 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, other: Vector4Int) Vector4Int { + 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, other: Vector4Int) Vector4Int { + 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) i32 { + return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; + } + + pub inline fn dot(self: Vector4Int, other: Vector4Int) i32 { + return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; + } +}; diff --git a/packages/vecmath/src/vectors/Vector4Int_x8.zig b/packages/vecmath/src/vectors/Vector4Int_x8.zig new file mode 100644 index 0000000..135a478 --- /dev/null +++ b/packages/vecmath/src/vectors/Vector4Int_x8.zig @@ -0,0 +1,217 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector4Int_x8 = struct { + x: i32x8, + y: i32x8, + z: i32x8, + w: i32x8, + + pub const Array = [32]i32; + + 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: i32x8, y: i32x8, z: i32x8, w: 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 = epi32(x), .y = epi32(y), .z = epi32(z), .w = epi32(w) }; + } + + pub inline fn initScalar(scalar: i32x8) Vector4Int_x8 { + return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; + } + + pub inline fn initScalarSingle(scalar: i32) Vector4Int_x8 { + return .{ .x = epi32(scalar), .y = epi32(scalar), .z = epi32(scalar), .w = epi32(scalar) }; + } + + pub inline fn initSplat(vector: Vector4Int) Vector4Int_x8 { + return .{ .x = epi32(vector.x), .y = epi32(vector.y), .z = epi32(vector.z), .w = epi32(vector.w) }; + } + + pub inline fn initArray(array: Array) Vector4Int_x8 { + const x: i32x8 = array[0..8].*; + const y: i32x8 = array[8..16].*; + const z: i32x8 = array[16..24].*; + const w: i32x8 = array[24..32].*; + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initArrayTranspose(array: Array) Vector4Int_x8 { + const vector: @Vector(32, i32) = array; + const x: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }); + const y: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }); + const z: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }); + const w: i32x8 = @shuffle(i32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }); + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initArrayOfVectors(vectors: [8]Vector4Int) Vector4Int_x8 { + return initArrayTranspose(@bitCast(vectors)); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector4Int_x8) Array { + const x: [8]i32 = self.x; + const y: [8]i32 = self.y; + const z: [8]i32 = self.z; + const w: [8]i32 = self.w; + return x ++ y ++ z ++ w; + } + + pub inline fn asArrayTranspose(self: Vector4Int_x8) Array { + const vector: @Vector(32, i32) = self.asArray(); + const transposed: @Vector(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, + }); + return transposed; + } + + pub inline fn asArrayOfVectors(self: Vector4Int_x8) [8]Vector4Int { + return @bitCast(self.asArrayTranspose()); + } + + pub inline fn unpack(self: Vector4Int_x8) [4]i32x8 { + return .{ self.x, self.y, self.z, self.w }; + } + + // --- LOAD AND STORE --- + + pub inline fn loadArray(self: *Vector4Int_x8, array: *const Array) void { + self.x = array[0..8].*; + self.y = array[8..16].*; + self.z = array[16..24].*; + self.w = array[24..32].*; + } + + pub inline fn loadArrayTranspose(self: *Vector4Int_x8, array: *const Array) void { + const vector: @Vector(24, i32) = 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 loadArrayOfVectors(self: *Vector4Int_x8, vectors: *const [8]Vector4Int) void { + self.loadArrayTranspose(@ptrCast(vectors)); + } + + pub inline fn storeArray(self: *const Vector4Int_x8, array: *Array) void { + array[0..8].* = self.x; + array[8..16].* = self.y; + array[16..24].* = self.z; + array[24..32].* = self.w; + } + + pub inline fn storeArrayTranspose(self: *const Vector4Int_x8, array: *Array) void { + const vector: @Vector(32, i32) = self.asArray(); + const transposed: @Vector(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, + }); + array.* = transposed; + } + + pub inline fn storeArrayOfVectors(self: *const Vector4Int_x8, vectors: *[8]Vector4Int) void { + self.storeArrayTranspose(@ptrCast(vectors)); + } + + // --- 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: 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 * epi32(scalar), .y = self.y * epi32(scalar), .z = self.z * epi32(scalar), .w = self.w * 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: 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, epi32(scalar)), .y = @divFloor(self.y, epi32(scalar)), .z = @divFloor(self.z, epi32(scalar)), .w = @divFloor(self.w, 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: 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, epi32(scalar)), .y = @mod(self.y, epi32(scalar)), .z = @mod(self.z, epi32(scalar)), .w = @mod(self.w, 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) 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) i32x8 { + return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; + } +}; diff --git a/packages/vecmath/src/vectors/Vector4x8.zig b/packages/vecmath/src/vectors/Vector4x8.zig new file mode 100644 index 0000000..301827b --- /dev/null +++ b/packages/vecmath/src/vectors/Vector4x8.zig @@ -0,0 +1,230 @@ +const std = @import("std"); +const vm = @import("root"); + +pub const Vector4x8 = struct { + x: f32x8, + y: f32x8, + z: f32x8, + w: f32x8, + + pub const Array = [32]f32; + + 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: f32x8, y: f32x8, z: f32x8, w: f32x8) Vector4x8 { + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initSingle(x: f32, y: f32, z: f32, w: f32) Vector4x8 { + return .{ .x = ps(x), .y = ps(y), .z = ps(z), .w = ps(w) }; + } + + pub inline fn initScalar(scalar: f32x8) Vector4x8 { + return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; + } + + pub inline fn initScalarSingle(scalar: f32) Vector4x8 { + return .{ .x = ps(scalar), .y = ps(scalar), .z = ps(scalar), .w = ps(scalar) }; + } + + pub inline fn initSplat(vector: Vector4) Vector4x8 { + return .{ .x = ps(vector.x), .y = ps(vector.y), .z = ps(vector.z), .w = ps(vector.w) }; + } + + pub inline fn initArray(array: Array) Vector4x8 { + const x: f32x8 = array[0..8].*; + const y: f32x8 = array[8..16].*; + const z: f32x8 = array[16..24].*; + const w: f32x8 = array[24..32].*; + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initArrayTranspose(array: Array) Vector4x8 { + const vector: @Vector(32, f32) = array; + const x: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }); + const y: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }); + const z: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }); + const w: f32x8 = @shuffle(f32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }); + return .{ .x = x, .y = y, .z = z, .w = w }; + } + + pub inline fn initArrayOfVectors(vectors: [8]Vector4) Vector4x8 { + return initArrayTranspose(@bitCast(vectors)); + } + + // --- CONVERSION --- + + pub inline fn asArray(self: Vector4x8) Array { + const x: [8]f32 = self.x; + const y: [8]f32 = self.y; + const z: [8]f32 = self.z; + const w: [8]f32 = self.w; + return x ++ y ++ z ++ w; + } + + pub inline fn asArrayTranspose(self: Vector4x8) Array { + const vector: @Vector(32, f32) = self.asArray(); + const transposed: @Vector(32, f32) = @shuffle(f32, 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, + }); + return transposed; + } + + pub inline fn asArrayOfVectors(self: Vector4x8) [8]Vector4 { + return @bitCast(self.asArrayTranspose()); + } + + pub inline fn unpack(self: Vector4x8) [4]f32x8 { + return .{ self.x, self.y, self.z, self.w }; + } + + // --- LOAD AND STORE --- + + pub inline fn loadArray(self: *Vector4x8, array: *const Array) void { + self.x = array[0..8].*; + self.y = array[8..16].*; + self.z = array[16..24].*; + self.w = array[24..32].*; + } + + pub inline fn loadArrayTranspose(self: *Vector4x8, array: *const Array) void { + const vector: @Vector(32, f32) = array; + self.x = @shuffle(f32, vector, undefined, [_]i32{ 0, 4, 8, 12, 16, 20, 24, 28 }); + self.y = @shuffle(f32, vector, undefined, [_]i32{ 1, 5, 9, 13, 17, 21, 25, 29 }); + self.z = @shuffle(f32, vector, undefined, [_]i32{ 2, 6, 10, 14, 18, 22, 26, 30 }); + self.w = @shuffle(f32, vector, undefined, [_]i32{ 3, 7, 11, 15, 19, 23, 27, 31 }); + } + + pub inline fn loadArrayOfVectors(self: *Vector4x8, vectors: *const [8]Vector4) void { + self.loadArrayTranspose(@ptrCast(vectors)); + } + + pub inline fn storeArray(self: *const Vector4x8, array: *Array) void { + array[0..8].* = self.x; + array[8..16].* = self.y; + array[16..24].* = self.z; + array[24..32].* = self.w; + } + + pub inline fn storeArrayTranspose(self: *const Vector4x8, array: *Array) void { + const vector: @Vector(32, f32) = self.asArray(); + const transposed: @Vector(32, f32) = @shuffle(f32, 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, + }); + array.* = transposed; + } + + pub inline fn storeArrayOfVectors(self: *const Vector4x8, vectors: *[8]Vector4) void { + self.storeArrayTranspose(@ptrCast(vectors)); + } + + // --- COMPONENT-WISE --- + + pub inline fn add(self: Vector4x8, other: Vector4x8) Vector4x8 { + 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: Vector4x8, other: Vector4x8) Vector4x8 { + 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: Vector4x8, other: Vector4x8) Vector4x8 { + 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: Vector4x8, scalar: f32x8) Vector4x8 { + return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; + } + + pub inline fn mulScalarSingle(self: Vector4x8, scalar: f32) Vector4x8 { + return .{ .x = self.x * ps(scalar), .y = self.y * ps(scalar), .z = self.z * ps(scalar), .w = self.w * ps(scalar) }; + } + + pub inline fn div(self: Vector4x8, other: Vector4x8) Vector4x8 { + return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w }; + } + + pub inline fn divScalar(self: Vector4x8, scalar: f32x8) Vector4x8 { + return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar }; + } + + pub inline fn divScalarSingle(self: Vector4x8, scalar: f32) Vector4x8 { + return .{ .x = self.x / ps(scalar), .y = self.y / ps(scalar), .z = self.z / ps(scalar), .w = self.w / ps(scalar) }; + } + + pub inline fn negate(self: Vector4x8) Vector4x8 { + return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; + } + + pub inline fn abs(self: Vector4x8) Vector4x8 { + return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) }; + } + + pub inline fn floor(self: Vector4x8) Vector4x8 { + return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) }; + } + + pub inline fn ceil(self: Vector4x8) Vector4x8 { + return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) }; + } + + pub inline fn round(self: Vector4x8) Vector4x8 { + return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) }; + } + + pub inline fn min(self: Vector4x8, other: Vector4x8) Vector4x8 { + 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: Vector4x8, other: Vector4x8) Vector4x8 { + 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 len(self: Vector4x8) f32x8 { + return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w); + } + + pub inline fn lenSquared(self: Vector4x8) f32x8 { + return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; + } + + pub inline fn dot(self: Vector4x8, other: Vector4x8) f32x8 { + return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; + } + + pub inline fn lerp(a: Vector4x8, b: Vector4x8, t: f32x8) Vector4x8 { + 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)), + .z = @mulAdd(f32x8, t, b.z, @mulAdd(f32x8, -t, a.z, a.z)), + .w = @mulAdd(f32x8, t, b.w, @mulAdd(f32x8, -t, a.w, a.w)), + }; + } +};