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?
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.
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.
hi Jakob,
You might also be interested in the OpenEXR project's Half type which a C++ class representing a half precision float.. http://www.openexr.com
OpenEXR is focused on the image file format of that name but the Half type and the associated utilities (in the Ilmbase library) can be used entirely separately from the OpenEXR library which is built on top of that.
Cheers,
- James
How to do it in Swift? Should Swift have a 16 bit float type?