Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 1 | /*- |
Elliott Hughes | bac0ebb | 2021-01-26 14:17:20 -0800 | [diff] [blame] | 2 | * SPDX-License-Identifier: BSD-3-Clause |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 3 | * |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4 | * Copyright (c) 1985, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
Elliott Hughes | bac0ebb | 2021-01-26 14:17:20 -0800 | [diff] [blame] | 15 | * 3. Neither the name of the University nor the names of its contributors |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | */ |
| 31 | |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 32 | /* @(#)exp.c 8.1 (Berkeley) 6/4/93 */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | #include <sys/cdefs.h> |
Elliott Hughes | bac0ebb | 2021-01-26 14:17:20 -0800 | [diff] [blame] | 34 | __FBSDID("$FreeBSD$"); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | /* EXP(X) |
| 37 | * RETURN THE EXPONENTIAL OF X |
| 38 | * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS) |
| 39 | * CODED IN C BY K.C. NG, 1/19/85; |
| 40 | * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86. |
| 41 | * |
| 42 | * Required system supported functions: |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 43 | * ldexp(x,n) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | * copysign(x,y) |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 45 | * isfinite(x) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | * |
| 47 | * Method: |
| 48 | * 1. Argument Reduction: given the input x, find r and integer k such |
| 49 | * that |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 50 | * x = k*ln2 + r, |r| <= 0.5*ln2. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | * r will be represented as r := z+c for better accuracy. |
| 52 | * |
| 53 | * 2. Compute exp(r) by |
| 54 | * |
| 55 | * exp(r) = 1 + r + r*R1/(2-R1), |
| 56 | * where |
| 57 | * R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))). |
| 58 | * |
| 59 | * 3. exp(x) = 2^k * exp(r) . |
| 60 | * |
| 61 | * Special cases: |
| 62 | * exp(INF) is INF, exp(NaN) is NaN; |
| 63 | * exp(-INF)= 0; |
| 64 | * for finite argument, only exp(0)=1 is exact. |
| 65 | * |
| 66 | * Accuracy: |
| 67 | * exp(x) returns the exponential of x nearly rounded. In a test run |
| 68 | * with 1,156,000 random arguments on a VAX, the maximum observed |
| 69 | * error was 0.869 ulps (units in the last place). |
| 70 | */ |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 71 | static const double |
| 72 | p1 = 1.6666666666666660e-01, /* 0x3fc55555, 0x55555553 */ |
| 73 | p2 = -2.7777777777564776e-03, /* 0xbf66c16c, 0x16c0ac3c */ |
| 74 | p3 = 6.6137564717940088e-05, /* 0x3f11566a, 0xb5c2ba0d */ |
| 75 | p4 = -1.6534060280704225e-06, /* 0xbebbbd53, 0x273e8fb7 */ |
| 76 | p5 = 4.1437773411069054e-08; /* 0x3e663f2a, 0x09c94b6c */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 78 | static const double |
| 79 | ln2hi = 0x1.62e42fee00000p-1, /* High 32 bits round-down. */ |
| 80 | ln2lo = 0x1.a39ef35793c76p-33; /* Next 53 bits round-to-nearst. */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 82 | static const double |
| 83 | lnhuge = 0x1.6602b15b7ecf2p9, /* (DBL_MAX_EXP + 9) * log(2.) */ |
| 84 | lntiny = -0x1.77af8ebeae354p9, /* (DBL_MIN_EXP - 53 - 10) * log(2.) */ |
| 85 | invln2 = 0x1.71547652b82fep0; /* 1 / log(2.) */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 86 | |
| 87 | /* returns exp(r = x + c) for |c| < |x| with no overlap. */ |
| 88 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 89 | static double |
| 90 | __exp__D(double x, double c) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 91 | { |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 92 | double hi, lo, z; |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | int k; |
| 94 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 95 | if (x != x) /* x is NaN. */ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | return(x); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 98 | if (x <= lnhuge) { |
| 99 | if (x >= lntiny) { |
| 100 | /* argument reduction: x --> x - k*ln2 */ |
| 101 | z = invln2 * x; |
| 102 | k = z + copysign(0.5, x); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 104 | /* |
| 105 | * Express (x + c) - k * ln2 as hi - lo. |
| 106 | * Let x = hi - lo rounded. |
| 107 | */ |
| 108 | hi = x - k * ln2hi; /* Exact. */ |
| 109 | lo = k * ln2lo - c; |
| 110 | x = hi - lo; |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 112 | /* Return 2^k*[1+x+x*c/(2+c)] */ |
| 113 | z = x * x; |
| 114 | c = x - z * (p1 + z * (p2 + z * (p3 + z * (p4 + |
| 115 | z * p5)))); |
| 116 | c = (x * c) / (2 - c); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 118 | return (ldexp(1 + (hi - (lo - c)), k)); |
| 119 | } else { |
| 120 | /* exp(-INF) is 0. exp(-big) underflows to 0. */ |
| 121 | return (isfinite(x) ? ldexp(1., -5000) : 0); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 122 | } |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 123 | } else |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | /* exp(INF) is INF, exp(+big#) overflows to INF */ |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 125 | return (isfinite(x) ? ldexp(1., 5000) : x); |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | } |