Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any performance difference between sinf(), cosf() and sin(), cos()

I have code that works mainly with single-precision floating point numbers. Calls to transcendental functions occur fairly often. Right now, I'm using sin(), cos(), sqrt(), etc--functions that accept and return double. When it's compiled for x87 only, I know there's no difference between single and double precision. I read in Agner Fog's optimization guide however that software versions of these function utilizing SSE instructions are faster for single-precision floating point numbers.

My question is whether the compiler would automatically use the faster function when it encounters something like:

float x = 1.23;
float y = sin(x);

Or does rounding rule preclude such an optimization?

It'd be easy enough to just do a search-and-replace and see whether there's any performance gain. Trouble is that I also need pointers to these functions. In MSVC, sinf(), cosf(), and friends are inline functions. Using them would therefore requires a bit of gymnastics. Before making the effort, I would like to know whether it's worthwhile.

Besides MSVC, I'm also targeting gcc.

like image 964
cleong Avatar asked Oct 21 '22 11:10

cleong


1 Answers

There is really no need to have the cast when calling sin. In fact it would be counterproductive if you'd use the <tgmath.h> header that comes with C99. That provides you type generic macros that would chose the right function according to your argument, not for the target type unfortunately. So if you'd use that header (not sure if this is available for MS)

float x = 1.23;
float y = sin(x);

would automatically use sinf under the hood.

like image 62
Jens Gustedt Avatar answered Oct 29 '22 21:10

Jens Gustedt