GNU C中的__attribute __((const))vs __attribute __((pure))

时间:2021-02-06 01:51:37

What is the difference between __attribute__((const)) and __attribute__((pure)) in GNU C?

GNU C中__attribute __((const))和__attribute __((纯))有什么区别?

__attribute__((const)) int f() {
    /* ... */
    return 4;
}

vs

VS

__attribute__((pure)) int f() {
    /* ... */
    return 4;
}

2 个解决方案

#1


26  

From the documentation for the ARM compiler (which is based on gcc):

从ARM编译器的文档(基于gcc):

__attribute__((pure)) function attribute
Many functions have no effects except to return a value, and their return value depends only on the parameters and global variables. Functions of this kind can be subject to data flow analysis and might be eliminated.

__attribute __((pure))函数属性除了返回值之外,许多函数都没有效果,它们的返回值仅取决于参数和全局变量。这种功能可以进行数据流分析,可以消除。

__attribute__((const)) function attribute
Many functions examine only the arguments passed to them, and have no effects except for the return value. This is a much stricter class than __attribute__((pure)), because a function is not permitted to read global memory. If a function is known to operate only on its arguments then it can be subject to common sub-expression elimination and loop optimizations.

__attribute __((const))function属性许多函数只检查传递给它们的参数,除了返回值之外没有任何效果。这是一个比__attribute __((pure))更严格的类,因为不允许函数读取全局内存。如果已知函数仅对其参数进行操作,则可以对常见的子表达式消除和循环优化进行处理。

So, TL;DR: __attribute__((const)) is the same as __attribute__((pure)) but without any access to global variables.

因此,TL; DR:__ attribute __((const))与__attribute __((纯))相同,但不能访问全局变量。

#2


5  

The difference is explained in the GCC manuals

GCC手册中解释了这种差异

The __attribute__ ((pure)) means that the function has no side effects and the value returned depends on the arguments and the state of global variables. Therefore it is safe for the optimizer to elide some calls to it, if the arguments are the same, and the state of the globals didn't change in between calls.

__attribute __((纯))表示该函数没有副作用,返回的值取决于参数和全局变量的状态。因此,如果参数相同,并且全局变量的状态在调用之间没有变化,优化器可以安全地忽略对它的一些调用。

The __attribute__ ((const)) means that the return value is solely a function of the arguments, and if any of the arguments are pointers, then the pointers must not be dereferenced.

__attribute __((const))表示返回值只是参数的函数,如果任何参数是指针,则指针不得解除引用。

A const function is always pure.

const函数总是纯粹的。

Examples of const functions would be the mathematical functions from <math.h>: sqrt, exp, etc. (Though they might be subject to rounding modes).

const函数的例子是来自 的数学函数:sqrt,exp等(尽管它们可能受到舍入模式的影响)。

Examples of pure but non-const functions would be such functions as strlen - as it dereferences the pointer passed in.

纯但非const函数的例子是strlen这样的函数 - 因为它取消引用传入的指针。

#1


26  

From the documentation for the ARM compiler (which is based on gcc):

从ARM编译器的文档(基于gcc):

__attribute__((pure)) function attribute
Many functions have no effects except to return a value, and their return value depends only on the parameters and global variables. Functions of this kind can be subject to data flow analysis and might be eliminated.

__attribute __((pure))函数属性除了返回值之外,许多函数都没有效果,它们的返回值仅取决于参数和全局变量。这种功能可以进行数据流分析,可以消除。

__attribute__((const)) function attribute
Many functions examine only the arguments passed to them, and have no effects except for the return value. This is a much stricter class than __attribute__((pure)), because a function is not permitted to read global memory. If a function is known to operate only on its arguments then it can be subject to common sub-expression elimination and loop optimizations.

__attribute __((const))function属性许多函数只检查传递给它们的参数,除了返回值之外没有任何效果。这是一个比__attribute __((pure))更严格的类,因为不允许函数读取全局内存。如果已知函数仅对其参数进行操作,则可以对常见的子表达式消除和循环优化进行处理。

So, TL;DR: __attribute__((const)) is the same as __attribute__((pure)) but without any access to global variables.

因此,TL; DR:__ attribute __((const))与__attribute __((纯))相同,但不能访问全局变量。

#2


5  

The difference is explained in the GCC manuals

GCC手册中解释了这种差异

The __attribute__ ((pure)) means that the function has no side effects and the value returned depends on the arguments and the state of global variables. Therefore it is safe for the optimizer to elide some calls to it, if the arguments are the same, and the state of the globals didn't change in between calls.

__attribute __((纯))表示该函数没有副作用,返回的值取决于参数和全局变量的状态。因此,如果参数相同,并且全局变量的状态在调用之间没有变化,优化器可以安全地忽略对它的一些调用。

The __attribute__ ((const)) means that the return value is solely a function of the arguments, and if any of the arguments are pointers, then the pointers must not be dereferenced.

__attribute __((const))表示返回值只是参数的函数,如果任何参数是指针,则指针不得解除引用。

A const function is always pure.

const函数总是纯粹的。

Examples of const functions would be the mathematical functions from <math.h>: sqrt, exp, etc. (Though they might be subject to rounding modes).

const函数的例子是来自 的数学函数:sqrt,exp等(尽管它们可能受到舍入模式的影响)。

Examples of pure but non-const functions would be such functions as strlen - as it dereferences the pointer passed in.

纯但非const函数的例子是strlen这样的函数 - 因为它取消引用传入的指针。