Microfloats is a Julia package that implements types and arithmetic (through wider intermediates) for sub-byte floating points, supporting arbitrary combinations of sign, exponent, and significand (mantissa) bits.
In ordinary arrays, or as single instances, sub-8 bit floating points are one byte wide; Microfloats provides a numerical reference for any conceivable microfloat, but also offers canonical types for common narrow data formats which can then be used downstream by packages like cuTile.jl.
Define your own microfloat with the macro:
using Microfloats: @microfloat
@microfloat Float8_E5M2 sign=1 exponent=5 significand=2 nonfinite=Microfloats.IEEEor find predefined types in the documentation.
Conversions take a rounding mode and an overflow policy, and work on scalars, on static vectors with one value per byte, and on densely packed vectors:
using Microfloats
using Microfloats: SVector, NVector, SAT
Float8_E4M3FN(1000f0, RoundToZero; overflow=SAT) # Float8_E4M3FN(448.0)
packed = NVector{Float4_E2M1FN,4}(SVector(0.5f0, 1f0, -6f0, 100f0)) # two bytes
SVector{4,Float32}(packed) # 0.5, 1.0, -6.0, 6.0All of them reduce to one dispatchable function, Microfloats.cvt. With CUDACore loaded, kernels and broadcasts lower it to native conversion instructions on GPUs that have them. See Conversion in the documentation.
using Pkg
Pkg.add("Microfloats")