You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In GCC attribute__((const)) and attribute((pure)) are used to mark functions as depending only their input. Having this information allows compilers to make more aggressive optimizations correctly and more easily.
pure
Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure. For example,
int square (int) __attribute__ ((pure));
says that the hypothetical function square is safe to call fewer times than the program says.
Some of common examples of pure functions are strlen or memcmp. Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between two consecutive calls (such as feof in a multithreading environment).
The attribute pure is not implemented in GCC versions earlier than 2.96.
Const is just like pure except the function also isn't allowed to dereference pointers it is passed. Here is the manual entry for the const attribute:
const
Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory.
Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void.
The attribute const is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function has no side effects, which works in the current version and in some older versions, is as follows:
typedef int intfn ();
extern const intfn square;
This approach does not work in GNU C++ from 2.6.0 on, since the language specifies that the `const' must be attached to the return value.
I propose that we add two macros for tagging functions PURE for const and PURE_WITH_GLOBAL for pure. Which apply these attributes to functions in compilers that support them and resolve to empty on compilers that don't. We might want to come up with better names before.
The text was updated successfully, but these errors were encountered:
In GCC attribute__((const)) and attribute((pure)) are used to mark functions as depending only their input. Having this information allows compilers to make more aggressive optimizations correctly and more easily.
According to the GCC manual - https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html :
Const is just like pure except the function also isn't allowed to dereference pointers it is passed. Here is the manual entry for the const attribute:
Most other compilers seem to support GCC attributes including Clang and intel: https://stackoverflow.com/questions/2798188/pure-const-function-attributes-in-different-compilers
I propose that we add two macros for tagging functions PURE for
const
and PURE_WITH_GLOBAL for pure. Which apply these attributes to functions in compilers that support them and resolve to empty on compilers that don't. We might want to come up with better names before.The text was updated successfully, but these errors were encountered: