// ===================================================================
// Dropout forward kernel -- f32-native with built-in xoshiro128+ PRNG.
//
// Common header (common_device_functions.cuh) is prepended by build.rs
// providing: float, (), 0.0f, etc.
// ===================================================================

extern "C" __global__
void dropout_forward(const float* __restrict__ x,
                          float* __restrict__ y,
                          float* __restrict__ mask,
                          float p,
                          float scale,
                          unsigned int seed_lo,
                          unsigned int seed_hi,
                          int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        // Simple hash-based RNG: mix seed with thread index
        unsigned int h = seed_lo ^ (unsigned int)i;
        h *= 2654435761u;  // Knuth multiplicative hash
        h ^= seed_hi;
        h *= 2246822519u;
        h ^= (h >> 13);
        h *= 3266489917u;
        h ^= (h >> 16);

        // Convert to [0, 1) via float division
        float r = (float)(int)(h & 0x00FFFFFFu) / (float)(int)0x01000000u;

        float m = (r >= p) ? scale : 0.0f;
        mask[i] = m;
        y[i] = x[i] * m;
    }
}
