Add widening/shortening vector dimensions

This commit is contained in:
2026-01-04 17:13:31 +01:00
parent ed6391e97a
commit 7e8103565d
12 changed files with 102 additions and 6 deletions

View File

@@ -34,6 +34,14 @@ pub const Vector2 = extern struct {
return @bitCast(self);
}
pub inline fn withZ(self: Vector2, z: f32) vm.Vector3 {
return .{ .x = self.x, .y = self.y, .z = z };
}
pub inline fn withZW(self: Vector2, z: f32, w: f32) vm.Vector4 {
return .{ .x = self.x, .y = self.y, .z = z, .w = w };
}
// --- COMPONENT-WISE ------------------------------------------------------
pub inline fn add(self: Vector2, other: Vector2) Vector2 {

View File

@@ -34,6 +34,14 @@ pub const Vector2Int = extern struct {
return @bitCast(self);
}
pub inline fn withZ(self: Vector2Int, z: i32) vm.Vector3Int {
return .{ .x = self.x, .y = self.y, .z = z };
}
pub inline fn withZW(self: Vector2Int, z: i32, w: i32) vm.Vector4Int {
return .{ .x = self.x, .y = self.y, .z = z, .w = w };
}
// --- COMPONENT-WISE ------------------------------------------------------
pub inline fn add(self: Vector2Int, other: Vector2Int) Vector2Int {

View File

@@ -44,6 +44,14 @@ pub const Vector2Int_x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn withZ(self: Vector2Int_x8, z: vm.i32x8) vm.Vector3Int_x8 {
return .{ .x = self.x, .y = self.y, .z = z };
}
pub inline fn withZW(self: Vector2Int_x8, z: vm.i32x8, w: vm.i32x8) vm.Vector4Int_x8 {
return .{ .x = self.x, .y = self.y, .z = z, .w = w };
}
pub inline fn asArrayOfVectors(self: Vector2Int_x8) [8]vm.Vector2Int {
const vector: @Vector(16, i32) = self.x ++ self.y;
return @bitCast(@as([16]i32, @shuffle(i32, vector, undefined, [_]i32{

View File

@@ -44,6 +44,14 @@ pub const Vector2x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn withZ(self: Vector2x8, z: vm.f32x8) vm.Vector3x8 {
return .{ .x = self.x, .y = self.y, .z = z };
}
pub inline fn withZW(self: Vector2x8, z: vm.f32x8, w: vm.f32x8) vm.Vector4x8 {
return .{ .x = self.x, .y = self.y, .z = z, .w = w };
}
pub inline fn asArrayOfVectors(self: Vector2x8) [8]vm.Vector2 {
const vector: @Vector(16, f32) = self.x ++ self.y;
return @bitCast(@as([16]f32, @shuffle(f32, vector, undefined, [_]i32{

View File

@@ -37,6 +37,14 @@ pub const Vector3 = extern struct {
return @bitCast(self);
}
pub inline fn dropZ(self: Vector3) vm.Vector2 {
return .{ .x = self.x, .y = self.y };
}
pub inline fn withW(self: Vector3, w: f32) vm.Vector4 {
return .{ .x = self.x, .y = self.y, .z = self.z, .w = w };
}
// --- COMPONENT-WISE ------------------------------------------------------
pub inline fn add(self: Vector3, other: Vector3) Vector3 {

View File

@@ -37,6 +37,14 @@ pub const Vector3Int = extern struct {
return @bitCast(self);
}
pub inline fn dropZ(self: Vector3Int) vm.Vector2Int {
return .{ .x = self.x, .y = self.y };
}
pub inline fn withW(self: Vector3Int, w: i32) vm.Vector4Int {
return .{ .x = self.x, .y = self.y, .z = self.z, .w = w };
}
// --- COMPONENT-WISE ------------------------------------------------------
pub inline fn add(self: Vector3Int, other: Vector3Int) Vector3Int {
@@ -51,7 +59,7 @@ pub const Vector3Int = extern struct {
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 {
pub inline fn mulScalar(self: Vector3Int, scalar: i32) Vector3Int {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
}
@@ -59,7 +67,7 @@ pub const Vector3Int = extern struct {
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 {
pub inline fn divScalar(self: Vector3Int, scalar: i32) Vector3Int {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) };
}
@@ -67,7 +75,7 @@ pub const Vector3Int = extern struct {
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 {
pub inline fn modScalar(self: Vector3Int, scalar: i32) Vector3Int {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) };
}

View File

@@ -48,6 +48,14 @@ pub const Vector3Int_x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn dropZ(self: Vector3Int_x8) vm.Vector2Int_x8 {
return .{ .x = self.x, .y = self.y };
}
pub inline fn withW(self: Vector3Int_x8, w: vm.i32x8) vm.Vector4Int_x8 {
return .{ .x = self.x, .y = self.y, .z = self.z, .w = w };
}
pub inline fn asArrayOfVectors(self: Vector3Int_x8) [8]vm.Vector3Int {
const vector: @Vector(24, i32) = self.x ++ self.y ++ self.z;
return @bitCast(@as([16]i32, @shuffle(i32, vector, undefined, [_]i32{

View File

@@ -48,6 +48,14 @@ pub const Vector3x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn dropZ(self: Vector3x8) vm.Vector2x8 {
return .{ .x = self.x, .y = self.y };
}
pub inline fn withW(self: Vector3x8, w: vm.f32x8) vm.Vector4x8 {
return .{ .x = self.x, .y = self.y, .z = self.z, .w = w };
}
pub inline fn asArrayOfVectors(self: Vector3x8) [8]vm.Vector3 {
const vector: @Vector(24, f32) = self.x ++ self.y ++ self.z;
return @bitCast(@as([16]f32, @shuffle(f32, vector, undefined, [_]i32{

View File

@@ -40,6 +40,14 @@ pub const Vector4 = extern struct {
return @bitCast(self);
}
pub inline fn dropW(self: Vector4) vm.Vector3 {
return .{ .x = self.x, .y = self.y, .z = self.z };
}
pub inline fn dropZW(self: Vector4) vm.Vector2 {
return .{ .x = self.x, .y = self.y };
}
// --- COMPONENT-WISE ------------------------------------------------------
pub inline fn add(self: Vector4, other: Vector4) Vector4 {

View File

@@ -40,6 +40,14 @@ pub const Vector4Int = extern struct {
return @bitCast(self);
}
pub inline fn dropW(self: Vector4Int) vm.Vector3Int {
return .{ .x = self.x, .y = self.y, .z = self.z };
}
pub inline fn dropZW(self: Vector4Int) vm.Vector2Int {
return .{ .x = self.x, .y = self.y };
}
// --- COMPONENT-WISE ------------------------------------------------------
pub inline fn add(self: Vector4Int, other: Vector4Int) Vector4Int {
@@ -54,7 +62,7 @@ pub const Vector4Int = extern struct {
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 {
pub inline fn mulScalar(self: Vector4Int, scalar: i32) Vector4Int {
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar };
}
@@ -62,7 +70,7 @@ pub const Vector4Int = extern struct {
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 {
pub inline fn divScalar(self: Vector4Int, scalar: i32) Vector4Int {
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar), .w = @divFloor(self.w, scalar) };
}
@@ -70,7 +78,7 @@ pub const Vector4Int = extern struct {
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 {
pub inline fn modScalar(self: Vector4Int, scalar: i32) Vector4Int {
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar), .w = @mod(self.w, scalar) };
}

View File

@@ -52,6 +52,14 @@ pub const Vector4Int_x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn dropW(self: Vector4Int_x8) vm.Vector3Int_x8 {
return .{ .x = self.x, .y = self.y, .z = self.z };
}
pub inline fn dropZW(self: Vector4Int_x8) vm.Vector2Int_x8 {
return .{ .x = self.x, .y = self.y };
}
pub inline fn asArrayOfVectors(self: Vector4Int_x8) [8]vm.Vector4 {
const vector: @Vector(32, i32) = self.x ++ self.y ++ self.z ++ self.w;
return @bitCast(@as([32]i32, @shuffle(i32, vector, undefined, [_]i32{

View File

@@ -52,6 +52,14 @@ pub const Vector4x8 = struct {
// --- CONVERSION ----------------------------------------------------------
pub inline fn dropW(self: Vector4x8) vm.Vector3x8 {
return .{ .x = self.x, .y = self.y, .z = self.z };
}
pub inline fn dropZW(self: Vector4x8) vm.Vector2x8 {
return .{ .x = self.x, .y = self.y };
}
pub inline fn asArrayOfVectors(self: Vector4x8) [8]vm.Vector4 {
const vector: @Vector(32, f32) = self.x ++ self.y ++ self.z ++ self.w;
return @bitCast(@as([32]f32, @shuffle(f32, vector, undefined, [_]i32{