// @generated by tool/gen/fn/generate.py; DO NOT EDIT.
// schema=crypto_bigint.json schema_version=1 generator_version=64
// schema_sha256=a34ad504b52a086742db5b531f6494ed8c64744556b6db85e3e893c56bd149b0
// Cooperative-lane batch provider; one workgroup owns each batch row.
// Exact carry/borrow semantics match the per-invocation baseline.
import attributes;
import bigint_words;
import wide_u32;
struct PushConstants {
uint left_index;
uint right_index;
uint output_index;
uint count;
uint grid_width;
uint words;
};
[[vk::push_constant]] PushConstants push;
[[vk::binding(0, 0)]] RWByteAddressBuffer storage_buffers[];
groupshared uint lane_carry[32];
groupshared uint lane_propagate[32];
[kernel_name("add_cooperative")]
[domain("cryptography")]
[variant("generic")]
[dtype("u32")]
[status("experimental")]
[shader("compute")]
[numthreads(32, 1, 1)]
void main(uint3 group : SV_GroupID, uint lane : SV_GroupIndex) {
uint row = group.x + group.y * push.grid_width;
// Every early exit is uniform across the workgroup, before any barrier.
if (row >= push.count || push.words == 0u || push.words > 128u) return;
uint input_row_bytes = push.words * 4u;
uint left_extent, right_extent, output_extent;
storage_buffers[push.left_index].GetDimensions(left_extent);
storage_buffers[push.right_index].GetDimensions(right_extent);
storage_buffers[push.output_index].GetDimensions(output_extent);
uint output_row_bytes = input_row_bytes;
if (push.count > left_extent / input_row_bytes || push.count > right_extent / input_row_bytes
|| push.count > output_extent / output_row_bytes) return;
uint input_base = row * input_row_bytes;
uint chunk = (push.words + 31u) / 32u;
uint lane_base = lane * chunk;
uint generate = 0u, propagate = 1u;
[loop]
for (uint j = 0u; j < chunk; ++j) {
uint i = lane_base + j;
uint a = 0u, b = 0u;
if (i < push.words) {
a = storage_buffers[push.left_index].Load(input_base + i * 4u);
b = storage_buffers[push.right_index].Load(input_base + i * 4u);
}
uint s0 = a + b + generate;
generate = uint(s0 < a || (generate == 1u && s0 == a));
uint s1 = a + b + propagate;
propagate = uint(s1 < a || (propagate == 1u && s1 == a));
}
lane_carry[lane] = generate;
lane_propagate[lane] = propagate;
GroupMemoryBarrierWithGroupSync();
if (lane == 0u) {
uint carry = 0u;
for (uint l = 0u; l < 32u; ++l) {
uint next = carry == 0u ? lane_carry[l] : lane_propagate[l];
lane_carry[l] = carry;
carry = next;
}
}
GroupMemoryBarrierWithGroupSync();
uint carry = lane_carry[lane];
[loop]
for (uint j = 0u; j < chunk; ++j) {
uint i = lane_base + j;
if (i < push.words) {
uint a = storage_buffers[push.left_index].Load(input_base + i * 4u);
uint b = storage_buffers[push.right_index].Load(input_base + i * 4u);
uint sum = a + b + carry;
carry = uint(sum < a || (carry == 1u && sum == a));
storage_buffers[push.output_index].Store(input_base + i * 4u, sum);
}
}
}