const PROBE_F64_TOLERANCE_TIGHT: f64 = 1e-14;
const PROBE_F64_TOLERANCE_STANDARD: f64 = 1e-10;
const PROBE_F64_TOLERANCE_RELAXED: f64 = 1e-6;
pub(super) struct ProbeShader {
pub name: &'static str,
pub wgsl: &'static str,
pub expected: f64,
pub tolerance: f64,
}
const PROBE_DF64_ARITH_WGSL: &str = "\
struct Df64 { hi: f32, lo: f32, }\n\
fn two_sum(a: f32, b: f32) -> Df64 {\n\
let s = a + b;\n\
let v = s - a;\n\
let e = (a - (s - v)) + (b - v);\n\
return Df64(s, e);\n\
}\n\
fn df64_add(a: Df64, b: Df64) -> Df64 {\n\
let s = two_sum(a.hi, b.hi);\n\
let e = a.lo + b.lo;\n\
let v = two_sum(s.hi, s.lo + e);\n\
return v;\n\
}\n\
fn df64_to_f64(v: Df64) -> f64 {\n\
return f64(v.hi) + f64(v.lo);\n\
}\n\
@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
let one = Df64(1.0, 0.0);\n\
let two = df64_add(one, one);\n\
out[0] = df64_to_f64(two);\n\
}";
const PROBE_DF64_FMA_TWO_PROD_WGSL: &str = "\
struct Df64 { hi: f32, lo: f32, }\n\
fn two_prod(a: f32, b: f32) -> Df64 {\n\
let p = a * b;\n\
let e = fma(a, b, -p);\n\
return Df64(p, e);\n\
}\n\
@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
let x: f32 = 1234567.0;\n\
let y: f32 = 7654321.0;\n\
let t = two_prod(x, y);\n\
out[0] = f64(t.hi) + f64(t.lo);\n\
}";
const PROBE_DF64_WORKGROUP_REDUCE_WGSL: &str = "\
struct Df64 { hi: f32, lo: f32, }\n\
fn two_sum(a: f32, b: f32) -> Df64 {\n\
let s = a + b;\n\
let v = s - a;\n\
let e = (a - (s - v)) + (b - v);\n\
return Df64(s, e);\n\
}\n\
fn df64_add(a: Df64, b: Df64) -> Df64 {\n\
let s = two_sum(a.hi, b.hi);\n\
let e = a.lo + b.lo;\n\
let v = two_sum(s.hi, s.lo + e);\n\
return v;\n\
}\n\
fn df64_to_f64(v: Df64) -> f64 {\n\
return f64(v.hi) + f64(v.lo);\n\
}\n\
@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
var<workgroup> shared_hi: array<f32, 256>;\n\
var<workgroup> shared_lo: array<f32, 256>;\n\
@compute @workgroup_size(256)\n\
fn probe(@builtin(local_invocation_id) lid: vec3<u32>) {\n\
let tid = lid.x;\n\
shared_hi[tid] = 1.0;\n\
shared_lo[tid] = 0.0;\n\
workgroupBarrier();\n\
for (var stride = 128u; stride > 0u; stride = stride >> 1u) {\n\
if (tid < stride) {\n\
let a = Df64(shared_hi[tid], shared_lo[tid]);\n\
let b = Df64(shared_hi[tid + stride], shared_lo[tid + stride]);\n\
let s = df64_add(a, b);\n\
shared_hi[tid] = s.hi;\n\
shared_lo[tid] = s.lo;\n\
}\n\
workgroupBarrier();\n\
}\n\
if (tid == 0u) {\n\
out[0] = df64_to_f64(Df64(shared_hi[0], shared_lo[0]));\n\
}\n\
}";
pub(super) const PROBES: &[ProbeShader] = &[
ProbeShader {
name: "basic_f64",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
let x: f64 = f64(3.0);\n\
let y: f64 = x * f64(2.0) + f64(1.0);\n\
out[0] = y;\n\
}",
expected: 7.0,
tolerance: PROBE_F64_TOLERANCE_TIGHT,
},
ProbeShader {
name: "exp",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = exp(f64(1.0));\n\
}",
expected: std::f64::consts::E,
tolerance: PROBE_F64_TOLERANCE_RELAXED,
},
ProbeShader {
name: "log",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = log(f64(2.718281828459045));\n\
}",
expected: 1.0,
tolerance: PROBE_F64_TOLERANCE_RELAXED,
},
ProbeShader {
name: "exp2",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = exp2(f64(3.0));\n\
}",
expected: 8.0,
tolerance: PROBE_F64_TOLERANCE_STANDARD,
},
ProbeShader {
name: "log2",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = log2(f64(8.0));\n\
}",
expected: 3.0,
tolerance: PROBE_F64_TOLERANCE_STANDARD,
},
ProbeShader {
name: "sin",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = sin(f64(9.21460183660255));\n\
}",
expected: 0.156_434_465_040_230_9,
tolerance: PROBE_F64_TOLERANCE_RELAXED,
},
ProbeShader {
name: "cos",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = cos(f64(9.21460183660255));\n\
}",
expected: -0.987_688_340_595_137_8,
tolerance: PROBE_F64_TOLERANCE_RELAXED,
},
ProbeShader {
name: "sqrt",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = sqrt(f64(2.0));\n\
}",
expected: std::f64::consts::SQRT_2,
tolerance: PROBE_F64_TOLERANCE_STANDARD,
},
ProbeShader {
name: "fma",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
out[0] = fma(f64(2.0), f64(3.0), f64(1.0));\n\
}",
expected: 7.0,
tolerance: PROBE_F64_TOLERANCE_TIGHT,
},
ProbeShader {
name: "abs_min_max",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
let a = abs(f64(-3.5));\n\
let b = min(a, f64(4.0));\n\
let c = max(b, f64(2.0));\n\
out[0] = c;\n\
}",
expected: 3.5,
tolerance: PROBE_F64_TOLERANCE_TIGHT,
},
ProbeShader {
name: "shared_mem_f64",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
var<workgroup> wg_data: array<f64, 4>;\n\
@compute @workgroup_size(4)\n\
fn probe(@builtin(local_invocation_id) lid: vec3<u32>) {\n\
wg_data[lid.x] = f64(lid.x + 1u);\n\
workgroupBarrier();\n\
if lid.x == 0u {\n\
out[0] = wg_data[0] + wg_data[1] + wg_data[2] + wg_data[3];\n\
}\n\
}",
expected: 10.0,
tolerance: PROBE_F64_TOLERANCE_TIGHT,
},
ProbeShader {
name: "composite_transcendental",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
let x = f64(2.0);\n\
let a = log(x);\n\
let b = exp(a);\n\
let c = sqrt(b);\n\
let d = sin(c) * sin(c) + cos(c) * cos(c);\n\
out[0] = d;\n\
}",
expected: 1.0,
tolerance: PROBE_F64_TOLERANCE_RELAXED,
},
ProbeShader {
name: "exp_log_chain",
wgsl: "@group(0) @binding(0) var<storage, read_write> out: array<f64>;\n\
@compute @workgroup_size(1)\n\
fn probe(@builtin(global_invocation_id) _id: vec3<u32>) {\n\
let x = f64(3.0);\n\
let lg = log(exp(x));\n\
let r = exp(lg - log(x));\n\
out[0] = r * sqrt(abs(f64(1.0)));\n\
}",
expected: 1.0,
tolerance: PROBE_F64_TOLERANCE_RELAXED,
},
ProbeShader {
name: "df64_arith",
wgsl: PROBE_DF64_ARITH_WGSL,
expected: 2.0,
tolerance: PROBE_F64_TOLERANCE_TIGHT,
},
ProbeShader {
name: "df64_fma_two_prod",
wgsl: PROBE_DF64_FMA_TWO_PROD_WGSL,
expected: 9_449_772_114_007.0,
tolerance: 1024.0,
},
ProbeShader {
name: "df64_workgroup_reduce",
wgsl: PROBE_DF64_WORKGROUP_REDUCE_WGSL,
expected: 256.0,
tolerance: PROBE_F64_TOLERANCE_STANDARD,
},
];