Universal: a header-only C++ template library of custom arithmetic plug-in types
| System | Status | More information |
|---|---|---|
| Codacy Code Quality | Code Quality Assessment | |
| FOSSA Status | Open-source license dependency scanner | |
| GitHub Actions | Latest Linux/MacOS/Windows builds and regression tests | |
| Development Branch | Development Branch | |
| Regression Status | Regression Status | |
| Code Coverage | Code coverage scanner | |
| Docker Pulls | Container pulls | |
| Awesome C++ | Awesome C++ Libraries | |
| JOSS Markdown | Journal of Open-Source Software paper | |
| Zenodo | Zenodo DOI Badge | |
| CodeRabbitAI | CodeRabbit Badge |
The goal of the Universal Numbers Library is to offer alternatives to native integer and floating-point for mixed-precision algorithm development and optimization. Tailoring the arithmetic types to the application's precision and dynamic range enables a new level of application performance and energy efficiency, particularly valuable for embedded applications that need autonomy through intelligent behavior.
Deep Learning algorithms in particular provide a core application vertical where alternative formats and precisions, such as half-precision floating-point and bfloat16, yield speed-ups of two to three orders of magnitude, making rapid innovation in AI possible.
The Universal Library is a ready-to-use header-only library that provides a plug-in replacement for native types and offers a low-friction environment to explore alternatives to IEEE-754 floating-point in AI, DSP, HPC, and HFT algorithms.
The basic use pattern is as simple as:
// bring in the parameterized type of interest, in this case
// a fixed-sized, arbitrary configuration classic floating-point
#include <universal/number/cfloat/cfloat.hpp>
// define your computational kernel parameterized by arithmitic type
template<typename Real>
Real MyKernel(const Real& a, const Real& b) {
return a * b; // replace this with your kernel computation
}
constexpr double pi = 3.14159265358979323846;
int main() {
// if desired, create an application type alias to avoid errors
using Real = sw::universal::half; // half-precision IEEE-754 floating-point
Real a = sqrt(2);
Real b = pi;
// finally, call your kernel with your desired arithmetic type
std::cout << "Result: " << MyKernel(a, b) << std::endl;
}
The library contains fast implementations of special IEEE-754 formats that do not have universal hardware implementations across x86, ARM, POWER, RISC-V, and GPUs. Special formats such as quarter precision, quarter, half-precision, half, and quad precision, quad, are provided, as well as vendor-specific extensions, such as NVIDIA TensorFloat, Google's Brain Float, bfloat16, or TI DSP fixed-points, fixpnt. In addition to these often-used specializations, Universal supports static and elastic integers, decimals, fixed-points, rationals, linear floats, tapered floats, logarithmic, interval and adaptive-precision integers, rationals, and floats.
Communication channels
- GitHub Issue: bug reports, feature requests, etc.
- Forum: discussion of alternatives to IEEE-754 for computational science.
- Slack: online chats, discussions, and collaboration with other users, researchers and developers.
Citation
Please cite our work if you use Universal.
Research results using Universal arithmetic types are also available:
Lorenz System ODE solver using low-precision posits:

Bibtex records
@article{Quinlan:2024,
author = {James Quinlan, E. Theodore L. Omtzigt},
title = {Iterative Refinement with Low-Precision Posits},
journal = {arXiv:2408.13400},
year = {2024},
}
@article{omtzigt2023universal,
title={Universal Numbers Library: Multi-format Variable Precision Arithmetic Library},
author={Omtzigt, E Theodore L and Quinlan, James},
journal={Journal of Open Source Software},
volume={8},
number={83},
pages={5072},
year={2023}
}
@inproceedings{Omtzigt:2022,
title = {Universal: Reliable, Reproducible, and Energy-Efficient Numerics},
author = {E. Theodore L. Omtzigt and James Quinlan},
booktitle = {Conference on Next Generation Arithmetic},
pages = {100--116},
year = {2022},
organization = {Springer}
}
@article{Omtzigt2020,
author = {E. Theodore L. Omtzigt and Peter Gottschling and Mark Seligman and William Zorn},
title = {{Universal Numbers Library}: design and implementation of a high-performance reproducible number systems library},
journal = {arXiv:2012.11011},
year = {2020},
}
Talks and Presentations
The following presentations describe Universal and the number systems it contained as of the time of publication.
Slides of a presentation at FPTalks'21
Presentation: Application-Driven Custom Number Systems
Slides of the presentation at CoNGA'22
Presentation: Universal: Reliable, Reproducible, and Energy-Efficient Numerics
A quick description of the structure of the number system parameterization can be found here.
Quick start
Follow the local workflow notes in how-to-run-sanitizers.md.
If you just want to experiment with Universal without cloning and building the source code, there is a Docker container with compilers, cmake, and the library pre-installed:
> docker pull stillwater/universal
> docker run -it --rm stillwater/universal bash
stillwater@container:~$ ieee 1.5
stillwater@container:~$ cat > hello.cpp << 'EOF'
#include <universal/number/posit/posit.hpp>
#include <iostream>
int main() {
sw::universal::posit<16,2> p = 1.5;
std::cout << p << '\n';
}
EOF
stillwater@container:~$ g++ -std=c++20 -I/usr/local/include/sw -o hello hello.cpp && ./hello
Here is a quick reference of what the command line tools have to offer.
VS Code Dev Container
For a full development environment with both GCC and Clang, install Docker and the Dev Containers VS Code extension, then open the repository and select Reopen in Container. CMake presets (gcc-debug, clang-release, etc.) are available in the CMake Tools preset selector.
How to build
If you do want to work with the code, the universal numbers software library is built using cmake version v4.2.1. Install the latest cmake. There are interactive installers for MacOS and Windows. For Linux, a portable approach downloads the shell archive and installs it at /usr/local:
> wget https://github.com/Kitware/CMake/releases/download/v4.2.1/cmake-4.2.1-Linux-x86_64.sh
> sudo sh cmake-4.2.1-Linux-x86_64.sh --prefix=/usr/local --exclude-subdir
For Ubuntu, snap will install the latest cmake, and would be the preferred method:
> sudo snap install cmake --classic
The Universal Library is a pure C++ template library without any further dependencies, even for the regression test suites to enable hassle-free installation and use.
Clone the GitHub repo, and you are ready to build the different components of the Universal library.
The library contains tools for using integers, decimals, fixed-points, floats, posits, valids, and logarithmic
number systems. It includes educational programs that showcase simple use cases to familiarize yourself with
different number systems and application examples to highlight the use of other number systems to gain performance
or numerical accuracy. Finally, each number system offers its own verification suite.
The easiest way to become familiar with all the options in the build process is to fire up the CMake GUI (or ccmake if you are on a headless server). The CMake output will summarize which options have been set.
The output will look something like this:
$ git clone https://github.com/stillwater-sc/universal
$ cd universal
$ mkdir build
$ cd build
$ cmake -DUNIVERSAL_BUILD_ALL=ON ..
_____ _____ ____ _____ _____ ____ ____ ________ _______ ______ _ _____
|_ _||_ _||_ \|_ _||_ _||_ _| |_ _||_ __ ||_ __ \ .' ____ \ / \ |_ _|
| | | | | \ | | | | \ \ / / | |_ \_| | |__) | | (___ \_| / _ \ | |
| ' ' | | |\ \| | | | \ \ / / | _| _ | __ / _.____`. / ___ \ | | _
\ \__/ / _| |_\ |_ _| |_ \ ' / _| |__/ | _| | \ \_| \____) | _/ / \ \_ _| |__/ |
`.__.' |_____|\____||_____| \_/ |________||____| |___|\______.'|____| |____||________|
-- Version from git tag v4.6.11: 4.6.11
-- The C compiler identification is GNU 13.3.0
-- The CXX compiler identification is GNU 13.3.0
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/cc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- No default build type specified: setting CMAKE_BUILD_TYPE=Release
-- C++20 has been enabled by default
-- UNIVERSAL_BUILD_REGRESSION_SANITY ON
-- UNIVERSAL_BUILD_REGRESSION_LEVEL_1 ON
-- Performing Test COMPILER_HAS_SSE3_FLAG
-- Performing Test COMPILER_HAS_SSE3_FLAG - Success
-- Performing Test COMPILER_HAS_AVX_FLAG
-- Performing Test COMPILER_HAS_AVX_FLAG - Success
-- Performing Test COMPILER_HAS_AVX2_FLAG
-- Performing Test COMPILER_HAS_AVX2_FLAG - Success
--
-- PROJECT_NAME = universal
-- PROJECT_NAME_NOSPACES = universal
-- PROJECT_SOURCE_DIR = /home/stillwater/dev/stillwater/clones/universal
-- PROJECT_VERSION = 4.6.11.9a7c0243
-- CMAKE_C_COMPILER = /usr/bin/cc
-- CMAKE_CXX_COMPILER = /usr/bin/c++
-- CMAKE_CURRENT_SOURCE_DIR = /home/stillwater/dev/stillwater/clones/universal
-- CMAKE_CURRENT_BINARY_DIR = /home/stillwater/dev/stillwater/clones/universal/build
-- GIT_COMMIT_HASH = 9a7c0243
-- GIT_BRANCH = main
-- include_install_dir = include
-- include_install_dir_full = include
-- config_install_dir = share/universal
-- include_install_dir_postfix =
-- Add test multifile from source folder Applications/Environment/Multifile.
-- CMAKE_CXX_COMPILER ID is -GNU-
-- Adding regression suite for complex arithmetic using fixpnt
-- Adding regression suite for complex math functions using fixpnt
-- Removing regression suite for complex arithmetic using posit1
-- Removing regression suite for complex math functions using posit1
-- Removing regression suite for complex arithmetic using cfloats
-- Removing regression suite for complex math functions using cfloats
-- Adding regression suite for sw::universal::complex<T>
-- adding rpoly test suite
-- ucalc: readline not found, using basic line input
-- Adding playground complex experiment
-- Adding playground concurrency experiment
-- TBB found: linking to concurrency experiment
-- Ignoring complex tests for application/performance
-- Found clang-format: /usr/bin/clang-format-18
-- Version: Ubuntu clang-format version 18.1.3 (1ubuntu1)
-- Files to format: 2017
-- Found clang-tidy: /usr/bin/clang-tidy-18
-- Version: Ubuntu LLVM version 18.1.3
Optimized build.
-- Header files for analysis: 1008
-- Sample test files for analysis: 29
--
-- Clang-tidy targets configured:
-- make tidy-check - Run clang-tidy on headers (read-only)
-- make tidy-check-samples - Run clang-tidy on sample test files
-- make tidy-fix - Apply clang-tidy fixes to headers
-- make tidy-info - Show detailed configuration
-- Script: /home/stillwater/dev/stillwater/clones/universal/build/run-clang-tidy.sh
--
-- ******************* Universal Arithmetic Library Configuration Summary *******************
-- General:
-- Version : 4.6.11.9a7c0243
-- System : Linux
-- C++ Language Requirement : C++20
-- C compiler : /usr/bin/cc
-- Release C flags : -O3 -DNDEBUG -Wall -Wpedantic -Wno-narrowing -Wno-deprecated
-- Debug C flags : -g -Wall -Wpedantic -Wno-narrowing -Wno-deprecated
-- C++ compiler : /usr/bin/c++
-- Release CXX flags : -O3 -DNDEBUG -Wall -Wpedantic -Wno-narrowing -Wno-deprecated -Wall -Wpedantic -Wno-narrowing -Wno-deprecated
-- Debug CXX flags : -g -Wall -Wpedantic -Wno-narrowing -Wno-deprecated -Wall -Wpedantic -Wno-narrowing -Wno-deprecated
-- Build type : Release
--
-- UNIVERSAL_BUILD_ALL : ON
-- UNIVERSAL_BUILD_CI : OFF
-- UNIVERSAL_BUILD_CI_LITE : OFF
--
-- UNIVERSAL_BUILD_DEMONSTRATION : ON
-- UNIVERSAL_BUILD_NUMBERS : ON
-- UNIVERSAL_BUILD_NUMBER_CASCADES : ON
-- UNIVERSAL_BUILD_NUMERICS : ON
-- UNIVERSAL_BUILD_BENCHMARKS : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_SDK : ON
-- UNIVERSAL_BUILD_COMPLEX : ON
-- UNIVERSAL_BUILD_CONCURRENCY : ON
-- UNIVERSAL_BUILD_UTILITY : ON
--
-- UNIVERSAL_BUILD_TOOLS_CMD_LINE : ON
-- UNIVERSAL_BUILD_TOOLS_UTILS : ON
-- UNIVERSAL_BUILD_TOOLS_CLOSURE : OFF
-- UNIVERSAL_BUILD_PLAYGROUND : ON
-- UNIVERSAL_BUILD_EDUCATION : ON
-- UNIVERSAL_BUILD_EDUCATION_INTERACTIVE : OFF
-- UNIVERSAL_BUILD_APPLICATIONS : ON
-- UNIVERSAL_BUILD_PAPERS : ON
--
-- UNIVERSAL_BUILD_NUMERIC_FUNCTIONS : ON
-- UNIVERSAL_BUILD_NUMERIC_FAITHFUL : ON
-- UNIVERSAL_BUILD_NUMERIC_QUIRES : ON
-- UNIVERSAL_BUILD_NUMERIC_CHALLENGES : ON
-- UNIVERSAL_BUILD_NUMERIC_UTILS : ON
-- UNIVERSAL_BUILD_NUMERIC_FPBENCH : ON
--
-- UNIVERSAL_BUILD_LINEAR_ALGEBRA_BLAS : ON
-- UNIVERSAL_BUILD_LINEAR_ALGEBRA_VMATH : ON
-- UNIVERSAL_BUILD_LINEAR_ALGEBRA_DATA : ON
--
-- UNIVERSAL_BUILD_BENCHMARK_ERROR : ON
-- UNIVERSAL_BUILD_BENCHMARK_ACCURACY : ON
-- UNIVERSAL_BUILD_BENCHMARK_RANGE : ON
-- UNIVERSAL_BUILD_BENCHMARK_REPRODUCIBILITY : ON
-- UNIVERSAL_BUILD_BENCHMARK_PERFORMANCE : ON
-- UNIVERSAL_BUILD_BENCHMARK_ENERGY : ON
--
-- UNIVERSAL_BUILD_MIXEDPRECISION_ROOTS : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_APPROXIMATE : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_INTEGRATE : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_INTERPOLATE : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_OPTIMIZE : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_TENSOR : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_METHODOLOGY : ON
-- UNIVERSAL_BUILD_MIXEDPRECISION_POP : ON
--
-- UNIVERSAL_BUILD_TYPE_HIERARCHY : ON
-- UNIVERSAL_BUILD_APP_ENVIRONMENT : ON
--
-- UNIVERSAL_BUILD_C_API_PURE_LIB : ON
-- UNIVERSAL_BUILD_C_API_SHIM_LIB : ON
-- UNIVERSAL_BUILD_C_API_LIB_PIC : ON
--
-- UNIVERSAL_BUILD_NUMBER_INTERNALS : ON
-- UNIVERSAL_BUILD_NUMBER_NATIVE_TYPES : ON
--
-- UNIVERSAL_BUILD_NUMBER_ELASTICS : ON
-- UNIVERSAL_BUILD_NUMBER_EINTEGERS : ON
-- UNIVERSAL_BUILD_NUMBER_EDECIMALS : ON
-- UNIVERSAL_BUILD_NUMBER_ERATIONALS : ON
-- UNIVERSAL_BUILD_NUMBER_EFLOATS : ON
-- UNIVERSAL_BUILD_NUMBER_EREALS : ON
-- UNIVERSAL_BUILD_NUMBER_ELREALS : OFF
--
-- UNIVERSAL_BUILD_NUMBER_STATICS : ON
-- UNIVERSAL_BUILD_NUMBER_INTEGERS : ON
-- UNIVERSAL_BUILD_NUMBER_DECIMALS : ON
-- UNIVERSAL_BUILD_NUMBER_RATIONALS : ON
-- UNIVERSAL_BUILD_NUMBER_FIXPNTS : ON
-- UNIVERSAL_BUILD_NUMBER_DFIXPNTS : ON
-- UNIVERSAL_BUILD_NUMBER_BFLOATS : ON
-- UNIVERSAL_BUILD_NUMBER_CFLOATS : ON
-- UNIVERSAL_BUILD_NUMBER_DFLOATS : ON
-- UNIVERSAL_BUILD_NUMBER_HFLOATS : ON
-- UNIVERSAL_BUILD_NUMBER_DOUBLE_DOUBLE : ON
-- UNIVERSAL_BUILD_NUMBER_QUAD_DOUBLE : ON
-- UNIVERSAL_BUILD_NUMBER_DD_CASCADE : ON
-- UNIVERSAL_BUILD_NUMBER_TD_CASCADE : ON
-- UNIVERSAL_BUILD_NUMBER_QD_CASCADE : ON
-- UNIVERSAL_BUILD_NUMBER_AREALS : ON
-- UNIVERSAL_BUILD_NUMBER_INTERVALS : ON
-- UNIVERSAL_BUILD_NUMBER_UNUM1S : ON
-- UNIVERSAL_BUILD_NUMBER_UNUM2S : ON
-- UNIVERSAL_BUILD_NUMBER_POSITS : ON
-- UNIVERSAL_BUILD_NUMBER_POSITOS : ON
-- UNIVERSAL_BUILD_NUMBER_VALIDS : ON
-- UNIVERSAL_BUILD_NUMBER_TAKUMS : ON
-- UNIVERSAL_BUILD_NUMBER_LNS : ON
-- UNIVERSAL_BUILD_NUMBER_DBNS : ON
-- UNIVERSAL_BUILD_NUMBER_QUIRES : ON
-- UNIVERSAL_BUILD_NUMBER_SORNS : ON
-- UNIVERSAL_BUILD_NUMBER_MICROFLOATS : ON
-- UNIVERSAL_BUILD_NUMBER_E8M0 : ON
-- UNIVERSAL_BUILD_NUMBER_MXF
