math libraries

Status
Not open for further replies.

kenhorner

Active member
I'm in the process of moving a set of subsystems over from an Arduino Mega 2560 setup to a Teensy 3.2. All is working fine so far, and running much faster, but I do have a question. One of the subs .. a GPS parser and location calculator uses the standard math library from Atmel for haversine caculations... sines, cosines, tans, etc.. The Mega does it in 16 bit math and given the Teensy is 32 bit, how might I take advantage of the extra precision? I'm pretty sure their code has been optimized for 16 bit work and won't just automatically change algorithms. Do the Cortex folks provide the library support that the Atmel team does for their 32 bit processor?
 
I'm also interested in some math libraries but for the 3.5 and 3.6 which include single precision floating point unit. The FPU greatly speeds up addition, subtraction, multiply and divide but when using the standard math libraries for trig functions they are all implemented as double float and take almost as long as if there was no FPU. It would be great if someone could recommend a single precision math library to accelerate trig functions for the Teensy's that include an FPU!
 
I'm also interested in some math libraries but for the 3.5 and 3.6 which include single precision floating point unit. The FPU greatly speeds up addition, subtraction, multiply and divide but when using the standard math libraries for trig functions they are all implemented as double float and take almost as long as if there was no FPU. It would be great if someone could recommend a single precision math library to accelerate trig functions for the Teensy's that include an FPU!

#include <math.h>

and use sinf, cosf...

You can use the same for Teensys with, or without FPU. If a FPU is present, it will be used automatically.

It Includes:
Code:
• [URL="https://sourceware.org/newlib/libm.html#version"]version[/URL]:[TABLE="class: menu"]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Version of library[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#acos"]acos[/URL]:[/TD]
[TD][/TD]
[TD]Arccosine[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#acosh"]acosh[/URL]:[/TD]
[TD][/TD]
[TD]Inverse hyperbolic cosine[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#asin"]asin[/URL]:[/TD]
[TD][/TD]
[TD]Arcsine[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#asinh"]asinh[/URL]:[/TD]
[TD][/TD]
[TD]Inverse hyperbolic sine[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#atan"]atan[/URL]:[/TD]
[TD][/TD]
[TD]Arctangent[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#atan2"]atan2[/URL]:[/TD]
[TD][/TD]
[TD]Arctangent of y/x[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#atanh"]atanh[/URL]:[/TD]
[TD][/TD]
[TD]Inverse hyperbolic tangent[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#jN"]jN[/URL]:[/TD]
[TD][/TD]
[TD]Bessel functions (jN, yN)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#cbrt"]cbrt[/URL]:[/TD]
[TD][/TD]
[TD]Cube root[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#copysign"]copysign[/URL]:[/TD]
[TD][/TD]
[TD]Sign of Y, magnitude of X[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#cosh"]cosh[/URL]:[/TD]
[TD][/TD]
[TD]Hyperbolic cosine[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#erf"]erf[/URL]:[/TD]
[TD][/TD]
[TD]Error function (erf, erfc)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#exp"]exp[/URL]:[/TD]
[TD][/TD]
[TD]Exponential, base e[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#exp2"]exp2[/URL]:[/TD]
[TD][/TD]
[TD]Exponential, base 2[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#expm1"]expm1[/URL]:[/TD]
[TD][/TD]
[TD]Exponential, base e, of x - 1[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fabs"]fabs[/URL]:[/TD]
[TD][/TD]
[TD]Absolute value (magnitude)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fdim"]fdim[/URL]:[/TD]
[TD][/TD]
[TD]Positive difference[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#floor"]floor[/URL]:[/TD]
[TD][/TD]
[TD]Floor and ceiling (floor, ceil)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fma"]fma[/URL]:[/TD]
[TD][/TD]
[TD]Floating multiply add[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fmax"]fmax[/URL]:[/TD]
[TD][/TD]
[TD]Maximum[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fmin"]fmin[/URL]:[/TD]
[TD][/TD]
[TD]Minimum[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fmod"]fmod[/URL]:[/TD]
[TD][/TD]
[TD]Floating-point remainder (modulo)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#fpclassify"]fpclassify[/URL]:[/TD]
[TD][/TD]
[TD]Floating-point classification macro[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#frexp"]frexp[/URL]:[/TD]
[TD][/TD]
[TD]Split floating-point number[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#gamma"]gamma[/URL]:[/TD]
[TD][/TD]
[TD]Logarithmic gamma function[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#hypot"]hypot[/URL]:[/TD]
[TD][/TD]
[TD]Distance from origin[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#ilogb"]ilogb[/URL]:[/TD]
[TD][/TD]
[TD]Get exponent[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#infinity"]infinity[/URL]:[/TD]
[TD][/TD]
[TD]Floating infinity[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#isgreater"]isgreater[/URL]:[/TD]
[TD][/TD]
[TD]Comparison macros[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#ldexp"]ldexp[/URL]:[/TD]
[TD][/TD]
[TD]Scale by a power of 2[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#log"]log[/URL]:[/TD]
[TD][/TD]
[TD]Natural logarithms[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#log10"]log10[/URL]:[/TD]
[TD][/TD]
[TD]Base 10 logarithms[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#log1p"]log1p[/URL]:[/TD]
[TD][/TD]
[TD]Log of 1 + X[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#log2"]log2[/URL]:[/TD]
[TD][/TD]
[TD]Base 2 logarithms[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#logb"]logb[/URL]:[/TD]
[TD][/TD]
[TD]Get exponent[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#lrint"]lrint[/URL]:[/TD]
[TD][/TD]
[TD]Round to integer[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#lround"]lround[/URL]:[/TD]
[TD][/TD]
[TD]Round to integer, away from zero (lround, llround)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#matherr"]matherr[/URL]:[/TD]
[TD][/TD]
[TD]Modifiable math error handler[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#modf"]modf[/URL]:[/TD]
[TD][/TD]
[TD]Split fractional and integer parts[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#nan"]nan[/URL]:[/TD]
[TD][/TD]
[TD]Floating Not a Number[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#nearbyint"]nearbyint[/URL]:[/TD]
[TD][/TD]
[TD]Round to integer[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#nextafter"]nextafter[/URL]:[/TD]
[TD][/TD]
[TD]Get next representable number[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#pow"]pow[/URL]:[/TD]
[TD][/TD]
[TD]X to the power Y[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#remainder"]remainder[/URL]:[/TD]
[TD][/TD]
[TD]remainder of X divided by Y[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#remquo"]remquo[/URL]:[/TD]
[TD][/TD]
[TD]Remainder and part of quotient[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#rint"]rint[/URL]:[/TD]
[TD][/TD]
[TD]Round to integer[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#round"]round[/URL]:[/TD]
[TD][/TD]
[TD]Round to integer, away from zero[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#scalbn"]scalbn[/URL]:[/TD]
[TD][/TD]
[TD]Scale by a power of FLT_RADIX (2)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#signbit"]signbit[/URL]:[/TD]
[TD][/TD]
[TD]Does floating-point number have negative sign?[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#sin"]sin[/URL]:[/TD]
[TD][/TD]
[TD]Sine or cosine (sin, cos)[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#sinh"]sinh[/URL]:[/TD]
[TD][/TD]
[TD]Hyperbolic sine[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#sqrt"]sqrt[/URL]:[/TD]
[TD][/TD]
[TD]Positive square root[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#tan"]tan[/URL]:[/TD]
[TD][/TD]
[TD]Tangent[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#tanh"]tanh[/URL]:[/TD]
[TD][/TD]
[TD]Hyperbolic tangent[/TD]
[/TR]
[TR]
[TD]• [URL="https://sourceware.org/newlib/libm.html#trunc"]trunc[/URL]:[/TD]
[TD][/TD]
[TD]Round to integer, towards zero[/TD]
[/TR]
[/TABLE]

Most of them are available as "float" version.

It's just ANSI-C with extensions..

Here's the link to the documentaion, including more specialized libs (i.e. for complex numbers)
https://sourceware.org/newlib/libm.html#Top
 
Last edited:
Frank B, thanks!
I've been using the math library but I didn't realize that there was a float version of each of the math functions!
A simple loop with 100,000 sin(float input) calls takes 846 msec on the Teensy 3.6, the same loop with 100,000 sinf(float input) calls takes 56 msec!!!
Similarly, 100,000 sqrt(float input) calls takes 668 msec while the same loop with 100,000 sqrtf(float input) calls takes 24 msec.
Thanks again!
 
Last edited:
Is this true for the arm_math library as well? I would like to use the arm_cfft_radix4_f32 function with the new teensies (when they arrive) to avoid the loss of resolutions and upscalings needed with the q15 version. Will the FPU kick in automatically with this as well?
Thanks.
 
Ray,
The arm_math library looks great! Have you used the q15 versions with Teensy 3.x before?
If so, how did you install it and which version?
  • arm_cortexM4lf_math.lib (Little endian and Floating Point Unit on Cortex-M4)
  • arm_cortexM4bf_math.lib (Big endian and Floating Point Unit on Cortex-M4)
  • arm_cortexM4l_math.lib (Little endian on Cortex-M4)
  • arm_cortexM4b_math.lib (Big endian on Cortex-M4)
Thanks,
Brian
 
The libraries are already installed. E.G. if you compile a sketch which contains a call to arm_sqrt_f32 for a Teensy 3.6 (or presumably 3.5) the argument "-larm_cortexM4lf_math" is tacked on the end of one of the last steps in the compile/link process. If you only use q15 it may load arm_cortexM4l_math instead (haven't tested that).

Pete
 
el_supremo, thanks.

I just tried arm_sin_f32 and arm_sqrt_f32, the both work!
My simple loop summing 100,000 arm_sin_f32() took 51 msec vs. 54 msec for the same loop using sinf() vs. 846 msec for sin().
The result of the sum for sin() and sinf() was identical (45969.410156), the result for arm_sin_f32() was slightly different (45969.406250).
The same loop with arm_sqrt_f32() took 26 msec vs. 24 msec using sqrtf(), perhaps because it's storing the result in a pointer that I then have to add?

I'll definitely be checking out some of the other trig, vector and matrix functions from this library!

Brian
 
Brian, I have only use the arm_cfft q15 functions so far. They work as documented, but have some limitations based on the 16 bit integer depth they use.

A point about the arm_math functions - not all all of the functions identified in the documentation I use (http://www.keil.com/pack/doc/CMSIS/DSP/html/index.html) seem to be included in the teensy arm_math library, in particular for me the arm_cfft_f32 function.

Does anyone know what the issue is? Maybe licensing? Will they be available later sometime?
 
Status
Not open for further replies.
Back
Top