public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Who is fixing libm-ieee754?
       [not found] <199802220053.TAA29191@rabi.phys.columbia.edu>
@ 1998-02-21 17:50 ` H.J. Lu
  1998-02-21 18:55 ` A patch for libm-ieee754 H.J. Lu
  1 sibling, 0 replies; 9+ messages in thread
From: H.J. Lu @ 1998-02-21 17:50 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: drepper, libc-linux, egcs

> 
> 
> In case it helps anyone -
> 
> I ran the ELEFUNT elementary function tests against the math libraries
> from libc 5 and 6.  I don't really know how to interpret them, but
> they're at ftp://rabi.phys.columbia.edu/pub/elefunt.shar .  I used egcs
> 1.0.1's C compiler in combination with f2c from the Netlib archive.
> 
> The single precision tests probably didn't test libc6's true single
> precision math functions.
> 

I believe egcs 1.0.2 has some serious bugs on x86 when fp is
used. It miscompiled libm-ieee754 in glibc 2.1 when -O2 is used.
I will try to come up with a small test case to show it.

-- 
H.J. Lu (hjl@gnu.org)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* A patch for libm-ieee754
       [not found] <199802220053.TAA29191@rabi.phys.columbia.edu>
  1998-02-21 17:50 ` Who is fixing libm-ieee754? H.J. Lu
@ 1998-02-21 18:55 ` H.J. Lu
  1998-02-22  7:42   ` Andreas Jaeger
  1998-02-22 18:18   ` Geoffrey KEATING
  1 sibling, 2 replies; 9+ messages in thread
From: H.J. Lu @ 1998-02-21 18:55 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: egcs, GNU C Library

Hi,

It turns out those libm-ieee754 bugs are not in egcs. Here is the patch
for glibc 2.1 to fix a few libm-ieee754 bugs. Ulrich, could you please
take a look?

When you use fpu/cpu to do rounding, you have to mark the variable
volatile. Otherwise, the compiler may do some thing you don't want.

Thanks.


-- 
H.J. Lu (hjl@gnu.org)
----
Sat Feb 21 18:47:44 1998  H.J. Lu  (hjl@gnu.org)

	* sysdeps/libm-ieee754/e_exp.c (__ieee754_exp): Changed type
	of TWO43, TWO52 from float to double. Make 'n' and 't' volatile.
	Use __isinf.

	* sysdeps/libm-ieee754/e_expf.c (__ieee754_expf): Make 'n' and
	't' volatile. Use __isinff.

	* sysdeps/libm-ieee754/s_exp2.c (__ieee754_exp2): Changed type
	of TWO43 from float to double. Make "rx" volatile. Use __isinf.

	* sysdeps/libm-ieee754/s_exp2f.c (__ieee754_exp2f): Make "rx"
	volatile. Use __isinff.

Index: sysdeps/libm-ieee754/e_exp.c
===================================================================
RCS file: /home/work/cvs/gnu/glibc/sysdeps/libm-ieee754/e_exp.c,v
retrieving revision 1.1.1.2
diff -u -p -r1.1.1.2 e_exp.c
--- e_exp.c	1998/02/14 03:01:26	1.1.1.2
+++ e_exp.c	1998/02/22 02:43:20
@@ -71,14 +71,13 @@ static const volatile double TWOM1000 = 
 double
 __ieee754_exp (double x)
 {
-  static const uint32_t a_minf = 0xff800000;
   static const double himark = 709.7827128933840868;
   static const double lomark = -745.1332191019412221;
   /* Check for usual case.  */
   if (isless (x, himark) && isgreater (x, lomark))
     {
-      static const float TWO43 = 8796093022208.0;
-      static const float TWO52 = 4503599627370496.0;
+      static const double TWO43 = 8796093022208.0;
+      static const double TWO52 = 4503599627370496.0;
       /* 1/ln(2).  */
       static const double M_1_LN2 = 1.442695040888963387;
       /* ln(2), part 1 */
@@ -87,7 +86,8 @@ __ieee754_exp (double x)
       static const double M_LN2_1 = 5.497923018708371155e-14;
 
       int tval, unsafe, n_i;
-      double x22, n, t, dely, result;
+      double x22, dely, result;
+      volatile double n, t;
       union ieee754_double ex2_u, scale_u;
       fenv_t oldenv;
 
@@ -166,7 +166,7 @@ __ieee754_exp (double x)
   /* Exceptional cases:  */
   else if (isless (x, himark))
     {
-      if (x == *(const float *) &a_minf)
+      if (__isinf (x))
 	/* e^-inf == 0, with no error.  */
 	return 0;
       else
Index: sysdeps/libm-ieee754/e_expf.c
===================================================================
RCS file: /home/work/cvs/gnu/glibc/sysdeps/libm-ieee754/e_expf.c,v
retrieving revision 1.1.1.2
diff -u -p -r1.1.1.2 e_expf.c
--- e_expf.c	1998/02/14 03:01:27	1.1.1.2
+++ e_expf.c	1998/02/22 02:43:11
@@ -66,7 +66,6 @@ static const volatile float TWO127 = 1.7
 float
 __ieee754_expf (float x)
 {
-  static const uint32_t a_minf = 0xff800000;
   static const float himark = 88.72283935546875;
   static const float lomark = -103.972084045410;
   /* Check for usual case.  */
@@ -82,8 +81,10 @@ __ieee754_expf (float x)
       static const double M_LN2 = .6931471805599452862;
 
       int tval;
-      double x22, t, result, dx;
-      float n, delta;
+      double x22, result, dx;
+      volatile double t;
+      float delta;
+      volatile float n;
       union ieee754_double ex2_u;
       fenv_t oldenv;
 
@@ -144,7 +145,7 @@ __ieee754_expf (float x)
   /* Exceptional cases:  */
   else if (isless (x, himark))
     {
-      if (x == *(const float *) &a_minf)
+      if (__isinff (x))
 	/* e^-inf == 0, with no error.  */
 	return 0;
       else
Index: sysdeps/libm-ieee754/s_exp2.c
===================================================================
RCS file: /home/work/cvs/gnu/glibc/sysdeps/libm-ieee754/s_exp2.c,v
retrieving revision 1.1.1.3
diff -u -p -r1.1.1.3 s_exp2.c
--- s_exp2.c	1998/02/14 03:01:32	1.1.1.3
+++ s_exp2.c	1998/02/22 02:43:24
@@ -42,16 +42,16 @@ static const volatile double TWOM1000 = 
 double
 __ieee754_exp2 (double x)
 {
-  static const uint32_t a_minf = 0xff800000;
   static const double himark = (double) DBL_MAX_EXP;
   static const double lomark = (double) (DBL_MIN_EXP - DBL_MANT_DIG - 1) - 1.0;
 
   /* Check for usual case.  */
   if (isless (x, himark) && isgreater (x, lomark))
     {
-      static const float TWO43 = 8796093022208.0;
+      static const double TWO43 = 8796093022208.0;
       int tval, unsafe;
-      double rx, x22, result;
+      double x22, result;
+      volatile double rx;
       union ieee754_double ex2_u, scale_u;
       fenv_t oldenv;
 
@@ -125,7 +125,7 @@ __ieee754_exp2 (double x)
   /* Exceptional cases:  */
   else if (isless (x, himark))
     {
-      if (x == *(const float *) &a_minf)
+      if (__isinf (x))
 	/* e^-inf == 0, with no error.  */
 	return 0;
       else
Index: sysdeps/libm-ieee754/s_exp2f.c
===================================================================
RCS file: /home/work/cvs/gnu/glibc/sysdeps/libm-ieee754/s_exp2f.c,v
retrieving revision 1.1.1.4
diff -u -p -r1.1.1.4 s_exp2f.c
--- s_exp2f.c	1998/02/14 03:01:33	1.1.1.4
+++ s_exp2f.c	1998/02/22 02:43:16
@@ -43,7 +43,6 @@ static const volatile float TWO127 = 1.7
 float
 __ieee754_exp2f (float x)
 {
-  static const uint32_t a_minf = 0xff800000;
   static const float himark = (float) FLT_MAX_EXP;
   static const float lomark = (float) (FLT_MIN_EXP - FLT_MANT_DIG - 1) - 1.0;
 
@@ -52,7 +51,8 @@ __ieee754_exp2f (float x)
     {
       static const float TWO15 = 32768.0;
       int tval, unsafe;
-      float rx, x22, result;
+      float x22, result;
+      volatile float rx;
       union ieee754_float ex2_u, scale_u;
       fenv_t oldenv;
 
@@ -123,7 +123,7 @@ __ieee754_exp2f (float x)
   /* Exceptional cases:  */
   else if (isless (x, himark))
     {
-      if (x == *(const float *) &a_minf)
+      if (__isinff (x))
 	/* e^-inf == 0, with no error.  */
 	return 0;
       else

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-21 18:55 ` A patch for libm-ieee754 H.J. Lu
@ 1998-02-22  7:42   ` Andreas Jaeger
  1998-02-22 13:03     ` Toon Moene
  1998-02-22 18:18   ` Geoffrey KEATING
  1 sibling, 1 reply; 9+ messages in thread
From: Andreas Jaeger @ 1998-02-22  7:42 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Zack Weinberg, egcs, GNU C Library

>>>>> H J Lu writes:

 > Hi,
 > It turns out those libm-ieee754 bugs are not in egcs. Here is the patch
 > for glibc 2.1 to fix a few libm-ieee754 bugs. Ulrich, could you please
 > take a look?

I've added all your math patches (and removed isless/islessequal i486
macros) and it's really an improvement (results for float and double
are appended below)!  The functions you've fixed (and those dependend
on the correct results of e.g. exp) seem to be ok now (according to
libm-test).

scalb and gamma fail for some values.  The other results are just
rounding errors, we should change the epsilons in libm-test for those
results.  I'm appending a patch for BUGS (Uli, please add this to
2.1).

Btw. none of those functions are in glibc 2.0.x - log2 and exp2 are
new and exp has been rewritten recently.  These are problems only in
the development tree.

Andreas

1998-02-22  Andreas Jaeger  <aj@arthur.rhein-neckar.de>

	* BUGS: log2 and asin should be ok now, removed those entries and
	added entries for scalb and gamma.

Index: BUGS
===================================================================
RCS file: /egcs/carton/cvsfiles/libc/BUGS,v
retrieving revision 1.20
diff -u -r1.20 BUGS
--- BUGS	1998/02/20 15:03:12	1.20
+++ BUGS	1998/02/22 14:48:35
@@ -27,8 +27,6 @@
 
 [ **]  There are problems with signal handling when using LinuxThreads.
 
-[ **]  The libm-ieee `log2' function seems to be very inaccurate.
-
 [  *]  The precision of the `sinhl' and/or `asinhl' function do not seem
        to be the best.
 
@@ -55,8 +53,12 @@
        checked for errors, but the whole file containing the same
        category.
        [PR libc/207]
+
+[  *]  The libm-ieee `gamma' function gives wrong results (at least for
+       -0.5).
 
-[  *]  The libm-ieee `asin' function gives wrong results (at least for 0.5).
+[  *]  The libm-ieee `scalb' function gives wrong results for
+       non-integral second parameters. 
 
 [  *]  _IO_getline can loop forever, at least with C++
        [PR libc/332]

===================================================================
Results of test-double and test-float:

testing float (without inline functions)
Fail: asin (0.7) ==  0.775397496...
Result:
 is:          7.75397598743438720703e-01   0x1.8d00ea00000000000000p-1
 should be:   7.75397479534149169922e-01   0x1.8d00e600000000000000p-1
 difference:  1.19209289550781250000e-07   0x1.00000000000000000000p-23
Fail: asinh(0.7) == 0.652666566...
Result:
 is:          6.52666509151458740234e-01   0x1.4e2a4e00000000000000p-1
 should be:   6.52666568756103515625e-01   0x1.4e2a5000000000000000p-1
 difference:  5.96046447753906250000e-08   0x1.00000000000000000000p-24
Fail: tanh (0.7) == 0.6043677771...
Result:
 is:          6.04367733001708984375e-01   0x1.356fb000000000000000p-1
 should be:   6.04367792606353759766e-01   0x1.356fb200000000000000p-1
 difference:  5.96046447753906250000e-08   0x1.00000000000000000000p-24
Fail: scalb (2, 0.5) == NaN
 Value:  2.00000000000000000000e+00   0x1.00000000000000000000p+1
Fail: scalb (3, -2.5) == NaN
 Value:  7.50000000000000000000e-01   0x1.80000000000000000000p-1
Fail: gamma (-0.5) == -2*sqrt(pi): Exception "Invalid operation" set
Fail: gamma (-0.5) == -2*sqrt(pi)
Result:
 is:          nan   nan
 should be:  -3.54490780830383300781e+00  -0x1.c5bf8a00000000000000p+1
 difference:  nan   nan
Fail: imag(ctanh(0.7 + i 1.2)) == -0.47786...
Result:
 is:          4.77864027023315429688e-01   0x1.e9553000000000000000p-2
 should be:   4.77864116430282592773e-01   0x1.e9553600000000000000p-2
 difference:  8.94069671630859375000e-08   0x1.80000000000000000000p-24

Test suite completed:
  2146 test cases plus 2134 tests for exception flags executed.
  8 errors occured.


testing double (without inline functions)
Fail: scalb (2, 0.5) == NaN
 Value:  2.00000000000000000000e+00   0x1.00000000000000000000p+1
Fail: scalb (3, -2.5) == NaN
 Value:  7.50000000000000000000e-01   0x1.80000000000000000000p-1
Fail: gamma (-0.5) == -2*sqrt(pi): Exception "Invalid operation" set
Fail: gamma (-0.5) == -2*sqrt(pi)
Result:
 is:          nan   nan
 should be:  -3.54490770181103176384e+00  -0x1.c5bf891b4ef6a0000000p+1
 difference:  nan   nan
Fail: real(cexp(0.7 + i 1.2)) == 0.72969...
Result:
 is:          7.29698909150323760109e-01   0x1.759b186d747cf0000000p-1
 should be:   7.29698909150323649087e-01   0x1.759b186d747ce0000000p-1
 difference:  1.11022302462515654042e-16   0x1.00000000000000000000p-53
Fail: real(ctanh(0.7 + i 1.2)) == 1.34721...
Result:
 is:          1.34721973990611898486e+00   0x1.58e364936c22e0000000p+0
 should be:   1.34721973990611920691e+00   0x1.58e364936c22f0000000p+0
 difference:  2.22044604925031308085e-16   0x1.00000000000000000000p-52

Test suite completed:
  2031 test cases plus 2019 tests for exception flags executed.
  6 errors occured.

-- 
 Andreas Jaeger   aj@arthur.rhein-neckar.de    jaeger@informatik.uni-kl.de
  for pgp-key finger ajaeger@alma.student.uni-kl.de

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-22  7:42   ` Andreas Jaeger
@ 1998-02-22 13:03     ` Toon Moene
  1998-02-22 13:33       ` Ulrich Drepper
  1998-02-22 13:42       ` Andreas Jaeger
  0 siblings, 2 replies; 9+ messages in thread
From: Toon Moene @ 1998-02-22 13:03 UTC (permalink / raw)
  To: Andreas Jaeger; +Cc: egcs, GNU C Library

Andreas wrote:

>>>>> H J Lu writes:

 > Hi,
 > It turns out those libm-ieee754 bugs are not in egcs. Here is  
the patch
 > for glibc 2.1 to fix a few libm-ieee754 bugs. Ulrich, could you please
 > take a look?

>  I've added all your math patches (and removed
>  isless/islessequal i486 macros) and it's really an
>  improvement

Andreas, would it be useful to mobilise some people to help do the  
math related stuff in glibc-2.x ?

I might be able to stirr some interest in the group that did  
`enquire.c' originally (CWI - het Centrum voor Wiskunde en  
Informatica in Amsterdam).

If you think that would be useful, could you outline the current  
problem areas in glibc ?

TIA,
Toon.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-22 13:03     ` Toon Moene
@ 1998-02-22 13:33       ` Ulrich Drepper
  1998-02-22 13:42       ` Andreas Jaeger
  1 sibling, 0 replies; 9+ messages in thread
From: Ulrich Drepper @ 1998-02-22 13:33 UTC (permalink / raw)
  To: Toon Moene; +Cc: Andreas Jaeger, egcs, GNU C Library

Toon Moene <toon@moene.indiv.nluug.nl> writes:

> Andreas, would it be useful to mobilise some people to help do the  
> math related stuff in glibc-2.x ?
> 
> I might be able to stirr some interest in the group that did  
> `enquire.c' originally (CWI - het Centrum voor Wiskunde en  
> Informatica in Amsterdam).

Actually I think that for glibc 2.1 we should just fix the bugs which
were located.  For one it is too late now to make major changes in
libm and second, for glibc 2.2 or 2.3 we need to think about a new
organization for the libm since we have to support long double not
only on ix86 and m68k but also on the new architectures which use
different formats (128 bit).

We would like to see tests for the libm precision any time and I would
appreciate if somebody could activate people with the necessary
knowledge.

-- Uli
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-22 13:03     ` Toon Moene
  1998-02-22 13:33       ` Ulrich Drepper
@ 1998-02-22 13:42       ` Andreas Jaeger
  1 sibling, 0 replies; 9+ messages in thread
From: Andreas Jaeger @ 1998-02-22 13:42 UTC (permalink / raw)
  To: Toon Moene; +Cc: egcs, GNU C Library

>>>>> Toon Moene writes:

Toon> Andreas wrote:
>>>>> H J Lu writes:

>> Hi,
>> It turns out those libm-ieee754 bugs are not in egcs. Here is  
Toon> the patch
>> for glibc 2.1 to fix a few libm-ieee754 bugs. Ulrich, could you please
>> take a look?

>> I've added all your math patches (and removed
>> isless/islessequal i486 macros) and it's really an
>> improvement

Toon> Andreas, would it be useful to mobilise some people to help do the  
Toon> math related stuff in glibc-2.x ?
It would be better to ask Ulrich Drepper for details since he should
know the situation better than I do.  He might add some comments to
the following personal view of the issue.

Toon> I might be able to stirr some interest in the group that did  
Toon> `enquire.c' originally (CWI - het Centrum voor Wiskunde en  
Toon> Informatica in Amsterdam).

Toon> If you think that would be useful, could you outline the current  
Toon> problem areas in glibc ?
The problems I know of are:
- The libm-ieee `gamma' function needs to be reimplemented. At the
  moment the function just calls exp(lgamma) :-(.
- A number of functions need to be implemented as `long double'
  versions, I'm appending below the document README.libm which gives a 
  description.
- The libm-ieee `scalb' function gives wrong results for
  non-integral second parameters (Please note that scalb is defined as
  scalb (double x, double n)).
- We've got a test suite that just tests some single values (only 2152
  at the moment :-), e.g. behaviour for NaN, Inf, 0, some defined
  properties (e.g. sin (0), exp(1)) and some random values.  The
  current libm passes all tests.  I hope that we caught most of the
  problems - but better tests might be added.  For some functions
  (exp, exp2, sin, cos) the taylor polynomials are calculated with GNU
  MP for a range of number and checked against the functions in libm.
  It might be a good idea to add also such tests for other functions.
- Implement optimized complex math functions in assembler.  Uli has
  implemented most of these functions in C and states:
>     Beside this most of the complex math functions which are new in
>     ISO C 9X should be improved.  Writing some of them in assembler is
>     useful to exploit the parallelism which often is available.


If you want to start you might take the current glibc 2.1 snapshot as
basis since it contains the full ISO C9X set of math functions and
also more tests than glibc 2.0.x.  

Andreas

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
README.libm:
The following functions for the `long double' versions of the libm
function have to be written:

e_acosl.c
e_asinl.c
e_atan2l.c
e_expl.c
e_fmodl.c
e_hypotl.c
e_j0l.c
e_j1l.c
e_jnl.c
e_lgammal_r.c
e_logl.c
e_log10l.c
e_powl.c
e_rem_pio2l.c
e_sinhl.c
e_sqrtl.c

k_cosl.c
k_rem_pio2l.c
k_sinl.c
k_tanl.c

s_atanl.c
s_erfl.c
s_expm1l.c
s_log1pl.c

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			       Methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arcsin
~~~~~~
 *	Since  asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
 *	we approximate asin(x) on [0,0.5] by
 *		asin(x) = x + x*x^2*R(x^2)
 *	where
 *		R(x^2) is a rational approximation of (asin(x)-x)/x^3
 *	and its remez error is bounded by
 *		|(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
 *
 *	For x in [0.5,1]
 *		asin(x) = pi/2-2*asin(sqrt((1-x)/2))
 *	Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
 *	then for x>0.98
 *		asin(x) = pi/2 - 2*(s+s*z*R(z))
 *			= pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
 *	For x<=0.98, let pio4_hi = pio2_hi/2, then
 *		f = hi part of s;
 *		c = sqrt(z) - f = (z-f*f)/(s+f) 	...f+c=sqrt(z)
 *	and
 *		asin(x) = pi/2 - 2*(s+s*z*R(z))
 *			= pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
 *			= pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arccos
~~~~~~
 * Method :
 *	acos(x)  = pi/2 - asin(x)
 *	acos(-x) = pi/2 + asin(x)
 * For |x|<=0.5
 *	acos(x) = pi/2 - (x + x*x^2*R(x^2))	(see asin.c)
 * For x>0.5
 * 	acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
 *		= 2asin(sqrt((1-x)/2))
 *		= 2s + 2s*z*R(z) 	...z=(1-x)/2, s=sqrt(z)
 *		= 2f + (2c + 2s*z*R(z))
 *     where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
 *     for f so that f+c ~ sqrt(z).
 * For x<-0.5
 *	acos(x) = pi - 2asin(sqrt((1-|x|)/2))
 *		= pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
atan2
~~~~~
 * Method :
 *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
 *	2. Reduce x to positive by (if x and y are unexceptional):
 *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
 *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
atan
~~~~
 * Method
 *   1. Reduce x to positive by atan(x) = -atan(-x).
 *   2. According to the integer k=4t+0.25 chopped, t=x, the argument
 *      is further reduced to one of the following intervals and the
 *      arctangent of t is evaluated by the corresponding formula:
 *
 *      [0,7/16]      atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
 *      [7/16,11/16]  atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
 *      [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
 *      [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
 *      [39/16,INF]   atan(x) = atan(INF) + atan( -1/t )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exp
~~~
 * Method
 *   1. Argument reduction:
 *      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
 *	Given x, find r and integer k such that
 *
 *               x = k*ln2 + r,  |r| <= 0.5*ln2.
 *
 *      Here r will be represented as r = hi-lo for better
 *	accuracy.
 *
 *   2. Approximation of exp(r) by a special rational function on
 *	the interval [0,0.34658]:
 *	Write
 *	    R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
 *      We use a special Reme algorithm on [0,0.34658] to generate
 * 	a polynomial of degree 5 to approximate R. The maximum error
 *	of this polynomial approximation is bounded by 2**-59. In
 *	other words,
 *	    R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
 *  	(where z=r*r, and the values of P1 to P5 are listed below)
 *	and
 *	    |                  5          |     -59
 *	    | 2.0+P1*z+...+P5*z   -  R(z) | <= 2
 *	    |                             |
 *	The computation of exp(r) thus becomes
 *                             2*r
 *		exp(r) = 1 + -------
 *		              R - r
 *                                 r*R1(r)
 *		       = 1 + r + ----------- (for better accuracy)
 *		                  2 - R1(r)
 *	where
 *			         2       4             10
 *		R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
 *
 *   3. Scale back to obtain exp(x):
 *	From step 1, we have
 *	   exp(x) = 2^k * exp(r)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hypot
~~~~~
 *	If (assume round-to-nearest) z=x*x+y*y
 *	has error less than sqrt(2)/2 ulp, than
 *	sqrt(z) has error less than 1 ulp (exercise).
 *
 *	So, compute sqrt(x*x+y*y) with some care as
 *	follows to get the error below 1 ulp:
 *
 *	Assume x>y>0;
 *	(if possible, set rounding to round-to-nearest)
 *	1. if x > 2y  use
 *		x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
 *	where x1 = x with lower 32 bits cleared, x2 = x-x1; else
 *	2. if x <= 2y use
 *		t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
 *	where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
 *	y1= y with lower 32 bits chopped, y2 = y-y1.
 *
 *	NOTE: scaling may be necessary if some argument is too
 *	      large or too tiny
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
j0/y0
~~~~~
 * Method -- j0(x):
 *	1. For tiny x, we use j0(x) = 1 - x^2/4 + x^4/64 - ...
 *	2. Reduce x to |x| since j0(x)=j0(-x),  and
 *	   for x in (0,2)
 *		j0(x) = 1-z/4+ z^2*R0/S0,  where z = x*x;
 *	   (precision:  |j0-1+z/4-z^2R0/S0 |<2**-63.67 )
 *	   for x in (2,inf)
 * 		j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)-q0(x)*sin(x0))
 * 	   where x0 = x-pi/4. It is better to compute sin(x0),cos(x0)
 *	   as follow:
 *		cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4)
 *			= 1/sqrt(2) * (cos(x) + sin(x))
 *		sin(x0) = sin(x)cos(pi/4)-cos(x)sin(pi/4)
 *			= 1/sqrt(2) * (sin(x) - cos(x))
 * 	   (To avoid cancellation, use
 *		sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
 * 	    to compute the worse one.)
 *
 * Method -- y0(x):
 *	1. For x<2.
 *	   Since
 *		y0(x) = 2/pi*(j0(x)*(ln(x/2)+Euler) + x^2/4 - ...)
 *	   therefore y0(x)-2/pi*j0(x)*ln(x) is an even function.
 *	   We use the following function to approximate y0,
 *		y0(x) = U(z)/V(z) + (2/pi)*(j0(x)*ln(x)), z= x^2
 *	   where
 *		U(z) = u00 + u01*z + ... + u06*z^6
 *		V(z) = 1  + v01*z + ... + v04*z^4
 *	   with absolute approximation error bounded by 2**-72.
 *	   Note: For tiny x, U/V = u0 and j0(x)~1, hence
 *		y0(tiny) = u0 + (2/pi)*ln(tiny), (choose tiny<2**-27)
 *	2. For x>=2.
 * 		y0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)+q0(x)*sin(x0))
 * 	   where x0 = x-pi/4. It is better to compute sin(x0),cos(x0)
 *	   by the method mentioned above.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
j1/y1
~~~~~
 * Method -- j1(x):
 *	1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ...
 *	2. Reduce x to |x| since j1(x)=-j1(-x),  and
 *	   for x in (0,2)
 *		j1(x) = x/2 + x*z*R0/S0,  where z = x*x;
 *	   (precision:  |j1/x - 1/2 - R0/S0 |<2**-61.51 )
 *	   for x in (2,inf)
 * 		j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1))
 * 		y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
 * 	   where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
 *	   as follow:
 *		cos(x1) =  cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
 *			=  1/sqrt(2) * (sin(x) - cos(x))
 *		sin(x1) =  sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
 *			= -1/sqrt(2) * (sin(x) + cos(x))
 * 	   (To avoid cancellation, use
 *		sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
 * 	    to compute the worse one.)
 *
 * Method -- y1(x):
 *	1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN
 *	2. For x<2.
 *	   Since
 *		y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...)
 *	   therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function.
 *	   We use the following function to approximate y1,
 *		y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2
 *	   where for x in [0,2] (abs err less than 2**-65.89)
 *		U(z) = U0[0] + U0[1]*z + ... + U0[4]*z^4
 *		V(z) = 1  + v0[0]*z + ... + v0[4]*z^5
 *	   Note: For tiny x, 1/x dominate y1 and hence
 *		y1(tiny) = -2/pi/tiny, (choose tiny<2**-54)
 *	3. For x>=2.
 * 		y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
 * 	   where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
 *	   by method mentioned above.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jn/yn
~~~~~
 * Note 2. About jn(n,x), yn(n,x)
 *	For n=0, j0(x) is called,
 *	for n=1, j1(x) is called,
 *	for n<x, forward recursion us used starting
 *	from values of j0(x) and j1(x).
 *	for n>x, a continued fraction approximation to
 *	j(n,x)/j(n-1,x) is evaluated and then backward
 *	recursion is used starting from a supposed value
 *	for j(n,x). The resulting value of j(0,x) is
 *	compared with the actual value to correct the
 *	supposed value of j(n,x).
 *
 *	yn(n,x) is similar in all respects, except
 *	that forward recursion is used for all
 *	values of n>1.

jn:
    /* (x >> n**2)
     *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
     *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
     *	    Let s=sin(x), c=cos(x),
     *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
     *
     *		   n	sin(xn)*sqt2	cos(xn)*sqt2
     *		----------------------------------
     *		   0	 s-c		 c+s
     *		   1	-s-c 		-c+s
     *		   2	-s+c		-c-s
     *		   3	 s+c		 c-s
...
    /* x is tiny, return the first Taylor expansion of J(n,x)
     * J(n,x) = 1/n!*(x/2)^n  - ...
...
		/* use backward recurrence */
		/* 			x      x^2      x^2
		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
		 *			2n  - 2(n+1) - 2(n+2)
		 *
		 * 			1      1        1
		 *  (for large x)   =  ----  ------   ------   .....
		 *			2n   2(n+1)   2(n+2)
		 *			-- - ------ - ------ -
		 *			 x     x         x
		 *
		 * Let w = 2n/x and h=2/x, then the above quotient
		 * is equal to the continued fraction:
		 *		    1
		 *	= -----------------------
		 *		       1
		 *	   w - -----------------
		 *			  1
		 * 	        w+h - ---------
		 *		       w+2h - ...
		 *
		 * To determine how many terms needed, let
		 * Q(0) = w, Q(1) = w(w+h) - 1,
		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
		 * When Q(k) > 1e4	good for single
		 * When Q(k) > 1e9	good for double
		 * When Q(k) > 1e17	good for quadruple

...
		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
		 *  Hence, if n*(log(2n/x)) > ...
		 *  single 8.8722839355e+01
		 *  double 7.09782712893383973096e+02
		 *  long double 1.1356523406294143949491931077970765006170e+04
		 *  then recurrent value may overflow and the result is
		 *  likely underflow to zero

yn:
    /* (x >> n**2)
     *	    Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
     *	    Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
     *	    Let s=sin(x), c=cos(x),
     *		xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
     *
     *		   n	sin(xn)*sqt2	cos(xn)*sqt2
     *		----------------------------------
     *		   0	 s-c		 c+s
     *		   1	-s-c 		-c+s
     *		   2	-s+c		-c-s
     *		   3	 s+c		 c-s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lgamma
~~~~~~
 * Method:
 *   1. Argument Reduction for 0 < x <= 8
 * 	Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
 * 	reduce x to a number in [1.5,2.5] by
 * 		lgamma(1+s) = log(s) + lgamma(s)
 *	for example,
 *		lgamma(7.3) = log(6.3) + lgamma(6.3)
 *			    = log(6.3*5.3) + lgamma(5.3)
 *			    = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
 *   2. Polynomial approximation of lgamma around its
 *	minimun ymin=1.461632144968362245 to maintain monotonicity.
 *	On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
 *		Let z = x-ymin;
 *		lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
 *	where
 *		poly(z) is a 14 degree polynomial.
 *   2. Rational approximation in the primary interval [2,3]
 *	We use the following approximation:
 *		s = x-2.0;
 *		lgamma(x) = 0.5*s + s*P(s)/Q(s)
 *	with accuracy
 *		|P/Q - (lgamma(x)-0.5s)| < 2**-61.71
 *	Our algorithms are based on the following observation
 *
 *                             zeta(2)-1    2    zeta(3)-1    3
 * lgamma(2+s) = s*(1-Euler) + --------- * s  -  --------- * s  + ...
 *                                 2                 3
 *
 *	where Euler = 0.5771... is the Euler constant, which is very
 *	close to 0.5.
 *
 *   3. For x>=8, we have
 *	lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
 *	(better formula:
 *	   lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
 *	Let z = 1/x, then we approximation
 *		f(z) = lgamma(x) - (x-0.5)(log(x)-1)
 *	by
 *	  			    3       5             11
 *		w = w0 + w1*z + w2*z  + w3*z  + ... + w6*z
 *	where
 *		|w - f(z)| < 2**-58.74
 *
 *   4. For negative x, since (G is gamma function)
 *		-x*G(-x)*G(x) = pi/sin(pi*x),
 * 	we have
 * 		G(x) = pi/(sin(pi*x)*(-x)*G(-x))
 *	since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
 *	Hence, for x<0, signgam = sign(sin(pi*x)) and
 *		lgamma(x) = log(|Gamma(x)|)
 *			  = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
 *	Note: one should avoid compute pi*(-x) directly in the
 *	      computation of sin(pi*(-x)).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log
~~~
 * Method :
 *   1. Argument Reduction: find k and f such that
 *			x = 2^k * (1+f),
 *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
 *
 *   2. Approximation of log(1+f).
 *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
 *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
 *	     	 = 2s + s*R
 *      We use a special Reme algorithm on [0,0.1716] to generate
 * 	a polynomial of degree 14 to approximate R The maximum error
 *	of this polynomial approximation is bounded by 2**-58.45. In
 *	other words,
 *		        2      4      6      8      10      12      14
 *	    R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s  +Lg6*s  +Lg7*s
 *  	(the values of Lg1 to Lg7 are listed in the program)
 *	and
 *	    |      2          14          |     -58.45
 *	    | Lg1*s +...+Lg7*s    -  R(z) | <= 2
 *	    |                             |
 *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
 *	In order to guarantee error in log below 1ulp, we compute log
 *	by
 *		log(1+f) = f - s*(f - R)	(if f is not too large)
 *		log(1+f) = f - (hfsq - s*(hfsq+R)).	(better accuracy)
 *
 *	3. Finally,  log(x) = k*ln2 + log(1+f).
 *			    = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
 *	   Here ln2 is split into two floating point number:
 *			ln2_hi + ln2_lo,
 *	   where n*ln2_hi is always exact for |n| < 2000.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log10
~~~~~
 * Method :
 *	Let log10_2hi = leading 40 bits of log10(2) and
 *	    log10_2lo = log10(2) - log10_2hi,
 *	    ivln10   = 1/log(10) rounded.
 *	Then
 *		n = ilogb(x),
 *		if(n<0)  n = n+1;
 *		x = scalbn(x,-n);
 *		log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
 *
 * Note 1:
 *	To guarantee log10(10**n)=n, where 10**n is normal, the rounding
 *	mode must set to Round-to-Nearest.
 * Note 2:
 *	[1/log(10)] rounded to 53 bits has error  .198   ulps;
 *	log10 is monotonic at all binary break points.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pow
~~~
 * Method:  Let x =  2   * (1+f)
 *	1. Compute and return log2(x) in two pieces:
 *		log2(x) = w1 + w2,
 *	   where w1 has 53-24 = 29 bit trailing zeros.
 *	2. Perform y*log2(x) = n+y' by simulating muti-precision
 *	   arithmetic, where |y'|<=0.5.
 *	3. Return x**y = 2**n*exp(y'*log2)
 *
 * Special cases:
 *	1.  (anything) ** 0  is 1
 *	2.  (anything) ** 1  is itself
 *	3.  (anything) ** NAN is NAN
 *	4.  NAN ** (anything except 0) is NAN
 *	5.  +-(|x| > 1) **  +INF is +INF
 *	6.  +-(|x| > 1) **  -INF is +0
 *	7.  +-(|x| < 1) **  +INF is +0
 *	8.  +-(|x| < 1) **  -INF is +INF
 *	9.  +-1         ** +-INF is NAN
 *	10. +0 ** (+anything except 0, NAN)               is +0
 *	11. -0 ** (+anything except 0, NAN, odd integer)  is +0
 *	12. +0 ** (-anything except 0, NAN)               is +INF
 *	13. -0 ** (-anything except 0, NAN, odd integer)  is +INF
 *	14. -0 ** (odd integer) = -( +0 ** (odd integer) )
 *	15. +INF ** (+anything except 0,NAN) is +INF
 *	16. +INF ** (-anything except 0,NAN) is +0
 *	17. -INF ** (anything)  = -0 ** (-anything)
 *	18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
 *	19. (-anything except 0 and inf) ** (non-integer) is NAN
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem_pio2	return the remainder of x rem pi/2 in y[0]+y[1]
~~~~~~~~
This is one of the basic functions which is written with highest accuracy
in mind.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sinh
~~~~
 * Method :
 * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
 *	1. Replace x by |x| (sinh(-x) = -sinh(x)).
 *	2.
 *		                                    E + E/(E+1)
 *	    0        <= x <= 22     :  sinh(x) := --------------, E=expm1(x)
 *			       			        2
 *
 *	    22       <= x <= lnovft :  sinh(x) := exp(x)/2
 *	    lnovft   <= x <= ln2ovft:  sinh(x) := exp(x/2)/2 * exp(x/2)
 *	    ln2ovft  <  x	    :  sinh(x) := x*shuge (overflow)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sqrt
~~~~
 * Method:
 *   Bit by bit method using integer arithmetic. (Slow, but portable)
 *   1. Normalization
 *	Scale x to y in [1,4) with even powers of 2:
 *	find an integer k such that  1 <= (y=x*2^(2k)) < 4, then
 *		sqrt(x) = 2^k * sqrt(y)
 *   2. Bit by bit computation
 *	Let q  = sqrt(y) truncated to i bit after binary point (q = 1),
 *	     i							 0
 *                                     i+1         2
 *	    s  = 2*q , and	y  =  2   * ( y - q  ).		(1)
 *	     i      i            i                 i
 *
 *	To compute q    from q , one checks whether
 *		    i+1       i
 *
 *			      -(i+1) 2
 *			(q + 2      ) <= y.			(2)
 *     			  i
 *							      -(i+1)
 *	If (2) is false, then q   = q ; otherwise q   = q  + 2      .
 *		 	       i+1   i             i+1   i
 *
 *	With some algebric manipulation, it is not difficult to see
 *	that (2) is equivalent to
 *                             -(i+1)
 *			s  +  2       <= y			(3)
 *			 i                i
 *
 *	The advantage of (3) is that s  and y  can be computed by
 *				      i      i
 *	the following recurrence formula:
 *	    if (3) is false
 *
 *	    s     =  s  ,	y    = y   ;			(4)
 *	     i+1      i		 i+1    i
 *
 *	    otherwise,
 *                         -i                     -(i+1)
 *	    s	  =  s  + 2  ,  y    = y  -  s  - 2  		(5)
 *           i+1      i          i+1    i     i
 *
 *	One may easily use induction to prove (4) and (5).
 *	Note. Since the left hand side of (3) contain only i+2 bits,
 *	      it does not necessary to do a full (53-bit) comparison
 *	      in (3).
 *   3. Final rounding
 *	After generating the 53 bits result, we compute one more bit.
 *	Together with the remainder, we can decide whether the
 *	result is exact, bigger than 1/2ulp, or less than 1/2ulp
 *	(it will never equal to 1/2ulp).
 *	The rounding mode can be detected by checking whether
 *	huge + tiny is equal to huge, and whether huge - tiny is
 *	equal to huge for some floating point number "huge" and "tiny".
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cos
~~~
 * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
 * Input x is assumed to be bounded by ~pi/4 in magnitude.
 * Input y is the tail of x.
 *
 * Algorithm
 *	1. Since cos(-x) = cos(x), we need only to consider positive x.
 *	2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
 *	3. cos(x) is approximated by a polynomial of degree 14 on
 *	   [0,pi/4]
 *		  	                 4            14
 *	   	cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
 *	   where the remez error is
 *
 * 	|              2     4     6     8     10    12     14 |     -58
 * 	|cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  )| <= 2
 * 	|    					               |
 *
 * 	               4     6     8     10    12     14
 *	4. let r = C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  , then
 *	       cos(x) = 1 - x*x/2 + r
 *	   since cos(x+y) ~ cos(x) - sin(x)*y
 *			  ~ cos(x) - x*y,
 *	   a correction term is necessary in cos(x) and hence
 *		cos(x+y) = 1 - (x*x/2 - (r - x*y))
 *	   For better accuracy when x > 0.3, let qx = |x|/4 with
 *	   the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
 *	   Then
 *		cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
 *	   Note that 1-qx and (x*x/2-qx) is EXACT here, and the
 *	   magnitude of the latter is at least a quarter of x*x/2,
 *	   thus, reducing the rounding error in the subtraction.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sin
~~~
 * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
 * Input x is assumed to be bounded by ~pi/4 in magnitude.
 * Input y is the tail of x.
 * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
 *
 * Algorithm
 *	1. Since sin(-x) = -sin(x), we need only to consider positive x.
 *	2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
 *	3. sin(x) is approximated by a polynomial of degree 13 on
 *	   [0,pi/4]
 *		  	         3            13
 *	   	sin(x) ~ x + S1*x + ... + S6*x
 *	   where
 *
 * 	|sin(x)         2     4     6     8     10     12  |     -58
 * 	|----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x  +S6*x   )| <= 2
 * 	|  x 					           |
 *
 *	4. sin(x+y) = sin(x) + sin'(x')*y
 *		    ~ sin(x) + (1-x*x/2)*y
 *	   For better accuracy, let
 *		     3      2      2      2      2
 *		r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
 *	   then                   3    2
 *		sin(x) = x + (S1*x + (x *(r-y/2)+y))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tan
~~~
 * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
 * Input x is assumed to be bounded by ~pi/4 in magnitude.
 * Input y is the tail of x.
 * Input k indicates whether tan (if k=1) or
 * -1/tan (if k= -1) is returned.
 *
 * Algorithm
 *	1. Since tan(-x) = -tan(x), we need only to consider positive x.
 *	2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0.
 *	3. tan(x) is approximated by a odd polynomial of degree 27 on
 *	   [0,0.67434]
 *		  	         3             27
 *	   	tan(x) ~ x + T1*x + ... + T13*x
 *	   where
 *
 * 	        |tan(x)         2     4            26   |     -59.2
 * 	        |----- - (1+T1*x +T2*x +.... +T13*x    )| <= 2
 * 	        |  x 					|
 *
 *	   Note: tan(x+y) = tan(x) + tan'(x)*y
 *		          ~ tan(x) + (1+x*x)*y
 *	   Therefore, for better accuracy in computing tan(x+y), let
 *		     3      2      2       2       2
 *		r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
 *	   then
 *		 		    3    2
 *		tan(x+y) = x + (T1*x + (x *(r+y)+y))
 *
 *      4. For x in [0.67434,pi/4],  let y = pi/4 - x, then
 *		tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
 *		       = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
atan
~~~~
 * Method
 *   1. Reduce x to positive by atan(x) = -atan(-x).
 *   2. According to the integer k=4t+0.25 chopped, t=x, the argument
 *      is further reduced to one of the following intervals and the
 *      arctangent of t is evaluated by the corresponding formula:
 *
 *      [0,7/16]      atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
 *      [7/16,11/16]  atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
 *      [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
 *      [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
 *      [39/16,INF]   atan(x) = atan(INF) + atan( -1/t )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
erf
~~~
 *			     x
 *		      2      |\
 *     erf(x)  =  ---------  | exp(-t*t)dt
 *	 	   sqrt(pi) \|
 *			     0
 *
 *     erfc(x) =  1-erf(x)
 *  Note that
 *		erf(-x) = -erf(x)
 *		erfc(-x) = 2 - erfc(x)
 *
 * Method:
 *	1. For |x| in [0, 0.84375]
 *	    erf(x)  = x + x*R(x^2)
 *          erfc(x) = 1 - erf(x)           if x in [-.84375,0.25]
 *                  = 0.5 + ((0.5-x)-x*R)  if x in [0.25,0.84375]
 *	   where R = P/Q where P is an odd poly of degree 8 and
 *	   Q is an odd poly of degree 10.
 *						 -57.90
 *			| R - (erf(x)-x)/x | <= 2
 *
 *
 *	   Remark. The formula is derived by noting
 *          erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
 *	   and that
 *          2/sqrt(pi) = 1.128379167095512573896158903121545171688
 *	   is close to one. The interval is chosen because the fix
 *	   point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
 *	   near 0.6174), and by some experiment, 0.84375 is chosen to
 * 	   guarantee the error is less than one ulp for erf.
 *
 *      2. For |x| in [0.84375,1.25], let s = |x| - 1, and
 *         c = 0.84506291151 rounded to single (24 bits)
 *         	erf(x)  = sign(x) * (c  + P1(s)/Q1(s))
 *         	erfc(x) = (1-c)  - P1(s)/Q1(s) if x > 0
 *			  1+(c+P1(s)/Q1(s))    if x < 0
 *         	|P1/Q1 - (erf(|x|)-c)| <= 2**-59.06
 *	   Remark: here we use the taylor series expansion at x=1.
 *		erf(1+s) = erf(1) + s*Poly(s)
 *			 = 0.845.. + P1(s)/Q1(s)
 *	   That is, we use rational approximation to approximate
 *			erf(1+s) - (c = (single)0.84506291151)
 *	   Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
 *	   where
 *		P1(s) = degree 6 poly in s
 *		Q1(s) = degree 6 poly in s
 *
 *      3. For x in [1.25,1/0.35(~2.857143)],
 *         	erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1)
 *         	erf(x)  = 1 - erfc(x)
 *	   where
 *		R1(z) = degree 7 poly in z, (z=1/x^2)
 *		S1(z) = degree 8 poly in z
 *
 *      4. For x in [1/0.35,28]
 *         	erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0
 *			= 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6<x<0
 *			= 2.0 - tiny		(if x <= -6)
 *         	erf(x)  = sign(x)*(1.0 - erfc(x)) if x < 6, else
 *         	erf(x)  = sign(x)*(1.0 - tiny)
 *	   where
 *		R2(z) = degree 6 poly in z, (z=1/x^2)
 *		S2(z) = degree 7 poly in z
 *
 *      Note1:
 *	   To compute exp(-x*x-0.5625+R/S), let s be a single
 *	   precision number and s := x; then
 *		-x*x = -s*s + (s-x)*(s+x)
 *	        exp(-x*x-0.5626+R/S) =
 *			exp(-s*s-0.5625)*exp((s-x)*(s+x)+R/S);
 *      Note2:
 *	   Here 4 and 5 make use of the asymptotic series
 *			  exp(-x*x)
 *		erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) )
 *			  x*sqrt(pi)
 *	   We use rational approximation to approximate
 *      	g(s)=f(1/x^2) = log(erfc(x)*x) - x*x + 0.5625
 *	   Here is the error bound for R1/S1 and R2/S2
 *      	|R1/S1 - f(x)|  < 2**(-62.57)
 *      	|R2/S2 - f(x)|  < 2**(-61.52)
 *
 *      5. For inf > x >= 28
 *         	erf(x)  = sign(x) *(1 - tiny)  (raise inexact)
 *         	erfc(x) = tiny*tiny (raise underflow) if x > 0
 *			= 2 - tiny if x<0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
expm1	Returns exp(x)-1, the exponential of x minus 1
~~~~~
 * Method
 *   1. Argument reduction:
 *	Given x, find r and integer k such that
 *
 *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
 *
 *      Here a correction term c will be computed to compensate
 *	the error in r when rounded to a floating-point number.
 *
 *   2. Approximating expm1(r) by a special rational function on
 *	the interval [0,0.34658]:
 *	Since
 *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
 *	we define R1(r*r) by
 *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
 *	That is,
 *	    R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
 *		     = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
 *		     = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
 *      We use a special Reme algorithm on [0,0.347] to generate
 * 	a polynomial of degree 5 in r*r to approximate R1. The
 *	maximum error of this polynomial approximation is bounded
 *	by 2**-61. In other words,
 *	    R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
 *	where 	Q1  =  -1.6666666666666567384E-2,
 * 		Q2  =   3.9682539681370365873E-4,
 * 		Q3  =  -9.9206344733435987357E-6,
 * 		Q4  =   2.5051361420808517002E-7,
 * 		Q5  =  -6.2843505682382617102E-9;
 *  	(where z=r*r, and the values of Q1 to Q5 are listed below)
 *	with error bounded by
 *	    |                  5           |     -61
 *	    | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
 *	    |                              |
 *
 *	expm1(r) = exp(r)-1 is then computed by the following
 * 	specific way which minimize the accumulation rounding error:
 *			       2     3
 *			      r     r    [ 3 - (R1 + R1*r/2)  ]
 *	      expm1(r) = r + --- + --- * [--------------------]
 *		              2     2    [ 6 - r*(3 - R1*r/2) ]
 *
 *	To compensate the error in the argument reduction, we use
 *		expm1(r+c) = expm1(r) + c + expm1(r)*c
 *			   ~ expm1(r) + c + r*c
 *	Thus c+r*c will be added in as the correction terms for
 *	expm1(r+c). Now rearrange the term to avoid optimization
 * 	screw up:
 *		        (      2                                    2 )
 *		        ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
 *	 expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
 *	                ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
 *                      (                                             )
 *
 *		   = r - E
 *   3. Scale back to obtain expm1(x):
 *	From step 1, we have
 *	   expm1(x) = either 2^k*[expm1(r)+1] - 1
 *		    = or     2^k*[expm1(r) + (1-2^-k)]
 *   4. Implementation notes:
 *	(A). To save one multiplication, we scale the coefficient Qi
 *	     to Qi*2^i, and replace z by (x^2)/2.
 *	(B). To achieve maximum accuracy, we compute expm1(x) by
 *	  (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
 *	  (ii)  if k=0, return r-E
 *	  (iii) if k=-1, return 0.5*(r-E)-0.5
 *        (iv)	if k=1 if r < -0.25, return 2*((r+0.5)- E)
 *	       	       else	     return  1.0+2.0*(r-E);
 *	  (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
 *	  (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
 *	  (vii) return 2^k(1-((E+2^-k)-r))
 *
 * Special cases:
 *	expm1(INF) is INF, expm1(NaN) is NaN;
 *	expm1(-INF) is -1, and
 *	for finite argument, only expm1(0)=0 is exact.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log1p
~~~~~
 * Method :
 *   1. Argument Reduction: find k and f such that
 *			1+x = 2^k * (1+f),
 *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
 *
 *      Note. If k=0, then f=x is exact. However, if k!=0, then f
 *	may not be representable exactly. In that case, a correction
 *	term is need. Let u=1+x rounded. Let c = (1+x)-u, then
 *	log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
 *	and add back the correction term c/u.
 *	(Note: when x > 2**53, one can simply return log(x))
 *
 *   2. Approximation of log1p(f).
 *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
 *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
 *	     	 = 2s + s*R
 *      We use a special Reme algorithm on [0,0.1716] to generate
 * 	a polynomial of degree 14 to approximate R The maximum error
 *	of this polynomial approximation is bounded by 2**-58.45. In
 *	other words,
 *		        2      4      6      8      10      12      14
 *	    R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
 *  	(the values of Lp1 to Lp7 are listed in the program)
 *	and
 *	    |      2          14          |     -58.45
 *	    | Lp1*s +...+Lp7*s    -  R(z) | <= 2
 *	    |                             |
 *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
 *	In order to guarantee error in log below 1ulp, we compute log
 *	by
 *		log1p(f) = f - (hfsq - s*(hfsq+R)).
 *
 *	3. Finally, log1p(x) = k*ln2 + log1p(f).
 *		 	     = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
 *	   Here ln2 is split into two floating point number:
 *			ln2_hi + ln2_lo,
 *	   where n*ln2_hi is always exact for |n| < 2000.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- 
 Andreas Jaeger   aj@arthur.rhein-neckar.de    jaeger@informatik.uni-kl.de
  for pgp-key finger ajaeger@alma.student.uni-kl.de

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-21 18:55 ` A patch for libm-ieee754 H.J. Lu
  1998-02-22  7:42   ` Andreas Jaeger
@ 1998-02-22 18:18   ` Geoffrey KEATING
  1998-02-22 18:39     ` David Edelsohn
  1 sibling, 1 reply; 9+ messages in thread
From: Geoffrey KEATING @ 1998-02-22 18:18 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Zack Weinberg, egcs, GNU C Library

> From: hjl@lucon.org (H.J. Lu)
> Date: Sat, 21 Feb 1998 18:55:18 -0800 (PST)

> It turns out those libm-ieee754 bugs are not in egcs. Here is the patch
> for glibc 2.1 to fix a few libm-ieee754 bugs. Ulrich, could you please
> take a look?
> 
> When you use fpu/cpu to do rounding, you have to mark the variable
> volatile. Otherwise, the compiler may do some thing you don't want.

libm-ieee754 expects ieee754 behaviour, and in particular that when a
value is stored to a 'double' variable or returned from a function of
type 'double', then that value is representable as a 'double' (and
similarly for 'float').  In fact, some functions basically do nothing
but invoke the FPU's rounder (like s_rint.c).

To get this behaviour with gcc, you may need to use the -ffloat-store
flag:


For most programs, the excess precision does only good, but a few
programs rely on the precise definition of IEEE floating point.  Use
`-ffloat-store' for such programs.


Do _not_ mark the variables 'volatile'.  This causes a performance
reduction on sparc and powerpc, and probably on alpha and x86 too,
because of the memory traffic it generates.

--
Geoff Keating <Geoff.Keating@anu.edu.au>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-22 18:18   ` Geoffrey KEATING
@ 1998-02-22 18:39     ` David Edelsohn
  1998-02-22 19:30       ` Geoffrey KEATING
  0 siblings, 1 reply; 9+ messages in thread
From: David Edelsohn @ 1998-02-22 18:39 UTC (permalink / raw)
  To: Geoffrey KEATING; +Cc: H.J. Lu, Zack Weinberg, egcs, GNU C Library

>>>>> Geoffrey KEATING writes:

Geoff> Do _not_ mark the variables 'volatile'.  This causes a performance
Geoff> reduction on sparc and powerpc, and probably on alpha and x86 too,
Geoff> because of the memory traffic it generates.

	On PowerPC you probably also need -mno-fused-madd option to
disable use of the fma, etc. instructions because of the intermediate
extended precision.  Note that this will halve FP performance in some
cases. 

David

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A patch for libm-ieee754
  1998-02-22 18:39     ` David Edelsohn
@ 1998-02-22 19:30       ` Geoffrey KEATING
  0 siblings, 0 replies; 9+ messages in thread
From: Geoffrey KEATING @ 1998-02-22 19:30 UTC (permalink / raw)
  To: David Edelsohn; +Cc: H.J. Lu, Zack Weinberg, egcs, GNU C Library

> Date: Sun, 22 Feb 1998 21:37:55 -0500
> From: David Edelsohn <dje@watson.ibm.com>

> 	On PowerPC you probably also need -mno-fused-madd option to
> disable use of the fma, etc. instructions because of the intermediate
> extended precision.  Note that this will halve FP performance in some
> cases. 

This is not necessary.

The libm doesn't mind if _expressions_ have extra precision, the
important thing is that _variables_ have the right precision.

In the same way, I expect

unsigned char x;
unsigned char y=0xff, z = 1;

x = y + z;
assert(x == 0);

to have the assertion succeed; I don't expect the compiler to decide
"you said 'unsigned char', but it'll actually behave like an 'unsigned
int'".

[I hope that gcc won't combine

double x,y,z,r;
r = x*y;
r = r+z;

into a fused multiply-add!]


Besides, I test these routines on PPC, and I make sure they do the
right thing :-).  I don't think anyone had tried running them on x86
before, because the 387 FPU has most of these routines in hardware.

--
Geoff Keating <Geoff.Keating@anu.edu.au>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~1998-02-22 19:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <199802220053.TAA29191@rabi.phys.columbia.edu>
1998-02-21 17:50 ` Who is fixing libm-ieee754? H.J. Lu
1998-02-21 18:55 ` A patch for libm-ieee754 H.J. Lu
1998-02-22  7:42   ` Andreas Jaeger
1998-02-22 13:03     ` Toon Moene
1998-02-22 13:33       ` Ulrich Drepper
1998-02-22 13:42       ` Andreas Jaeger
1998-02-22 18:18   ` Geoffrey KEATING
1998-02-22 18:39     ` David Edelsohn
1998-02-22 19:30       ` Geoffrey KEATING

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).