A brief history of open source libms (mathematical libraries)

 In this article, we try to provide a brief history of the C mathematical library and its various implementation (focusing on open-source variants for now).

The C libm seems to be the de facto standard for mathematical functions.

 

Libm specification ?

Some information can be found on in the ISO standards, for example in C99 standard (ISO/IEC 9899:[2011|1999|1990]) [6]. The main libm header, namely math.h is described in Section 7.2 of this standard. 

Section 5.3.4.2.2-§5 covers the characteristics of floating types. In particular it states that: 

“The accuracy of the floating-point operations (+, -, *, /) and of the library functions in and that return floating-point results is implementation defined,”. This means that there is no requirement on operation nor function accuracy: not even faithful rounding is required !


Section 6.5-§8 of standard covers the possibility for expression contraction. It states the following: 

“A floating expression may be contracted, that is, evaluated as though it were an atomic operation, thereby omitting rounding errors implied by the source code and the expression evaluation method.75) The FP_CONTRACT pragma in provides a way to disallow contracted expressions. Otherwise, whether and how expressions are contracted is implementation-defined.”

This seems to indicate that function composition simplification, a.k.a function expression contraction, is allowed.


The standard’s  Annex F 9 (which is normative) covers the list of functions and indicates which behaviours are expected in special cases.


Online libm documentations are available in [7, 8].


The libm is used as a basis for various other languages, e.g. C++ https://en.cppreference.com/w/cpp/header/cmath.



Libm implementation

Most libm implementations originate either from fdlibm or IBM’s libultim [4]


OpenLibm: (https://openlibm.org/, https://github.com/JuliaMath/openlibm )

Supported by the JuliaProject

The OpenLibm code derives from the FreeBSD msun and OpenBSD libm implementations, which in turn derive from FDLIBM 5.3. Over and above that, OpenLibm itself has received a number of patches to make it platform independent and portable.


FDLibm: Freely Distributable Libm (http://www.netlib.org/fdlibm/)

Version 5.3, developed at Sun Microsystems, Inc.


Newlib (https://sourceware.org/newlib/)

C libraries for embedded systems


CRlibm: https://gforge.inria.fr/scm/browser.php?group_id=5929&extra=crlibm

Correctly rounded library (error less or equal to half an ulp)


Glibc’s libm (https://sourceware.org/glibc/wiki/libm)

https://sourceware.org/git?p=glibc.git;a=tree;f=math;hb=HEAD


MUSL’s libm (https://git.musl-libc.org/cgit/musl/tree/src/math)


Sleef https://sleef.org/

Focus on fast SIMD implementations


APMathLibm, Libultim (https://www.math.utah.edu/cgi-bin/man2html.cgi?/usr/local/man/man3/libultim.3)


Sun Microsystems libMCR https://github.com/simonbyrne/libmcr

http://www.math.utah.edu/cgi-bin/man2html.cgi?/usr/local/man/man3/libmcr.3


The standard libm provides the most current mathematical functions. There are extension for “special” functions: Mathematical Special Functions: https://en.cppreference.com/w/cpp/numeric/special_functions


Open questions: 

How much code came from Sun Microsystem development ?

  • What are the libraries’ performances ?

  • What are the libraries’ accuracies ? [crlibm, libultim and libmcr seems to fall in the correctly rounded category]

  • What are the libraries’ specificities ?

Intel MKL is another reference (also not open source and not portable)


Bibliography