5
votes

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.

1
Are you sure that calls to these functions are indeed a slowdown and worth wasting time thinking about? Have your profiled your code?Michael Dorgan

1 Answers

3
votes

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.