#pragma clang diagnostic ignored "-Wmissing-prototypes"

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

using namespace metal;

// Implementation of the GLSL findLSB() function
template<typename T>
T findLSB(T x)
{
    return select(ctz(x), T(-1), x == T(0));
}

// Implementation of the signed GLSL findMSB() function
template<typename T>
T findSMSB(T x)
{
    T v = select(x, T(-1) - x, x < T(0));
    return select(clz(T(0)) - (clz(v) + T(1)), T(-1), v == T(0));
}

// Implementation of the unsigned GLSL findMSB() function
template<typename T>
T findUMSB(T x)
{
    return select(clz(T(0)) - (clz(x) + T(1)), T(-1), x == T(0));
}

kernel void main0()
{
    int signed_value = 0;
    uint unsigned_value = 0u;
    int s = extract_bits(signed_value, 5, 20);
    uint u = extract_bits(unsigned_value, 6, 21);
    s = insert_bits(s, 40, 5, 4);
    u = insert_bits(u, 60u, 5, 4);
    u = reverse_bits(u);
    s = reverse_bits(s);
    int v0 = popcount(u);
    int v1 = popcount(s);
    int v2 = findUMSB(u);
    int v3 = findSMSB(s);
    int v4 = findLSB(u);
    int v5 = findLSB(s);
}

