Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-30 21:59:14 -05:00
commit 75e86cd8c3
42 files changed
+7557

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
#! Contains `vec4`, a vector of four: a place of space with its `w`, as a
#! matrix projects one, or four numbers worked alike, as a color's. It's an
#! `f32x4`'s 16 bytes, which an executable holding vectors in registers works at
#! once.
import std::math;
import std::serial;
module malunal::linear;
#! A vector of four, as a point of space with its `w`.
@serial
pub struct vec4 {
@serial pub var x: f32;
@serial pub var y: f32;
@serial pub var z: f32;
@serial pub var w: f32;
}
pub val vec4::zero := vec4{ 0, 0, 0, 0 };
pub val vec4::one := vec4{ 1, 1, 1, 1 };
#! Its numbers as lanes, for code of its own to work several at once.
pub def vec4::lanes(&this): f32x4 { return lanes_of(this); }
#! The vector of lanes.
pub def vec4::of_lanes(lanes: f32x4): vec4 { return of_lanes::<vec4>(lanes); }
pub def vec4::add(&this, other: &vec4): vec4 {
return of_lanes::<vec4>(lanes_of(this) + lanes_of(other)) if lanes_held();
return vec4{ this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w };
}
pub def vec4::sub(&this, other: &vec4): vec4 {
return of_lanes::<vec4>(lanes_of(this) - lanes_of(other)) if lanes_held();
return vec4{ this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w };
}
pub def vec4::mul(&this, by: &f32): vec4 {
return of_lanes::<vec4>(lanes_of(this) * f32x4::splat(by)) if lanes_held();
return vec4{ this.x * by, this.y * by, this.z * by, this.w * by };
}
pub def vec4::mul(&this, other: &vec4): vec4 {
return of_lanes::<vec4>(lanes_of(this) * lanes_of(other)) if lanes_held();
return vec4{ this.x * other.x, this.y * other.y, this.z * other.z, this.w * other.w };
}
pub def vec4::div(&this, by: &f32): vec4 {
return of_lanes::<vec4>(lanes_of(this) / f32x4::splat(by)) if lanes_held();
return vec4{ this.x / by, this.y / by, this.z / by, this.w / by };
}
pub def vec4::div(&this, other: &vec4): vec4 {
return of_lanes::<vec4>(lanes_of(this) / lanes_of(other)) if lanes_held();
return vec4{ this.x / other.x, this.y / other.y, this.z / other.z, this.w / other.w };
}
pub def vec4::negate(&this): vec4 {
return of_lanes::<vec4>(-lanes_of(this)) if lanes_held();
return vec4{ -this.x, -this.y, -this.z, -this.w };
}
pub def vec4::equals(&this, other: &vec4): bool {
return this.x == other.x && this.y == other.y && this.z == other.z && this.w == other.w;
}
pub def vec4::dot(&this, other: vec4): f32 {
if lanes_held() {
val p := lanes_of(this) * lanes_of(&other);
return p[0] + p[1] + p[2] + p[3];
}
return this.x * other.x + this.y * other.y + this.z * other.z + this.w * other.w;
}
pub val vec4::length: f32 {
get => math::sqrt(this.dot(this));
}
#! The length squared, which needs no square root, to compare lengths by.
pub val vec4::length_squared: f32 {
get => this.dot(this);
}
#! The vector at a length of 1, or none where it has no length.
pub def vec4::normalized(&this): vec4 {
val l := this.length;
return l > 0 ? this / l : vec4{};
}
#! The vector a fraction of the way to another.
pub def vec4::lerp(&this, to: vec4, t: f32): vec4 {
return this + (to - this) * t;
}
#! The lesser of each pair of numbers, as `math::min` gives it.
pub def vec4::min(&this, other: vec4): vec4 {
return of_lanes::<vec4>(lanes_of(this).min(&lanes_of(&other))) if lanes_held();
return vec4{ math::min(this.x, other.x), math::min(this.y, other.y), math::min(this.z, other.z), math::min(this.w, other.w) };
}
#! The greater of each pair of numbers, as `math::max` gives it.
pub def vec4::max(&this, other: vec4): vec4 {
return of_lanes::<vec4>(lanes_of(this).max(&lanes_of(&other))) if lanes_held();
return vec4{ math::max(this.x, other.x), math::max(this.y, other.y), math::max(this.z, other.z), math::max(this.w, other.w) };
}
#! Each number's distance from zero.
pub def vec4::abs(&this): vec4 {
return of_lanes::<vec4>(lanes_of(this).abs()) if lanes_held();
return vec4{ math::abs(this.x), math::abs(this.y), math::abs(this.z), math::abs(this.w) };
}
#! Its first three.
pub val vec4::xyz: vec3 {
get => vec3{ this.x, this.y, this.z };
}