Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math libraries
15-03-2015, 09:45 PM, (This post was last modified: 16-03-2015, 12:24 PM by joel.)
#1
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.1920928955078125e-7f;

 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.1920928955078125e-7f;
 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.2629582881927490e-8f;
 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
Reply
16-03-2015, 02:08 PM,
#2
RE: Math libraries
(15-03-2015, 09:45 PM)joel Wrote: Edit: i can write the library(with pdl and so on) if pinguino team want to implement these fast approx functions

It would be great. You might add the new functions to trigo.c (sin and cos, float and integer approximations) and mathlib.c or merge all functions in one file, it's up to you.

As far as I know the LGPL is compatible with the BSD license so It shouldn't be a problem to include this work in Pinguino as long as you make reference to the author and (at least) a link to the license text.
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
21-03-2015, 08:12 PM, (This post was last modified: 21-03-2015, 08:17 PM by joel.)
#3
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.




.pdl32   fastmath.pdl32 (Size: 2.35 KB / Downloads: 2)

.h   fastmath.h (Size: 15.4 KB / Downloads: 4)


Joël
Reply
23-03-2015, 09:29 AM,
#4
RE: Math libraries
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 ?
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
23-03-2015, 06:11 PM,
#5
RE: Math libraries
(23-03-2015, 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 Big Grin  !!! It will be a pleasure to do the test... Cool

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
Reply
23-03-2015, 06:58 PM,
#6
RE: Math libraries
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 = ...
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
23-03-2015, 08:26 PM,
#7
RE: Math libraries
(23-03-2015, 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.... Sad

So for the time being..this lib will be useable only with P32.... Undecided

Joël
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)