libm page update
[www] / libm / index.html
index 313773e..360146e 100644 (file)
@@ -1,42 +1,26 @@
 <html><head><title>libm</title></head><body>
 <h2>libm</h2>
 
-<p>This page is about designing libm for the
-<a href="http://www.etalabs.net/musl/">musl</a> libc.
-<p>Writing the math code from scratch is a huge work
-so already existing code is used.
+<p>This page is about libm for the
+<a href="http://www.musl-libc.org/">musl</a> libc.
 
 <ul>
-<li><a href="#code">Code</a>
 <li><a href="#sources">Sources</a>
-<li><a href="#issues">Design issues</a>
+<li><a href="#rules">General rules</a>
 <li><a href="#representation">Representation</a>
 <li><a href="#ugly">Ugly</a>
 <li><a href="#implementations">libm implementations</a>
 <li><a href="#tests">libm tests</a>
 </ul>
 
-<h3><a name="code" href="#code">Code</a></h3>
-<p>musl branch for math hacks:
-<ul>
-<li><a href="/git/?p=musl">browse</a> with gitweb
-<li>over git (recommended)
-<pre>
-git clone git://nsz.repo.hu:45100/repo/musl
-</pre>
-<li>over http (slower)
-<pre>
-git clone http://nsz.repo.hu/repo/musl
-</pre>
-<li>over ssh (passwd is anon)
-<pre>
-git clone ssh://anon@nsz.repo.hu:45022/repo/musl
-</pre>
-</ul>
-
 <h3><a name="sources" href="#sources">Sources</a></h3>
-<p>The math code is mostly from freebsd which in turn
-is based on <a href="http://www.netlib.org/fdlibm/">fdlibm</a>.
+<p>Writing math code from scratch is a huge work so already existing code is
+used. Several math functions are taken from the
+<a href="http://freebsd.org/">freebsd</a> libm and a few from the
+<a href="http://openbsd.org/">openbsd</a> libm implementations.
+Both of them are based on <a href="http://www.netlib.org/fdlibm/">fdlibm</a>.
+The freebsd libm seems to be the most well maintained and most correct version
+of fdlibm.
 <p>sources:
 <ul>
 <li>freebsd /lib/msun (<a href="http://svnweb.FreeBSD.org/base/head/lib/msun/">browse</a>)
@@ -49,59 +33,100 @@ cvs -d <a href="http://openbsd.org/anoncvs.html#CVSROOT">$CVSROOT</a> get src/li
 </pre>
 </ul>
 
-<h3><a name="issues" href="#issues">Design issues</a></h3>
-
-<li>Workarounds
-<p>The bsd libm code has many workarounds for various
-compiler issues. It's not clear what's the best way
-to handle the uglyness.
-<p>In long double code fpu is assumed to be set to extended precision
-<p>Pending questions:
+<h3><a name="rules" href="#rules">General rules</a></h3>
 <ul>
-<li>Use STDC pragmas (eventhough gcc does not support them)?
-<li>Where should we use volatile (to avoid evaluation precision and const folding issues)?
-<li>What tricks to use for proper exception raising? (huge*huge, etc)
-<li>Should signaling nans be considered?
-<li>Which compiler flags should be used (-frounding-math, -ffloat-store)?
-<li>TODO: inline/macro optimization for small functions (isnan, isinf, signbit, creal, cimag,..)
-</ul>
-
-<li>Code cleanups
-<p>The fdlibm code has code style and correctness issues
-which might worth addressing.
-<p>Pending questions:
+<li>Assumption about floating-point representation and arithmetics
+(see <a href="http://port70.net/~nsz/c/c99/n1256.html#F.2">c99 annex F.2</a>):
 <ul>
-<li>Can we use a polyeval(coeffs, x) function instead of writing out the poly?
-<li>TODO: prefer 0x1p0 format over decimal one
-<li>TODO: use 1.0f instead of (float)1 and const float one = 1;
-<li>TODO: use uint32_t instead of int32_t (to avoid signed int overflow and right shifts)
-<li>TODO: prefer isnan, signbit,.. over inplace bithacks
+<li>float is ieee binary32
+<li>double is ieee binary64
+<li>long double is either ieee binary64 or little-endian 80bit extended precision (x87 fpu)
 </ul>
+(other long double representations may be supported in the future, until then
+long double math functions will be missing on non-supported platforms)
+<li>On soft-float architectures fenv should not be expected to work according to
+the c and ieee standards (ie. rounding modes and exceptions are not supported,
+the fenv functions are dummy ones)
+<li>Floating-point exception flags should be properly set in math functions
+according to c99 annex F, but without using fenv.h
+(eg. overflow flag can be raised by 0x1p900*0x1p900, because this works even
+without fenv support)
+<li>Most functions need to be precise only in nearest rounding mode.
+<li>Returned floating-point values should be correctly rounded in most cases,
+but the last bit may be wrong:
+<pre>
+    |error| &lt; 1.5 ulp
+</pre>
+should hold.
+(error is the difference between the exact result and the calculated
+floating-point value)
+(in theory correct rounding can be achieved but with big implementation cost,
+see <a href="http://lipforge.ens-lyon.fr/www/crlibm/">crlibm</a>)
+<li>At least the following functions must be correctly rounded:
+ceil, copysign, fabs, fdim, floor, fma, fmax, fmin, frexp, ldexp,
+modf, nearbyint, nextafter, nexttoward, rint, round, scalbln, scalbn,
+sqrt, trunc.
+<li>Mathematical properties of functions should be as expected
+(monotonicity, range, symmetries).
+<li>If the FPU precision is altered then nothing is guaranteed to work.
+(ie. when long double does not have full 80bit precision on i386 then things may break)
+<li>Signaling NaN is not supported
+<li>Quiet NaN is supported but all NaNs are treated equally without special
+attention to the internal representation of a NaN
+(eg. the sign of NaN may not be preserved).
+<li>Most gcc bug workarounds should be removed from the code
+(STRICT_ASSIGN macro is used when excessive precision is harmful and
+FORCE_EVAL when expressions must be evaluated for their sideeffect, other
+usage of volatile is not justified, hacks around long double constants are
+not justified eventhough gcc can miscompile those with non-default FPU setting)
+<li>Whenever fenv is accessed the FENV_ACCESS pragma of c99 should be used
+(eventhough gcc does not yet support it), and all usage of optional FE_
+macros should be protected by #ifdef
+<li>For bit manipulation of floating-point values an union should be used
+(eg. union {float f; uint32_t i;})
+<li>uint32_t and uint64_t should be used for bit manipulations.
+(eg signed int must not be used in bit shifts etc when it might invoke
+undefined or implementation defined behaviour).
+<li>POSIX namespace rules must be respected.
+<li>c99 hexfloat syntax (0x1.0p0) should be used when it makes the
+code clearer, but not in public header files
+(those should be c++ and ansi c compatible)
+<li>The 'f' suffix should be used for single precision values (0.1f) when the
+value cannot be exactly represented ((float)0.1 is not ok, that style may lead
+to double rounding issues, but eg. 1.0 or 0.5 can be used instead of 1.0f or
+0.5f)
+<li>Prefer classification macros (eg. isnan) over inplace bit hacks.
+<li>For every math function there should be a c implementation.
+(a notable exception now is sqrtl, since most fpu has instruction for it
+and on soft-float architectures long double == double)
+<li>The c implementation of a long double function should use ifdefs with the
+LDBL_MANT_DIG etc constants from float.h for architecture specific
+implementations.
+<li>In the musl source tree math.h functions go to src/math, complex.h functions
+to src/complex and fenv.h functions to src/fenv. And files are named after the
+functions they implement.
 </ul>
 
 <h3><a name="representation" href="#representation">Representation</a></h3>
 <p>
 Binary representation of floating point numbers matter
-because lots of bithacks are needed in the math code.
+because bit hacks are often needed in the math code.
 <p>
-float and double bit manipulation can be handled
-in a portable way in c:
+float and double bit manipulation can be handled in a portable way in c using
+union types:
 <ul>
-<li>float is ieee binary32
-<li>double is ieee binary64
+<li>union {float f; uint32_t i;};
+<li>union {double f; uint64_t i;};
 </ul>
-(<a href="http://repo.or.cz/w/c-standard.git/blob_plain/HEAD:/n1256.html#F.2">C99 annex F</a>
-makes these mandatory)
-<p>
-The endianness may still vary, but that can be worked
-around by using a union with a single large enough
-unsigned int. (The only exception is probably arm/oabi fpa
-where the word order of a double is not consistent with the
-endianness [<a href="http://wiki.debian.org/ArmEabiPort#ARM_floating_points">debian wiki on arm</a>],
-but we probably won't support that)
-<p>
-long double bit manipulation is harder as there are
-various representations:
+(assuming the bits in the object representation of 32bit and 64bit unsigned ints
+map to the floating-point representation according to ieee-754, this is not
+always the case, eg. old
+<a href="http://wiki.debian.org/ArmEabiPort#ARM_floating_points">arm floating-point accelerator</a>
+(FPA) used mixed endian double representation, but musl does not support the old
+arm ABI)
+<p>
+long double bit manipulation is harder as there are various representations
+and some of them don't map to any unsigned integer type:
 <ul>
 <li>ld64: long double is the same as double (ieee binary64)
 <li>ld80: 80bit extended precision format [<a href="https://en.wikipedia.org/wiki/Extended_precision">wikipedia</a>]
@@ -109,24 +134,39 @@ various representations:
 </ul>
 (and other non-standard formats are not supported)
 <p>
-ld64 is easy to handle: all long double functions
-are just wrappers around the corresponding double ones
-(aliasing is non-conformant)
+In case of ld64 the bit manipulation is the same as with double
+and all long double math functions can be just wrappers around the
+corresponding double ones.
+(using symbol aliasing on the linker level is non-conformant
+since functions would not have unique address then)
 <p>
-ld80 is the most common (i386, x86_64), it means
-64bit significand with explicit msb (inconsistent with other ieee formats),
-15bit exp, 1 sign bit.
+ld80 is the most common long double on linux (i386 and x86_64 abi),
+it means 64bit significand with explicit msb
+(inconsistent with other ieee formats), 15bit exp, 1 sign bit.
+The m68k (and m88k) architecture uses the same format, but different endianness:
+<ul>
+<li>union {long double f; struct{uint64_t m; uint16_t se; uint16_t pad;} i;}; // x86
+<li>union {long double f; struct{uint16_t se; uint16_t pad; uint64_t m;} i;}; // m68k
+</ul>
+where m is the significand and se is the sign and exponent.
 <p>
-ld128 is rare (sparc64 with software emulation), it means
-113bit significand with implicit msb, 15bit exp, 1 sign bit.
+ld128 is rare (eg. sparc64 with software emulation), it means
+113bit significand with implicit msb, 15bit exp, 1 sign bit:
+<ul>
+<li>union {long double f; struct{uint16_t se; uint16_t hi; uint32_t mid; uint64_t lo;} i;};
+</ul>
 <p>
-Endianness can vary (although on the supported i386 and x86_64 it is the same)
-and there is no large enough unsigned int to handle it.
-(In case of ld80 internal padding can vary as well, eg
-m68k and m88k cpus use different ld80 than the intel ones.)
-So there can be further variations in the binary representation
-than just ld80 and ld128.
-
+There are other non-conformant long double types: eg. the old SVR4 abi for ppc
+uses 128 bit long doubles, but it's software emulated using
+<a href="https://en.wikipedia.org/wiki/Quadruple_precision#Double-double_arithmetic">two doubles</a>.
+(the newer <a href="http://www.freescale.com/files/32bit/doc/app_note/PPCEABI.pdf">ppc eabi</a> uses ld64).
+The ibm s390 supports the ieee 754-2008 compliant binary128 floating-point
+format, but previous ibm machines (S/370, S/360) used slightly different
+representation.
+<p>
+This variation shows the difficulty to consistently handle
+long double: the solution is to use ifdefs based on float.h and
+on the endianness and write different code for different architectures.
 
 <h3><a name="ugly" href="#ugly">Ugly</a></h3>
 <p>The ugly parts of libm hacking.