JUMBO vecmath completion update
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
const std = @import("std");
|
||||
const vm = @import("root");
|
||||
const vm = @import("../root.zig");
|
||||
|
||||
pub const Complex_x8 = struct {
|
||||
re: vm.f32x8,
|
||||
im: vm.f32x8,
|
||||
|
||||
pub const Array = [16]f32;
|
||||
|
||||
pub const identity = initSingle(1, 0);
|
||||
|
||||
// --- INIT ---
|
||||
// --- INIT ----------------------------------------------------------------
|
||||
|
||||
pub inline fn init(re: vm.f32x8, im: vm.f32x8) Complex_x8 {
|
||||
return .{ .re = re, .im = im };
|
||||
@@ -20,36 +18,179 @@ pub const Complex_x8 = struct {
|
||||
}
|
||||
|
||||
pub inline fn initRotation(angle_turns: vm.f32x8) Complex_x8 {
|
||||
const c, const s = vm.cossin_x8(angle_turns).asArray();
|
||||
return .{ .re = c, .im = s };
|
||||
return vm.cossin_x8(angle_turns);
|
||||
}
|
||||
|
||||
pub inline fn initRotationSingle(angle_turns: f32) Complex_x8 {
|
||||
const c, const s = vm.cossin(angle_turns).asArray();
|
||||
return .{ .re = vm.ps(c), .im = vm.ps(s) };
|
||||
}
|
||||
|
||||
pub inline fn initSplat(complex: vm.Complex) Complex_x8 {
|
||||
const complex = vm.cossin(angle_turns);
|
||||
return .{ .re = vm.ps(complex.re), .im = vm.ps(complex.im) };
|
||||
}
|
||||
|
||||
pub inline fn initArray(array: Array) Complex_x8 {
|
||||
const re: vm.f32x8 = array[0..8].*;
|
||||
const im: vm.f32x8 = array[8..16].*;
|
||||
return .{ .re = re, .im = im };
|
||||
pub inline fn initArrayOfComplex(complex: [8]vm.Complex) Complex_x8 {
|
||||
const vector: @Vector(16, f32) = @as([16]f32, @bitCast(complex));
|
||||
return .{
|
||||
.re = @shuffle(f32, vector, undefined, [_]i32{ 0, 2, 4, 6, 8, 10, 12, 14 }),
|
||||
.im = @shuffle(f32, vector, undefined, [_]i32{ 1, 3, 5, 7, 9, 11, 13, 15 }),
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn initVector2(vector: vm.Vector2x8) Complex_x8 {
|
||||
return @bitCast(vector);
|
||||
pub inline fn splat(complex: vm.Complex) Complex_x8 {
|
||||
return .{ .re = vm.ps(complex.re), .im = vm.ps(complex.im) };
|
||||
}
|
||||
|
||||
// --- CONVERSION ---
|
||||
// --- CONVERSION ----------------------------------------------------------
|
||||
|
||||
pub inline fn asArray(self: Complex_x8) Array {
|
||||
return @bitCast(self);
|
||||
pub inline fn asArrayOfComplex(self: Complex_x8) [8]vm.Complex {
|
||||
const vector: @Vector(16, f32) = self.re ++ self.im;
|
||||
return @bitCast(@as([16]f32, @shuffle(f32, vector, undefined, [_]i32{
|
||||
0, 8,
|
||||
1, 9,
|
||||
2, 10,
|
||||
3, 11,
|
||||
4, 12,
|
||||
5, 13,
|
||||
6, 14,
|
||||
7, 15,
|
||||
})));
|
||||
}
|
||||
|
||||
pub inline fn asVector2(self: Complex_x8) vm.Vector2_x8 {
|
||||
return @bitCast(self);
|
||||
pub inline fn unpack(self: Complex_x8) [2]vm.f32x8 {
|
||||
return .{ self.re, self.im };
|
||||
}
|
||||
|
||||
// --- LOAD AND STORE ------------------------------------------------------
|
||||
|
||||
pub inline fn loadArrayOfComplex(self: *Complex_x8, array: *const [8]vm.Complex) void {
|
||||
const vector: @Vector(16, f32) = @as(*const [16]f32, @ptrCast(array)).*;
|
||||
self.re = @shuffle(f32, vector, undefined, [_]i32{ 0, 2, 4, 6, 8, 10, 12, 14 });
|
||||
self.im = @shuffle(f32, vector, undefined, [_]i32{ 1, 3, 5, 7, 9, 11, 13, 15 });
|
||||
}
|
||||
|
||||
pub inline fn storeArrayOfComplex(self: *const Complex_x8, array: *[8]vm.Complex) void {
|
||||
const vector: @Vector(16, f32) = self.re ++ self.im;
|
||||
@as(*[16]f32, @ptrCast(array)).* = @shuffle(f32, vector, undefined, [_]i32{
|
||||
0, 8,
|
||||
1, 9,
|
||||
2, 10,
|
||||
3, 11,
|
||||
4, 12,
|
||||
5, 13,
|
||||
6, 14,
|
||||
7, 15,
|
||||
});
|
||||
}
|
||||
|
||||
// --- COMPONENT-WISE ------------------------------------------------------
|
||||
|
||||
pub inline fn add(self: Complex_x8, other: Complex_x8) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re + other.re,
|
||||
.im = self.im + other.im,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn sub(self: Complex_x8, other: Complex_x8) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re - other.re,
|
||||
.im = self.im - other.im,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn mulScalar(self: Complex_x8, scalar: vm.f32x8) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re * scalar,
|
||||
.im = self.im * scalar,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn mulScalarSingle(self: Complex_x8, scalar: f32) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re * vm.ps(scalar),
|
||||
.im = self.im * vm.ps(scalar),
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn divScalar(self: Complex_x8, scalar: vm.f32x8) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re / scalar,
|
||||
.im = self.im / scalar,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn divScalarSingle(self: Complex_x8, scalar: f32) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re / vm.ps(scalar),
|
||||
.im = self.im / vm.ps(scalar),
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn negate(self: Complex_x8) Complex_x8 {
|
||||
return .{
|
||||
.re = -self.re,
|
||||
.im = -self.im,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn conjugate(self: Complex_x8) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re,
|
||||
.im = -self.im,
|
||||
};
|
||||
}
|
||||
|
||||
// --- COMPOSE -------------------------------------------------------------
|
||||
|
||||
pub inline fn mulComplex(self: Complex_x8, other: Complex_x8) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re * other.re - self.im * other.im,
|
||||
.im = self.re * other.im + self.im * other.re,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn mulComplexSingle(self: Complex_x8, other: vm.Complex) Complex_x8 {
|
||||
return .{
|
||||
.re = self.re * vm.ps(other.re) - self.im * vm.ps(other.im),
|
||||
.im = self.re * vm.ps(other.im) + self.im * vm.ps(other.re),
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn inverseUnit(self: Complex_x8) Complex_x8 {
|
||||
std.debug.assert(@reduce(.And, @abs(self.mag() - vm.ps(1.0)) <= vm.ps(0x1p-10)));
|
||||
return .{
|
||||
.re = self.re,
|
||||
.im = -self.im,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn inverseFull(self: Complex_x8) Complex_x8 {
|
||||
const inv_mag_squared = 1.0 / (self.re * self.re + self.im * self.im);
|
||||
return .{
|
||||
.re = inv_mag_squared * self.re,
|
||||
.im = -inv_mag_squared * self.im,
|
||||
};
|
||||
}
|
||||
|
||||
// --- OTHER ---------------------------------------------------------------
|
||||
|
||||
pub inline fn mag(self: Complex_x8) vm.f32x8 {
|
||||
return @sqrt(self.re * self.re + self.im * self.im);
|
||||
}
|
||||
|
||||
pub inline fn magSquared(self: Complex_x8) vm.f32x8 {
|
||||
return self.re * self.re + self.im * self.im;
|
||||
}
|
||||
|
||||
pub inline fn lerp(a: Complex_x8, b: Complex_x8, t: vm.f32x8) Complex_x8 {
|
||||
return .{
|
||||
.re = @mulAdd(vm.f32x8, t, b.re, @mulAdd(vm.f32x8, -t, a.re, a.re)),
|
||||
.im = @mulAdd(vm.f32x8, t, b.im, @mulAdd(vm.f32x8, -t, a.im, a.im)),
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn lerpSingle(a: Complex_x8, b: Complex_x8, t: f32) Complex_x8 {
|
||||
return .{
|
||||
.re = @mulAdd(vm.f32x8, vm.ps(t), b.re, @mulAdd(vm.f32x8, -vm.ps(t), a.re, a.re)),
|
||||
.im = @mulAdd(vm.f32x8, vm.ps(t), b.im, @mulAdd(vm.f32x8, -vm.ps(t), a.im, a.im)),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user