Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 29 May 2013 09:24:41 -0700
From:      Steve Kargl <sgk@troutmask.apl.washington.edu>
To:        Bruce Evans <brde@optusnet.com.au>
Cc:        freebsd-numerics@freebsd.org
Subject:   Re: Patches for s_expl.c
Message-ID:  <20130529162441.GA58773@troutmask.apl.washington.edu>
In-Reply-To: <20130529062437.V4648@besplex.bde.org>
References:  <20130528172242.GA51485@troutmask.apl.washington.edu> <20130529062437.V4648@besplex.bde.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, May 29, 2013 at 07:39:04AM +1000, Bruce Evans wrote:
> On Tue, 28 May 2013, Steve Kargl wrote:
> 
> > Here are two patches for ld80/s_expl.c and ld128/s_expl.c.
> > Instead of committing the one large patch that I have spent
> > hours testing, I have split it into two.  One patch fixes/updates
> > expl().  The other patch is the implementation of expm1l().
> >
> > My commit messages will be:
> >
> > Patch 1:
> >
> >   ld80/s_expl.c:
> >
> >   * Use the LOG2_INTERVALS macro instead of hardcoding 7.
> 
> The use of LOG2_INTERVALS isn't merged into the ld128 version.  Patch 2
> merges its use for expm1l() only.

Hopefully, fixed.

> >   * Use LD80C to set overflow and underflow thresholds, and then use
> >     #defines to access the .e component to reduce diffs with ld128 version.
> >   * Rename polynomial coefficients P# to A#, which is used in Tang.
> 
> Almost all the declarations polynomial coefficients are still formatted
> in a nonstandard way, but differently than in previous development
> versions.  I keep sending you patches for this.

Hopefully, fixed.  All fancy whitespace has been removed including
in comments with hex values.

> >   * Compute expm1l(x) for IEEE 754 128-bit format.
> 
> There is a fairly large bug in this, from only merging half of the
> most recent micro-optimization in the development version of the ld80
> version.  This might only be an efficiency bug, but I haven't tested
> the ld128 version with either the full merge or the half merge.
> 
> The ld128 version still has excessive optimizations for |x| near 0.
> It uses a slightly different high-degree polynomial on each side of
> 0.  The ld80 version uses the same poly on each side.  Most of the
> style bugs in the 4 exp[!2]l functions are in the coeffs for the
> polys on each side.  I haven't tried so hard to get you to fix them
> since I want to remove them.

Hopefully, fixed to the extent that opened ld80/s_expl.c in one
nedit window and ld128/s_expl.c in another.  I copied everything
from ld80 to ld128 except of course literal constants and 
polynomials that must be different.

> >
> >   These are based on:
> >
> >   PTP Tang, "Table-driven implementation of the Expm1 function
> >   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 18,
> >   211-222 (1992).
> >
> > These commit logs may be too terse for some, but quite frankly after
> > 2 or 3 years of submitting and resubmitting diffs, I've forgotten
> > why some changes have or have not been made.
> >
> > expm1l() resides in s_expl.c because she shares the same table,
> > polynomial coefficients, and some numerical constants with expl().
> 
> There are some minor style regressions relative to previous development
> versions outside of poly coeffs.  Patches later.

I'm sure you're going to hate the new patch at the end.

> > Index: ld80/s_expl.c
> > ===================================================================
> > --- ld80/s_expl.c	(revision 251062)
> > +++ ld80/s_expl.c	(working copy)
> > ...
> > @@ -78,11 +82,11 @@
> >  * |exp(x) - p(x)| < 2**-77.2
> >  * (0.002708 is ln2/(2*INTERVALS) rounded up a little).
> >  */
> > -P2 =  0.5,
> > -P3 =  1.6666666666666119e-1,		/*  0x15555555555490.0p-55 */
> > -P4 =  4.1666666666665887e-2,		/*  0x155555555554e5.0p-57 */
> > -P5 =  8.3333354987869413e-3,		/*  0x1111115b789919.0p-59 */
> > -P6 =  1.3888891738560272e-3;		/*  0x16c16c651633ae.0p-62 */
> > +A2  = 0.5,
> > +A3  = 1.6666666666666119e-1,		/*  0x15555555555490.0p-55 */
> > +A4  = 4.1666666666665887e-2,		/*  0x155555555554e5.0p-57 */
> > +A5  = 8.3333354987869413e-3,		/*  0x1111115b789919.0p-59 */
> > +A6  = 1.3888891738560272e-3;		/*  0x16c16c651633ae.0p-62 */
> 
> Example of a formatting regression.  The extra space that was before the
> values is for a possible minus sign.  This space is still there for the
> hex values.  The extra space before the equals sign is used for fancy
> formatting to line up the values when the variable names reach A10.  Since
> thee variable names only reach A6, this is not needed.

All coefficient are now formatted with the form:

A6 = 1.3888891738560272e-3;       /* 0x16c16c651633ae.0p-62 */

ie., 1 space before and 1 space after =.  The space in the comments
for the implicit + sign has been removed.

> > Index: ld128/s_expl.c
> > ===================================================================
> > --- ld128/s_expl.c	(revision 251062)
> > +++ ld128/s_expl.c	(working copy)
> > ...
> > @@ -38,34 +40,56 @@
> > #include "math_private.h"
> >
> > #define	INTERVALS	128
> > +#define	LOG2_INTERVALS	7
> 
> Not used.

Hopefully, fixed.

> > 	n2 = (unsigned)n % INTERVALS;
> > 	k = (n - n2) / INTERVALS;
> > 	r1 = x - fn * L1;
> > -	r2 = -fn * L2;
> > +	r2 = fn * -L2;
> > +	r = r1 + r2;
> 
> 1 micro-optimization (that uses LOG2_INTERVALS) not merrged here.
> 

Hopefully, fixed.

> > ...
> > +	if (k > LDBL_MANT_DIG - 1)
> > +		t = s[n2].lo - twomk + t * (q + r1) + s[n2].hi;
> > +	else
> > +		t = s[n2].lo + t * (q + r1)  + (s[n2].hi - twomk);
> 
> The last statement isn't accurate enough for k = 0 and k = -1, so
> handling of those cases were moved earlier so that this statement
> could be optimized to what it is now.  The ld128 version is missing
> this.

ld80 code merged into ld128.

> > --- ld128/s_expl.c	2013-05-28 09:36:11.000000000 -0700
> > +++ ld128/s_expl.c.all	2013-05-28 09:34:52.000000000 -0700
> > ...
> > +	if (k == 0) {
> > +		t = s[n2].lo * (r1 + 1) + t * q + s[n2].hi * r1 +
> > +		    (s[n2].hi - 1);
> > +		RETURNI(t);
> > +	}
> > +
> > +	if (k == -1) {
> > +		t = s[n2].lo * (r1 + 1) + t * q + s[n2].hi * r1 +
> > +		    (s[n2].hi - 2);
> > +		RETURNI(t / 2);
> > +	}
> > +
> > +
> 
> Same as for ld808, except for 2 style bugs instead of 1 (1 more extra
> blank line).

Hopefully, fixed.

> > +	if (k > LDBL_MANT_DIG - 1)
> > +		t = s[n2].lo - twomk + t * (q + r1) + s[n2].hi;
> > +	else if (k < 1)
> > +		t = s[n2].lo * (r1 + 1) + t * q + s[n2].hi * r1 +
> > +		   (s[n2].hi - twomk);
> > +	else
> > +		t = s[n2].lo * (q + r1 + 1) + s[n2].hi * (q + r1) +
> > +		    (s[n2].hi - twomk);
> 
> Not the same as for ld128.  Still has the old slower code, so it probably
> still works, but even more slowly than before except for k == 0 and k == -1,
> since there are extra branches to filter out those values.

ld80 and ld128 now use identical code.

> 
> Some patches relative to my version now instead of later:
> 
> @ --- z22/s_expl.c	Wed May 29 04:48:10 2013
> @ +++ ./s_expl.c	Wed May 29 06:16:29 2013
> @ @@ -30,5 +30,5 @@
> @  __FBSDID("$FreeBSD: src/lib/msun/ld80/s_expl.c,v 1.10 2012/10/13 19:53:11 kargl Exp $");
> @ 
> @ -/*-
> @ +/**
> @   * Compute the exponential of x for Intel 80-bit format.  This is based on:
> @   *
> 
> This ugliness is now required by style(9) :-(.  You only made this change in
> some places places.

Hopefully, fixed.

> @ @@ -83,9 +83,9 @@
> @   * (0.002708 is ln2/(2*INTERVALS) rounded up a little).
> @   */
> @ -A2  = 0.5,
> @ -A3  = 1.6666666666666119e-1,		/*  0x15555555555490.0p-55 */
> @ -A4  = 4.1666666666665887e-2,		/*  0x155555555554e5.0p-57 */
> @ -A5  = 8.3333354987869413e-3,		/*  0x1111115b789919.0p-59 */
> @ -A6  = 1.3888891738560272e-3;		/*  0x16c16c651633ae.0p-62 */
> @ +A2 =  0.5,
> @ +A3 =  1.6666666666666119e-1,		/*  0x15555555555490.0p-55 */
> @ +A4 =  4.1666666666665887e-2,		/*  0x155555555554e5.0p-57 */
> @ +A5 =  8.3333354987869413e-3,		/*  0x1111115b789919.0p-59 */
> @ +A6 =  1.3888891738560272e-3;		/*  0x16c16c651633ae.0p-62 */
> @ 
> @  /*
> 
> Fix regressions relative to a previous development version.

I made this conform to style(9).

> @ @@ -267,11 +275,12 @@
> @  	r = x - fn * L1 - fn * L2;	/* r = r1 + r2 done independently. */
> @  #if defined(HAVE_EFFICIENT_IRINTL)
> @ -	n  = irintl(fn);
> @ +	n = irintl(fn);
> @  #elif defined(HAVE_EFFICIENT_IRINT)
> @ -	n  = irint(fn);
> @ +	n = irint(fn);
> @  #else
> @ -	n  = (int)fn;
> @ +	n = (int)fn;
> 
> Fix more regressions.

Hopefully, fixed.

> @  #endif
> @  	n2 = (unsigned)n % INTERVALS;
> @ +	/* Depend on the sign bit being propagated: */
> @  	k = n >> LOG2_INTERVALS;
> @  	r1 = x - fn * L1;
> 
> I think a comment is needed.  This micro-optimization was merged from
> s_exp2*.c, where it is commented on more prominently for the long
> double versions only.

Ignored adding a comment.

> 
> The coeffs have lots of style bugs, though not as many as for ld128.
> 

Hopefully, fixed.

> @ @@ -389,4 +409,9 @@
> @  		x4 = x2 * x2;
> @  		q = x4 * (x2 * (x4 *
> @ +		    /*
> @ +		     * XXX the number of terms is no longer good for
> @ +		     * pairwise grouping of all except B3, and the
> @ +		     * grouping is no longer from highest down.
> @ +		     */
> @  		    (x2 *            B12  + (x * B11 + B10)) +
> @  		    (x2 * (x * B9 +  B8) +  (x * B7 +  B6))) +

I left this as-is with whitespace and did not add the comment.
This should be the only place where there is a substantial
deviation from style(9).

> @ @@ -407,9 +432,9 @@
> @  	fn = x * INV_L + 0x1.8p63 - 0x1.8p63;
> @  #if defined(HAVE_EFFICIENT_IRINTL)
> @ -	n  = irintl(fn);
> @ +	n = irintl(fn);
> @  #elif defined(HAVE_EFFICIENT_IRINT)
> @ -	n  = irint(fn);
> @ +	n = irint(fn);
> @  #else
> @ -	n  = (int)fn;
> @ +	n = (int)fn;
> @  #endif

Hopefully, fixed.

> @  	n2 = (unsigned)n % INTERVALS;
> @ @@ -434,22 +459,21 @@
> @ 
> @  	if (k == 0) {
> @ -		t = s[n2].lo * (r1 + 1) + t * q + s[n2].hi * r1 +
> @ -		    (s[n2].hi - 1);
> @ +		t = SUM2P(s[n2].hi - 1, s[n2].lo * (r1 + 1) + t * q +
> @ +		    s[n2].hi * r1);
> @  		RETURNI(t);
> @  	}
> @ -
> 
> Style bug (extra blank line between related statements).

Hopefully, fixed.

> 
> @  	if (k == -1) {
> @ -		t = s[n2].lo * (r1 + 1) + t * q + s[n2].hi * r1 + 
> @ -		    (s[n2].hi - 2);
> @ +		t = SUM2P(s[n2].hi - 2, s[n2].lo * (r1 + 1) + t * q +
> @ +		    s[n2].hi * r1);
> @  		RETURNI(t / 2);
> @  	}
> @
> 
> This blank line is correct since the statements are unrelated -- the
> evaluation method changes significantly.  For k = 0 and k = -1, the
> evaluation is the same but we repeat it all to avoid using a variable
> for (k - 1) for the 2 values of k.
> 
> @  	if (k < -7) {
> @ -		t = s[n2].lo + t * (q + r1) + s[n2].hi;
> @ +		t = SUM2P(s[n2].hi, s[n2].lo + t * (q + r1));
> @  		RETURNI(t * twopk - 1);
> @  	}
> @ 
> @  	if (k > 2 * LDBL_MANT_DIG - 1) {
> @ -		t = s[n2].lo + t * (q + r1) + s[n2].hi;
> @ +		t = SUM2P(s[n2].hi, s[n2].lo + t * (q + r1));
> @  		if (k == LDBL_MAX_EXP)
> @  			RETURNI(t * 2 * 0x1p16383L - 1);
> 
> Ignore all the other changes in this hunk.

After making the changes, current unscientific testing gives
(best viewed in a 95 column window):

expl

Timing:
                               1M        2M       10M       100M
i386    [-11355.0:11356.0]   0.088302           0.867567   8.64871
amd64   [-11355.0:11356.0]   0.062994           0.631960   6.30295
sparc64 [-11355.0:11356.0]  39.5309    79.1927

Accuracy:
                             M    Max ULP      x at Max ULP
i386    [-11355.0:11356.0]   1   0.50465  -3.5510383760383760e+03 -0x1.bbe13a6062b8cdd4p+11
i386    [-11355.0:11356.0]  10   0.50556  -9.6479456830945683e+03 -0x1.2d7f90c24c5c686p+13
i386    [-11355.0:11356.0] 100   0.50654  -7.9982712426427124e+03 -0x1.f3e45702867bb01p+12
amd64   [-11355.0:11356.0]   1   0.50465  -3.5510383760383760e+03 -0x1.bbe13a6062b8cdd4p+11
amd64   [-11355.0:11356.0]  10   0.50556  -9.6479456830945683e+03 -0x1.2d7f90c24c5c686p+13
amd64   [-11355.0:11356.0] 100   0.50654  -7.9982712426427124e+03 -0x1.f3e45702867bb01p+12
sparc64 [-11355.0:11356.0]   1   0.50619  1.79779355979355979355979355979355983e+03
sparc64 {-11355.0:11356.0]   2   0.50541  1.11496704618352309176154588077294027e+04


expm1l
 
Timing:
                             1M          10M        100M
i386    [-64.0000:-0.1659]   0.435783   4.342621  43.41397
i386    [ -0.1659: 0.1659]   0.082880   0.829142   8.28948
i386    [  0.1659:11356.0]   0.110590   1.096098  10.96253
amd64   [-64.0000:-0.1659]   0.066751   0.648734   6.46649
amd64   [ -0.1659: 0.1659]   0.061531   0.614824   6.14377
amd64   [  0.1659:11356.0]   0.071677   0.716927   7.16819
sparc64 [-113.000:-0.1659]  37.84224
sparc64 [ -0.1659: 0.1659]  66.28533
sparc64 [  0.1659:11356.0]  41.20714
 
Accuracy:
                            M   Max ULP      x at Max ULP
i386    [-64.0000:-0.1659]   1   0.50824  -1.7579429539429599e-01 -0x1.6806d6ec55bd2cp-3
i386    [ -0.1659: 0.1659]   1   0.50807   1.5765476175476175e-01  0x1.42e07fee5cecaa04p-3
i386    [  0.1659:11356.0]   1   0.50533   4.6558240641420642e+03  0x1.22fd2f5de1bf8cb2p+12
i386    [-64.0000:-0.1659]  10   0.51163  -1.8666523480652408e-01 -0x1.7e4a57b65a7cp-3
i386    [ -0.1659: 0.1659]  10   0.51031  -1.6139564864956486e-01 -0x1.4a89cd45552be4a8p-3
i386    [  0.1659:11356.0]  10   0.50597   7.2029609713952472e+03  0x1.c22f60238aafa618p+12
i386    [-64.0000:-0.1659] 100   0.51520  -1.8119337383093434e-01 -0x1.731582f6d89b72p-3
i386    [ -0.1659: 0.1659] 100   0.51161   1.6120475455904754e-01  0x1.4a25b7e6539760ecp-3
i386    [  0.1659:11356.0] 100   0.50645   1.5581592136564341e+03  0x1.858a308e79dd8494p+10

amd64   [-64.0000:-0.1659]   1   0.50502  -1.8115636515636515e-01 -0x1.73021bbe7877ccp-3
amd64   [ -0.1659: 0.1659]   1   0.50807   1.5765476175476175e-01  0x1.42e07fee5cecaa04p-3
amd64   [  0.1659:11356.0]   1   0.50522   5.3732636683514684e+03  0x1.4fd437fc4e28bfb6p+12
amd64   [-64.0000:-0.1659]  10   0.51363  -1.7086629347662934e-01 -0x1.5def25b3c452dap-3
amd64   [ -0.1659: 0.1659]  10   0.51031  -1.6139564864956486e-01 -0x1.4a89cd45552be4a8p-3
amd64   [  0.1659:11356.0]  10   0.50595   2.2495034322503431e-01  0x1.ccb2c3fb0104dbe4p-3
amd64   [-64.0000:-0.1659] 100   0.51376  -2.7335577165055771e-01 -0x1.17ea934da5e086p-2
amd64   [ -0.1659: 0.1659] 100   0.51161   1.6120475455904754e-01  0x1.4a25b7e6539760ecp-3
amd64   [  0.1659:11356.0] 100   0.50662   3.9436528827225188e+02  0x1.8a5d83883eef2676p+8

sparc64 [-113.000:-0.1659]   1   0.50339  -4.89331501511501510727132103685011835e+00
sparc64 [  -0.1659:0.1659]   1   0.50837  -1.28120218820218813976976441251060453e-01
sparc64 [   0.1659:11356.]   1   0.50514   6.45515777662077662077313264157127259e+03

Testing on flame is excrudiating slow especially because rdivacky
is building clang.  Yes, the following is one massive patch. 

-- 
Steve

Index: ld80/s_expl.c
===================================================================
--- ld80/s_expl.c	(revision 251067)
+++ ld80/s_expl.c	(working copy)
@@ -29,7 +29,7 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-/*-
+/**
  * Compute the exponential of x for Intel 80-bit format.  This is based on:
  *
  *   PTP Tang, "Table-driven implementation of the exponential function
@@ -50,6 +50,7 @@
 #include "math_private.h"
 
 #define	INTERVALS	128
+#define	LOG2_INTERVALS	7
 #define	BIAS	(LDBL_MAX_EXP - 1)
 
 static const long double
@@ -60,9 +61,12 @@
 
 static const union IEEEl2bits
 /* log(2**16384 - 0.5) rounded towards zero: */
-o_threshold = LD80C(0xb17217f7d1cf79ab, 13,  11356.5234062941439488L),
+/* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
+o_thresholdu = LD80C(0xb17217f7d1cf79ab, 13, 11356.5234062941439488L),
+#define o_threshold	 (o_thresholdu.e)
 /* log(2**(-16381-64-1)) rounded towards zero: */
-u_threshold = LD80C(0xb21dfe7f09e2baa9, 13, -11399.4985314888605581L);
+u_thresholdu = LD80C(0xb21dfe7f09e2baa9, 13, -11399.4985314888605581L);
+#define u_threshold	 (u_thresholdu.e)
 
 static const double
 /*
@@ -70,19 +74,19 @@
  * have at least 22 (= log2(|LDBL_MIN_EXP-extras|) + log2(INTERVALS)) lowest
  * bits zero so that multiplication of it by n is exact.
  */
-INV_L = 1.8466496523378731e+2,		/*  0x171547652b82fe.0p-45 */
-L1 =  5.4152123484527692e-3,		/*  0x162e42ff000000.0p-60 */
+INV_L = 1.8466496523378731e+2,		/* 0x171547652b82fe.0p-45 */
+L1 = 5.4152123484527692e-3,		/* 0x162e42ff000000.0p-60 */
 L2 = -3.2819649005320973e-13,		/* -0x1718432a1b0e26.0p-94 */
 /*
  * Domain [-0.002708, 0.002708], range ~[-5.7136e-24, 5.7110e-24]:
  * |exp(x) - p(x)| < 2**-77.2
  * (0.002708 is ln2/(2*INTERVALS) rounded up a little).
  */
-P2 =  0.5,
-P3 =  1.6666666666666119e-1,		/*  0x15555555555490.0p-55 */
-P4 =  4.1666666666665887e-2,		/*  0x155555555554e5.0p-57 */
-P5 =  8.3333354987869413e-3,		/*  0x1111115b789919.0p-59 */
-P6 =  1.3888891738560272e-3;		/*  0x16c16c651633ae.0p-62 */
+A2 = 0.5,
+A3 = 1.6666666666666119e-1,		/* 0x15555555555490.0p-55 */
+A4 = 4.1666666666665887e-2,		/* 0x155555555554e5.0p-57 */
+A5 = 8.3333354987869413e-3,		/* 0x1111115b789919.0p-59 */
+A6 = 1.3888891738560272e-3;		/* 0x16c16c651633ae.0p-62 */
 
 /*
  * 2^(i/INTERVALS) for i in [0,INTERVALS] is represented by two values where
@@ -96,8 +100,7 @@
 static const struct {
 	double	hi;
 	double	lo;
-/* XXX should rename 's'. */
-} s[INTERVALS] = {
+} tbl[INTERVALS] = {
 	0x1p+0, 0x0p+0,
 	0x1.0163da9fb3335p+0, 0x1.b61299ab8cdb7p-54,
 	0x1.02c9a3e778060p+0, 0x1.dcdef95949ef4p-53,
@@ -232,7 +235,8 @@
 expl(long double x)
 {
 	union IEEEl2bits u, v;
-	long double fn, q, r, r1, r2, t, t23, t45, twopk, twopkp10000, z;
+	long double fn, q, r, r1, r2, t, twopk, twopkp10000;
+	long double z;
 	int k, n, n2;
 	uint16_t hx, ix;
 
@@ -242,40 +246,38 @@
 	ix = hx & 0x7fff;
 	if (ix >= BIAS + 13) {		/* |x| >= 8192 or x is NaN */
 		if (ix == BIAS + LDBL_MAX_EXP) {
-			if (hx & 0x8000 && u.xbits.man == 1ULL << 63)
-				return (0.0L);	/* x is -Inf */
-			return (x + x); /* x is +Inf, NaN or unsupported */
+			if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
+				return (-1 / x);
+ 			return (x + x);	/* x is +Inf, +NaN or unsupported */
 		}
-		if (x > o_threshold.e)
+		if (x > o_threshold)
 			return (huge * huge);
-		if (x < u_threshold.e)
+		if (x < u_threshold)
 			return (tiny * tiny);
-	} else if (ix < BIAS - 66) {	/* |x| < 0x1p-66 */
-					/* includes pseudo-denormals */
-		if (huge + x > 1.0L)	/* trigger inexact iff x != 0 */
-			return (1.0L + x);
+	} else if (ix < BIAS - 65) {	/* |x| < 0x1p-65 (includes pseudos) */
+		return (1 + x);		/* 1 with inexact iff x != 0 */
 	}
 
 	ENTERI();
 
-	/* Reduce x to (k*ln2 + midpoint[n2] + r1 + r2). */
+	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
 	/* Use a specialized rint() to get fn.  Assume round-to-nearest. */
 	fn = x * INV_L + 0x1.8p63 - 0x1.8p63;
 	r = x - fn * L1 - fn * L2;	/* r = r1 + r2 done independently. */
 #if defined(HAVE_EFFICIENT_IRINTL)
-	n  = irintl(fn);
+	n = irintl(fn);
 #elif defined(HAVE_EFFICIENT_IRINT)
-	n  = irint(fn);
+	n = irint(fn);
 #else
-	n  = (int)fn;
+	n = (int)fn;
 #endif
 	n2 = (unsigned)n % INTERVALS;
-	k = (n - n2) / INTERVALS;
+	k = n >> LOG2_INTERVALS;
 	r1 = x - fn * L1;
-	r2 = -fn * L2;
+	r2 = fn * -L2;
 
 	/* Prepare scale factors. */
-	v.xbits.man = 1ULL << 63;
+	v.e = 1;
 	if (k >= LDBL_MIN_EXP) {
 		v.xbits.expsign = BIAS + k;
 		twopk = v.e;
@@ -284,21 +286,181 @@
 		twopkp10000 = v.e;
 	}
 
-	/* Evaluate expl(midpoint[n2] + r1 + r2) = s[n2] * expl(r1 + r2). */
-	/* Here q = q(r), not q(r1), since r1 is lopped like L1. */
-	t45 = r * P5 + P4;
+	/* Evaluate expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2). */
 	z = r * r;
-	t23 = r * P3 + P2;
-	q = r2 + z * t23 + z * z * t45 + z * z * z * P6;
-	t = (long double)s[n2].lo + s[n2].hi;
-	t = s[n2].lo + t * (q + r1) + s[n2].hi;
+	q = r2 + z * (A2 + r * A3) + z * z * (A4 + r * A5) + z * z * z * A6;
+	t = (long double)tbl[n2].lo + tbl[n2].hi;
+	t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
 
 	/* Scale by 2**k. */
 	if (k >= LDBL_MIN_EXP) {
 		if (k == LDBL_MAX_EXP)
-			RETURNI(t * 2.0L * 0x1p16383L);
+			RETURNI(t * 2 * 0x1p16383L);
 		RETURNI(t * twopk);
 	} else {
 		RETURNI(t * twopkp10000 * twom10000);
 	}
 }
+
+/**
+ * Compute expm1l(x) for Intel 80-bit format.  This is based on:
+ *
+ *   PTP Tang, "Table-driven implementation of the Expm1 function
+ *   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 18,
+ *   211-222 (1992).
+ */
+
+/*
+ * Our T1 and T2 are chosen to be approximately the points where method
+ * A and method B have the same accuracy.  Tang's T1 and T2 are the
+ * points where method A's accuracy changes by a full bit.  For Tang,
+ * this drop in accuracy makes method A immediately less accurate than
+ * method B, but our larger INTERVALS makes method A 2 bits more
+ * accurate so it remains the most accurate method significantly
+ * closer to the origin despite losing the full bit in our extended
+ * range for it.
+ */
+static const double
+T1 = -0.1659,				/* ~-30.625/128 * log(2) */
+T2 = 0.1659;				/* ~30.625/128 * log(2) */
+
+/*
+ * Domain [-0.1659, 0.1659], range ~[-1.2027e-22, 3.4417e-22]:
+ * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-71.2
+ */
+static const union IEEEl2bits
+B3 = LD80C(0xaaaaaaaaaaaaaaab, -3, 1.66666666666666666671e-01L),
+B4 = LD80C(0xaaaaaaaaaaaaaaac, -5, 4.16666666666666666712e-02L);
+
+static const double
+B5 = 8.3333333333333245e-03,		/* 0x1.111111111110cp-7 */
+B6 = 1.3888888888888861e-03,		/* 0x1.6c16c16c16c0ap-10 */
+B7 = 1.9841269841532042e-04,		/* 0x1.a01a01a0319f9p-13 */
+B8 = 2.4801587302069236e-05,		/* 0x1.a01a01a03cbbcp-16 */
+B9 = 2.7557316558468562e-06,		/* 0x1.71de37fd33d67p-19 */
+B10 = 2.7557315829785151e-07,		/* 0x1.27e4f91418144p-22 */
+B11 = 2.5063168199779829e-08,		/* 0x1.ae94fabdc6b27p-26 */
+B12 = 2.0887164654459567e-09;		/* 0x1.1f122d6413fe1p-29 */
+
+long double
+expm1l(long double x)
+{
+	union IEEEl2bits u, v;
+	long double fn, hx2_hi, hx2_lo, q, r, r1, r2, t, twomk, twopk, x_hi;
+	long double x_lo, x2, z;
+	long double x4;
+	int k, n, n2;
+	uint16_t hx, ix;
+
+	/* Filter out exceptional cases. */
+	u.e = x;
+	hx = u.xbits.expsign;
+	ix = hx & 0x7fff;
+	if (ix >= BIAS + 6) {		/* |x| >= 64 or x is NaN */
+		if (ix == BIAS + LDBL_MAX_EXP) {
+			if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
+				return (-1 / x - 1);
+			return (x + x);	/* x is +Inf, +NaN or unsupported */
+		}
+		if (x > o_threshold)
+			return (huge * huge);
+		/*
+		 * expm1l() never underflows, but it must avoid
+		 * unrepresentable large negative exponents.  We used a
+		 * much smaller threshold for large |x| above than in
+		 * expl() so as to handle not so large negative exponents
+		 * in the same way as large ones here.
+		 */
+		if (hx & 0x8000)	/* x <= -64 */
+			return (tiny - 1);	/* good for x < -65ln2 - eps */
+	}
+
+	ENTERI();
+
+	if (T1 < x && x < T2) {
+		if (ix < BIAS - 64) {	/* |x| < 0x1p-64 (includes pseudos) */
+			/* x (rounded) with inexact if x != 0: */
+			RETURNI(x == 0 ? x :
+			    (0x1p100 * x + fabsl(x)) * 0x1p-100);
+		}
+
+		x2 = x * x;
+		x4 = x2 * x2;
+
+		q = x4 * (x2 * (x4 *
+		    (x2 *            B12  + (x * B11 + B10)) +
+		    (x2 * (x * B9 +  B8) +  (x * B7 +  B6))) +
+			  (x * B5 +  B4.e)) + x2 * x * B3.e;
+
+		x_hi = (float)x;
+		x_lo = x - x_hi;
+		hx2_hi = x_hi * x_hi / 2;
+		hx2_lo = x_lo * (x + x_hi) / 2;
+		if (ix >= BIAS - 7)
+			RETURNI(hx2_lo + x_lo + q + (hx2_hi + x_hi));
+		else
+			RETURNI(hx2_lo + q + hx2_hi + x);
+	}
+
+	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
+	/* Use a specialized rint() to get fn.  Assume round-to-nearest. */
+	fn = x * INV_L + 0x1.8p63 - 0x1.8p63;
+#if defined(HAVE_EFFICIENT_IRINTL)
+	n = irintl(fn);
+#elif defined(HAVE_EFFICIENT_IRINT)
+	n = irint(fn);
+#else
+	n = (int)fn;
+#endif
+	n2 = (unsigned)n % INTERVALS;
+	k = n >> LOG2_INTERVALS;
+	r1 = x - fn * L1;
+	r2 = fn * -L2;
+	r = r1 + r2;
+
+	/* Prepare scale factor. */
+	v.e = 1;
+	v.xbits.expsign = BIAS + k;
+	twopk = v.e;
+
+	/*
+	 * Evaluate lower terms of
+	 * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
+	 */
+	z = r * r;
+	q = r2 + z * (A2 + r * A3) + z * z * (A4 + r * A5) + z * z * z * A6;
+
+	t = (long double)tbl[n2].lo + tbl[n2].hi;
+
+	if (k == 0) {
+		t = tbl[n2].lo * (r1 + 1) + t * q + tbl[n2].hi * r1 +
+		    (tbl[n2].hi - 1);
+		RETURNI(t);
+	}
+
+	if (k == -1) {
+		t = tbl[n2].lo * (r1 + 1) + t * q + tbl[n2].hi * r1 + 
+		    (tbl[n2].hi - 2);
+		RETURNI(t / 2);
+	}
+
+	if (k < -7) {
+		t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
+		RETURNI(t * twopk - 1);
+	}
+
+	if (k > 2 * LDBL_MANT_DIG - 1) {
+		t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
+		if (k == LDBL_MAX_EXP)
+			RETURNI(t * 2 * 0x1p16383L - 1);
+		RETURNI(t * twopk - 1);
+	}
+
+	v.xbits.expsign = BIAS - k;
+	twomk = v.e;
+	if (k > LDBL_MANT_DIG - 1)
+		t = tbl[n2].lo - twomk + t * (q + r1) + tbl[n2].hi;
+	else
+		t = tbl[n2].lo + t * (q + r1) + (tbl[n2].hi - twomk);
+	RETURNI(t * twopk);
+}
Index: ld128/s_expl.c
===================================================================
--- ld128/s_expl.c	(revision 251067)
+++ ld128/s_expl.c	(working copy)
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2012 Steven G. Kargl
+ * Copyright (c) 2009-2012 Steven G. Kargl
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -22,6 +22,8 @@
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Optimized by Bruce D. Evans.
  */
 
 #include <sys/cdefs.h>
@@ -38,35 +40,56 @@
 #include "math_private.h"
 
 #define	INTERVALS	128
+#define	LOG2_INTERVALS	7
 #define	BIAS	(LDBL_MAX_EXP - 1)
 
+static const long double
+huge = 0x1p10000L,
+twom10000 = 0x1p-10000L;
+/* XXX Prevent gcc from erroneously constant folding this: */
 static volatile const long double tiny = 0x1p-10000L;
 
 static const long double
-INV_L = 1.84664965233787316142070359168242182e+02L,
-L1 = 5.41521234812457272982212595914567508e-03L,
-L2 = -1.02536706388947310094527932552595546e-29L,
-huge = 0x1p10000L,
-o_threshold =  11356.523406294143949491931077970763428L,
-twom10000 = 0x1p-10000L,
+/* log(2**16384 - 0.5) rounded towards zero: */
+/* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
+o_threshold = 11356.523406294143949491931077970763428L,
+/* log(2**(-16381-64-1)) rounded towards zero: */
 u_threshold = -11433.462743336297878837243843452621503L;
 
+static const double
+/*
+ * ln2/INTERVALS = L1+L2 (hi+lo decomposition for multiplication).  L1 must
+ * have at least 22 (= log2(|LDBL_MIN_EXP-extras|) + log2(INTERVALS)) lowest
+ * bits zero so that multiplication of it by n is exact.
+ */
+INV_L = 1.8466496523378731e+2,		/* 0x171547652b82fe.0p-45 */
+L2 = -1.0253670638894731e-29;		/* -0x1.9ff0342542fc3p-97 */
 static const long double
-P2 = 5.00000000000000000000000000000000000e-1L,
-P3 = 1.66666666666666666666666666666666972e-1L,
-P4 = 4.16666666666666666666666666653708268e-2L,
-P5 = 8.33333333333333333333333315069867254e-3L,
-P6 = 1.38888888888888888888996596213795377e-3L,
-P7 = 1.98412698412698412718821436278644414e-4L,
-P8 = 2.48015873015869681884882576649543128e-5L,
-P9 = 2.75573192240103867817876199544468806e-6L,
-P10 = 2.75573236172670046201884000197885520e-7L,
-P11 = 2.50517544183909126492878226167697856e-8L;
+/* 0x1.62e42fefa39ef35793c768000000p-8 */
+L1 = 5.41521234812457272982212595914567508e-03L;
 
+static const long double
+/*
+ * Domain [-0.002708, 0.002708], range ~[-2.4011e-38, 2.4244e-38]:
+ * |exp(x) - p(x)| < 2**-124.9
+ * (0.002708 is ln2/(2*INTERVALS) rounded up a little).
+ */
+A2 = 0.5,
+A3 = 1.66666666666666666666666666651085500e-01L,
+A4 = 4.16666666666666666666666666425885320e-02L,
+A5 = 8.33333333333333333334522877160175842e-03L,
+A6 = 1.38888888888888888889971139751596836e-03L;
+
+static const double
+A7 = 1.9841269841269471e-04,
+A8 = 2.4801587301585284e-05,
+A9 = 2.7557324277411234e-06,
+A10 = 2.7557333722375072e-07;
+
 static const struct {
 	long double	hi;
 	long double	lo;
-} s[INTERVALS] = {
+} tbl[INTERVALS] = {
 	0x1p0L, 0x0p0L,
 	0x1.0163da9fb33356d84a66aep0L, 0x3.36dcdfa4003ec04c360be2404078p-92L,
 	0x1.02c9a3e778060ee6f7cacap0L, 0x4.f7a29bde93d70a2cabc5cb89ba10p-92L,
@@ -201,9 +224,10 @@
 expl(long double x)
 {
 	union IEEEl2bits u, v;
-	long double fn, r, r1, r2, q, t, twopk, twopkp10000;
+	long double q, r, r1, t, twopk, twopkp10000;
+	double dr, fn, r2;
 	int k, n, n2;
-	uint32_t hx, ix;
+	uint16_t hx, ix;
 
 	/* Filter out exceptional cases. */
 	u.e = x;
@@ -211,31 +235,36 @@
 	ix = hx & 0x7fff;
 	if (ix >= BIAS + 13) {		/* |x| >= 8192 or x is NaN */
 		if (ix == BIAS + LDBL_MAX_EXP) {
-			if (hx & 0x8000 && u.xbits.manh == 0 &&
-			    u.xbits.manl == 0)
-				return (0.0L);	/* x is -Inf */
-			return (x + x);	/* x is +Inf or NaN */
+			if (hx & 0x8000)  /* x is -Inf or -NaN */
+				return (-1 / x);
+			return (x + x);	/* x is +Inf or +NaN */
 		}
 		if (x > o_threshold)
 			return (huge * huge);
 		if (x < u_threshold)
 			return (tiny * tiny);
-	} else if (ix < BIAS - 115) {	/* |x| < 0x1p-115 */
-	    	if (huge + x > 1.0L)	/* trigger inexact iff x != 0 */
-			return (1.0L + x);
+	} else if (ix < BIAS - 114) {	/* |x| < 0x1p-114 */
+		return (1 + x);		/* 1 with inexact iff x != 0 */
 	}
 
-	/* Reduce x to (k*ln2 + midpoint[n2] + r1 + r2). */
-	fn = x * INV_L + 0x1.8p112 - 0x1.8p112;
-	n  = (int)fn;
+	ENTERI();
+
+	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
+	/* Use a specialized rint() to get fn.  Assume round-to-nearest. */
+	fn = (double)x * INV_L + 0x1.8p52 - 0x1.8p52;
+	r = x - fn * L1 - fn * L2;	/* r = r1 + r2 done independently. */
+#if defined(HAVE_EFFICIENT_IRINT)
+	n = irint(fn);
+#else
+	n = (int)fn;
+#endif
 	n2 = (unsigned)n % INTERVALS;
-	k = (n - n2) / INTERVALS;
+	k = n >> LOG2_INTERVALS;
 	r1 = x - fn * L1;
-	r2 = -fn * L2;
+	r2 = fn * -L2;
 
 	/* Prepare scale factors. */
-	v.xbits.manh = 0;
-	v.xbits.manl = 0;
+	v.e = 1;
 	if (k >= LDBL_MIN_EXP) {
 		v.xbits.expsign = BIAS + k;
 		twopk = v.e;
@@ -244,18 +273,223 @@
 		twopkp10000 = v.e;
 	}
 
-	r = r1 + r2;
-	q = r * r * (P2 + r * (P3 + r * (P4 + r * (P5 + r * (P6 + r * (P7 +
-	    r * (P8 + r * (P9 + r * (P10 + r * P11)))))))));
-	t = s[n2].lo + s[n2].hi;
-	t = s[n2].hi + (s[n2].lo + t * (r2 + q + r1));
+	/* Evaluate expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2). */
+	dr = r;
+	q = r2 + r * r * (A2 + r * (A3 + r * (A4 + r * (A5 + r * (A6 +
+	    dr * (A7 + dr * (A8 + dr * (A9 + dr * A10))))))));
+	t = tbl[n2].lo + tbl[n2].hi;
+	t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
 
 	/* Scale by 2**k. */
 	if (k >= LDBL_MIN_EXP) {
 		if (k == LDBL_MAX_EXP)
-			return (t * 2.0L * 0x1p16383L);
-		return (t * twopk);
+			RETURNI(t * 2 * 0x1p16383L);
+		RETURNI(t * twopk);
 	} else {
-		return (t * twopkp10000 * twom10000);
+		RETURNI(t * twopkp10000 * twom10000);
 	}
 }
+
+/*
+ * Our T1 and T2 are chosen to be approximately the points where method
+ * A and method B have the same accuracy.  Tang's T1 and T2 are the
+ * points where method A's accuracy changes by a full bit.  For Tang,
+ * this drop in accuracy makes method A immediately less accurate than
+ * method B, but our larger INTERVALS makes method A 2 bits more
+ * accurate so it remains the most accurate method significantly
+ * closer to the origin despite losing the full bit in our extended
+ * range for it.
+ */
+static const double
+T1 = -0.1659,				/* ~-30.625/128 * log(2) */
+T2 = 0.1659;				/* ~30.625/128 * log(2) */
+
+/*
+ * Split the interval [T1:T2] into two intervals [T1:T3] and [T3:T2].
+ * Setting T3 to 0 would require the |x| < 0x1p-113  condition to appear
+ * in both subintervals, so set T3 = 2**-5, which places the condition
+ * into the [T1:T3] interval.
+ */
+static const double
+T3 = 0.03125;
+
+/*
+ * XXX Estimated range is for absolute error.
+ * Domain [-0.1659, 0.03125], range ~[-1.8933e-38, 1.8943e-38]:
+ * |(exp(x)-1-x-x**2/2)/x**3 - p(x)| < 2**-125.3
+ */
+static const long double
+C3 = 1.66666666666666666666666666666666667e-01L,
+C4 = 4.16666666666666666666666666666666645e-02L,
+C5 = 8.33333333333333333333333333333371638e-03L,
+C6 = 1.38888888888888888888888888891188658e-03L,
+C7 = 1.98412698412698412698412697235950394e-04L,
+C8 = 2.48015873015873015873015112487849040e-05L,
+C9 = 2.75573192239858906525606685484412005e-06L,
+C10 = 2.75573192239858906612966093057020362e-07L,
+C11 = 2.50521083854417203619031960151253944e-08L,
+C12 = 2.08767569878679576457272282566520649e-09L,
+C13 = 1.60590438367252471783548748824255707e-10L;
+
+static const double
+C14 = 1.1470745580491932e-11,		/* 0x1.93974a81dae3p-37 */
+C15 = 7.6471620181090468e-13,		/* 0x1.ae7f3820adab1p-41 */
+C16 = 4.7793721460260450e-14,		/* 0x1.ae7cd18a18eacp-45 */
+C17 = 2.8074757356658877e-15,		/* 0x1.949992a1937d9p-49 */
+C18 = 1.4760610323699476e-16;		/* 0x1.545b43aabfbcdp-53 */
+
+/*
+ * XXX Estimated range is for absolute error.
+ * Domain [0.03125, 0.1659], range ~[-2.7597e-38, 2.7602e-38]:
+ * |(exp(x)-1-x-x**2/2)/x**3 - p(x)| < 2**-124.8
+ */
+static const long double
+D3 = 1.66666666666666666666666666666682245e-01L,
+D4 = 4.16666666666666666666666666634228324e-02L,
+D5 = 8.33333333333333333333333364022244481e-03L,
+D6 = 1.38888888888888888888887138722762072e-03L,
+D7 = 1.98412698412698412699085805424661471e-04L,
+D8 = 2.48015873015873015687993712101479612e-05L,
+D9 = 2.75573192239858944101036288338208042e-06L,
+D10 = 2.75573192239853161148064676533754048e-07L,
+D11 = 2.50521083855084570046480450935267433e-08L,
+D12 = 2.08767569819738524488686318024854942e-09L,
+D13 = 1.60590442297008495301927448122499313e-10L;
+
+static const double
+D14 = 1.1470726176204336e-11,		/* 0x1.93971dc395d9ep-37 */
+D15 = 7.6478532249581686e-13,		/* 0x1.ae892e3D16fcep-41 */
+D16 = 4.7628892832607741e-14,		/* 0x1.ad00Dfe41feccp-45 */
+D17 = 3.0524857220358650e-15;		/* 0x1.D7e8d886Df921p-49 */
+
+long double
+expm1l(long double x)
+{
+	union IEEEl2bits u, v;
+	long double hx2_hi, hx2_lo, q, r, r1, t, twomk, twopk, x_hi;
+	long double x_lo, x2;
+	double dr, dx, fn, r2;
+	int k, n, n2;
+	uint16_t hx, ix;
+
+	/* Filter out exceptional cases. */
+	u.e = x;
+	hx = u.xbits.expsign;
+	ix = hx & 0x7fff;
+	if (ix >= BIAS + 7) {		/* |x| >= 128 or x is NaN */
+		if (ix == BIAS + LDBL_MAX_EXP) {
+			if (hx & 0x8000)  /* x is -Inf or -NaN */
+				return (-1 / x - 1);
+			return (x + x);	/* x is +Inf or +NaN */
+		}
+		if (x > o_threshold)
+			return (huge * huge);
+		/*
+		 * expm1l() never underflows, but it must avoid
+		 * unrepresentable large negative exponents.  We used a
+		 * much smaller threshold for large |x| above than in
+		 * expl() so as to handle not so large negative exponents
+		 * in the same way as large ones here.
+		 */
+		if (hx & 0x8000)	/* x <= -128 */
+			return (tiny - 1);	/* good for x < -114ln2 - eps */
+	}
+
+	ENTERI();
+
+	if (T1 < x && x < T2) {
+		if (ix < BIAS - 113) {	/* |x| < 0x1p-113 */
+			/* x (rounded) with inexact if x != 0: */
+			RETURNI(x == 0 ? x :
+			    (0x1p200 * x + fabsl(x)) * 0x1p-200);
+		}
+
+		x2 = x * x;
+		dx = x;
+
+		if (x < T3) {
+			q = x * x2 * C3 + x2 * x2 * (C4 + x * (C5 + x * (C6 +
+			    x * (C7 + x * (C8 + x * (C9 + x * (C10 +
+			    x * (C11 + x * (C12 + x * (C13 +
+			    dx * (C14 + dx * (C15 + dx * (C16 +
+			    dx * (C17 + dx * C18))))))))))))));
+		} else {
+			q = x * x2 * D3 + x2 * x2 * (D4 + x * (D5 + x * (D6 +
+			    x * (D7 + x * (D8 + x * (D9 + x * (D10 +
+			    x * (D11 + x * (D12 + x * (D13 +
+			    dx * (D14 + dx * (D15 + dx * (D16 +
+			    dx * D17)))))))))))));
+		}
+
+		x_hi = (float)x;
+		x_lo = x - x_hi;
+		hx2_hi = x_hi * x_hi / 2;
+		hx2_lo = x_lo * (x + x_hi) / 2;
+		if (ix >= BIAS - 7)
+			RETURNI(hx2_lo + x_lo + q + (hx2_hi + x_hi));
+		else
+			RETURNI(hx2_lo + q + hx2_hi + x);
+	}
+
+	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
+	/* Use a specialized rint() to get fn.  Assume round-to-nearest. */
+	fn = (double)x * INV_L + 0x1.8p52 - 0x1.8p52;
+#if defined(HAVE_EFFICIENT_IRINT)
+	n = irint(fn);
+#else
+	n = (int)fn;
+#endif
+	n2 = (unsigned)n % INTERVALS;
+	k = n >> LOG2_INTERVALS;
+	r1 = x - fn * L1;
+	r2 = fn * -L2;
+	r = r1 + r2;
+
+	/* Prepare scale factor. */
+	v.e = 1;
+	v.xbits.expsign = BIAS + k;
+	twopk = v.e;
+
+	/*
+	 * Evaluate lower terms of
+	 * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
+	 */
+	dr = r;
+	q = r2 + r * r * (A2 + r * (A3 + r * (A4 + r * (A5 + r * (A6 +
+	    dr * (A7 + dr * (A8 + dr * (A9 + dr * A10))))))));
+
+	t = tbl[n2].lo + tbl[n2].hi;
+
+	if (k == 0) {
+		t = tbl[n2].lo * (r1 + 1) + t * q + tbl[n2].hi * r1 +
+		    (tbl[n2].hi - 1);
+		RETURNI(t);
+	}
+
+	if (k == -1) {
+		t = tbl[n2].lo * (r1 + 1) + t * q + tbl[n2].hi * r1 + 
+		    (tbl[n2].hi - 2);
+		RETURNI(t / 2);
+	}
+
+	if (k < -7) {
+		t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
+		RETURNI(t * twopk - 1);
+	}
+
+	if (k > 2 * LDBL_MANT_DIG - 1) {
+		t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
+		if (k == LDBL_MAX_EXP)
+			RETURNI(t * 2 * 0x1p16383L - 1);
+		RETURNI(t * twopk - 1);
+	}
+
+	v.xbits.expsign = BIAS - k;
+	twomk = v.e;
+
+	if (k > LDBL_MANT_DIG - 1)
+		t = tbl[n2].lo - twomk + t * (q + r1) + tbl[n2].hi;
+	else
+		t = tbl[n2].lo + t * (q + r1) + (tbl[n2].hi - twomk);
+	RETURNI(t * twopk);
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130529162441.GA58773>