The GPU can use 16-bit floats by using the "half" data type. Do the CPU have a 16-bit float?
My actual issue is that I want to use "half" in my GPU uniform structs - how can I initialize such values on the CPU?
For storage, you can use the
__fp16
type, or any type that has the same size and alignment as half
, such as uint16_t
.Assignments from
float
to __fp16
will emit the appropriate fcvt
instruction or intrinsic, depending on the target platform.If you elect to use
uint16_t
as your storage type, you can still type-pun through the __fp16
type to load and store:static float loadFromF16(const uint16_t *pointer) { return *(const __fp16 *)pointer; }
static void storeAsF16(float value, uint16_t *pointer) { *(__fp16 *)pointer = value; }
If you have a long list of floats you need to convert to half, rather than a few struct members, using the
vImageConvert_PlanarFtoPlanar16F
function may be more efficient.