Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / common / explog_approx.c
blob6f961f3c30f90c9b1b1e8b668a95401c56139366
1 /*
3 The MIT License (MIT)
5 Copyright (c) 2015 Jacques-Henri Jourdan <jourgun@gmail.com>
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
25 Taken from https://github.com/jhjourdan/SIMD-math-prims/blob/master/simd_math_prims.h
26 Stripped down for BF use
30 #include <math.h>
31 #include <stdint.h>
33 #include "platform.h"
35 /* Workaround a lack of optimization in gcc */
36 float exp_cst1 = 2139095040.f;
37 float exp_cst2 = 0.f;
39 /* Relative error bounded by 1e-5 for normalized outputs
40 Returns invalid outputs for nan inputs
41 Continuous error */
42 float exp_approx(float val) {
43 union { int32_t i; float f; } xu, xu2;
44 float val2, val3, val4, b;
45 int32_t val4i;
46 val2 = 12102203.1615614f*val+1065353216.f;
47 val3 = val2 < exp_cst1 ? val2 : exp_cst1;
48 val4 = val3 > exp_cst2 ? val3 : exp_cst2;
49 val4i = (int32_t) val4;
50 xu.i = val4i & 0x7F800000; // mask exponent / round down to neareset 2^n (implicit mantisa bit)
51 xu2.i = (val4i & 0x7FFFFF) | 0x3F800000; // force exponent to 0
52 b = xu2.f;
54 /* Generated in Sollya with:
55 > f=remez(1-x*exp(-(x-1)*log(2)),
56 [|(x-1)*(x-2), (x-1)*(x-2)*x, (x-1)*(x-2)*x*x|],
57 [1.000001,1.999999], exp(-(x-1)*log(2)));
58 > plot(exp((x-1)*log(2))/(f+x)-1, [1,2]);
59 > f+x;
61 return
62 xu.f * (0.509871020343597804469416f + b *
63 (0.312146713032169896138863f + b *
64 (0.166617139319965966118107f + b *
65 (-2.19061993049215080032874e-3f + b *
66 1.3555747234758484073940937e-2f))));
69 /* Absolute error bounded by 1e-6 for normalized inputs
70 Returns a finite number for +inf input
71 Returns -inf for nan and <= 0 inputs.
72 Continuous error. */
73 float log_approx(float val) {
74 union { float f; int32_t i; } valu;
75 float exp, addcst, x;
76 valu.f = val;
77 exp = valu.i >> 23;
78 /* 89.970756366f = 127 * log(2) - constant term of polynomial */
79 addcst = val > 0 ? -89.970756366f : -(float)INFINITY;
80 valu.i = (valu.i & 0x7FFFFF) | 0x3F800000;
81 x = valu.f;
84 /* Generated in Sollya using:
85 > f = remez(log(x)-(x-1)*log(2),
86 [|1,(x-1)*(x-2), (x-1)*(x-2)*x, (x-1)*(x-2)*x*x,
87 (x-1)*(x-2)*x*x*x|], [1,2], 1, 1e-8);
88 > plot(f+(x-1)*log(2)-log(x), [1,2]);
89 > f+(x-1)*log(2)
91 return
92 x * (3.529304993f + x * (-2.461222105f +
93 x * (1.130626167f + x * (-0.288739945f +
94 x * 3.110401639e-2f))))
95 + (addcst + 0.69314718055995f*exp);
98 float pow_approx(float a, float b)
100 return exp_approx(b * log_approx(a));