C++ gcc 2 byte float

Does anyone know how to make gcc compiler for c++ treat the builtin _Float16 type as an actual 2byte float and not auto promote it to a standard 4byte float?

Thought i would ask before just writing my own implementation, to force the issue.

Its been a bit since I used gcc but this might be what you are looking for.

If this is not, might be handy if you could provide more information since from your question I am not sure if you are saying the compiler is treating _Float16 as a standard float, it is being implicitly cast to a full float when used, or it takes up 4 bytes of memory even when it should be using 2 (this could be just a memory bounds packing issue in this case).

Yea i came across that while googling before posting and read it it, although at my understanding level, not sure i’m reading it right.

What i do know is if i set the float variables to int’s the runtime memory size of the program does not change(expected as both are 4bytes and this is a sanity check), problem is that the memory size is the same with when setting the float variables to _Float16. More interestingly when using a short int 2 bytes i do see the reduction i’m expecting. Something funny is going on.

Ah, ya that may be expected behavior actually. Take what I am about to say with a grain of salt as this topic gets into the weeds of some black magic C/C++ topics, and I will be talking out of my ass on some of it as it has been such a long time since I have had to look into it myself.

What is likely catching you is something called memory alignment. CPUs prefer and have been optimized to access memory in very specific ways. Most modern CPUs like memory aligned on 4 or 8 byte bounds. Compilers will usually pad any data structures that dont fit into this alignment to force them into it. So in your case your 2 byte float may be getting padded with 2 additional bytes of junk to make it fit that 4 byte alignment. This is a very common question when someone creates a struct that should be 6 bytes in size but it actually takes up 8 for example.

The other option is _Float16 is actually implemented with an underlying 4 byte structure. I have seen this done more frequently then you may think so its very possible that the gcc implementation is actually 32bits in size but only allows for 16bit precision. Most of the time this is done for the above issue though sometimes is also done bc it was simpler to do it that way.

End of the day though, unless your application is very memory sensitive, like a micro controller or an app that has to hold a ton of data in memory, it should not matter and breaking memory alignment can really hurt processing performance.

I would suggest instead verifying that it behaves as expected instead of looking at the memory metrics. As long as its within the normal bounds for a half precision floating point you should be good and due to all the low level stuff the compiler does judging something like that by memory footprint is not very reliable.