source: mainline/uspace/lib/math/generic/log.c@ a35b458

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 4.0 KB
Line 
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
42/** log(1 - arg) approximation by Taylor series (32-bit floating point)
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 */
53static float32_t taylor_log_32(float32_t arg)
54{
55 float32_t ret = 0;
56 float32_t num = 1;
57
58 for (unsigned int i = 1; i <= TAYLOR_DEGREE_32; i++) {
59 num *= arg;
60
61 if ((i % 2) == 0)
62 ret += num / i;
63 else
64 ret -= num / i;
65 }
66
67 return ret;
68}
69
70/** log(1 - arg) approximation by Taylor series (64-bit floating point)
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 */
81static float64_t taylor_log_64(float64_t arg)
82{
83 float64_t ret = 0;
84 float64_t num = 1;
85
86 for (unsigned int i = 1; i <= TAYLOR_DEGREE_64; i++) {
87 num *= arg;
88
89 if ((i % 2) == 0)
90 ret += num / i;
91 else
92 ret -= num / i;
93 }
94
95 return ret;
96}
97
98/** Natural logarithm (32-bit floating point)
99 *
100 * @param arg Argument.
101 *
102 * @return Logarithm.
103 *
104 */
105float32_t float32_log(float32_t arg)
106{
107 float32_u m;
108 int e;
109
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;
120
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
128/** Natural logarithm (64-bit floating point)
129 *
130 * @param arg Argument.
131 *
132 * @return Logarithm.
133 *
134 */
135float64_t float64_log(float64_t arg)
136{
137 float64_u m;
138 int e;
139
140 m.val = arg;
141
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;
151
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 */
Note: See TracBrowser for help on using the repository browser.