| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
|---|
| 3 | * Copyright (c) 2014 Martin Decky
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - 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.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libmath
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <exp.h>
|
|---|
| 37 | #include <math.h>
|
|---|
| 38 | #include <trunc.h>
|
|---|
| 39 |
|
|---|
| 40 | #define TAYLOR_DEGREE_32 13
|
|---|
| 41 | #define TAYLOR_DEGREE_64 21
|
|---|
| 42 |
|
|---|
| 43 | /** Precomputed values for factorial (starting from 1!) */
|
|---|
| 44 | static float64_t factorials[TAYLOR_DEGREE_64] = {
|
|---|
| 45 | 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
|
|---|
| 46 | 479001600, 6227020800.0L, 87178291200.0L, 1307674368000.0L,
|
|---|
| 47 | 20922789888000.0L, 355687428096000.0L, 6402373705728000.0L,
|
|---|
| 48 | 121645100408832000.0L, 2432902008176640000.0L, 51090942171709440000.0L
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | /** Exponential approximation by Taylor series (32-))
|
|---|
| 52 | *
|
|---|
| 53 | * Compute the approximation of exponential by a Taylor
|
|---|
| 54 | * series (using the first TAYLOR_DEGREE terms).
|
|---|
| 55 | * The approximation is reasonably accurate for
|
|---|
| 56 | * arguments within the interval XXXX.
|
|---|
| 57 | *
|
|---|
| 58 | * @param arg Argument.
|
|---|
| 59 | *
|
|---|
| 60 | * @return Exponential value approximation.
|
|---|
| 61 | *
|
|---|
| 62 | */
|
|---|
| 63 | static float64_t taylor_exp_32(float64_t arg)
|
|---|
| 64 | {
|
|---|
| 65 | float64_t ret = 1;
|
|---|
| 66 | float64_t nom = 1;
|
|---|
| 67 |
|
|---|
| 68 | for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) {
|
|---|
| 69 | nom *= arg;
|
|---|
| 70 | ret += nom / factorials[i];
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return ret;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /** Exponential approximation by Taylor series
|
|---|
| 77 | *
|
|---|
| 78 | * Compute the approximation of exponential by a Taylor
|
|---|
| 79 | * series (using the first TAYLOR_DEGREE terms).
|
|---|
| 80 | * The approximation is reasonably accurate for
|
|---|
| 81 | * arguments within the interval XXXX.
|
|---|
| 82 | *
|
|---|
| 83 | * @param arg Argument.
|
|---|
| 84 | *
|
|---|
| 85 | * @return Exponential value approximation.
|
|---|
| 86 | *
|
|---|
| 87 | */
|
|---|
| 88 | static float64_t taylor_exp_64(float64_t arg)
|
|---|
| 89 | {
|
|---|
| 90 | float64_t ret = 1;
|
|---|
| 91 | float64_t nom = 1;
|
|---|
| 92 |
|
|---|
| 93 | for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
|
|---|
| 94 | nom *= arg;
|
|---|
| 95 | ret += nom / factorials[i];
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | return ret;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Single precision exponential
|
|---|
| 102 | *
|
|---|
| 103 | * Compute exponential value.
|
|---|
| 104 | *
|
|---|
| 105 | * @param arg Exponential argument.
|
|---|
| 106 | *
|
|---|
| 107 | * @return Exponential value.
|
|---|
| 108 | *
|
|---|
| 109 | */
|
|---|
| 110 | float32_t float32_exp(float32_t arg)
|
|---|
| 111 | {
|
|---|
| 112 | float32_t f;
|
|---|
| 113 | float32_u i;
|
|---|
| 114 | float32_u m;
|
|---|
| 115 | float32_u r;
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * e^a = (2 ^ log2(e))^a = 2 ^ (log2(e) * a)
|
|---|
| 119 | * log2(e) * a = i + f | f in [0, 1]
|
|---|
| 120 | * e ^ a = 2 ^ (i + f) = 2^f * 2^i = (e ^ log(2))^f * 2^i =
|
|---|
| 121 | * e^(log(2)*f) * 2^i
|
|---|
| 122 | */
|
|---|
| 123 |
|
|---|
| 124 | m.val = arg * M_LOG2E;
|
|---|
| 125 | i.data = trunc_float32(m.data);
|
|---|
| 126 | f = arg * M_LOG2E - i.val;
|
|---|
| 127 |
|
|---|
| 128 | r.val = taylor_exp_32(M_LN2 * f);
|
|---|
| 129 | r.data.parts.exp += i.val;
|
|---|
| 130 | return r.val;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Double precision exponential
|
|---|
| 134 | *
|
|---|
| 135 | * Compute exponential value.
|
|---|
| 136 | *
|
|---|
| 137 | * @param arg Exponential argument.
|
|---|
| 138 | *
|
|---|
| 139 | * @return Exponential value.
|
|---|
| 140 | *
|
|---|
| 141 | */
|
|---|
| 142 | float64_t float64_exp(float64_t arg)
|
|---|
| 143 | {
|
|---|
| 144 | float64_t f;
|
|---|
| 145 | float64_u i;
|
|---|
| 146 | float64_u m;
|
|---|
| 147 | float64_u r;
|
|---|
| 148 |
|
|---|
| 149 | /*
|
|---|
| 150 | * e^a = (2 ^ log2(e))^a = 2 ^ (log2(e) * a)
|
|---|
| 151 | * log2(e) * a = i + f | f in [0, 1]
|
|---|
| 152 | * e ^ a = 2 ^ (i + f) = 2^f * 2^i = (e ^ log(2))^f * 2^i =
|
|---|
| 153 | * e^(log(2)*f) * 2^i
|
|---|
| 154 | */
|
|---|
| 155 |
|
|---|
| 156 | m.val = arg * M_LOG2E;
|
|---|
| 157 | i.data = trunc_float64(m.data);
|
|---|
| 158 | f = arg * M_LOG2E - i.val;
|
|---|
| 159 |
|
|---|
| 160 | r.val = taylor_exp_64(M_LN2 * f);
|
|---|
| 161 | r.data.parts.exp += i.val;
|
|---|
| 162 | return r.val;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** @}
|
|---|
| 166 | */
|
|---|