A small library for quickly packing and unpacking half-precision floating point values.
The implementation is based on
the Fast Half Float Conversions paper
by Jeroen van der Zijp
, referenced by the
Wikipedia article on
half-precision floating point.
The only distinguishing feature of this implementation is that it uses the compile-time lookup table feature
of C++20
. There are no classical unit tests, just some static_assert's
sprinkled around.
The easiest way to include this library in your project is to use the CMake's FetchContent
functionality, for example:
cmake_minimum_required(VERSION 3.16)
project(integration_test)
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
fhf
GIT_REPOSITORY https://github.com/yowidin/fast-half-float
GIT_TAG master
)
FetchContent_MakeAvailable(fhf)
add_executable(test main.cpp)
target_link_libraries(test PRIVATE FastHalfFloat::library)
You can now be able to pack and unpack the half-precision float values:
#include <fhf/fhf.hh>
#include <iostream>
#include <limits>
void foo() {
auto h = fhf::pack(std::numeric_limits<float>::infinity());
std::cout << "Infinity:" << std::hex << h << std::endl;
h = fhf::pack(-std::numeric_limits<float>::infinity());
std::cout << "-Infinity:" << std::hex << h << std::endl;
auto f = unpack(0x3C00);
std::cout << "0x3C00:" << f << std::endl;
}