#![forbid(unsafe_code)]
use archmage::Wasm128Token;
use core::arch::wasm32::{self as w, v128};
pub(crate) fn powf_f32_fuses() -> bool {
#[cfg(target_feature = "relaxed-simd")]
{
relaxed_probe::fuses_f32()
}
#[cfg(not(target_feature = "relaxed-simd"))]
{
false
}
}
pub(crate) fn powf_f64_fuses() -> bool {
#[cfg(target_feature = "relaxed-simd")]
{
relaxed_probe::fuses_f64()
}
#[cfg(not(target_feature = "relaxed-simd"))]
{
false
}
}
#[cfg(not(target_feature = "relaxed-simd"))]
#[archmage::rite]
pub(crate) fn madd_f32x4(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f32x4_add(w::f32x4_mul(a, b), c)
}
#[cfg(not(target_feature = "relaxed-simd"))]
#[archmage::rite]
pub(crate) fn msub_f32x4(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f32x4_sub(w::f32x4_mul(a, b), c)
}
#[cfg(not(target_feature = "relaxed-simd"))]
#[archmage::rite]
pub(crate) fn madd_f64x2(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f64x2_add(w::f64x2_mul(a, b), c)
}
#[cfg(not(target_feature = "relaxed-simd"))]
#[archmage::rite]
pub(crate) fn msub_f64x2(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f64x2_sub(w::f64x2_mul(a, b), c)
}
#[cfg(target_feature = "relaxed-simd")]
#[archmage::rite]
pub(crate) fn madd_f32x4(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f32x4_relaxed_madd(a, b, c)
}
#[cfg(target_feature = "relaxed-simd")]
#[archmage::rite]
pub(crate) fn msub_f32x4(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f32x4_relaxed_madd(a, b, w::f32x4_neg(c))
}
#[cfg(target_feature = "relaxed-simd")]
#[archmage::rite]
pub(crate) fn madd_f64x2(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f64x2_relaxed_madd(a, b, c)
}
#[cfg(target_feature = "relaxed-simd")]
#[archmage::rite]
pub(crate) fn msub_f64x2(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
w::f64x2_relaxed_madd(a, b, w::f64x2_neg(c))
}
#[cfg(target_feature = "relaxed-simd")]
mod relaxed_probe {
use core::arch::wasm32 as w;
use core::hint::black_box;
use core::sync::atomic::{AtomicU8, Ordering};
static F32: AtomicU8 = AtomicU8::new(0);
static F64: AtomicU8 = AtomicU8::new(0);
#[inline(always)]
pub(crate) fn fuses_f32() -> bool {
let state = F32.load(Ordering::Relaxed);
state == 2 || (state == 0 && store(&F32, probe_f32()))
}
#[inline(always)]
pub(crate) fn fuses_f64() -> bool {
let state = F64.load(Ordering::Relaxed);
state == 2 || (state == 0 && store(&F64, probe_f64()))
}
#[inline(always)]
fn store(cache: &AtomicU8, fused: bool) -> bool {
cache.store(if fused { 2 } else { 1 }, Ordering::Relaxed);
fused
}
#[inline(always)]
fn probe_f32() -> bool {
let x = w::f32x4_splat(black_box(1.0 + 1.0 / 4096.0));
let c = w::f32x4_splat(black_box(-(1.0 + 1.0 / 2048.0)));
let r = w::f32x4_relaxed_madd(x, x, c);
w::f32x4_extract_lane::<0>(r) == 1.0 / 16_777_216.0
&& w::f32x4_extract_lane::<3>(r) == 1.0 / 16_777_216.0
}
#[inline(always)]
fn probe_f64() -> bool {
let e = 1.0 / 134_217_728.0;
let x = w::f64x2_splat(black_box(1.0 + e));
let c = w::f64x2_splat(black_box(-(1.0 + 2.0 * e)));
let r = w::f64x2_relaxed_madd(x, x, c);
w::f64x2_extract_lane::<0>(r) == e * e && w::f64x2_extract_lane::<1>(r) == e * e
}
}
#[archmage::rite]
pub(crate) fn fused_f32x4(token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
#[cfg(target_feature = "relaxed-simd")]
if relaxed_probe::fuses_f32() {
return w::f32x4_relaxed_madd(a, b, c);
}
let lo = wide_sum(token, a, b, c);
let hi = wide_sum(
token,
w::i32x4_shuffle::<2, 3, 2, 3>(a, a),
w::i32x4_shuffle::<2, 3, 2, 3>(b, b),
w::i32x4_shuffle::<2, 3, 2, 3>(c, c),
);
w::i32x4_shuffle::<0, 1, 4, 5>(lo, hi)
}
#[archmage::rite]
fn wide_sum(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
let product = w::f64x2_mul(w::f64x2_promote_low_f32x4(a), w::f64x2_promote_low_f32x4(b));
let addend = w::f64x2_promote_low_f32x4(c);
let sum = w::f64x2_add(product, addend);
let virtual_addend = w::f64x2_sub(sum, product);
let error = w::f64x2_add(
w::f64x2_sub(product, w::f64x2_sub(sum, virtual_addend)),
w::f64x2_sub(addend, virtual_addend),
);
let zero = w::f64x2_splat(0.0);
let one = w::i64x2_splat(1);
let even = w::i64x2_eq(w::v128_and(sum, one), w::i64x2_splat(0));
let finite = w::f64x2_lt(w::f64x2_abs(sum), w::f64x2_splat(f64::INFINITY));
let adjust = w::v128_and(finite, w::v128_and(even, w::f64x2_ne(error, zero)));
let same_sign = w::i64x2_eq(w::f64x2_gt(error, zero), w::f64x2_gt(sum, zero));
let step = w::v128_bitselect(one, w::i64x2_splat(-1), same_sign);
let odd = w::i64x2_add(sum, step);
w::f32x4_demote_f64x2_zero(w::v128_bitselect(odd, sum, adjust))
}
#[archmage::rite]
pub(crate) fn powf_soft_f32x4(token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
let lo = wide_sum(token, a, b, c);
let hi = wide_sum(
token,
w::i32x4_shuffle::<2, 3, 2, 3>(a, a),
w::i32x4_shuffle::<2, 3, 2, 3>(b, b),
w::i32x4_shuffle::<2, 3, 2, 3>(c, c),
);
w::i32x4_shuffle::<0, 1, 4, 5>(lo, hi)
}
#[archmage::rite]
pub(crate) fn fused_f64x2(_token: Wasm128Token, a: v128, b: v128, c: v128) -> v128 {
#[cfg(target_feature = "relaxed-simd")]
if relaxed_probe::fuses_f64() {
return w::f64x2_relaxed_madd(a, b, c);
}
#[cfg(target_feature = "relaxed-simd")]
return soft_f64x2(a, b, c);
#[cfg(not(target_feature = "relaxed-simd"))]
soft_f64x2_inline(a, b, c)
}
#[cfg(target_feature = "relaxed-simd")]
#[inline(never)]
fn soft_f64x2(a: v128, b: v128, c: v128) -> v128 {
soft_f64x2_inline(a, b, c)
}
#[inline(always)]
fn soft_f64x2_inline(a: v128, b: v128, c: v128) -> v128 {
let lanes = |v: v128| [w::f64x2_extract_lane::<0>(v), w::f64x2_extract_lane::<1>(v)];
let [lo, hi] = crate::soft_fma::fma_lanes(lanes(a), lanes(b), lanes(c));
w::f64x2(lo, hi)
}
#[archmage::rite]
pub(crate) fn software_lanes<const N: usize>(
token: Wasm128Token,
a: [f32; N],
b: [f32; N],
c: [f32; N],
) -> [f32; N] {
assert_eq!(
N % 4,
0,
"private packed software adapter requires whole vectors"
);
let mut output = [0.0; N];
for (((a, b), c), out) in a
.as_chunks::<4>()
.0
.iter()
.zip(b.as_chunks::<4>().0)
.zip(c.as_chunks::<4>().0)
.zip(output.as_chunks_mut::<4>().0)
{
let a = w::f32x4(a[0], a[1], a[2], a[3]);
let b = w::f32x4(b[0], b[1], b[2], b[3]);
let c = w::f32x4(c[0], c[1], c[2], c[3]);
let r = powf_soft_f32x4(token, a, b, c);
*out = [
w::f32x4_extract_lane::<0>(r),
w::f32x4_extract_lane::<1>(r),
w::f32x4_extract_lane::<2>(r),
w::f32x4_extract_lane::<3>(r),
];
}
output
}
#[cfg(all(test, feature = "std", target_feature = "relaxed-simd"))]
mod tests {
#[test]
fn relaxed_probe_matches_engine() {
let (f32_fuses, f64_fuses) = (
super::relaxed_probe::fuses_f32(),
super::relaxed_probe::fuses_f64(),
);
assert_eq!(super::relaxed_probe::fuses_f32(), f32_fuses);
assert_eq!(super::relaxed_probe::fuses_f64(), f64_fuses);
std::eprintln!("relaxed madd probe: f32 fuses={f32_fuses}, f64 fuses={f64_fuses}");
if let Ok(expected) = std::env::var("ARCHMAGE_EXPECT_RELAXED_FUSION") {
assert!(matches!(expected.as_str(), "yes" | "no"));
let expected = expected == "yes";
assert_eq!((f32_fuses, f64_fuses), (expected, expected));
}
}
}