有没有办法强制 C++ 编译器将变量存储在寄存器中?
Is there a way to force a C++ compiler to store a variable in a register?
我知道 inline
关键字使编译器更有可能内联一个函数,尽管决定权在编译器,而 GNU 扩展 __attribute__((always_inline))
强制编译器内联它。
相应的,有没有...
__attribute__((always_register)) register int foo = 678;
...或类似的东西?
GCC 有一种方法可以通过使用关键字 asm
and register
:
来指定局部变量的寄存器
register int *foo asm ("r12");
注意引用:
The only supported use for this feature is to specify registers for
input and output operands when calling Extended asm
.
此功能在某些体系结构的 Linux 内核中用于访问存储在通用寄存器之一中的线程本地存储。这就是 current
宏在 ARC architecture:
上的实现方式
register struct task_struct *curr_arc asm("r25");
#define current (curr_arc)
这种用法很有趣,因为它在全局范围内使用 register
关键字,而在标准 C 中它只能在本地使用。
我知道 inline
关键字使编译器更有可能内联一个函数,尽管决定权在编译器,而 GNU 扩展 __attribute__((always_inline))
强制编译器内联它。
相应的,有没有...
__attribute__((always_register)) register int foo = 678;
...或类似的东西?
GCC 有一种方法可以通过使用关键字 asm
and register
:
register int *foo asm ("r12");
注意引用:
The only supported use for this feature is to specify registers for input and output operands when calling Extended
asm
.
此功能在某些体系结构的 Linux 内核中用于访问存储在通用寄存器之一中的线程本地存储。这就是 current
宏在 ARC architecture:
register struct task_struct *curr_arc asm("r25");
#define current (curr_arc)
这种用法很有趣,因为它在全局范围内使用 register
关键字,而在标准 C 中它只能在本地使用。