View math.cpp code?

Hi there!

Yes I might be in the wrong section with this question, couldn’t find the right topic section, but anyway…

I have the problem that I can not find any source code of the math.cpp from <math.h>. I’d like to see hiw atan2 is implemented because there is something strange about it concerning time consumption. When I measure the time only used by z = atan2(x, y) and a variable as x and y, it uses 4us. BUT when I use “z” - variable below my time measurement process and the serial.print(time) again as f. e. S =atan2(z, u) , the same section z=atan2(x, y) which I measure the time and print it out uses 200us, completely crazy…

Example

first scenario:

Start_time = micros()
z = atan2(x, y) ;
Stop_time = micros()
timeused = Stop_time
Serial.println(time used) //4us

And the strange scenario:

Start_time = micros()
z = atan2(x, y) ;
Stop_time = micros()
timeused = Stop_time
Serial.println(time used) //200us!!!

s = atan2(z, r)

makes no sense to me seems like the z-result from atan2() is still connected with atan2() and baaaah I don’t know nothing makes sense here…

So thats why I’d like to see the source code of math.cpp, thanks for your suggestions!

On my Windows PC math.h is in C:\Program Files (x86)\Arduino\1-8-10\hardware\tools\avr\avr\include\math.h

/**
    The atan() function computes the principal value of the arc tangent
    of \a __x.  The returned value is in the range [-pi/2, pi/2] radians.
 */
extern double atan(double __x) __ATTR_CONST__;
#define atanf	atan		/**< The alias for atan().	*/

/**
    The atan2() function computes the principal value of the arc tangent
    of <em>__y / __x</em>, using the signs of both arguments to determine
    the quadrant of the return value.  The returned value is in the range
    [-pi, +pi] radians.
 */
extern double atan2(double __y, double __x) __ATTR_CONST__;
#define atan2f	atan2		/**< The alias for atan2().	*/

/**

Please give a working sketch that we can try. Tell us which Arduino board you use.

I think that the 4µs is when the compiler already pre-calculated the result and the 200µs is when the atan2() is actually used runtime.

The source code of math.cpp is in the source of the GCC compiler. It depends on the board that you use.

GCC: https://gcc.gnu.org/.
This might be a mirror of the source code: GitHub - gcc-mirror/gcc.
But when I search in that mirror for ‘atan2’, then there are too many for me. I do not understand how the source of the GCC compiler is organized.

EDIT: It is not part of the GCC compiler but part of the avr libc, see the answer by westfw below.

myself_earlier:

timeused = Stop_time

Serial.println(time used)



Look closer. You are not calculating duration.

EDIT -- never mind, that was not the issue

extern double atan2(double __y, double __x) __ATTR_CONST__;

https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

const

Calls to functions whose return value is not affected by changes to the observable state of the program and that have no observable effects on such state other than to return a value may lend themselves to optimizations such as common subexpression elimination. Declaring such functions with the const attribute allows GCC to avoid emitting some calls in repeated invocations of the function with the same argument values.

If you don't use z for anything, the function call can be optimized away.

About 200us is correct for a 16MHz AVR. See avr-libc: Benchmarks

Source code is actually part of avr-libc (for AVR) - routines that are much more highly optimized for the AVR than the standard gcc code.
Actual source code here: [avr-libc] Contents of /trunk/avr-libc/libm/fplib/atan2.S
(It's probably not very useful.)

If you don't use z for anything, the function call can be optimized away.

About 200us is correct for a 16MHz AVR.

Thank you so much everyone. You are fantastic. I use the arduino mega 2560 embedded version. Super cool board. I am working on a drone controller with multiple sensors, thats why time consumption is everything. And the list from westfw with the different timeconsumptions used for trigonometric functions helped me a lot to see the potential of writing my own trigo-functions. atan2() fromI will see how accurate i can land with the own implementations and will also share it if it works out.
I also thought of making a lookuptable for the best speed, i have esc signals from 1000 to 2000 so and because this are ints i think a list of 1000 values would be enough but that's only my firts guess...

Cheers!