macro_rules! sort_cmp_f32_wgsl {
() => {
r#"
// Tested on the bit pattern rather than `v != v`: shader compilers are free to
// fold a self-comparison to false, which would silently drop NaN handling.
fn sort_is_nan_f32(v: f32) -> bool {
let bits = bitcast<u32>(v);
return (bits & 0x7f800000u) == 0x7f800000u && (bits & 0x007fffffu) != 0u;
}
fn sort_cmp_f32(a: f32, b: f32) -> i32 {
let a_nan = sort_is_nan_f32(a);
let b_nan = sort_is_nan_f32(b);
if (a_nan || b_nan) {
if (a_nan && b_nan) {
return 0;
}
return select(-1, 1, a_nan);
}
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
"#
};
}
macro_rules! sort_rank_f32_wgsl {
() => {
r#"
// Rank order: the requested output order with ties broken by original index, so
// the network sorts by a single total order and is stable in both directions.
fn sort_rank_less_f32(a: f32, idx_a: i32, b: f32, idx_b: i32, descending: bool) -> bool {
var c = sort_cmp_f32(a, b);
if (descending) {
c = -c;
}
if (c != 0) {
return c < 0;
}
return idx_a < idx_b;
}
// Padding value of maximum rank, so pad entries sort into the discarded tail.
// Ascending that is NaN (the greatest value); descending it is -inf. Both must
// be beyond any real value, otherwise real infinities get dropped.
fn sort_pad_f32(descending: bool) -> f32 {
return select(bitcast<f32>(0x7fc00000u), bitcast<f32>(0xff800000u), descending);
}
"#
};
}
macro_rules! sort_rank_i32_wgsl {
() => {
r#"
// Rank order: the requested output order with ties broken by original index, so
// the network sorts by a single total order and is stable in both directions.
fn sort_rank_less_i32(a: i32, idx_a: i32, b: i32, idx_b: i32, descending: bool) -> bool {
if (a != b) {
return select(a < b, (a > b), descending);
}
return idx_a < idx_b;
}
// Rank order without indices, for the values-only kernel.
fn sort_value_rank_less_i32(a: i32, b: i32, descending: bool) -> bool {
return select(a < b, (a > b), descending);
}
// Padding value of maximum rank, so pad entries sort into the discarded tail:
// the type's maximum ascending, its minimum descending. Pad indices are all at
// or above the real element count, so a real value that ties with the pad still
// wins on the index tiebreak.
fn sort_pad_i32(descending: bool) -> i32 {
return select(2147483647i, (-2147483647i - 1i), descending);
}
"#
};
}
macro_rules! sort_rank_u32_wgsl {
() => {
r#"
// Rank order: the requested output order with ties broken by original index, so
// the network sorts by a single total order and is stable in both directions.
fn sort_rank_less_u32(a: u32, idx_a: i32, b: u32, idx_b: i32, descending: bool) -> bool {
if (a != b) {
return select(a < b, (a > b), descending);
}
return idx_a < idx_b;
}
// Rank order without indices, for the values-only kernel.
fn sort_value_rank_less_u32(a: u32, b: u32, descending: bool) -> bool {
return select(a < b, (a > b), descending);
}
// Padding value of maximum rank, so pad entries sort into the discarded tail:
// the type's maximum ascending, its minimum descending. Pad indices are all at
// or above the real element count, so a real value that ties with the pad still
// wins on the index tiebreak.
fn sort_pad_u32(descending: bool) -> u32 {
return select(4294967295u, 0u, descending);
}
"#
};
}
pub(crate) use {sort_cmp_f32_wgsl, sort_rank_f32_wgsl};
pub(crate) use {sort_rank_i32_wgsl, sort_rank_u32_wgsl};