[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 <log.h>
|
---|
| 37 | #include <math.h>
|
---|
| 38 |
|
---|
| 39 | #define TAYLOR_DEGREE_32 31
|
---|
| 40 | #define TAYLOR_DEGREE_64 63
|
---|
| 41 |
|
---|
[9adb61d] | 42 | /** log(1 - arg) approximation by Taylor series (32-bit floating point)
|
---|
[992ffa6] | 43 | *
|
---|
| 44 | * Compute the approximation of log(1 - arg) by a Taylor
|
---|
| 45 | * series (using the first TAYLOR_DEGREE terms).
|
---|
| 46 | * arg must be within [-1, 1].
|
---|
| 47 | *
|
---|
| 48 | * @param arg Argument.
|
---|
| 49 | *
|
---|
| 50 | * @return log(1 - arg)
|
---|
| 51 | *
|
---|
| 52 | */
|
---|
| 53 | static float32_t taylor_log_32(float32_t arg)
|
---|
| 54 | {
|
---|
| 55 | float32_t ret = 0;
|
---|
| 56 | float32_t num = 1;
|
---|
[a35b458] | 57 |
|
---|
[992ffa6] | 58 | for (unsigned int i = 1; i <= TAYLOR_DEGREE_32; i++) {
|
---|
| 59 | num *= arg;
|
---|
[a35b458] | 60 |
|
---|
[992ffa6] | 61 | if ((i % 2) == 0)
|
---|
| 62 | ret += num / i;
|
---|
| 63 | else
|
---|
| 64 | ret -= num / i;
|
---|
| 65 | }
|
---|
[a35b458] | 66 |
|
---|
[992ffa6] | 67 | return ret;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[9adb61d] | 70 | /** log(1 - arg) approximation by Taylor series (64-bit floating point)
|
---|
[992ffa6] | 71 | *
|
---|
| 72 | * Compute the approximation of log(1 - arg) by a Taylor
|
---|
| 73 | * series (using the first TAYLOR_DEGREE terms).
|
---|
| 74 | * arg must be within [-1, 1].
|
---|
| 75 | *
|
---|
| 76 | * @param arg Argument.
|
---|
| 77 | *
|
---|
| 78 | * @return log(1 - arg)
|
---|
| 79 | *
|
---|
| 80 | */
|
---|
| 81 | static float64_t taylor_log_64(float64_t arg)
|
---|
| 82 | {
|
---|
| 83 | float64_t ret = 0;
|
---|
| 84 | float64_t num = 1;
|
---|
[a35b458] | 85 |
|
---|
[992ffa6] | 86 | for (unsigned int i = 1; i <= TAYLOR_DEGREE_64; i++) {
|
---|
| 87 | num *= arg;
|
---|
[a35b458] | 88 |
|
---|
[992ffa6] | 89 | if ((i % 2) == 0)
|
---|
| 90 | ret += num / i;
|
---|
| 91 | else
|
---|
| 92 | ret -= num / i;
|
---|
| 93 | }
|
---|
[a35b458] | 94 |
|
---|
[992ffa6] | 95 | return ret;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[9adb61d] | 98 | /** Natural logarithm (32-bit floating point)
|
---|
[992ffa6] | 99 | *
|
---|
| 100 | * @param arg Argument.
|
---|
| 101 | *
|
---|
| 102 | * @return Logarithm.
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
| 105 | float32_t float32_log(float32_t arg)
|
---|
| 106 | {
|
---|
| 107 | float32_u m;
|
---|
| 108 | int e;
|
---|
[a35b458] | 109 |
|
---|
[992ffa6] | 110 | m.val = arg;
|
---|
| 111 | /*
|
---|
| 112 | * Factor arg into m * 2^e where m has exponent -1,
|
---|
| 113 | * which means it is in [1.0000..e-1, 1.1111..e-1] = [0.5, 1.0]
|
---|
| 114 | * so the argument to taylor_log_32 will be in [0, 0.5]
|
---|
| 115 | * ensuring that we get at least one extra bit of precision
|
---|
| 116 | * in each iteration.
|
---|
| 117 | */
|
---|
| 118 | e = m.data.parts.exp - (FLOAT32_BIAS - 1);
|
---|
| 119 | m.data.parts.exp = FLOAT32_BIAS - 1;
|
---|
[a35b458] | 120 |
|
---|
[992ffa6] | 121 | /*
|
---|
| 122 | * arg = m * 2^e ; log(arg) = log(m) + log(2^e) =
|
---|
| 123 | * log(m) + log2(2^e) / log2(e) = log(m) + e / log2(e)
|
---|
| 124 | */
|
---|
| 125 | return - taylor_log_32(m.val - 1.0) + e / M_LOG2E;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[9adb61d] | 128 | /** Natural logarithm (64-bit floating point)
|
---|
[992ffa6] | 129 | *
|
---|
| 130 | * @param arg Argument.
|
---|
| 131 | *
|
---|
| 132 | * @return Logarithm.
|
---|
| 133 | *
|
---|
| 134 | */
|
---|
| 135 | float64_t float64_log(float64_t arg)
|
---|
| 136 | {
|
---|
| 137 | float64_u m;
|
---|
| 138 | int e;
|
---|
[a35b458] | 139 |
|
---|
[992ffa6] | 140 | m.val = arg;
|
---|
[a35b458] | 141 |
|
---|
[992ffa6] | 142 | /*
|
---|
| 143 | * Factor arg into m * 2^e where m has exponent -1,
|
---|
| 144 | * which means it is in [1.0000..e-1, 1.1111..e-1] = [0.5, 1.0]
|
---|
| 145 | * so the argument to taylor_log_32 will be in [0, 0.5]
|
---|
| 146 | * ensuring that we get at least one extra bit of precision
|
---|
| 147 | * in each iteration.
|
---|
| 148 | */
|
---|
| 149 | e = m.data.parts.exp - (FLOAT64_BIAS - 1);
|
---|
| 150 | m.data.parts.exp = FLOAT64_BIAS - 1;
|
---|
[a35b458] | 151 |
|
---|
[992ffa6] | 152 | /*
|
---|
| 153 | * arg = m * 2^e ; log(arg) = log(m) + log(2^e) =
|
---|
| 154 | * log(m) + log2(2^e) / log2(e) = log(m) + e / log2(e)
|
---|
| 155 | */
|
---|
| 156 | return - taylor_log_64(m.val - 1.0) + e / M_LOG2E;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /** @}
|
---|
| 160 | */
|
---|