Help language development. Donate to The Perl Foundation

TinyFloats zef:japhb last updated on 2021-12-28


Actions Status


TinyFloats - Convert to/from tiny float formats


use TinyFloats;

my $bin16 = bin16-from-num(1e0);     # 0x3C00
my $num   = num-from-bin16(0xFBFF);  # -65504e0

my $bf16  = bf16-from-num(1e0);      # 0x3F80
my $num2  = num-from-bf16(0xFF7F);   # -3.3895313892515355e+38


TinyFloats is a collection of simple conversion routines to help with storing floating point data in tiny float formats.

This version supports only conversion between native nums and the following 16-bit floating point storage formats:

bin16 attempts to balance the reduction in exponent and mantissa bits, is fairly slow to convert, is used most often in graphics formats, and is commonly converted to native binary32 internally by modern graphics hardware.

bf16 reduces only the mantissa bits, is relatively quick to convert, is used most often in machine learning systems, and is usually used directly by the ML hardware.


Geoffrey Broadwell [email protected]


Copyright 2021 Geoffrey Broadwell

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.