Home | History | Annotate | Line # | Download | only in ld128
      1  1.1  christos /*-
      2  1.1  christos  * SPDX-License-Identifier: BSD-2-Clause
      3  1.1  christos  *
      4  1.1  christos  * Copyright (c) 2009-2013 Steven G. Kargl
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice unmodified, this list of conditions, and the following
     12  1.1  christos  *    disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  *
     28  1.1  christos  * Optimized by Bruce D. Evans.
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos /*
     33  1.1  christos  * ld128 version of s_expl.c.  See ../ld80/s_expl.c for most comments.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos #include <float.h>
     37  1.1  christos 
     38  1.1  christos #include "math.h"
     39  1.1  christos #include "math_private.h"
     40  1.1  christos #include "k_expl.h"
     41  1.1  christos 
     42  1.1  christos /* XXX Prevent compilers from erroneously constant folding these: */
     43  1.1  christos static const volatile long double
     44  1.1  christos huge = 0x1p10000L,
     45  1.1  christos tiny = 0x1p-10000L;
     46  1.1  christos 
     47  1.1  christos static const long double
     48  1.1  christos twom10000 = 0x1p-10000L;
     49  1.1  christos 
     50  1.1  christos static const long double
     51  1.1  christos /* log(2**16384 - 0.5) rounded towards zero: */
     52  1.1  christos /* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
     53  1.1  christos o_threshold =  11356.523406294143949491931077970763428L,
     54  1.1  christos /* log(2**(-16381-64-1)) rounded towards zero: */
     55  1.1  christos u_threshold = -11433.462743336297878837243843452621503L;
     56  1.1  christos 
     57  1.1  christos long double
     58  1.1  christos expl(long double x)
     59  1.1  christos {
     60  1.1  christos 	union ieee_ext_u u;
     61  1.1  christos 	long double hi, lo, t, twopk;
     62  1.1  christos 	int k;
     63  1.1  christos 	uint16_t hx, ix;
     64  1.1  christos 
     65  1.1  christos 	/* Filter out exceptional cases. */
     66  1.1  christos 	u.extu_ld = x;
     67  1.1  christos 	hx = GET_EXPSIGN(&u);
     68  1.1  christos 	ix = hx & 0x7fff;
     69  1.1  christos 	if (ix >= BIAS + 13) {		/* |x| >= 8192 or x is NaN */
     70  1.1  christos 		if (ix == BIAS + LDBL_MAX_EXP) {
     71  1.1  christos 			if (hx & 0x8000)  /* x is -Inf or -NaN */
     72  1.1  christos 				RETURNF(-1 / x);
     73  1.1  christos 			RETURNF(x + x);	/* x is +Inf or +NaN */
     74  1.1  christos 		}
     75  1.1  christos 		if (x > o_threshold)
     76  1.1  christos 			RETURNF(huge * huge);
     77  1.1  christos 		if (x < u_threshold)
     78  1.1  christos 			RETURNF(tiny * tiny);
     79  1.1  christos 	} else if (ix < BIAS - 114) {	/* |x| < 0x1p-114 */
     80  1.1  christos 		RETURNF(1 + x);		/* 1 with inexact iff x != 0 */
     81  1.1  christos 	}
     82  1.1  christos 
     83  1.1  christos 	ENTERI();
     84  1.1  christos 
     85  1.1  christos 	twopk = 1;
     86  1.1  christos 	__k_expl(x, &hi, &lo, &k);
     87  1.1  christos 	t = SUM2P(hi, lo);
     88  1.1  christos 
     89  1.1  christos 	/* Scale by 2**k. */
     90  1.1  christos 	/*
     91  1.1  christos 	 * XXX sparc64 multiplication was so slow that scalbnl() is faster,
     92  1.1  christos 	 * but performance on aarch64 and riscv hasn't yet been quantified.
     93  1.1  christos 	 */
     94  1.1  christos 	if (k >= LDBL_MIN_EXP) {
     95  1.1  christos 		if (k == LDBL_MAX_EXP)
     96  1.1  christos 			RETURNI(t * 2 * 0x1p16383L);
     97  1.1  christos 		SET_LDBL_EXPSIGN(twopk, BIAS + k);
     98  1.1  christos 		RETURNI(t * twopk);
     99  1.1  christos 	} else {
    100  1.1  christos 		SET_LDBL_EXPSIGN(twopk, BIAS + k + 10000);
    101  1.1  christos 		RETURNI(t * twopk * twom10000);
    102  1.1  christos 	}
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos /*
    106  1.1  christos  * Our T1 and T2 are chosen to be approximately the points where method
    107  1.1  christos  * A and method B have the same accuracy.  Tang's T1 and T2 are the
    108  1.1  christos  * points where method A's accuracy changes by a full bit.  For Tang,
    109  1.1  christos  * this drop in accuracy makes method A immediately less accurate than
    110  1.1  christos  * method B, but our larger INTERVALS makes method A 2 bits more
    111  1.1  christos  * accurate so it remains the most accurate method significantly
    112  1.1  christos  * closer to the origin despite losing the full bit in our extended
    113  1.1  christos  * range for it.
    114  1.1  christos  *
    115  1.1  christos  * Split the interval [T1, T2] into two intervals [T1, T3] and [T3, T2].
    116  1.1  christos  * Setting T3 to 0 would require the |x| < 0x1p-113 condition to appear
    117  1.1  christos  * in both subintervals, so set T3 = 2**-5, which places the condition
    118  1.1  christos  * into the [T1, T3] interval.
    119  1.1  christos  *
    120  1.1  christos  * XXX we now do this more to (partially) balance the number of terms
    121  1.1  christos  * in the C and D polys than to avoid checking the condition in both
    122  1.1  christos  * intervals.
    123  1.1  christos  *
    124  1.1  christos  * XXX these micro-optimizations are excessive.
    125  1.1  christos  */
    126  1.1  christos static const double
    127  1.1  christos T1 = -0.1659,				/* ~-30.625/128 * log(2) */
    128  1.1  christos T2 =  0.1659,				/* ~30.625/128 * log(2) */
    129  1.1  christos T3 =  0.03125;
    130  1.1  christos 
    131  1.1  christos /*
    132  1.1  christos  * Domain [-0.1659, 0.03125], range ~[2.9134e-44, 1.8404e-37]:
    133  1.1  christos  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-122.03
    134  1.1  christos  *
    135  1.1  christos  * XXX none of the long double C or D coeffs except C10 is correctly printed.
    136  1.1  christos  * If you re-print their values in %.35Le format, the result is always
    137  1.1  christos  * different.  For example, the last 2 digits in C3 should be 59, not 67.
    138  1.1  christos  * 67 is apparently from rounding an extra-precision value to 36 decimal
    139  1.1  christos  * places.
    140  1.1  christos  */
    141  1.1  christos static const long double
    142  1.1  christos C3  =  1.66666666666666666666666666666666667e-1L,
    143  1.1  christos C4  =  4.16666666666666666666666666666666645e-2L,
    144  1.1  christos C5  =  8.33333333333333333333333333333371638e-3L,
    145  1.1  christos C6  =  1.38888888888888888888888888891188658e-3L,
    146  1.1  christos C7  =  1.98412698412698412698412697235950394e-4L,
    147  1.1  christos C8  =  2.48015873015873015873015112487849040e-5L,
    148  1.1  christos C9  =  2.75573192239858906525606685484412005e-6L,
    149  1.1  christos C10 =  2.75573192239858906612966093057020362e-7L,
    150  1.1  christos C11 =  2.50521083854417203619031960151253944e-8L,
    151  1.1  christos C12 =  2.08767569878679576457272282566520649e-9L,
    152  1.1  christos C13 =  1.60590438367252471783548748824255707e-10L;
    153  1.1  christos 
    154  1.1  christos /*
    155  1.1  christos  * XXX this has 1 more coeff than needed.
    156  1.1  christos  * XXX can start the double coeffs but not the double mults at C10.
    157  1.1  christos  * With my coeffs (C10-C17 double; s = best_s):
    158  1.1  christos  * Domain [-0.1659, 0.03125], range ~[-1.1976e-37, 1.1976e-37]:
    159  1.1  christos  * |(exp(x)-1-x-x**2/2)/x - p(x)| ~< 2**-122.65
    160  1.1  christos  */
    161  1.1  christos static const double
    162  1.1  christos C14 =  1.1470745580491932e-11,		/*  0x1.93974a81dae30p-37 */
    163  1.1  christos C15 =  7.6471620181090468e-13,		/*  0x1.ae7f3820adab1p-41 */
    164  1.1  christos C16 =  4.7793721460260450e-14,		/*  0x1.ae7cd18a18eacp-45 */
    165  1.1  christos C17 =  2.8074757356658877e-15,		/*  0x1.949992a1937d9p-49 */
    166  1.1  christos C18 =  1.4760610323699476e-16;		/*  0x1.545b43aabfbcdp-53 */
    167  1.1  christos 
    168  1.1  christos /*
    169  1.1  christos  * Domain [0.03125, 0.1659], range ~[-2.7676e-37, -1.0367e-38]:
    170  1.1  christos  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-121.44
    171  1.1  christos  */
    172  1.1  christos static const long double
    173  1.1  christos D3  =  1.66666666666666666666666666666682245e-1L,
    174  1.1  christos D4  =  4.16666666666666666666666666634228324e-2L,
    175  1.1  christos D5  =  8.33333333333333333333333364022244481e-3L,
    176  1.1  christos D6  =  1.38888888888888888888887138722762072e-3L,
    177  1.1  christos D7  =  1.98412698412698412699085805424661471e-4L,
    178  1.1  christos D8  =  2.48015873015873015687993712101479612e-5L,
    179  1.1  christos D9  =  2.75573192239858944101036288338208042e-6L,
    180  1.1  christos D10 =  2.75573192239853161148064676533754048e-7L,
    181  1.1  christos D11 =  2.50521083855084570046480450935267433e-8L,
    182  1.1  christos D12 =  2.08767569819738524488686318024854942e-9L,
    183  1.1  christos D13 =  1.60590442297008495301927448122499313e-10L;
    184  1.1  christos 
    185  1.1  christos /*
    186  1.1  christos  * XXX this has 1 more coeff than needed.
    187  1.1  christos  * XXX can start the double coeffs but not the double mults at D11.
    188  1.1  christos  * With my coeffs (D11-D16 double):
    189  1.1  christos  * Domain [0.03125, 0.1659], range ~[-1.1980e-37, 1.1980e-37]:
    190  1.1  christos  * |(exp(x)-1-x-x**2/2)/x - p(x)| ~< 2**-122.65
    191  1.1  christos  */
    192  1.1  christos static const double
    193  1.1  christos D14 =  1.1470726176204336e-11,		/*  0x1.93971dc395d9ep-37 */
    194  1.1  christos D15 =  7.6478532249581686e-13,		/*  0x1.ae892e3D16fcep-41 */
    195  1.1  christos D16 =  4.7628892832607741e-14,		/*  0x1.ad00Dfe41feccp-45 */
    196  1.1  christos D17 =  3.0524857220358650e-15;		/*  0x1.D7e8d886Df921p-49 */
    197  1.1  christos 
    198  1.1  christos long double
    199  1.1  christos expm1l(long double x)
    200  1.1  christos {
    201  1.1  christos 	union ieee_ext_u u, v;
    202  1.1  christos 	long double hx2_hi, hx2_lo, q, r, r1, t, twomk, twopk, x_hi;
    203  1.1  christos 	long double x_lo, x2;
    204  1.1  christos 	double dr, dx, fn, r2;
    205  1.1  christos 	int k, n, n2;
    206  1.1  christos 	uint16_t hx, ix;
    207  1.1  christos 
    208  1.1  christos 	/* Filter out exceptional cases. */
    209  1.1  christos 	u.extu_ld = x;
    210  1.1  christos 	hx = GET_EXPSIGN(&u);
    211  1.1  christos 	ix = hx & 0x7fff;
    212  1.1  christos 	if (ix >= BIAS + 7) {		/* |x| >= 128 or x is NaN */
    213  1.1  christos 		if (ix == BIAS + LDBL_MAX_EXP) {
    214  1.1  christos 			if (hx & 0x8000)  /* x is -Inf or -NaN */
    215  1.1  christos 				RETURNF(-1 / x - 1);
    216  1.1  christos 			RETURNF(x + x);	/* x is +Inf or +NaN */
    217  1.1  christos 		}
    218  1.1  christos 		if (x > o_threshold)
    219  1.1  christos 			RETURNF(huge * huge);
    220  1.1  christos 		/*
    221  1.1  christos 		 * expm1l() never underflows, but it must avoid
    222  1.1  christos 		 * unrepresentable large negative exponents.  We used a
    223  1.1  christos 		 * much smaller threshold for large |x| above than in
    224  1.1  christos 		 * expl() so as to handle not so large negative exponents
    225  1.1  christos 		 * in the same way as large ones here.
    226  1.1  christos 		 */
    227  1.1  christos 		if (hx & 0x8000)	/* x <= -128 */
    228  1.1  christos 			RETURNF(tiny - 1);	/* good for x < -114ln2 - eps */
    229  1.1  christos 	}
    230  1.1  christos 
    231  1.1  christos 	ENTERI();
    232  1.1  christos 
    233  1.1  christos 	if (T1 < x && x < T2) {
    234  1.1  christos 		x2 = x * x;
    235  1.1  christos 		dx = x;
    236  1.1  christos 
    237  1.1  christos 		if (x < T3) {
    238  1.1  christos 			if (ix < BIAS - 113) {	/* |x| < 0x1p-113 */
    239  1.1  christos 				/* x (rounded) with inexact if x != 0: */
    240  1.1  christos 				RETURNI(x == 0 ? x :
    241  1.1  christos 				    (0x1p200 * x + fabsl(x)) * 0x1p-200);
    242  1.1  christos 			}
    243  1.1  christos 			q = x * x2 * C3 + x2 * x2 * (C4 + x * (C5 + x * (C6 +
    244  1.1  christos 			    x * (C7 + x * (C8 + x * (C9 + x * (C10 +
    245  1.1  christos 			    x * (C11 + x * (C12 + x * (C13 +
    246  1.1  christos 			    dx * (C14 + dx * (C15 + dx * (C16 +
    247  1.1  christos 			    dx * (C17 + dx * C18))))))))))))));
    248  1.1  christos 		} else {
    249  1.1  christos 			q = x * x2 * D3 + x2 * x2 * (D4 + x * (D5 + x * (D6 +
    250  1.1  christos 			    x * (D7 + x * (D8 + x * (D9 + x * (D10 +
    251  1.1  christos 			    x * (D11 + x * (D12 + x * (D13 +
    252  1.1  christos 			    dx * (D14 + dx * (D15 + dx * (D16 +
    253  1.1  christos 			    dx * D17)))))))))))));
    254  1.1  christos 		}
    255  1.1  christos 
    256  1.1  christos 		x_hi = (float)x;
    257  1.1  christos 		x_lo = x - x_hi;
    258  1.1  christos 		hx2_hi = x_hi * x_hi / 2;
    259  1.1  christos 		hx2_lo = x_lo * (x + x_hi) / 2;
    260  1.1  christos 		if (ix >= BIAS - 7)
    261  1.1  christos 			RETURNI((hx2_hi + x_hi) + (hx2_lo + x_lo + q));
    262  1.1  christos 		else
    263  1.1  christos 			RETURNI(x + (hx2_lo + q + hx2_hi));
    264  1.1  christos 	}
    265  1.1  christos 
    266  1.1  christos 	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
    267  1.1  christos 	fn = rnint((double)x * INV_L);
    268  1.1  christos 	n = irint(fn);
    269  1.1  christos 	n2 = (unsigned)n % INTERVALS;
    270  1.1  christos 	k = n >> LOG2_INTERVALS;
    271  1.1  christos 	r1 = x - fn * L1;
    272  1.1  christos 	r2 = fn * -L2;
    273  1.1  christos 	r = r1 + r2;
    274  1.1  christos 
    275  1.1  christos 	/* Prepare scale factor. */
    276  1.1  christos 	v.extu_ld = 1;
    277  1.1  christos 	SET_EXPSIGN(&v, BIAS + k);
    278  1.1  christos 	twopk = v.extu_ld;
    279  1.1  christos 
    280  1.1  christos 	/*
    281  1.1  christos 	 * Evaluate lower terms of
    282  1.1  christos 	 * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
    283  1.1  christos 	 */
    284  1.1  christos 	dr = r;
    285  1.1  christos 	q = r2 + r * r * (A2 + r * (A3 + r * (A4 + r * (A5 + r * (A6 +
    286  1.1  christos 	    dr * (A7 + dr * (A8 + dr * (A9 + dr * A10))))))));
    287  1.1  christos 
    288  1.1  christos 	t = tbl[n2].lo + tbl[n2].hi;
    289  1.1  christos 
    290  1.1  christos 	if (k == 0) {
    291  1.1  christos 		t = SUM2P(tbl[n2].hi - 1, tbl[n2].lo * (r1 + 1) + t * q +
    292  1.1  christos 		    tbl[n2].hi * r1);
    293  1.1  christos 		RETURNI(t);
    294  1.1  christos 	}
    295  1.1  christos 	if (k == -1) {
    296  1.1  christos 		t = SUM2P(tbl[n2].hi - 2, tbl[n2].lo * (r1 + 1) + t * q +
    297  1.1  christos 		    tbl[n2].hi * r1);
    298  1.1  christos 		RETURNI(t / 2);
    299  1.1  christos 	}
    300  1.1  christos 	if (k < -7) {
    301  1.1  christos 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
    302  1.1  christos 		RETURNI(t * twopk - 1);
    303  1.1  christos 	}
    304  1.1  christos 	if (k > 2 * LDBL_MANT_DIG - 1) {
    305  1.1  christos 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
    306  1.1  christos 		if (k == LDBL_MAX_EXP)
    307  1.1  christos 			RETURNI(t * 2 * 0x1p16383L - 1);
    308  1.1  christos 		RETURNI(t * twopk - 1);
    309  1.1  christos 	}
    310  1.1  christos 
    311  1.1  christos 	SET_EXPSIGN(&v, BIAS - k);
    312  1.1  christos 	twomk = v.extu_ld;
    313  1.1  christos 
    314  1.1  christos 	if (k > LDBL_MANT_DIG - 1)
    315  1.1  christos 		t = SUM2P(tbl[n2].hi, tbl[n2].lo - twomk + t * (q + r1));
    316  1.1  christos 	else
    317  1.1  christos 		t = SUM2P(tbl[n2].hi - twomk, tbl[n2].lo + t * (q + r1));
    318  1.1  christos 	RETURNI(t * twopk);
    319  1.1  christos }
    320