Git Product home page Git Product logo

tlfloat's Introduction

TLFloat - C++ template library for floating point operations

Original distribution site : https://github.com/shibatch/tlfloat

Doxygen-generated reference : https://shibatch.sourceforge.net/tlfloat-doxygen/

Introduction

This library implements C++ classes with which half, single, double, quadruple and octuple precision IEEE 754 floating point numbers can be operated.

Internally, these classes are implemented as class templates on top of arbitrary-precision integer class templates so that the templates are expanded as arbitrary precision floating-point operations by just changing the template parameters, rather than implementing each floating-point operation for each precision. The arbitrary-precision integer class templates are also included in this library.

Features

  • Truly constexpr functions

    • Compilable with C++20 standard
    • Most of the functions are implemented as templates
      • Completely inlinable functions
      • The functions can be evaluated at compile time
    • No malloc required
    • Works without libstdc++
  • IEEE 754 compliant

    • Supports subnormal numbers, NaN, inf, and signed zero
  • Supports a wide range of precisions

    • Half, float, double, quad, and octuple precisions
    • Returns correctly rounded results for arithmetic oprations, fma and sqrt
    • Returns 1-ulp accuracy results for other math.h functions
      • All functions, including trigonometric functions, return 1ulp-accuracy results for all input range
  • Portable

    • Compatible with Linux, Windows, microcontrollers, wasm, CUDA (version 12 or later)
    • Constexpr functions can be called from CUDA devices with --expt-relaxed-constexpr compiler option
  • C language API / libquadmath emulation

    • Most of libquadmath functions can be used with x86_64 clang
  • Moderately optimized

    • Optimized for each architecture using intrinsics, etc.
    • Library design allows compilers to fully inline operations
  • Implements most of the math.h functions

    • Arithmetic operations, comparison, cast operations
    • fma, sqrt, hypot, cbrt, fmod, remainder
    • sin, cos, tan, asin, acos, atan, atan2
    • log, log2, log10, log1p, exp, exp2, exp10, expm1, pow
    • sinh, cosh, tanh, asinh, acosh, atanh
    • trunc, floor, ceil, round, rint
    • fabs, copysign, fmax, fmin, fdim
    • ldexp, frexp, modf, nextafter
    • isnan, isinf, finite, signbit
  • Implements I/O functions

    • Conversion to/from C strings
    • printf-family functions
  • The library provides BigInt template classes in addition to the FP classes

    • It provides operations for integers of artibrary length (2^N bits)
    • They can be used in the similar way to the ordinary int/uint types
    • Data formats are the same as ordinary int/uint
    • These classes are internally used to implement the FP classes in TLFloat

How to build

  1. Check out the source code from our GitHub repository : git clone https://github.com/shibatch/tlfloat

  2. Make a separate directory to create an out-of-source build : cd tlfloat && mkdir build && cd build

  3. Run cmake to configure the project : cmake .. -DCMAKE_INSTALL_PREFIX=../../install

  4. Run make to build and install the project : make && make install

Compiling hello world example

Below is a simple C++ source code utilizing TLFloat.

#include <iostream>
#include <tlfloat/tlmath.hpp>

using namespace tlfloat;

Octuple machin() {
  return 4 * (4 * atan(1 / Octuple(5)) - atan(1 / Octuple(239)));
}

int main(int argc, char **argv) {
  std::cout << to_string(machin(), 70) << std::endl;
}

To compile this source code, use the following command.

g++ -std=c++20 -I./install/include hello.cpp

You have to specify C++20 standard. Note that you do not need to link any library in this example. This program computes PI in octuple precision and shows it.

$ ./a.out
3.141592653589793238462643383279502884197169399375105820974944592307816

Libquadmath emulation

In gcc/g++ on x86_64 architecture, libquadmath provides math functions for quadruple precision floating point numbers. However, libquadmath is not available with clang or Visual Studio. By using the libquadmath emulation feature of TLFloat library, it is possible to use most of the features of libquadmath with clang and Visual Studio.

Below is a simple C source code utilizing this feature.

#include <stdio.h>
#include <stdlib.h>

#define TLFLOAT_LIBQUADMATH_EMULATION
#include "tlfloat/tlfloat.h"

int main(int argc, char **argv) {
  if (argc < 3) exit(-1);

  __float128 q1 = strtoflt128(argv[1], NULL);
  __float128 q2 = strtoflt128(argv[2], NULL);

  char str[256];
  quadmath_snprintf(str, sizeof(str), "%.30Qg", powq(q1, q2));
  puts(str);
}

To compile this source code, use the following command.

clang quad.c -I./install/include -L./install/lib -ltlfloat -lm

Below is an example of executing this program.

$ ./a.out 1.234 2.345
1.63732181977903314975233575019

In order to use the libquadmath emulation feature, define TLFLOAT_LIBQUADMATH_EMULATION macro and include tlfloat/tlfloat.h instead of quadmath.h, and link with -ltlfloat -lm. If you need portability, replace __float128 with tlfloat_quad.

Development status

  • This software is currently at the beta development stage

  • The following features will be added in future releases

    • Further documentation
    • More testing
      • Add more testers for I/O functions
    • Pre-C++20 API
    • Interoperability with SLEEF
    • Big endian support
    • Remaining math functions in math.h
    • BF16 support
    • Further optimization
    • Benchmarking tools

License

The software is distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE.txt or copy at : http://www.boost.org/LICENSE_1_0.txt.

Contributions to this project are accepted under the same license.

The fact that this software is released under an open source license only means that you can use the current version of the software for free. If you want this software to be maintained, you need to financially support the project.

Copyright Naoki Shibata 2024.

tlfloat's People

Contributors

shibatch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.