diff --git a/packages/media/build.zig b/packages/media/build.zig index 45875af..b181d66 100644 --- a/packages/media/build.zig +++ b/packages/media/build.zig @@ -1,7 +1,11 @@ const std = @import("std"); pub fn build(b: *std.Build) void { - _ = b.addModule("media", .{ + const vm_dep = b.dependency("vecmath", .{}); + const vm_module = vm_dep.module("vecmath"); + + const root_module = b.addModule("media", .{ .root_source_file = b.path("src/root.zig"), }); + root_module.addImport("vecmath", vm_module); } diff --git a/packages/media/build.zig.zon b/packages/media/build.zig.zon index 0f375f6..ae81272 100644 --- a/packages/media/build.zig.zon +++ b/packages/media/build.zig.zon @@ -8,4 +8,9 @@ "build.zig.zon", }, .fingerprint = 0x6a2ca10cfcadaa2b, + .dependencies = .{ + .vecmath = .{ + .path = "../vecmath", + }, + }, } diff --git a/packages/media/src/color.zig b/packages/media/src/color.zig deleted file mode 100644 index eb30cb7..0000000 --- a/packages/media/src/color.zig +++ /dev/null @@ -1,227 +0,0 @@ -const std = @import("std"); - -pub const ColorU8 = extern struct { - r: u8, - g: u8, - b: u8, - a: u8, - - pub const Array = [4]u8; - pub const Vector = @Vector(4, u8); - - pub const clear = ColorU8.init(0, 0, 0, 0); - pub const black = ColorU8.init(0, 0, 0, 255); - pub const white = ColorU8.init(255, 255, 255, 255); - - pub inline fn init(r: u8, g: u8, b: u8, a: u8) ColorU8 { - return .{ .r = r, .g = g, .b = b, .a = a }; - } - - pub inline fn initArray(array: Array) ColorU8 { - return @bitCast(array); - } - - pub inline fn initVector(vector: Vector) ColorU8 { - return @bitCast(@as(Array, vector)); - } - - pub inline fn initColorF16(color: ColorF16) ColorU8 { - const zero: ColorF16.Vector = @splat(0); - const one: ColorF16.Vector = @splat(1); - const scale: ColorF16.Vector = @splat(255); - - const vector = @round(std.math.clamp(color.asVector(), zero, one) * scale); - return .{ - .r = @intFromFloat(vector[0]), - .g = @intFromFloat(vector[1]), - .b = @intFromFloat(vector[2]), - .a = @intFromFloat(vector[3]), - }; - } - - pub inline fn initColorF32(color: ColorF32) ColorU8 { - const zero: ColorF32.Vector = @splat(0); - const one: ColorF32.Vector = @splat(1); - const scale: ColorF32.Vector = @splat(255); - - const vector = @round(std.math.clamp(color.asVector(), zero, one) * scale); - return .{ - .r = @intFromFloat(vector[0]), - .g = @intFromFloat(vector[1]), - .b = @intFromFloat(vector[2]), - .a = @intFromFloat(vector[3]), - }; - } - - pub inline fn asArray(self: ColorU8) Array { - return @bitCast(self); - } - - pub inline fn asVector(self: ColorU8) Vector { - return @as(Array, @bitCast(self)); - } -}; - -pub const ColorF16 = extern struct { - r: f16, - g: f16, - b: f16, - a: f16, - - pub const Array = [4]f16; - pub const Vector = @Vector(4, f16); - - pub const clear = ColorF16.init(0.0, 0.0, 0.0, 0.0); - pub const black = ColorF16.init(0.0, 0.0, 0.0, 1.0); - pub const white = ColorF16.init(1.0, 1.0, 1.0, 1.0); - - pub inline fn init(r: f16, g: f16, b: f16, a: f16) ColorF16 { - return .{ .r = r, .g = g, .b = b, .a = a }; - } - - pub inline fn initArray(array: Array) ColorF16 { - return @bitCast(array); - } - - pub inline fn initVector(vector: Vector) ColorF16 { - return @bitCast(@as(Array, vector)); - } - - pub inline fn initColorU8(color: ColorU8) ColorF16 { - const vector = Vector{ - @as(f32, color.r), - @as(f32, color.g), - @as(f32, color.b), - @as(f32, color.a), - } / @as(Vector, @splat(255.0)); - - return @bitCast(@as(Array, vector)); - } - - pub inline fn initColorF32(color: ColorF32) ColorF16 { - return @bitCast(@as(Array, @as(Vector, @floatCast(color.asVector())))); - } - - pub inline fn asArray(self: ColorF16) Array { - return @bitCast(self); - } - - pub inline fn asVector(self: ColorF16) Vector { - return @as(Array, @bitCast(self)); - } - - pub inline fn add(self: ColorF16, other: ColorF16) ColorF16 { - return .initVector(self.asVector() + other.asVector()); - } - - pub inline fn sub(self: ColorF16, other: ColorF16) ColorF16 { - return .initVector(self.asVector() - other.asVector()); - } - - pub inline fn mul(self: ColorF16, other: ColorF16) ColorF16 { - return .initVector(self.asVector() * other.asVector()); - } - - pub inline fn div(self: ColorF16, other: ColorF16) ColorF16 { - return .initVector(self.asVector() / other.asVector()); - } - - pub inline fn mulScalar(self: ColorF16, scalar: f16) ColorF16 { - const scalar_vector: Vector = @splat(scalar); - return .initVector(self.asVector() * scalar_vector); - } - - pub inline fn divScalar(self: ColorF16, scalar: f16) ColorF16 { - const scalar_vector: Vector = @splat(scalar); - return .initVector(self.asVector() / scalar_vector); - } - - pub inline fn lerp(self: ColorF16, other: ColorF16, t: f16) ColorF16 { - const s = 1.0 - t; - const t_vector: Vector = @splat(t); - const s_vector: Vector = @splat(s); - return .initVector(self.asVector() * s_vector + other.asVector() * t_vector); - } -}; - -pub const ColorF32 = extern struct { - r: f32, - g: f32, - b: f32, - a: f32, - - pub const Array = [4]f32; - pub const Vector = @Vector(4, f32); - - pub const clear = ColorF32.init(0.0, 0.0, 0.0, 0.0); - pub const black = ColorF32.init(0.0, 0.0, 0.0, 1.0); - pub const white = ColorF32.init(1.0, 1.0, 1.0, 1.0); - - pub inline fn init(r: f32, g: f32, b: f32, a: f32) ColorF32 { - return .{ .r = r, .g = g, .b = b, .a = a }; - } - - pub inline fn initArray(array: Array) ColorF32 { - return @bitCast(array); - } - - pub inline fn initVector(vector: Vector) ColorF32 { - return @bitCast(@as(Array, vector)); - } - - pub inline fn initColorU8(color: ColorU8) ColorF32 { - const vector = Vector{ - @as(f32, color.r), - @as(f32, color.g), - @as(f32, color.b), - @as(f32, color.a), - } / @as(Vector, @splat(255.0)); - - return @bitCast(@as(Array, vector)); - } - - pub inline fn initColorF16(color: ColorF16) ColorF32 { - return @bitCast(@as(Array, @as(Vector, @floatCast(color.asVector())))); - } - - pub inline fn asArray(self: ColorF32) Array { - return @bitCast(self); - } - - pub inline fn asVector(self: ColorF32) Vector { - return @as(Array, @bitCast(self)); - } - - pub inline fn add(self: ColorF32, other: ColorF32) ColorF32 { - return .initVector(self.asVector() + other.asVector()); - } - - pub inline fn sub(self: ColorF32, other: ColorF32) ColorF32 { - return .initVector(self.asVector() - other.asVector()); - } - - pub inline fn mul(self: ColorF32, other: ColorF32) ColorF32 { - return .initVector(self.asVector() * other.asVector()); - } - - pub inline fn div(self: ColorF32, other: ColorF32) ColorF32 { - return .initVector(self.asVector() / other.asVector()); - } - - pub inline fn mulScalar(self: ColorF32, scalar: f32) ColorF32 { - const scalar_vector: Vector = @splat(scalar); - return .initVector(self.asVector() * scalar_vector); - } - - pub inline fn divScalar(self: ColorF32, scalar: f32) ColorF32 { - const scalar_vector: Vector = @splat(scalar); - return .initVector(self.asVector() / scalar_vector); - } - - pub inline fn lerp(self: ColorF32, other: ColorF32, t: f32) ColorF32 { - const s = 1.0 - t; - const t_vector: Vector = @splat(t); - const s_vector: Vector = @splat(s); - return .initVector(self.asVector() * s_vector + other.asVector() * t_vector); - } -}; diff --git a/packages/media/src/image.zig b/packages/media/src/image.zig index cf9cdcb..62bf5ad 100644 --- a/packages/media/src/image.zig +++ b/packages/media/src/image.zig @@ -1,25 +1,24 @@ const std = @import("std"); - -const Color = @import("color.zig").Color; +const vm = @import("vecmath"); pub fn Static(comptime W: u32, comptime H: u32) type { return struct { - data: [W * H]Color, + data: [W * H]vm.Color, pub const width = W; pub const height = H; - pub fn getPixel(self: *const @This(), x: u32, y: u32) Color { + pub fn getPixel(self: *const @This(), x: u32, y: u32) vm.Color { std.debug.assert(x < width and y < height); return self.data[y * width + x]; } - pub fn setPixel(self: *@This(), x: u32, y: u32, color: Color) void { + pub fn setPixel(self: *@This(), x: u32, y: u32, color: vm.Color) void { std.debug.assert(x < width and y < height); self.data[y * width + x] = color; } - pub fn fill(self: *@This(), color: Color) void { + pub fn fill(self: *@This(), color: vm.Color) void { @memset(self.data, color); } }; @@ -29,9 +28,9 @@ pub const Dynamic = struct { width: u32, height: u32, - data: [*]Color, + data: [*]vm.Color, - pub fn initBuffer(width: u32, height: u32, buffer: []Color) @This() { + pub fn initBuffer(width: u32, height: u32, buffer: []vm.Color) @This() { std.debug.assert(buffer.len == width * height); return .{ .width = width, @@ -41,7 +40,7 @@ pub const Dynamic = struct { } pub fn initAlloc(width: u32, height: u32, allocator: std.mem.Allocator) !@This() { - const buffer = try allocator.alloc(Color, width * height); + const buffer = try allocator.alloc(vm.Color, width * height); return .{ .width = width, .height = height, @@ -53,17 +52,17 @@ pub const Dynamic = struct { allocator.free(self.data[0 .. self.width * self.height]); } - pub fn getPixel(self: *const @This(), x: u32, y: u32) Color { + pub fn getPixel(self: *const @This(), x: u32, y: u32) vm.Color { std.debug.assert(x < self.width and y < self.height); return self.data[y * self.width + x]; } - pub fn setPixel(self: *@This(), x: u32, y: u32, color: Color) void { + pub fn setPixel(self: *@This(), x: u32, y: u32, color: vm.Color) void { std.debug.assert(x < self.width and y < self.height); self.data[y * self.width + x] = color; } - pub fn fill(self: *@This(), color: Color) void { + pub fn fill(self: *@This(), color: vm.Color) void { @memset(self.data[0 .. self.width * self.height], color); } }; diff --git a/packages/media/src/qoa.zig b/packages/media/src/qoa.zig index 42aa355..23a7e07 100644 --- a/packages/media/src/qoa.zig +++ b/packages/media/src/qoa.zig @@ -35,9 +35,7 @@ pub fn info(buffer: []const u8) ?Header { } if (samples == 0) { - return .{ - .streaming = {}, - }; + return .streaming; } else { return .{ .static = .{ diff --git a/packages/media/src/root.zig b/packages/media/src/root.zig index 4cc64ad..a64314c 100644 --- a/packages/media/src/root.zig +++ b/packages/media/src/root.zig @@ -1,5 +1,4 @@ pub const audio = @import("audio.zig"); -pub const color = @import("color.zig"); pub const image = @import("image.zig"); pub const qoa = @import("qoa.zig"); pub const qoi = @import("qoi.zig"); diff --git a/packages/vecmath/build.zig b/packages/vecmath/build.zig new file mode 100644 index 0000000..a29f40b --- /dev/null +++ b/packages/vecmath/build.zig @@ -0,0 +1,7 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + _ = b.addModule("vecmath", .{ + .root_source_file = b.path("src/root.zig"), + }); +} diff --git a/packages/vecmath/build.zig.zon b/packages/vecmath/build.zig.zon new file mode 100644 index 0000000..65047e1 --- /dev/null +++ b/packages/vecmath/build.zig.zon @@ -0,0 +1,11 @@ +.{ + .name = .vecmath, + .version = "0.0.0", + .minimum_zig_version = "0.15.2", + .paths = .{ + "src", + "build.zig", + "build.zig.zon", + }, + .fingerprint = 0x1c1c724ae9962a2d, +} diff --git a/packages/vecmath/src/root.zig b/packages/vecmath/src/root.zig new file mode 100644 index 0000000..93bc0b0 --- /dev/null +++ b/packages/vecmath/src/root.zig @@ -0,0 +1,2651 @@ +const std = @import("std"); + +// --- 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); + +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); +} + +// --- TRIGONOMETRY ------------------------------------------------------------ + +pub fn cos(angle_turns: f32) f32 { + return cossin(angle_turns).x; +} + +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: 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()); +}