Files
sakurac/tests/core/simd.sakura
T

431 lines
18 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
# Tests of std's vectors: each operation of each type of lane against what the
# number's own operator or `std::math` gives lane by lane, to the bit, masks and
# selecting, loading and storing at the ends of views, and what stops the
# program. Built natively, the vectors are worked with the processor's vector
# instructions where it has them, so the same tests check those against the
# lanes; `SAKURA_CPU=x86-64` runs them as on a processor with SSE2 alone. The
# corpus program `simd_lanes_agree_everywhere_1` compares their bits between the
# compilers, the machine, executables, and constants.
import system;
import std::math;
import std::testing;
# Whether running the function stops the program, in a copy of the program that
# writes nothing.
def simd_stops(run: def(): void): bool {
when os == windows {
val stopped := copied(run);
return !(stopped.exited && stopped.code == 0);
}
val child := fork();
if child == 0 {
val out: fd = fd::stdout;
val err: fd = fd::stderr;
out.close();
err.close();
run();
exit(0);
}
var status: process_status;
return wait(child, &mut status) >= 0 && !(status.exited && status.code == 0);
}
# Whether two values have the same bytes, as NaNs of the same bits do.
def same_lane_bits<T>(a: &T, b: &T): bool {
val x := a as *u8;
val y := b as *u8;
for k in 0..size_of::<T>() {
return false if x[k] != y[k];
}
return true;
}
var lane_seed: u64 = 0x2545F4914F6CDD1D;
def next_bits(): u64 {
lane_seed = lane_seed ^ (lane_seed << 13);
lane_seed = lane_seed ^ (lane_seed >> 7);
lane_seed = lane_seed ^ (lane_seed << 17);
return lane_seed;
}
# A lane worth testing: one of the edges of its type, or something between.
def random_lane<L>(): L where L: numeric, ordered, zeroable {
val pick := next_bits() % 12;
if const L is real {
val edges: f64[9] = [0.0, -0.0, 1.0, -1.5, 2.5, -2.5, 1e-310, 0.75, -3.0];
return edges[pick as u64] as L if pick < 9;
return math::infinity::<L>() if pick == 9;
return math::nan::<L>() if pick == 10;
return ((next_bits() % 200000) as f64 / 100.0 - 1000.0) as L;
}
if const L is integer {
return pick as L if pick < 2;
return ~(0 as L) if pick == 2;
return ~(0 as L) >> 1 if pick == 3;
var made: L;
val bits := next_bits();
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 random_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] = random_lane::<L>();
}
return made;
}
# Whether a vector's lanes are those the scalar operation gives each pair of lanes.
def lanes_agree<L, const N: u64>(made: &simd<L[N]>, a: &simd<L[N]>, b: &simd<L[N]>, op: def(L, L): L): bool
where L: numeric, ordered, zeroable {
for i in 0..N {
val want := op(a[i], b[i]);
val got: L = made[i];
return false if !same_lane_bits(&got, &want);
}
return true;
}
# Whether a vector's lanes are those the scalar operation gives each lane.
def lanes_agree1<L, const N: u64>(made: &simd<L[N]>, a: &simd<L[N]>, op: def(L): L): bool
where L: numeric, ordered, zeroable {
for i in 0..N {
val want := op(a[i]);
val got: L = made[i];
return false if !same_lane_bits(&got, &want);
}
return true;
}
# Whether a mask has the lanes set the scalar comparison holds for.
def mask_agrees<L, const N: u64>(made: &mask<L[N]>, a: &simd<L[N]>, b: &simd<L[N]>, op: def(L, L): bool): bool
where L: numeric, ordered, zeroable {
var bits: u32 = 0;
for i in 0..N {
val holds := op(a[i], b[i]);
return false if made.has(i) != holds;
if holds {
bits = bits | ((1 as u32) << i);
}
}
return made.bits() == bits && made.any() == (bits != 0) && made.count() == count_ones(bits);
}
# Each operation of a type of lane, against the scalar one, over many vectors.
def all_agree<L, const N: u64>(t: &mut tester): void where L: numeric, ordered, zeroable {
for _ in 0..40 {
val a := random_vector::<L, N>();
val b := random_vector::<L, N>();
val c := random_vector::<L, N>();
t.expects(lanes_agree(&(a + b), &a, &b, def(x: L, y: L): L { return x + y; }));
t.expects(lanes_agree(&(a - b), &a, &b, def(x: L, y: L): L { return x - y; }));
t.expects(lanes_agree(&(a * b), &a, &b, def(x: L, y: L): L { return x * y; }));
t.expects(lanes_agree(&a.min(&b), &a, &b, def(x: L, y: L): L { return math::min(x, y); }));
t.expects(lanes_agree(&a.max(&b), &a, &b, def(x: L, y: L): L { return math::max(x, y); }));
t.expects(lanes_agree1(&(-a), &a, def(x: L): L { return -x; }));
t.expects(mask_agrees(&a.equal(&b), &a, &b, def(x: L, y: L): bool { return x == y; }));
t.expects(mask_agrees(&a.equal(&a), &a, &a, def(x: L, y: L): bool { return x == y; }));
t.expects(mask_agrees(&a.not_equal(&b), &a, &b, def(x: L, y: L): bool { return x != y; }));
t.expects(mask_agrees(&a.less(&b), &a, &b, def(x: L, y: L): bool { return x < y; }));
t.expects(mask_agrees(&a.less_equal(&b), &a, &b, def(x: L, y: L): bool { return x <= y; }));
t.expects(mask_agrees(&a.greater(&b), &a, &b, def(x: L, y: L): bool { return x > y; }));
t.expects(mask_agrees(&a.greater_equal(&b), &a, &b, def(x: L, y: L): bool { return x >= y; }));
val m := a.less(&b);
val chosen := select(m, a, b);
for i in 0..N {
val want: L = m.has(i) ? a[i] : b[i];
val got: L = chosen[i];
t.expects(same_lane_bits(&got, &want));
}
val fused := a.fma(&b, &c);
for i in 0..N {
val x: L = a[i];
val y: L = b[i];
val z: L = c[i];
var want: L;
if const L is real {
want = math::fma(x, y, z);
} else {
want = x * y + z;
}
val got: L = fused[i];
t.expects(same_lane_bits(&got, &want));
}
if const L is real {
t.expects(lanes_agree(&(a / b), &a, &b, def(x: L, y: L): L { return x / y; }));
t.expects(lanes_agree1(&a.abs(), &a, def(x: L): L { return math::abs(x); }));
t.expects(lanes_agree1(&a.sqrt(), &a, def(x: L): L { return math::sqrt(x); }));
t.expects(lanes_agree1(&a.floor(), &a, def(x: L): L { return math::floor(x); }));
t.expects(lanes_agree1(&a.ceil(), &a, def(x: L): L { return math::ceil(x); }));
t.expects(lanes_agree1(&a.trunc(), &a, def(x: L): L { return math::trunc(x); }));
t.expects(lanes_agree1(&a.round(), &a, def(x: L): L { return math::round(x); }));
t.expects(lanes_agree1(&a.round_even(), &a, def(x: L): L { return math::round_even(x); }));
}
if const L is integer {
t.expects(lanes_agree(&(a & b), &a, &b, def(x: L, y: L): L { return x & y; }));
t.expects(lanes_agree(&(a | b), &a, &b, def(x: L, y: L): L { return x | y; }));
t.expects(lanes_agree(&(a ^ b), &a, &b, def(x: L, y: L): L { return x ^ y; }));
t.expects(lanes_agree1(&(~a), &a, def(x: L): L { return ~x; }));
val counts: u32[8] = [0, 1, 3, 7, 15, 31, 63, 64];
val count := counts[next_bits() % 8];
val left := a << count;
val right := a >> count;
for i in 0..N {
val want_left: L = a[i] << count;
val want_right: L = a[i] >> count;
val got_left: L = left[i];
val got_right: L = right[i];
t.expects(same_lane_bits(&got_left, &want_left));
t.expects(same_lane_bits(&got_right, &want_right));
}
}
}
}
@test def every_operation_agrees_with_its_lanes(t: &mut tester): void {
all_agree::<f32, 4>(t);
all_agree::<f32, 8>(t);
all_agree::<f64, 2>(t);
all_agree::<f64, 4>(t);
all_agree::<i8, 16>(t);
all_agree::<u8, 32>(t);
all_agree::<i16, 8>(t);
all_agree::<u16, 16>(t);
all_agree::<i32, 4>(t);
all_agree::<u32, 8>(t);
all_agree::<i64, 2>(t);
all_agree::<u64, 4>(t);
}
@test def lanes_wrap_and_round_as_their_numbers_do(t: &mut tester): void {
val bytes := u8x16::splat(250) + u8x16::splat(10);
t.expects(bytes[0] == 4 && bytes[15] == 4);
val most := i32x4::splat(0x7FFFFFFF) + i32x4::splat(1);
t.expects(most[2] == -2147483648);
val least := i8x16::splat(-128);
t.expects((-least)[3] == -128 && least.abs()[3] == -128);
t.expects(u16x8::splat(40000).abs()[0] == 40000);
val halves := f32x4::of([0.5, 1.5, 2.5, -2.5]);
val away: f32[4] = [1.0, 2.0, 3.0, -3.0];
val even: f32[4] = [0.0, 2.0, 2.0, -2.0];
t.expects(halves.round().lanes() == away);
t.expects(halves.round_even().lanes() == even);
val roots := f64x2::of([2.0, -0.0]).sqrt();
val root: f64 = roots[0];
val zero: f64 = roots[1];
t.expects(root == math::sqrt(2.0) && math::sign_bit(zero));
val floor: f32 = f32x8::splat(-0.75).floor()[7];
val ceil: f32 = f32x8::splat(-0.75).ceil()[7];
t.expects(floor == -1.0 && ceil == 0.0 && math::sign_bit(ceil));
}
@test def vector_fma_rounds_once(t: &mut tester): void {
# (1 + 2^-52)(1 - 2^-52) - 1 is -2^-104 exactly, which multiplying then adding
# rounds to 0.
val epsilon := math::epsilon::<f64>();
val fused := f64x2::splat(1.0 + epsilon).fma(&f64x2::splat(1.0 - epsilon), &f64x2::splat(-1.0));
t.expects(fused[0] == -(epsilon * epsilon) && fused[1] == fused[0]);
val plain := f64x2::splat(1.0 + epsilon) * f64x2::splat(1.0 - epsilon) - f64x2::splat(1.0);
t.expects(plain[0] == 0.0);
}
@test def least_and_greatest_of_reals_are_ieee_754s(t: &mut tester): void {
val a := f32x4::of([-0.0, 0.0, math::nan::<f32>(), 1.0]);
val b := f32x4::of([0.0, -0.0, 1.0, math::nan::<f32>()]);
val least := a.min(&b);
val most := a.max(&b);
val small := least.lanes();
val large := most.lanes();
t.expects(math::sign_bit(small[0]) && math::sign_bit(small[1]));
t.expects(!math::sign_bit(large[0]) && !math::sign_bit(large[1]));
t.expects(math::is_nan(small[2]) && math::is_nan(small[3]) && math::is_nan(large[2]) && math::is_nan(large[3]));
t.expects(a.equal(&a).bits() == 0b1011);
t.expects(a.not_equal(&a).bits() == 0b0100);
}
@test def masks_say_which_lanes_hold(t: &mut tester): void {
val v := i16x8::of([1, -2, 3, -4, 5, -6, 7, -8]);
val negative := v.less(&i16x8::splat(0));
t.expects(negative.bits() == 0b10101010 && negative.count() == 4 && negative.any() && !negative.all());
t.expects((negative.first() else 99) == 1);
t.expects((~negative).bits() == 0b01010101);
t.expects((negative | ~negative).all() && (negative & ~negative).none());
val absolute: i16[8] = [1, 2, 3, 4, 5, 6, 7, 8];
t.expects(select(negative, -v, v).lanes() == absolute);
val wide := u8x32::splat(9).equal(&u8x32::splat(9));
t.expects(wide.all() && wide.count() == 32 && wide.bits() == 0xFFFFFFFF);
t.expects((u8x32::splat(1).equal(&u8x32::splat(2)).first() else 99) == 99);
}
@test def vectors_load_and_store_within_views(t: &mut tester): void {
val text: strview = "a vector of sixteen bytes and more";
val first := u8x16::load(text.view(), 0);
t.expects(first[0] == 'a' as u8 && first[15] == 't' as u8);
val last := u8x16::load(text.view(), text.length - 16);
t.expects(last[15] == 'e' as u8);
if const checking() {
t.expects(simd_stops(def() { val past := u8x16::load("too short".view(), 0); }));
t.expects(simd_stops(def() { val at := u8x16::load("sixteen bytes, 1".view(), 1); }));
}
var room: f64[6] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
f64x4::of([1.0, 2.0, 3.0, 4.0]).store(room[:], 2);
val stored: f64[6] = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
t.expects(room == stored);
if const checking() {
t.expects(simd_stops(def() { var little: f64[3]; f64x2::splat(1.0).store(little[:], 2); }));
}
}
@test def lanes_are_reached_moved_and_converted(t: &mut tester): void {
var v := i32x4::of([10, 20, 30, 40]);
v[2] = 33;
val written: i32[4] = [10, 20, 33, 40];
val reversed: i32[4] = [40, 33, 20, 10];
val big: f32 = 3e9;
val converted: i32[4] = [1, -1, big as i32, 0];
t.expects(v[2] == 33 && v.lanes() == written);
t.expects(v.shuffle([3, 2, 1, 0]).lanes() == reversed);
t.expects(v.sum() == 103 && v.product() == 10 * 20 * 33 * 40 && v.smallest() == 10 && v.largest() == 40);
t.expects(f32x4::of([1.9, -1.9, 3e9, math::nan::<f32>()]).convert::<i32>().lanes() == converted);
val ones := f32x4::splat(1.0).bits::<u32, 4>();
t.expects(ones[0] == 0x3F800000);
t.expects(v == i32x4::of([10, 20, 33, 40]) && v != i32x4::splat(0));
if const checking() {
t.expects(simd_stops(def() { val w := i32x4::splat(1); val x := w[4]; }));
}
}
@test def integer_lanes_divided_by_zero_stop(t: &mut tester): void {
val threes: i32[4] = [3, 3, 3, 3];
val ones: i32[4] = [-1, -1, -1, -1];
t.expects((i32x4::splat(7) / i32x4::splat(2)).lanes() == threes);
t.expects((i32x4::splat(-7) % i32x4::splat(2)).lanes() == ones);
t.expects(simd_stops(def() { val q := i32x4::splat(7) / i32x4::of([1, 1, 0, 1]); }));
val infinite: f32 = (f32x4::splat(1.0) / f32x4::splat(0.0))[0];
t.expects(math::is_infinite(infinite));
}
# Whether converting each lane agrees with `as`, and with the lanes' own bits.
def converts_as_lanes<L, M, const N: u64>(a: &simd<L[N]>): bool
where L: numeric, ordered, zeroable where M: numeric, ordered, zeroable {
val made := a.convert::<M>();
for i in 0..N {
val want: M = a[i] as M;
val got: M = made[i];
return false if !same_lane_bits(&got, &want);
}
return true;
}
# Whether widening each half agrees with `as` of its lanes.
def widens_as_lanes<L, M, const N: u64>(a: &simd<L[N]>): bool
where L: numeric, ordered, zeroable where M: numeric, ordered, zeroable {
val low := a.widened::<M>();
val high := a.widened_high::<M>();
for i in 0..N / 2 {
val want_low: M = a[i] as M;
val want_high: M = a[N / 2 + i] as M;
val got_low: M = low[i];
val got_high: M = high[i];
return false if !same_lane_bits(&got_low, &want_low) || !same_lane_bits(&got_high, &want_high);
}
return true;
}
# Whether reductions agree with the lanes worked one at a time, lane 0 first.
def reduces_as_lanes<L, const N: u64>(a: &simd<L[N]>): bool where L: numeric, ordered, zeroable {
var sum: L = a[0];
var least: L = a[0];
var most: L = a[0];
for i in 1..N {
val lane: L = a[i];
sum = sum + lane;
least = least < lane ? least : lane;
most = most > lane ? most : lane;
}
val got_sum: L = a.sum();
if const L is integer {
val got_least: L = a.smallest();
val got_most: L = a.largest();
return same_lane_bits(&got_sum, &sum) && got_least == least && got_most == most;
}
return same_lane_bits(&got_sum, &sum);
}
@test def reshaping_agrees_with_its_lanes(t: &mut tester): void {
for _ in 0..8 {
t.expects(converts_as_lanes::<i32, f32, 4>(&random_vector::<i32, 4>()));
t.expects(converts_as_lanes::<u32, f32, 8>(&random_vector::<u32, 8>()));
t.expects(converts_as_lanes::<i16, f32, 8>(&random_vector::<i16, 8>()));
t.expects(converts_as_lanes::<i32, f64, 4>(&random_vector::<i32, 4>()));
t.expects(converts_as_lanes::<f32, i32, 8>(&random_vector::<f32, 8>()));
t.expects(converts_as_lanes::<f32, u32, 4>(&random_vector::<f32, 4>()));
t.expects(converts_as_lanes::<f64, i32, 4>(&random_vector::<f64, 4>()));
t.expects(converts_as_lanes::<f32, f64, 4>(&random_vector::<f32, 4>()));
t.expects(converts_as_lanes::<f64, f32, 4>(&random_vector::<f64, 4>()));
t.expects(converts_as_lanes::<u8, u16, 16>(&random_vector::<u8, 16>()));
t.expects(converts_as_lanes::<i8, i16, 16>(&random_vector::<i8, 16>()));
t.expects(converts_as_lanes::<u16, u8, 16>(&random_vector::<u16, 16>()));
t.expects(converts_as_lanes::<i64, i32, 4>(&random_vector::<i64, 4>()));
t.expects(widens_as_lanes::<i8, i16, 16>(&random_vector::<i8, 16>()));
t.expects(widens_as_lanes::<u16, u32, 16>(&random_vector::<u16, 16>()));
t.expects(widens_as_lanes::<i32, i64, 4>(&random_vector::<i32, 4>()));
t.expects(widens_as_lanes::<f32, f64, 8>(&random_vector::<f32, 8>()));
t.expects(reduces_as_lanes::<u8, 32>(&random_vector::<u8, 32>()));
t.expects(reduces_as_lanes::<i16, 8>(&random_vector::<i16, 8>()));
t.expects(reduces_as_lanes::<u32, 8>(&random_vector::<u32, 8>()));
t.expects(reduces_as_lanes::<i64, 2>(&random_vector::<i64, 2>()));
t.expects(reduces_as_lanes::<u64, 4>(&random_vector::<u64, 4>()));
t.expects(reduces_as_lanes::<f32, 8>(&random_vector::<f32, 8>()));
t.expects(reduces_as_lanes::<f64, 4>(&random_vector::<f64, 4>()));
}
# Narrowing saturates, as packing does.
val words := i16x8::of([-300, 300, 1, -1, 32767, -32768, 255, 0]);
val signed: i8[16] = [-128, 127, 1, -1, 127, -128, 127, 0, 5, 5, 5, 5, 5, 5, 5, 5];
val unsigned: u8[16] = [0, 255, 1, 0, 255, 0, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255];
t.expects(words.narrowed::<i8>(&i16x8::splat(5)).lanes() == signed);
t.expects(words.narrowed::<u8>(&i16x8::splat(1000)).lanes() == unsigned);
val big: u16[4] = [65535, 7, 65535, 7];
t.expects(u32x4::of([70000, 7, 70000, 7]).narrowed::<u16>(&u32x4::of([70000, 7, 70000, 7])).lanes()[0:4] == big[0:4]);
# Halves taken apart and joined.
val eight := u32x8::of([1, 2, 3, 4, 5, 6, 7, 8]);
val first: u32[4] = [1, 2, 3, 4];
val second: u32[4] = [5, 6, 7, 8];
t.expects(eight.low().lanes() == first && eight.high().lanes() == second);
t.expects(simd::join(eight.low(), eight.high()) == eight);
# Rounding halves away from zero keeps the sign of what it does not move.
val halves := f64x4::of([-0.3, 0.49999999999999994, -2.5, 4503599627370495.5]).round();
t.expects(math::sign_bit(halves[0]) && halves[1] == 0.0 && halves[2] == -3.0 && halves[3] == 4503599627370496.0);
# Gathered and scattered by places, a later lane's write lasting.
val table: f64[6] = [0.5, 1.5, 2.5, 3.5, 4.5, 5.5];
val gathered := f64x4::gather(table[0:6], i32x4::of([5, 0, 5, 2]));
t.expects(gathered == f64x4::of([5.5, 0.5, 5.5, 2.5]));
var into: i32[8];
i32x8::of([1, 2, 3, 4, 5, 6, 7, 8]).scatter(into[0:8], i32x8::of([7, 6, 5, 4, 3, 2, 1, 7]));
t.expects(into[7] == 8 && into[1] == 7 && into[0] == 0);
# Shuffles of each size, against reading each lane.
val order: u8[8] = [7, 0, 6, 1, 5, 2, 4, 3];
val floats := random_vector::<f32, 8>();
val moved := floats.shuffle(order);
for i in 0..8u {
val want: f32 = floats[order[i] as u64];
val got: f32 = moved[i];
t.expects(same_lane_bits(&got, &want));
}
val bytes := random_vector::<u8, 16>();
t.expects(bytes.shuffle([15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])[0] == bytes[15]);
if const checking() {
t.expects(simd_stops(def() { val wrong := i32x4::splat(1).shuffle([0, 1, 4, 2]); }));
t.expects(simd_stops(def() { val far := f64x4::gather(table[0:6], i32x4::of([0, 6, 1, 2])); }));
}
}