#pragma clang diagnostic ignored "-Wmissing-prototypes"

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

template <typename F> struct SpvHalfTypeSelector;
template <> struct SpvHalfTypeSelector<float> { public: using H = half; };
template<uint N> struct SpvHalfTypeSelector<vec<float, N>> { using H = vec<half, N>; };
template<typename F, typename H = typename SpvHalfTypeSelector<F>::H>
[[clang::optnone]] F spvQuantizeToF16(F fval)
{
    H hval = H(fval);
    hval = select(copysign(H(0), hval), hval, isnormal(hval) || isinf(hval) || isnan(hval));
    return F(hval);
}

struct SSBO0
{
    float scalar;
    float2 vec2_val;
    float3 vec3_val;
    float4 vec4_val;
};

kernel void main0(device SSBO0& _4 [[buffer(0)]])
{
    _4.scalar = spvQuantizeToF16(_4.scalar);
    _4.vec2_val = spvQuantizeToF16(_4.vec2_val);
    _4.vec3_val = spvQuantizeToF16(_4.vec3_val);
    _4.vec4_val = spvQuantizeToF16(_4.vec4_val);
}

