C++ Half Floats

If you just want the code and don't want to read my drivel, click here to get it.

We have been giving our art pipeline some much needed cleanup lately, and part of that has involved adding support for the myriad different pixel formats supported by OpenGL. One of those is half precision floating point, which presents a minor problem as C++ does not support it.

I had a look for existing implementations, but all the code I came across was either unsuitable or used a naive implementation of the conversion. Then I found a paper by Jeroen van der Zijp which  details a reasonable algorithm for converting between a single precision float and a half precision float using lookup tables. This should be faster than the naive conversions, at the cost of a few KB of static data.

You can find my C++ implementation of Jeroen's algorithm on BitBucket here. It provides a HalfFloat class that you can use pretty much the same way as a float. See the readme for more details. It's licensed under the Apache 2.0 license, so you can use it however you want to.

I hope someone finds this useful.

Labels: ,