From fae6e34e50b31461279bbd6e2c2bf8dccbb624c8 Mon Sep 17 00:00:00 2001 From: Brian Vermillion Date: Mon, 15 Jun 2015 19:58:45 -0400 Subject: [PATCH 1/2] math: p_exp: Implement exponential function --- src/math/p_exp.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/math/p_exp.c b/src/math/p_exp.c index 64eecf2..fded25b 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -1,10 +1,13 @@ #include - +#include /** * * Calculate exponent (e^a), where e is the base of the natural logarithm * (2.71828.) * + * Based of the algorithm in this paper: http://www.schraudolph.org/pubs/Schraudolph99.pdf, + * It calculates a approximation of e^a very efficiently, but at the cost of accuracy. + * * @param a Pointer to input vector * * @param c Pointer to output vector @@ -17,9 +20,16 @@ #include void p_exp_f32(const float *a, float *c, int n) { - int i; for (i = 0; i < n; i++) { - *(c + i) = expf(*(a + i)); + union + { + float f; + uint32_t i; + } u; + + // = 2^23 / M_LN2 * (*(a+i)) + (127 * 2^23 - 100000) + u.i = 12102203.16156 * ( *( a + i )) + (1065353216 - 100000); + *(c + i) = u.f; } } From a98f111417aea4f0ce1f1f9eae2a5a7912bb04f8 Mon Sep 17 00:00:00 2001 From: Brian Vermillion Date: Mon, 15 Jun 2015 19:16:32 -0400 Subject: [PATCH 2/2] Removed leftover includes --- src/math/p_exp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/math/p_exp.c b/src/math/p_exp.c index fded25b..7f37671 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -1,5 +1,4 @@ #include -#include /** * * Calculate exponent (e^a), where e is the base of the natural logarithm @@ -17,7 +16,7 @@ * @return None * */ -#include + void p_exp_f32(const float *a, float *c, int n) { int i;