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(name: strview, v: &simd): 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::(), &mut out); io::println(out.view()); } def show_mask(name: strview, m: &mask): 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 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::(); } if pick == 13 { return -math::infinity::(); } if pick == 14 { return math::nan::(); } 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::() { p[k] = ((bits >> (8 * k)) & 0xFF) as u8; } return made; } var none: L; return none; } def vector(): simd where L: numeric, ordered, zeroable { var made: simd; for i in 0..N { made[i] = lane::(); } return made; } def nonzero(): simd where L: numeric, ordered, zeroable { var made := vector::(); for i in 0..N { if made[i] == 0 as L { made[i] = 3 as L; } } return made; } def exercise(name: strview): void where L: numeric, ordered, zeroable { io::println(name); for round in 0..2 { val a := vector::(); val b := vector::(); val c := vector::(); 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::())); 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::("f32x16"); exercise::("f64x8"); exercise::("i16x32"); exercise::("u16x32"); exercise::("i32x16"); exercise::("u32x16"); exercise::("i64x8"); exercise::("u64x8"); }