将变量存储在CPU寄存器中 [英] Storing variables in CPU registers
问题描述
我已经读到CPU寄存器是有限的,并且该值也可以自动存储。
I have read that CPU registers are limited and the value can also get stored in auto.
如何确认变量是否存储在C语言的寄存器中?
How to confirm if the variable is stored in a register in C?
可以存储什么样的变量?
What kind of variables can be stored?
推荐答案
大多数编译器支持诸如 -S
或 / S
之类的标志,这些标志生成汇编语言输出。您可以检查这段代码,以查看编译器是否在给定点将给定变量存储在寄存器中。
Most compilers support a flag such as -S
or /S
that generates assembly-language output. You can inspect this code to see whether your compiler stores a given variable in a register at a given point.
没有保证可以放入寄存器的类型。实际上,一些基于堆栈的计算机(包括Andrew Tannenbaum教科书中的JVM的硬件实现)根本没有显式寄存器。但是, int
通常与寄存器的大小相同(定义 int
和<$ c的64位环境$ c> long 为32位(向后兼容)是一个例外),指针通常保存一个机器地址,因此通常与寄存器的大小相同(分段存储器模型,例如16位x86,其中地址适合两个寄存器的情况除外),并且 size_t
和 ptrdiff_t
保存数组索引,因此通常是相同的大小作为寄存器(具有64位代码但具有32位存储空间的x32目标是例外)。大多数CPU都有浮点寄存器,每个浮点寄存器都可以容纳 double
来进行数学运算,但有些则不行。
There is no type guaranteed to fit into a register. In fact, some stack-based machines, including the hardware implementation of JVM in Andrew Tannenbaum’s textbook, do not have explicit registers at all. However, an int
usually is the same size as a register (64-bit environments that define int
and long
as 32-bit for backward compatibility are exceptions), a pointer usually holds a machine address and therefore is usually the same size as a register (segmented memory models such as 16-bit x86, where addresses fit into two registers, are exceptions), and size_t
and ptrdiff_t
hold array indices and are therefore usually the same size as a register (The x32 target, which has 64-bit code but a 32-bit memory space, is an exception). Most CPUs have floating-point registers that can each hold a double
to do math on it, but some don’t.
如果您想要的是快速可移植的代码,最好的选择是使用诸如 int_fast16_t
和 uint_fast32_t
来自< stdint.h>
。保证它们是通常最快的尺寸,至少足够宽。在正常的目标上,这将是机器寄存器的大小。
If what you want is fast portable code, your best bet is to use the types such as int_fast16_t
and uint_fast32_t
from <stdint.h>
. These are guaranteed to be the usually-fastest size that’s at least wide enough. and on normal targets, that’s going to be the size of a machine register.
这篇关于将变量存储在CPU寄存器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!