Fixed point 48.16 bit arithmetic type

fixed_math MIT CMake language

written from scratch fixed point math library in C++17

features

  • minimum c++17 compiler required
  • fixed point 48.16 arithmethic strong type without implicit convertions with assignment and construction, fixed type is safe against unwanted type convertions
  • all arithemtic types like float and integral types except double implicitly in base arithemitc operations are promoted to fixed_t
  • all arithmetic operations of fixed_t with double type yelds double result type, are promoted and evaluated as double operations
  • entire code including trigonometry functions is constexpr [1]
  • fully header only library as everything is constexpr, see api interface api and implementation
  • unit tests can be checked at compilation time just including header, see unittests

[1] - By default is used std:sqrt as current cpu's has hardware support for sqrt, but constexpr abacus algorithm could be used defining FIXEDMATH_ENABLE_SQRT_ABACUS_ALGO, which is slower than cpu one

performance comparisions of code 0.9.1

At this point code wasn't been optimised, so results are from just from code written with quality only at this point in mind. Results are relative times of computing functions over bigtable of source values in function type (no value convertions)

Cortex-A73 - Snapdragon 865+

tag function fixed clang fixed gcc float double status
0.9.1 sin 50 ms 31 ms 77 ms
0.9.4 sin 30 ms 24ms 31 ms 77 ms optimized
0.9.1 asin 124 ms 75 ms 128 ms
0.9.4 asin 63 ms 54 ms 75 ms 127 ms optimized
0.9.1 tan 136 ms 104 ms 206 ms
0.9.4 tan 136 ms 130 ms 104 ms 206 ms
0.9.1 atan 113 ms 110 ms 165 ms
0.9.5 atan 80 ms 65 ms 110 ms 165 ms optimized

Ryzen 9 - 3900X

tag function fixed clang fixed gcc float double status
0.9.1 sin 27 ms 22 ms 75 ms
0.9.4 sin 20 ms 18ms 21 ms 74 ms optimized
0.9.1 asin 92 ms 58 ms 106 ms
0.9.4 asin 49 ms 44 ms 57 ms 104 ms optimized
0.9.1 tan 81 ms 66 ms 180 ms
0.9.4 tan 78 ms 88 ms 67 ms 176 ms
0.9.1 atan 90 ms 78 ms 162 ms
0.9.5 atan 64 ms 55ms 77 ms 161 ms optimized

installation

this library is header only except tabelarized trigonometric functions. So If you can use precise trigonometric functions You don't have to build anything. Just add fixed_lib/include to include path and #include <fixedmath/fixed_math.hpp>. If you want additional inprecise aproxymated functions compile project like any other ordinary CMake project. At this point it doesn't have any tuning parameters for CMake.

c++ compilers

Thise are known to compile without errors, if You found any problem with c++17 compiler let me know and post issue as stl shoudn't be a problem

  • clang version 11.0.1 + gcc-libstdc++ 10.2.0 (main development platform) on x86-64 and aarch64
  • clang version 11.0.1 + llvm-libcxx on x86-64
  • gcc 10.2.0 on x86-64
  • clang version 10.0.1 x86-64 + gcc-libstdc++ 10.2.0
  • limitied msvc19 support *

* msvc problems

  • constexpresions evaulated at compile time are causing compiler crash like one below in code block, this is really riduculus.
  • there is no eficient easy way to calculate clz at compile time and runtime whith single code, as msvc doesn't have support for constexpr clz like clang or gcc with builtin. Implementation of countl_zero in msvc c++20 is somehow like a joke, different code will be exceuted at constexpr evaluation, different at runtime ... Thats not a way I implement code. So for c++17 msvc compile mode is simply a loop constexpr from msvc stl used, or for c++20 mode when available std::countl_zero. I'm not going to write broken implementations of countl_zero that can work differently at runtime compared to compile time and thus pass all bugs uncatched in unit tests.
1>D:\projects\fixed_math\fixed_lib\include\fixedmath\unittests\addition.h(19,43): fatal error C1001: Wewnętrzny błšd kompilatora.
1 > (plik kompilatora �d : \A01\_work\2\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp�, wiersz 6887)

usage

fixed_t is typename of fixed point arithmetic type with common operators like, +, -, * ..

example

#include <fixedmath/fixed_math.hpp>
#include <fixedmath/iostream.h>

using fixedmath::fixed_t;

//fixed and all functionality is constexpr so You can declare constants see features [1]
inline constexpr fixed_t foo_constant{ fixedmath::tan( 15 * fixedmath::phi/180) };

constexpr fixed_t my_function( fixed_t value )
 {
 using namespace fixedmath;
 return foo_constant + sin(value) / (1.41_fix - 2*cos(value) / 4);
 }

// converting to/from fixed_t
// construction from other arithmetic types is explicit
fixed_t val { 3.14 };
fixed_t val { 3u };
 
//- there is no implicit assignment from other types
float some_float{};
fixed_t some_fixed{};
...
some_fixed = fixed_t{some_float};
 
//- converting to other arithmetic types coud be done with static cast and is explicit
double some_double { static_cast<double>(some_fixed) };
 
// for constant values postfix operator _fix may be used
some_fixed = some_float * 2.45_fix; //operation with float is promoted to fixed_t
some_double = 4.15 * some_fixed; //operation with double is promoted to double

std::cout << some_fixed;

unit tests

To check unit tests just #include <fixedmath/unittests/compile_time_unit_tests.h> in any of Your source file. or run them wit CMake/CTest "ninja/make test" as they are available in default CMakeConfiguration of source folder see ENABLE_UNIT_TESTS cmake feature i CMakeLists.txt

version 1.0 goals status

  • base arithemtic operations
  • sqrt - abacus algorithm using CLZ ( CLZ is available on x86 and >=armv6, aarch64) error ~<= 0.000015
  • hypot - with normalization to suitable range, to avoid overflow and underflow
  • sin, cos - error ~<= 0.0001
  • tan - error on normalized range |x| <= PI/4 ~<= 0.0001, PI/4 |x| <= PI/2 ~<=0.001 exept near 90deg ~<= 0.01
  • tan - improve calculations, limit tan__ to |x|<=pi/4
  • atan, atan2 - error ~<= 0.00005
  • asin error ~<= 0.0001
  • [ ] remove all old compat code that is compiled
  • [ ] cover all functionality with static_assert unit tests
  • support clang, gcc c++17 compilers
  • support msvc c++17 compiler, partial see notes for compilers tested in "c++ compilers"
  • performance comparisions on arm64 and x86 with float/double arithemetic
  • [ ] dual licence project with MIT and BOOST licence

future goals

  • [ ] more math functionality, if somthing is missing in library that You need post issue and let me know about that.

  • [ ] more optimisations, calculation quality enchacments

Feedback

If you think you have found a bug, please file an issue via issue submission form. Include the relevant information to reproduce the bug for example as static_assert( expression ), if it is important also the compiler version and target architecture. Feature requests and contributions can be filed as issues or pull requests.

License

This library is available to anybody free of charge, under the terms of MIT License (see LICENSE.md).

Owner
Artur Bac
C++, Modern C++ enthusiast
Artur Bac
Similar Resources

A realtime/embedded-friendly C++11 variant type which is never empty and prevents undesirable implicit conversions

strict variant Do you use boost::variant or one of the many open-source C++11 implementations of a "tagged union" or variant type in your C++ projects

Oct 10, 2022

C++ Type Traits for Smart Pointer

C++ Type Traits for Smart Pointer

SmartPointerTypeTrait C++ Type Traits for Smart Pointer is_a_pointer is_smart_pointer template typename T struct is_smart_ptr : is_smart_ptr_impl

Sep 14, 2022

A simple single header 64 and 32 bit hash function using only add, sub, ror, and xor.

A simple single header 64 and 32 bit hash function using only add, sub, ror, and xor.

K-HASH A simple single header 64 bit hash function using only add, sub, ror, and xor. This a just general-purpose hash function for like making hash m

Dec 27, 2022

LIDAR(Livox Horizon) point cloud preprocessing, including point cloud filtering and point cloud feature extraction (edge points and plane points)

LIDAR(Livox Horizon) point cloud preprocessing, including point cloud filtering and point cloud feature extraction (edge points and plane points)

LIDAR(Livox Horizon) point cloud preprocessing, including point cloud filtering and point cloud feature extraction (edge points and plane points)

Dec 28, 2022

C++ header-only fixed-point math library

fpm A C++ header-only fixed-point math library. "fpm" stands for "fixed-point math". It is designed to serve as a drop-in replacement for floating-poi

Jan 7, 2023

Phyxed is a 2D physics engine with support for fixed point math.

Phyxed is a 2D physics engine with support for fixed point math.

Oct 5, 2022

Tests to check the determinism of the basic floating point arithmetic operations on different devices, using Unity and Rust.

Tests to check the determinism of the basic floating point arithmetic operations on different devices, using Unity and Rust.

This repo contains tests to check the determinism (consistency) of the basic floating point arithmetic operations (add, subtract, multiply, divide) on

Dec 20, 2022

A library of type safe sets over fixed size collections of types or values, including methods for accessing, modifying, visiting and iterating over those.

cpp_enum_set A library of type safe sets over fixed size collections of types or values, including methods for accessing, modifying, visiting and iter

Jun 16, 2022

Apache Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation

Apache Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation

Apache Thrift Introduction Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation. Thrift provides clean a

Jan 7, 2023

A universal type for non-type template parameters for C++20 or later.

uninttp A universal type for non-type template parameters for C++20 or later. Installation: uninttp (Universal Non-Type Template Parameters) is a head

Dec 24, 2021

Cross-platform STL-styled and STL-compatible library with implementing containers, ranges, iterators, type traits and other tools; actors system; type-safe config interface.

Yato A small repository where I'm gatherting useful snippets and abstractions for C++ development. Yato includes 3 main modules: multidimensional cont

Dec 18, 2022

Strong typedef - A class template that creates a new type that is distinct from the underlying type, but convertible to and from it

jss::strong_typedef Tag, ValueType, Properties... This is an implementation of a C++17 class template that provides a wrapper type that is convertib

Dec 21, 2022

Feature-rich C99 library for memory scanning purposes, designed for Windows running machines, meant to work on both 32-bit and 64-bit portable executables. Has a modern C++ wrapper.

memscan Feature-rich C99 library for memory scanning purposes, designed for Windows running machines, meant to work on both 32-bit and 64-bit portable

Oct 2, 2022

Bit-Map is a simple bit map.

Bit-Map Bit-Map is a simple bit map. Usage Create a map uint8** bitmap; uint64 map_size = 32; // bit number pfs_create_bitmap(bitmap,

Feb 18, 2022

A video input (V4L2) to NDI converter that works with Raspberry Pi (32-bit and 64-bit), and Intel/AMD CPUs

V4L2 to NDI V4L2 to NDI is an application that connects to attached V4L2 devices and encodes the video signal into NDI It uses the NDI library, allowi

Dec 30, 2022

COBS framing with implicit run-length-encoding, optimized for data containing statistically a bit more 0 and FF bytes in a row, as data often carry 16, 32 or 64 bit numbers with small values.

TCOBS Table of Contents About The project TCOBS Specification TCOBS code Getting Started 4.1. Prerequisites 4.2. Installation 4.3. Usage in Go 4.3.1.

Nov 6, 2022
Cross-platform STL-styled and STL-compatible library with implementing containers, ranges, iterators, type traits and other tools; actors system; type-safe config interface.

Yato A small repository where I'm gatherting useful snippets and abstractions for C++ development. Yato includes 3 main modules: multidimensional cont

Dec 18, 2022
Strong typedef - A class template that creates a new type that is distinct from the underlying type, but convertible to and from it

jss::strong_typedef <Tag, ValueType, Properties...> This is an implementation of a C++17 class template that provides a wrapper type that is convertib

Dec 21, 2022
Bit-Map is a simple bit map.

Bit-Map Bit-Map is a simple bit map. Usage Create a map uint8** bitmap; uint64 map_size = 32; // bit number pfs_create_bitmap(bitmap,

Feb 18, 2022
COBS framing with implicit run-length-encoding, optimized for data containing statistically a bit more 0 and FF bytes in a row, as data often carry 16, 32 or 64 bit numbers with small values.

TCOBS Table of Contents About The project TCOBS Specification TCOBS code Getting Started 4.1. Prerequisites 4.2. Installation 4.3. Usage in Go 4.3.1.

Nov 6, 2022
TurboRC - Turbo Range Coder / Arithmetic Coding

TurboRC: Turbo Range Coder Fastest Range Coder / Arithmetic Coder 100% C (C++ headers). OS/Arch: Linux amd/inte

Dec 2, 2022
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Easy to use, header only, macro generated, generic and type-safe Data Structures in C

C Macro Collections Easy to use, header only, macro generated, generic and type-safe Data Structures in C. Table of Contents Installation Contributing

Jan 8, 2023
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).

M*LIB: Generic type-safe Container Library for C language Overview M*LIB (M star lib) is a C library enabling to use generic and type safe container i

Jan 5, 2023
Functional, Type safe, Lazy abstractions for generic iterators in C

C-Iterplus Functional abstractions for working with iterators, as demonstrated in c-iterators. Finding the longest common prefix of an array of string

Oct 18, 2022
Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union.

Eggs.Variant Introduction Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union. See the documentation at http://eggs-cpp.github.io/va

Dec 3, 2022
ring-span lite - A C++yy-like ring_span type for C++98, C++11 and later in a single-file header-only library

ring-span lite: A circular buffer view for C++98 and later Contents Example usage In a nutshell Dependencies Installation Synopsis Reported to work wi

Dec 28, 2022