Home | History | Annotate | Line # | Download | only in noieee_src
n_exp2f.c revision 1.1
      1  1.1  martin /*-
      2  1.1  martin  * Copyright (c) 2005 David Schultz <das (at) FreeBSD.ORG>
      3  1.1  martin  * All rights reserved.
      4  1.1  martin  *
      5  1.1  martin  * Redistribution and use in source and binary forms, with or without
      6  1.1  martin  * modification, are permitted provided that the following conditions
      7  1.1  martin  * are met:
      8  1.1  martin  * 1. Redistributions of source code must retain the above copyright
      9  1.1  martin  *    notice, this list of conditions and the following disclaimer.
     10  1.1  martin  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  martin  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  martin  *    documentation and/or other materials provided with the distribution.
     13  1.1  martin  *
     14  1.1  martin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  martin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  martin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  martin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  martin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  martin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  martin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  martin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  martin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  martin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  martin  * SUCH DAMAGE.
     25  1.1  martin  */
     26  1.1  martin 
     27  1.1  martin #include <sys/cdefs.h>
     28  1.1  martin __RCSID("$NetBSD: n_exp2f.c,v 1.1 2014/03/06 10:55:57 martin Exp $");
     29  1.1  martin #ifdef __FBSDID
     30  1.1  martin __FBSDID("$FreeBSD: src/lib/msun/src/s_exp2f.c,v 1.9 2008/02/22 02:27:34 das Exp $");
     31  1.1  martin #endif
     32  1.1  martin 
     33  1.1  martin #include <stdint.h>
     34  1.1  martin #include <float.h>
     35  1.1  martin 
     36  1.1  martin #include "math.h"
     37  1.1  martin 
     38  1.1  martin #define	TBLBITS	4
     39  1.1  martin #define	TBLSIZE	(1 << TBLBITS)
     40  1.1  martin 
     41  1.1  martin static const float
     42  1.1  martin     huge    = 0x1p100f,
     43  1.1  martin     redux   = 0x1.8p23f / TBLSIZE,
     44  1.1  martin     P1	    = 0x1.62e430p-1f,
     45  1.1  martin     P2	    = 0x1.ebfbe0p-3f,
     46  1.1  martin     P3	    = 0x1.c6b348p-5f,
     47  1.1  martin     P4	    = 0x1.3b2c9cp-7f;
     48  1.1  martin 
     49  1.1  martin static volatile float twom100 = 0x1p-100f;
     50  1.1  martin 
     51  1.1  martin static const double exp2ft[TBLSIZE] = {
     52  1.1  martin 	0x1.6a09e667f3bcdp-1,
     53  1.1  martin 	0x1.7a11473eb0187p-1,
     54  1.1  martin 	0x1.8ace5422aa0dbp-1,
     55  1.1  martin 	0x1.9c49182a3f090p-1,
     56  1.1  martin 	0x1.ae89f995ad3adp-1,
     57  1.1  martin 	0x1.c199bdd85529cp-1,
     58  1.1  martin 	0x1.d5818dcfba487p-1,
     59  1.1  martin 	0x1.ea4afa2a490dap-1,
     60  1.1  martin 	0x1.0000000000000p+0,
     61  1.1  martin 	0x1.0b5586cf9890fp+0,
     62  1.1  martin 	0x1.172b83c7d517bp+0,
     63  1.1  martin 	0x1.2387a6e756238p+0,
     64  1.1  martin 	0x1.306fe0a31b715p+0,
     65  1.1  martin 	0x1.3dea64c123422p+0,
     66  1.1  martin 	0x1.4bfdad5362a27p+0,
     67  1.1  martin 	0x1.5ab07dd485429p+0,
     68  1.1  martin };
     69  1.1  martin 
     70  1.1  martin /*
     71  1.1  martin  * exp2f(x): compute the base 2 exponential of x
     72  1.1  martin  *
     73  1.1  martin  * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
     74  1.1  martin  *
     75  1.1  martin  * Method: (equally-spaced tables)
     76  1.1  martin  *
     77  1.1  martin  *   Reduce x:
     78  1.1  martin  *     x = 2**k + y, for integer k and |y| <= 1/2.
     79  1.1  martin  *     Thus we have exp2f(x) = 2**k * exp2(y).
     80  1.1  martin  *
     81  1.1  martin  *   Reduce y:
     82  1.1  martin  *     y = i/TBLSIZE + z for integer i near y * TBLSIZE.
     83  1.1  martin  *     Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
     84  1.1  martin  *     with |z| <= 2**-(TBLSIZE+1).
     85  1.1  martin  *
     86  1.1  martin  *   We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
     87  1.1  martin  *   degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
     88  1.1  martin  *   Using double precision for everything except the reduction makes
     89  1.1  martin  *   roundoff error insignificant and simplifies the scaling step.
     90  1.1  martin  *
     91  1.1  martin  *   This method is due to Tang, but I do not use his suggested parameters:
     92  1.1  martin  *
     93  1.1  martin  *	Tang, P.  Table-driven Implementation of the Exponential Function
     94  1.1  martin  *	in IEEE Floating-Point Arithmetic.  TOMS 15(2), 144-157 (1989).
     95  1.1  martin  */
     96  1.1  martin float
     97  1.1  martin exp2f(float x)
     98  1.1  martin {
     99  1.1  martin 	double tv, twopk, u, z;
    100  1.1  martin 	float t;
    101  1.1  martin 	uint32_t hx, ix, i0;
    102  1.1  martin 	int32_t k;
    103  1.1  martin 
    104  1.1  martin 	/* Filter out exceptional cases. */
    105  1.1  martin 	hx = *((uint32_t*)&x);
    106  1.1  martin 	ix = hx & 0x7fffffff;		/* high word of |x| */
    107  1.1  martin 	if(ix >= 0x43000000) {			/* |x| >= 128 */
    108  1.1  martin 		if(x >= 0x1.0p7f)
    109  1.1  martin 			return (huge * huge);	/* overflow */
    110  1.1  martin 		if(x <= -0x1.2cp7f)
    111  1.1  martin 			return (twom100 * twom100); /* underflow */
    112  1.1  martin 	} else if (ix <= 0x33000000) {		/* |x| <= 0x1p-25 */
    113  1.1  martin 		return (1.0f + x);
    114  1.1  martin 	}
    115  1.1  martin 
    116  1.1  martin 	/* Reduce x, computing z, i0, and k. */
    117  1.1  martin 	*((volatile float*)&t) = x + redux;
    118  1.1  martin 	i0 = *((uint32_t*)&t);
    119  1.1  martin 	i0 += TBLSIZE / 2;
    120  1.1  martin 	k = (i0 >> TBLBITS) << 20;
    121  1.1  martin 	i0 &= TBLSIZE - 1;
    122  1.1  martin 	t -= redux;
    123  1.1  martin 	z = x - t;
    124  1.1  martin 	((uint32_t*)&twopk)[0] = 0x3ff00000+k;
    125  1.1  martin 	((uint32_t*)&twopk)[1] = 0;
    126  1.1  martin 
    127  1.1  martin 	/* Compute r = exp2(y) = exp2ft[i0] * p(z). */
    128  1.1  martin 	tv = exp2ft[i0];
    129  1.1  martin 	u = tv * z;
    130  1.1  martin 	tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4);
    131  1.1  martin 
    132  1.1  martin 	/* Scale by 2**(k>>20). */
    133  1.1  martin 	return (tv * twopk);
    134  1.1  martin }
    135