How can I convert a float (float32) to a half (float16) and the other way around in C while accounting for edge cases like NaN, Infinity etc.
I don't need arithmetic because I just need the types in order to fulfill the requirement of supporting them. So the half type can just be a uint16_t
or corresponding typedef
. I only found ways to do it in C++ or some that didn't account for edge cases like NaN.
I need to convert a float into a half type that can be represented as a simple uint16_t
, this uint16_t
should just contain the binary representation of the half since I won't do arithmetic on it. I require this so I can fulfill a library requirement. I can't use existing implementations because they are build as shared libraries (also mostly for C++) which I can't use in this case. Also the GCC/Clang __fp16
and _Float16
won't work because I compile my code very to web assembly that will run in an isolated environment and as such can't use native dependent code (No WASI) (And EMCC throws an error when using _FloatXX
types).
You have a number of options:
Edit: Since you mainly just want to convert back-and-forth, the two functions to look at in the ILM code are: Float to half: Line 85 and half to float: line 62