15032015, 09:45 PM,
(This post was last modified: 16032015, 12:24 PM by joel.)


joel
Member

Posts: 107
Threads: 22
Joined: Dec 2011


Math libraries
Dear all,
Few monthes ago i wanted to write an application to calculate the dewpoint based on temperature and humidity measurements (with DHT22). I was stucked until recently, lacking of a proper ln/log function.
One week ago trying to get my hands dirty again on this topic i finally found a code (actually two variants) under New BSD license which is more than appropriate for my needs:
here they are
Code:
static inline float
fastlog2 (float x)
{
union { float f; u32 i; } vx = { x };
union { u32 i; float f; } mx = { (vx.i & 0x007FFFFF)  0x3f000000 };
float y = vx.i;
y *= 1.1920928955078125e7f;
return y  124.22551499f
 1.498030302f * mx.f
 1.72587999f / (0.3520887068f + mx.f);
}
static inline float
fastlog (float x) //ln(x)
{
return 0.69314718f * fastlog2 (x);
}
static inline float
fastlog10 (float x) //log10(x)
{
return 0.30102999f * fastlog2 (x);
}
static inline float
fasterlog2 (float x)
{
union { float f; u32 i; } vx = { x };
float y = vx.i;
y *= 1.1920928955078125e7f;
return y  126.94269504f;
}
static inline float
fasterlog (float x) //ln(x)
{
// return 0.69314718f * fasterlog2 (x);
union { float f; u32 i; } vx = { x };
float y = vx.i;
y *= 8.2629582881927490e8f;
return y  87.989971088f;
}
They return an approximation, but accurate enough (4 or 5 digits after the point..) and quite fast for my needs (did'nt tested the time,but that's easy to do )
There is also
 exponential, logarithm, and power
 lgamma and digamma
 cosh, sinh, tanh
 cos, sin, tan
 sigmoid and erf
 Lambert W
which may be interesting for our needs
May be a library with these function can be of interest for everyone using pinguino, which is currently lacking of some math functions (log, cosh, sinh, tanh), and even the others if they appears faster than those already available mind you?
Code source is at
https://code.google.com/p/fastapprox/sou...approx/src
and easy to adapt for our small µC, if we can include BSD licensed code within pinguino
Joël
Edit: i can write the library(with pdl and so on) if pinguino team want to implement these fast approx functions


21032015, 08:12 PM,
(This post was last modified: 21032015, 08:17 PM by joel.)


joel
Member

Posts: 107
Threads: 22
Joined: Dec 2011


RE: Math libraries
Hi all,
i have written these fastmath library, for pic32only (doesn't work on P8, inline do not work and the result of calculation are false if removed , maybe union do not work either).
here is the code to be place in the librarie directory and in the pdl directory
i did an estimate for the fastln function, it tooks around 1.5 µs for 1 call to the function.
fastmath.pdl32 (Size: 2.35 KB / Downloads: 2)
fastmath.h (Size: 15.4 KB / Downloads: 4)
Joël


23032015, 06:11 PM,


joel
Member

Posts: 107
Threads: 22
Joined: Dec 2011


RE: Math libraries
(23032015, 09:29 AM)regis Wrote: Thank you.
I will add your lib. to the v11.
By the way, do you have a PIC32MX270 ? The bootloader is almost finished and you maybe want to test it ?
Yes i do, for sure !!! It will be a pleasure to do the test...
For the library, will try to see if there is a solution to make it work on the P8. i believe the issue is in the union , but not sure. I don't know if we can use union with SDCC (or if we can use it the way it is in this code)
Joël


23032015, 08:26 PM,


joel
Member

Posts: 107
Threads: 22
Joined: Dec 2011


RE: Math libraries
(23032015, 06:58 PM)regis Wrote: Hi Joel,
Thank you for your help.
Please send me your email by PM and I will send you the bootloader.
About your lib., I noticed 2 things :
1/ AFAIK SDCC doesn't support the inline command so I think the best is to remove it and rename fastmath.h in fastmath.c (also in the pdl file).
2/ As you said, you can't use union this way with SDCC. You have to declare variable at the very beginning of functions not whatever you want. I used something like typedef union { u32 i; float f; } helper; in the printf library. So you could do the same :
at the beginning of the library :
typedef union {
u32 i;
float f;
} helper;
and then in each function where helper is needed :
helper v;
...
...
v = ...
Hi Regis,
PM sent.
For the library, i achieved the same conclusion...!
So i rewritten things the way you described....ant it works like a charm as long as you use constant when you call your function
(for example fastlog(5.0) produce the correct result and do not trigger any error but if you do something like
for (k=1.0, k<10.0, k++){
fastlog(k);
}
you get an error at compilation ( cannot assign values to aggregates) which is currently a known limitation of SDCC....
So for the time being..this lib will be useable only with P32....
Joël


