Created
March 13, 2012 13:05
-
-
Save tobin/2028637 to your computer and use it in GitHub Desktop.
Inline atan2 function (asm)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
/* | |
Custom atan2 function calling the FPU directly, for use when the | |
math library isn't available. | |
Tobin Fricke <tobin.fricke@ligo.org> 2012-03-13 | |
Here's a reference for the GCC inline assembly syntax: | |
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html | |
*/ | |
inline double my_atan2 (double y, double x) { | |
double result; | |
asm ( | |
"fpatan\n\t" | |
: "=t" (result) // outputs; t = top of fpu stack | |
: "0" (x), // inputs; 0 = same as result | |
"u" (y) // u = 2nd floating point register | |
); | |
return result; | |
} | |
/* Test program prints results of the custom function along side | |
the results of the C library function. | |
Example usage: | |
tobin@thinktop:~$ gcc -lm -Wall inlineatan2.c -o inlineatan2 | |
tobin@thinktop:~$ echo -1 0 | ./inlineatan2 | |
-1.000000 0.000000 3.141593 3.141593 | |
*/ | |
int main(int argc, char **argv) { | |
double a, b, x, y; | |
while (2==scanf("%lf %lf", &x, &y)) { | |
a = my_atan2(y, x); | |
b = atan2(y, x); | |
printf("%lf %lf %lf %lf\n", x, y, a, b); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment