[992ffa6] | 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 |
|
---|
[9adb61d] | 51 | /** Exponential approximation by Taylor series (32-bit floating point)
|
---|
[992ffa6] | 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 | */
|
---|
[9adb61d] | 63 | static float32_t taylor_exp_32(float32_t arg)
|
---|
[992ffa6] | 64 | {
|
---|
[9adb61d] | 65 | float32_t ret = 1;
|
---|
| 66 | float32_t nom = 1;
|
---|
[992ffa6] | 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 |
|
---|
[9adb61d] | 76 | /** Exponential approximation by Taylor series (64-bit floating point)
|
---|
[992ffa6] | 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 |
|
---|
[9adb61d] | 101 | /** Exponential (32-bit floating point)
|
---|
[992ffa6] | 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;
|
---|
[e6f5766] | 113 | float32_t i;
|
---|
[992ffa6] | 114 | float32_u r;
|
---|
| 115 |
|
---|
| 116 | /*
|
---|
| 117 | * e^a = (2 ^ log2(e))^a = 2 ^ (log2(e) * a)
|
---|
| 118 | * log2(e) * a = i + f | f in [0, 1]
|
---|
| 119 | * e ^ a = 2 ^ (i + f) = 2^f * 2^i = (e ^ log(2))^f * 2^i =
|
---|
| 120 | * e^(log(2)*f) * 2^i
|
---|
| 121 | */
|
---|
| 122 |
|
---|
[9adb61d] | 123 | i = trunc_f32(arg * M_LOG2E);
|
---|
[e6f5766] | 124 | f = arg * M_LOG2E - i;
|
---|
[992ffa6] | 125 |
|
---|
| 126 | r.val = taylor_exp_32(M_LN2 * f);
|
---|
[e6f5766] | 127 | r.data.parts.exp += i;
|
---|
[992ffa6] | 128 | return r.val;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[9adb61d] | 131 | /** Exponential (64-bit floating point)
|
---|
[992ffa6] | 132 | *
|
---|
| 133 | * Compute exponential value.
|
---|
| 134 | *
|
---|
| 135 | * @param arg Exponential argument.
|
---|
| 136 | *
|
---|
| 137 | * @return Exponential value.
|
---|
| 138 | *
|
---|
| 139 | */
|
---|
| 140 | float64_t float64_exp(float64_t arg)
|
---|
| 141 | {
|
---|
| 142 | float64_t f;
|
---|
[e6f5766] | 143 | float64_t i;
|
---|
[992ffa6] | 144 | float64_u r;
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * e^a = (2 ^ log2(e))^a = 2 ^ (log2(e) * a)
|
---|
| 148 | * log2(e) * a = i + f | f in [0, 1]
|
---|
| 149 | * e ^ a = 2 ^ (i + f) = 2^f * 2^i = (e ^ log(2))^f * 2^i =
|
---|
| 150 | * e^(log(2)*f) * 2^i
|
---|
| 151 | */
|
---|
| 152 |
|
---|
[9adb61d] | 153 | i = trunc_f64(arg * M_LOG2E);
|
---|
[e6f5766] | 154 | f = arg * M_LOG2E - i;
|
---|
[992ffa6] | 155 |
|
---|
| 156 | r.val = taylor_exp_64(M_LN2 * f);
|
---|
[e6f5766] | 157 | r.data.parts.exp += i;
|
---|
[992ffa6] | 158 | return r.val;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /** @}
|
---|
| 162 | */
|
---|