views:

68

answers:

3

I'm writing some code where there are a bunch of simple pure functions that get called a lot. It's perfectly safe if these functions get get optimized to be called less often.

Currently I am using gcc as my compiler and I'm wondering if there is a portable way of doing:

int foo(int) __attribute__ ((pure))

Info about the pure keyword can be found here: http://www.ohse.de/uwe/articles/gcc-attributes.html#func-pure

How would I go about implementing something like this if the pure keyword is not available?

+4  A: 

No, there is not.

wilx
:( I suspected this was the case.
shuttle87
+3  A: 
#ifdef __GNUC__
#define __pure __attribute__((pure))
#else
#define __pure
#endif

Use __pure when you need it

Nicolas Viennot
I like this suggestion however I was hoping that I could get the same performance elsewhere. If I can't I will use this though.
shuttle87
if you want that performance, make your function `static inline` in the header
Nicolas Viennot
Will static inline do the same sort of optimization as the pure attribute?
shuttle87
Quite the same yes.
Nicolas Viennot
+1  A: 

I think the portable way is to inline the functions and hope the compiler will figure out the rest.

Laurynas Biveinis