How to store float as half data type from cpu side

Metal shading language has half data type and it is recommended to use half for better performance of shader code. But we populate our MTLBuffer from CPU side and CPU does not support half precision float then in that case, how can we store our CPU float data as half in MTLBuffer?

Answered by iPerKard in 714454022

You can use __fp16 or _Float16 data types.

__fp16 is not an arithmetic data type and should be used for storage purposes. It allows conversion from and to floats. _Float16 is an arithmetic data type and have supports for standard operations.

_Float16 is more modern and a better choice. You can set a half constant with the f16 suffix following your real number.

Accepted Answer

You can use __fp16 or _Float16 data types.

__fp16 is not an arithmetic data type and should be used for storage purposes. It allows conversion from and to floats. _Float16 is an arithmetic data type and have supports for standard operations.

_Float16 is more modern and a better choice. You can set a half constant with the f16 suffix following your real number.

Fastest method is to use Neon vcvt_f32_f16 and vcvt_f16_f32. Intel also has ops for f16c extension, but note that Rosetta2 doesn't support emulated AVX for f16c.

How to store float as half data type from cpu side
 
 
Q