Files
sakurac/tests/programs/simd_lanes_agree_everywhere_2.sakura
T

165 lines
4.9 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
import std::io;
import std::math;
# Every operation of std's vectors of 64 bytes gives the same bits wherever it
# runs: in the machine, in an executable, where AVX-512 works the lanes or they
# are worked a half or a quarter at a time, and in constants worked out while
# compiling. Each line is an operation's result, its bytes in hex, for vectors of
# each type of lane made of random lanes and the edges of their types.
const compiled: f32[16] = (f32x16::splat(1.5) * f32x16::splat(3.0) + f32x16::splat(-0.0)).min(&f32x16::splat(4.0)).lanes();
const counted: u32 = u16x32::splat(7).less(&u16x32::splat(9)).bits();
var seed: u64 = 0x9E3779B97F4A7C15;
def next(): u64 {
seed = seed ^ (seed << 13);
seed = seed ^ (seed >> 7);
seed = seed ^ (seed << 17);
return seed;
}
const digits: strview = "0123456789abcdef";
def hex_of(bytes: *u8, count: u64, out: &mut string): void {
for i in 0..count {
val b := bytes[i];
out.push(digits[(b >> 4) as u64]);
out.push(digits[(b & 15) as u64]);
}
}
def show<L, const N: u64>(name: strview, v: &simd<L[N]>): void where L: numeric, ordered, zeroable {
var out := string::from(name, nothing {});
out.append(" ");
val held := v.lanes();
hex_of((&held) as *u8, N * size_of::<L>(), &mut out);
io::println(out.span);
}
def show_mask<L, const N: u64>(name: strview, m: &mask<L[N]>): void where L: numeric, ordered, zeroable {
val bits := m.bits();
io::println($"{name} {bits} {m.any()} {m.all()} {m.none()} {m.count()} {m.first()}");
}
# A lane worth testing: random bits, or a special value of its type.
def lane<L>(): L where L: numeric, ordered, zeroable {
val pick := next() % 16;
if const L is real {
val specials: f64[12] = [0.0, -0.0, 1.0, -1.0, 0.5, -2.5, 2.5, 1e300, -1e-310, 3.0, 1e-45, -7.25];
if pick < 12 {
return specials[pick] as L;
}
if pick == 12 {
return math::infinity::<L>();
}
if pick == 13 {
return -math::infinity::<L>();
}
if pick == 14 {
return math::nan::<L>();
}
return ((next() % 2000000) as f64 / 1000.0 - 1000.0) as L;
}
if const L is integer {
if pick < 3 {
return pick as L;
}
if pick == 3 {
return ~(0 as L);
}
var made: L;
val bits := next();
val p := (&mut made) as *mut u8;
for k in 0..size_of::<L>() {
p[k] = ((bits >> (8 * k)) & 0xFF) as u8;
}
return made;
}
var none: L;
return none;
}
def vector<L, const N: u64>(): simd<L[N]> where L: numeric, ordered, zeroable {
var made: simd<L[N]>;
for i in 0..N {
made[i] = lane::<L>();
}
return made;
}
def nonzero<L, const N: u64>(): simd<L[N]> where L: numeric, ordered, zeroable {
var made := vector::<L, N>();
for i in 0..N {
if made[i] == 0 as L {
made[i] = 3 as L;
}
}
return made;
}
def exercise<L, const N: u64>(name: strview): void where L: numeric, ordered, zeroable {
io::println(name);
for round in 0..2 {
val a := vector::<L, N>();
val b := vector::<L, N>();
val c := vector::<L, N>();
show("a", &a);
show("b", &b);
show("add", &(a + b));
show("sub", &(a - b));
show("mul", &(a * b));
show("negate", &(-a));
show("min", &a.min(&b));
show("max", &a.max(&b));
show("abs", &a.abs());
show("fma", &a.fma(&b, &c));
show_mask("equal", &a.equal(&b));
show_mask("equal_self", &a.equal(&a));
show_mask("not_equal", &a.not_equal(&b));
show_mask("less", &a.less(&b));
show_mask("less_equal", &a.less_equal(&b));
show_mask("greater", &a.greater(&b));
show_mask("greater_equal", &a.greater_equal(&b));
show_mask("greater_self", &a.greater_equal(&a));
val m := a.less(&b);
show("select", &select(m, a, b));
show("select_not", &select(~m, a, b));
io::println($"sum {a.sum() as f64} smallest {a.smallest() as f64} largest {a.largest() as f64}");
if const L is real {
show("div", &(a / b));
show("sqrt", &a.sqrt());
show("floor", &a.floor());
show("ceil", &a.ceil());
show("trunc", &a.trunc());
show("round", &a.round());
show("round_even", &a.round_even());
}
if const L is integer {
show("div", &(a / nonzero::<L, N>()));
show("and", &(a & b));
show("or", &(a | b));
show("xor", &(a ^ b));
show("not", &(~a));
val counts: u32[8] = [0, 1, 7, 15, 31, 63, 64, 100];
val count := counts[round as u64];
show("shl", &(a << count));
show("shr", &(a >> count));
val other := counts[(round + 2) as u64];
show("shl2", &(a << other));
show("shr2", &(a >> other));
}
}
}
def main(): void {
io::println($"compiled {compiled[0]} {compiled[15]} {counted}");
exercise::<f32, 16>("f32x16");
exercise::<f64, 8>("f64x8");
exercise::<i16, 32>("i16x32");
exercise::<u16, 32>("u16x32");
exercise::<i32, 16>("i32x16");
exercise::<u32, 16>("u32x16");
exercise::<i64, 8>("i64x8");
exercise::<u64, 8>("u64x8");
}