Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <metal_stdlib>
using namespace metal;
// only intended for 32-bit int types
template<typename T>
static T bitfieldExtract(T val, int offset, int size)
{
val <<= 32 - (offset + size);
val >>= 32 - size;
return val;
}
kernel void kern(uint invocation [[ thread_index_in_threadgroup ]])
{
int2 x = bitfieldExtract(int2(0x01234567, 0x89abcdef), int(invocation)*4, 4);
}
// generates:
// test.metal:15:14: error: no matching function for call to 'bitfieldExtract'
// int2 x = bitfieldExtract(int2(0x01234567, 0x89abcdef), int(invocation)*4, 4);
// ^~~~~~~~~~~~~~~
// test.metal:6:10: note: candidate template ignored: couldn't infer template argument 'T'
// static T bitfieldExtract(T val, int offset, int size)
// ^
// 1 error generated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment