// Noisy linear bias-add kernel -- F32 arithmetic.
//
// Common header (common_device_functions.cuh) is prepended by build.rs
// providing: float, () (identity), etc.
//
// Clamping prevents extreme values from propagating as Inf/NaN.

#define BF16_SAFE_MAX 500.0f

extern "C" __global__
void noisy_add_bias_kernel(float* __restrict__ y,
                           const float* __restrict__ bias,
                           int rows, int cols) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < rows * cols) {
        int col = idx % cols;
        float val = (float)y[idx] + (float)bias[col];
        val = fminf(fmaxf(val, -BF16_SAFE_MAX), BF16_SAFE_MAX);
        y[idx] = (val);
    }
}
