Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-27 22:20:40 -05:00
commit 9d3d6a8c75
14 files changed
+1467

No files matched your search

+304
View File
@@ -0,0 +1,304 @@
#! Times what `linear` does most, each over a thousand values two thousand
#! times, against the same worked a number at a time on a `vec3` of 12 bytes,
#! as hearth's scenes worked it before: adding and scaling vectors, their dots
#! and lengths, quaternions' products and turns, and matrices' products,
#! points, and inverses.
#!
#! Each pass is a function of its own, as a system is, and what it times is
#! called from elsewhere too, so nothing is inlined only for being called once:
#! the code an executable makes of a loop hangs on what is around it.
#!
#! `blossom build`, then run `linear.bench`, as an executable optimized with
#! `-O2`: the VM works numbers one at a time either way.
import std::io;
import std::math;
import std::time;
import std::containers;
import linear;
# A vector of three of 12 bytes, and the rest, as they were before.
struct old3 { var x: f32; var y: f32; var z: f32; }
struct oldq { var x: f32; var y: f32; var z: f32; var w: f32; }
struct oldm { var m: f32[16]; }
def old3::add(&this, o: &old3): old3 { return old3{ this.x + o.x, this.y + o.y, this.z + o.z }; }
def old3::mul(&this, by: &f32): old3 { return old3{ this.x * by, this.y * by, this.z * by }; }
def old3::dot(&this, o: old3): f32 { return this.x * o.x + this.y * o.y + this.z * o.z; }
def old3::cross(&this, o: old3): old3 { return old3{ this.y * o.z - this.z * o.y, this.z * o.x - this.x * o.z, this.x * o.y - this.y * o.x }; }
def old3::normalized(&this): old3 {
val l := math::sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
return l > 0 ? old3{ this.x / l, this.y / l, this.z / l } : old3{};
}
def oldq::mul(&this, o: &oldq): oldq {
return oldq{
this.w * o.x + this.x * o.w + this.y * o.z - this.z * o.y,
this.w * o.y - this.x * o.z + this.y * o.w + this.z * o.x,
this.w * o.z + this.x * o.y - this.y * o.x + this.z * o.w,
this.w * o.w - this.x * o.x - this.y * o.y - this.z * o.z,
};
}
def oldq::turn(&this, v: &old3): old3 {
val q := old3{ this.x, this.y, this.z };
val t := q.cross(v) * 2;
return v + t * this.w + q.cross(t);
}
def oldm::mul(&this, other: &oldm): oldm {
var made := oldm{};
for c in 0..4u {
for r in 0..4u {
made.m[c * 4 + r] = this.m[r] * other.m[c * 4] + this.m[4 + r] * other.m[c * 4 + 1]
+ this.m[8 + r] * other.m[c * 4 + 2] + this.m[12 + r] * other.m[c * 4 + 3];
}
}
return made;
}
# A matrix of three as one would be worked a number at a time.
struct old33 { var x: old3; var y: old3; var z: old3; }
def old33::turned(&this, v: &old3): old3 {
return old3{
this.x.x * v.x + this.y.x * v.y + this.z.x * v.z,
this.x.y * v.x + this.y.y * v.y + this.z.y * v.z,
this.x.z * v.x + this.y.z * v.y + this.z.z * v.z,
};
}
def old33::mul(&this, o: &old33): old33 {
return old33{ this.turned(&o.x), this.turned(&o.y), this.turned(&o.z) };
}
def oldm::point(&this, p: old3): old3 {
return old3{
this.m[0] * p.x + this.m[4] * p.y + this.m[8] * p.z + this.m[12],
this.m[1] * p.x + this.m[5] * p.y + this.m[9] * p.z + this.m[13],
this.m[2] * p.x + this.m[6] * p.y + this.m[10] * p.z + this.m[14],
};
}
const count: u64 = 1024;
const rounds: u64 = 2000;
const dt: f32 = 0.016;
# What each pass works over.
struct data {
var ps: vector<vec3>;
var vs: vector<vec3>;
var qs: vector<quat>;
var ms: vector<mat4>;
var rs: vector<mat3>;
var ws: vector<vec4>;
var out: vector<mat4>;
var ops: vector<old3>;
var ovs: vector<old3>;
var oqs: vector<oldq>;
var oms: vector<oldm>;
var oout: vector<oldm>;
var ors: vector<old33>;
var r_out: vector<mat3>;
var or_out: vector<old33>;
var sum: f32;
}
# How long a pass took, for each value, beside what it took before.
def report(what: strview, started: &instant, before: f64): f64 {
val ns := started.elapsed().in_nanoseconds as f64 / (count * rounds) as f64;
if before == 0 {
println($"{what}: {ns} ns");
} else {
println($"{what}: {ns} ns, before {before} ns, {before / ns}x");
}
return ns;
}
def step_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ops[i] = d.ops[i] + d.ovs[i] * dt; } }
return report("vec3 p + v * dt, before", &at, 0);
}
def step(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ps[i] = d.ps[i] + d.vs[i] * dt; } }
_ = report("vec3 p + v * dt", &at, before);
}
def dots_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.sum += d.ops[i].dot(d.ovs[i]); } }
return report("vec3 dot, before", &at, 0);
}
def dots(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.sum += d.ps[i].dot(d.vs[i]); } }
_ = report("vec3 dot", &at, before);
}
def normals_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ovs[i] = (d.ovs[i] + d.ops[i]).normalized(); } }
return report("vec3 normalized, before", &at, 0);
}
def normals(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.vs[i] = (d.vs[i] + d.ps[i]).normalized(); } }
_ = report("vec3 normalized", &at, before);
}
def crosses_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ovs[i] = d.ovs[i].cross(d.ops[i]); } }
return report("vec3 cross, before", &at, 0);
}
def crosses(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.vs[i] = d.vs[i].cross(d.ps[i]); } }
_ = report("vec3 cross", &at, before);
}
def turns_before(d: &mut data, by: &oldq): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.oqs[i] = d.oqs[i] * by; } }
return report("quat * quat, before", &at, 0);
}
def turns(d: &mut data, by: &quat, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.qs[i] = d.qs[i] * by; } }
_ = report("quat * quat", &at, before);
}
def rotations_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ops[i] = d.oqs[i].turn(&d.ops[i]); } }
return report("quat * vec3, before", &at, 0);
}
def rotations(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ps[i] = d.qs[i] * d.ps[i]; } }
_ = report("quat * vec3", &at, before);
}
def directions(d: &mut data): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ps[i] = d.rs[i] * d.ps[i]; } }
_ = report("mat3 * vec3", &at, 0);
}
def tensors_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.or_out[i] = d.ors[i] * d.ors[(i + 1) % count]; } }
return report("mat3 * mat3, a number at a time", &at, 0);
}
def tensors(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.r_out[i] = d.rs[i] * d.rs[(i + 1) % count]; } }
_ = report("mat3 * mat3", &at, before);
}
def products_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.oout[i] = d.oms[i] * d.oms[(i + 1) % count]; } }
return report("mat4 * mat4, before", &at, 0);
}
def products(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.out[i] = d.ms[i] * d.ms[(i + 1) % count]; } }
_ = report("mat4 * mat4", &at, before);
}
def projections(d: &mut data): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ws[i] = d.ms[i] * d.ws[i]; } }
_ = report("mat4 * vec4", &at, 0);
}
def points_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ops[i] = d.oms[i].point(d.ops[i]); } }
return report("mat4 transform_point, before", &at, 0);
}
def points(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.ps[i] = d.ms[i].transform_point(d.ps[i]); } }
_ = report("mat4 transform_point", &at, before);
}
def inverses(d: &mut data): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.out[i] = d.ms[i].inverse(); } }
_ = report("mat4 inverse", &at, 0);
}
# Each of what's timed, called once more, so none is called from one place.
def elsewhere(d: &mut data): void {
d.ps[0] = d.ps[1] + d.vs[1] * 2;
d.ops[0] = d.ops[1] + d.ovs[1] * 2;
d.sum += d.ps[1].dot(d.vs[1]) + d.ops[1].dot(d.ovs[1]);
d.vs[0] = d.vs[1].normalized().cross(d.ps[1]);
d.ovs[0] = d.ovs[1].normalized().cross(d.ops[1]);
d.qs[0] = d.qs[1] * d.qs[2];
d.oqs[0] = d.oqs[1] * d.oqs[2];
d.ps[0] = d.qs[1] * (d.rs[1] * d.ps[1]);
d.ops[0] = d.oqs[1].turn(&d.ops[1]);
d.out[0] = d.ms[1] * d.ms[2];
d.oout[0] = d.oms[1] * d.oms[2];
d.r_out[0] = d.rs[1] * d.rs[2];
d.or_out[0] = d.ors[1] * d.ors[2];
d.ws[0] = d.ms[1] * d.ws[1];
d.ps[0] = d.ms[1].transform_point(d.ps[1]);
d.ops[0] = d.oms[1].point(d.ops[1]);
d.out[0] = d.ms[1].inverse();
}
def main(): void {
println($"lanes held: {lanes_held()}");
var d := data{
ps = vector::<vec3>::new(), vs = vector::<vec3>::new(), qs = vector::<quat>::new(), ms = vector::<mat4>::new(),
rs = vector::<mat3>::new(), ws = vector::<vec4>::new(), out = vector::<mat4>::new(), ops = vector::<old3>::new(),
ovs = vector::<old3>::new(), oqs = vector::<oldq>::new(), oms = vector::<oldm>::new(), oout = vector::<oldm>::new(),
ors = vector::<old33>::new(), r_out = vector::<mat3>::new(), or_out = vector::<old33>::new(),
};
for i in 0..count {
d.ps.append(vec3{ i as f32 * 0.001, 1, 2 });
d.vs.append(vec3{ 0.5, 0.25, 0.125 });
d.ops.append(old3{ i as f32 * 0.001, 1, 2 });
d.ovs.append(old3{ 0.5, 0.25, 0.125 });
val q := quat::axis_angle(vec3{ 1, 2, i as f32 }, i as f32 * 0.01);
d.qs.append(q);
d.oqs.append(oldq{ q.x, q.y, q.z, q.w });
val r := mat3::rotation(q);
d.rs.append(r);
d.ors.append(old33{
old3{ r.x_axis.x, r.x_axis.y, r.x_axis.z }, old3{ r.y_axis.x, r.y_axis.y, r.y_axis.z }, old3{ r.z_axis.x, r.z_axis.y, r.z_axis.z },
});
val m := mat4::compose(vec3{ i as f32, 1, 2 }, q, vec3{ 1, 2, 1 });
d.ms.append(m);
d.oms.append(oldm{ m = m.m });
d.ws.append(vec4{ i as f32, 1, 2, 1 });
}
d.out.resize(count);
d.oout.resize(count);
d.r_out.resize(count);
d.or_out.resize(count);
elsewhere(&mut d);
step(&mut d, step_before(&mut d));
dots(&mut d, dots_before(&mut d));
normals(&mut d, normals_before(&mut d));
crosses(&mut d, crosses_before(&mut d));
val turn := quat::axis_angle(vec3::up, 0.001);
turns(&mut d, &turn, turns_before(&mut d, &oldq{ turn.x, turn.y, turn.z, turn.w }));
rotations(&mut d, rotations_before(&mut d));
directions(&mut d);
tensors(&mut d, tensors_before(&mut d));
products(&mut d, products_before(&mut d));
projections(&mut d);
points(&mut d, points_before(&mut d));
inverses(&mut d);
# What was worked, so none of it is left out.
println($"checks: {d.sum} {d.ps[3].y} {d.ops[3].y} {d.vs[5].x} {d.ovs[5].x} {d.qs[7].w} {d.oqs[7].w} {d.out[9].m[5]} {d.oout[9].m[5]} {d.ws[2].y}");
}