// Type-cast kernels for GPU replay buffer (u16<->f32, u32<->f32).
//
// Common header (common_device_functions.cuh) is prepended by build.rs
// providing: float, (), etc.

// u16 -> f32 (bit-pattern reinterpret)
extern "C" __global__
void u16_to_f32_cast(float* __restrict__ dst,
                     const float* __restrict__ src,
                     int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        dst[i] = (src[i]);
    }
}

// u32 -> f32
extern "C" __global__
void u32_to_f32_cast(float* __restrict__ dst,
                     const unsigned int* __restrict__ src,
                     int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        dst[i] = (float)src[i];
    }
}

// f32 -> u32 (index cast, rounds to nearest)
extern "C" __global__
void f32_idx_to_u32(unsigned int* __restrict__ dst,
                    const float* __restrict__ src,
                    int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        dst[i] = (unsigned int)(src[i] + 0.5f);
    }
}

// f32 -> u16 (bit-pattern reinterpret)
extern "C" __global__
void f32_to_u16_cast(unsigned short* __restrict__ dst,
                     const float* __restrict__ src,
                     int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        float val = (src[i]);
        dst[i] = *(unsigned short*)&val;
    }
}
