C语言 确定我的处理器的字长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2301007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine word size of my processor
提问by user69514
How do I determine the word size of my CPU? If I understand correct an intshould be one word right? I'm not sure if I am correct.
如何确定我的 CPU 的字长?如果我理解正确,int应该是一个词吧?我不确定我是否正确。
So should just printing sizeof(int)would be enough to determine the word size of my processor?
那么仅打印sizeof(int)就足以确定我的处理器的字长吗?
回答by Clifford
Your assumption about sizeof(int) is untrue; see this.
你对 sizeof(int) 的假设是不真实的;看到这个。
Since you must know the processor, OS and compiler at compilation time, the word size can be inferred using predefined architecture/OS/compiler macrosprovided by the compiler.
由于您必须在编译时了解处理器、操作系统和编译器,因此可以使用编译器提供的预定义架构/操作系统/编译器宏来推断字长。
However while on simpler and most RISC processors, word size, bus width, register size and memory organisation are often consistently one value, this may not be true to more complex CISC and DSP architectures with various sizes for floating point registers, accumulators, bus width, cache width, general purpose registers etc.
然而,虽然在更简单和大多数 RISC 处理器上,字长、总线宽度、寄存器大小和存储器组织通常始终是一个值,但对于具有各种大小的浮点寄存器、累加器、总线宽度的更复杂的 CISC 和 DSP 架构来说,这可能不是真的、缓存宽度、通用寄存器等。
Of course it begs the question why you might need to know this? Generally you would use the type appropriate to the application, and trust the compiler to provide any optimisation. If optimisation is what you think you need this information for, then you would probably be better off using the C99 'fast' types. If you need to optimise a specific algorithm, implement it for a number of types and profile it.
当然,这引出了一个问题,为什么您可能需要知道这一点?通常,您会使用适合应用程序的类型,并相信编译器会提供任何优化。如果优化是您认为需要此信息的目的,那么最好使用C99 'fast' types。如果您需要优化特定算法,请为多种类型实现它并对其进行分析。
回答by mctylr
an int should be one word right?
int 应该是一个词吧?
As I understand it, that depends on the data size model. For an explanation for UNIX Systems, 64-bit and Data Size Neutrality. For example Linux 32-bit is ILP32, and Linux 64-bit is LP64. I am not sure about the difference across Window systems and versions, other than I believe all 32-bit Window systems are ILP32.
据我了解,这取决于数据大小模型。有关 UNIX 系统、64 位和数据大小中立的解释。例如 Linux 32 位是 ILP32,Linux 64 位是 LP64。除了我相信所有 32 位 Window 系统都是 ILP32 之外,我不确定 Window 系统和版本之间的差异。
How do I determine the word size of my CPU?
如何确定我的 CPU 的字长?
That depends. Which version of C standard are you assuming. What platforms are we talking. Is this a compile or run time determination you're trying to make.
那要看。您假设的是哪个版本的 C 标准。我们在谈论什么平台。这是您正在尝试进行的编译或运行时确定。
The C header file <limits.h>may defines WORD_BITand/or __WORDSIZE.
C 头文件<limits.h>可以定义WORD_BIT和/或__WORDSIZE.
回答by Foredecker
sizeof(int) is not always the "word" size of your CPU. The most important question here is whyyou want to know the word size.... are you trying to do some kind of run-time and CPU specific optimization?
sizeof(int) 并不总是 CPU 的“字”大小。这里最重要的问题是为什么你想知道字的大小......你是否正在尝试进行某种运行时和 CPU 特定的优化?
That being said, on Windows with Intel processors, the nominal word size will be either 32 or 64 bits and you can easily figure this out:
话虽如此,在使用 Intel 处理器的 Windows 上,标称字长将是 32 位或 64 位,您可以轻松计算出这一点:
- if your program is compiled for 32-bits, then the nominal word size is 32-bits
- if you have compiled a 64-bit program then then the nominal word size is 64-bits.
- 如果您的程序是为 32 位编译的,那么标称字长是 32 位
- 如果您编译了 64 位程序,那么标称字长是 64 位。
This answer sounds trite, but its true to the first order. But there are some important subtleties. Even though the x86 registers on a modern Intel or AMD processor are 64-bits wide; you can only (easily) use their 32-bit widths in 32-bit programs - even though you may be running a 64-bit operating system. This will be true on Linux and OSX as well.
这个答案听起来很陈词滥调,但它是真实的。但是有一些重要的微妙之处。即使现代 Intel 或 AMD 处理器上的 x86 寄存器是 64 位宽;您只能(轻松)在 32 位程序中使用它们的 32 位宽度 - 即使您可能正在运行 64 位操作系统。在 Linux 和 OSX 上也是如此。
Moreover, on most modern CPU's the data bus width is wider than the standard ALU registers (EAX, EBX, ECX, etc). This bus width can vary, some systems have 128 bit, or even 192 bit wide busses.
此外,在大多数现代 CPU 上,数据总线宽度比标准 ALU 寄存器(EAX、EBX、ECX 等)更宽。这个总线宽度可以变化,有些系统有 128 位,甚至 192 位宽的总线。
If you are concerned about performance, then you also need to understand how the L1 and L2 data caches work. Note that some modern CPU's have an L3 cache. Caches including a unit called the Write Buffer
如果您关心性能,那么您还需要了解 L1 和 L2 数据缓存的工作原理。请注意,一些现代 CPU 具有 L3 缓存。缓存包括一个称为写缓冲区的单元
回答by mcleod_ideafix
Make a program that does some kind of integer operation many times, like an integer version of the SAXPY algorithm. Run it for different word sizes, from 8 to 64 bits (i.e. from charto long long).
编写一个多次执行某种整数运算的程序,例如 SAXPY 算法的整数版本。为不同的字长运行它,从 8 到 64 位(即从char到long long)。
Measure the time each version spends while running the algorithm. If there is one specific version that lasts noticeably less than the others, the word size used for that version is probably the native word size of your computer. On the other way, if there are several versions that last more or less the same time, pick up the one which has the greater word size.
测量每个版本在运行算法时花费的时间。如果有一个特定版本的持续时间明显短于其他版本,则用于该版本的字长可能是您计算机的本机字长。另一方面,如果有多个版本或多或少同时持续,则选择字长较大的版本。
Note that even with this technique you can get false data: your benchmark, compiled using Turbo C and running on a 80386 processor through DOS will report that the word size is 16 bits, just because the compiler doesn't use the 32-bit registers to perform integer aritmetic, but calls to internal functions that do the 32-bit version of each aritmetic operation.
请注意,即使使用这种技术,您也可以获得错误数据:使用 Turbo C 编译并通过 DOS 在 80386 处理器上运行的基准测试将报告字大小为 16 位,仅仅因为编译器不使用 32 位寄存器执行整数算术,但调用执行每个算术运算的 32 位版本的内部函数。
回答by uliwitness
In short: There's no good way. The original idea behind the C data types was that int would be the fastest (native) integer type, long the biggest etc.
简而言之:没有什么好办法。C 数据类型背后的最初想法是 int 将是最快的(本机)整数类型,long 是最大的,等等。
Then came operating systems that originated on one CPU and were then ported to different CPUs whose native word size was different. To maintain source code compatibility, some of the OSes broke with that definition and kept the data types at their old sizes, and added new, non-standard ones.
然后是起源于一个 CPU 的操作系统,然后被移植到不同的 CPU,它们的本机字大小不同。为了保持源代码的兼容性,一些操作系统打破了这个定义,将数据类型保持在原来的大小,并添加了新的、非标准的。
That said, depending on what you actually need, you might find some useful data types in stdint.h, or compiler-specific or platform-specific macros for various purposes.
也就是说,根据您的实际需要,您可能会在 中找到一些有用的数据类型stdint.h,或者用于各种用途的特定于编译器或特定于平台的宏。
回答by OMID
Many thinks of memory as an array of bytes. But CPU has another view of it. Which is about memory granularity. Depending on architecture, there would be 2, 4, 8, 16 or even 32 bytes memory granularity. Memory granularity and address alignment have great impact on performance, stability and correctness of software. Consider a granularity of 4 bytes and an unaligned memory access to read in 4 bytes. In this case every read, 75% if address is increasing by one byte, takes two more read instructions plus two shift operations and finally a bitwise instruction for final result which is performance killer. Further atomic operations could be affected as they must be indivisible. Other side effects would be caches, synchronization protocols, cpu internal bus traffic, cpu write buffer and you guess what else. A practical test could be run on a circular buffer to see how the results could be different. CPUs from different manufacturers, based on model, have different registers which will be used in general and specific operations. For example modern CPUs have extensions with 128 bits registers. So, the word size is not only about type of operation but memory granularity. Word size and address alignment are beasts which must be taken care about. There are some CPUs in market which does not take care of address alignment and simply ignore it if provided. And guess what happens?
许多人认为内存是一个字节数组。但 CPU 对此有另一种看法。这与内存粒度有关。根据体系结构,将有 2、4、8、16 甚至 32 字节的内存粒度。内存粒度和地址对齐对软件的性能、稳定性和正确性有很大影响。考虑 4 字节的粒度和未对齐的内存访问以读取 4 字节。在这种情况下,每次读取,如果地址增加一个字节,则为 75%,需要多两条读取指令加上两次移位操作,最后一条按位指令获得最终结果,这是性能杀手。进一步的原子操作可能会受到影响,因为它们必须是不可分割的。其他副作用是缓存、同步协议、cpu 内部总线流量、cpu 写入缓冲区,您猜还有什么。可以在循环缓冲区上运行实际测试以查看结果有何不同。不同制造商的 CPU,根据型号,具有不同的寄存器,用于一般和特定操作。例如,现代 CPU 具有 128 位寄存器的扩展。因此,字长不仅与操作类型有关,而且与内存粒度有关。字长和地址对齐是必须注意的野兽。市场上有一些 CPU 不负责地址对齐,如果提供,则直接忽略它。猜猜会发生什么?字长不仅与操作类型有关,而且与内存粒度有关。字长和地址对齐是必须注意的野兽。市场上有一些 CPU 不负责地址对齐,如果提供,则直接忽略它。猜猜会发生什么?字长不仅与操作类型有关,而且与内存粒度有关。字长和地址对齐是必须注意的野兽。市场上有一些 CPU 不负责地址对齐,如果提供,则直接忽略它。猜猜会发生什么?
回答by Mike
To use at compile time: sizeof(void*)
在编译时使用: sizeof(void*)
回答by user1419122
What every may be the reason for knowing the size of the processor it don't matter.
每个可能是知道处理器大小的原因都没有关系。
The size of the processor is the amount of date that Arthematic Logic Unit(ALU) of One CPU Core can work on at a single point of time. A CPU Cores's ALU will on Accumulator Register at any time. So, The size of a CPU in bits is the the size of Accumulator Register in bits.
处理器的大小是一个 CPU 内核的算术逻辑单元 (ALU) 在单个时间点可以工作的日期量。CPU 内核的 ALU 将随时在累加器寄存器上。因此,以位为单位的 CPU 大小就是以位为单位的累加器寄存器的大小。
You can find the size of the accumulator from the data sheet of the processor or by writing a small assembly language program.
您可以从处理器的数据表或编写一个小的汇编语言程序中找到累加器的大小。
Note that the effective usable size of Accumulator Register can change in some processors (like ARM) based on mode of operations (Thumb and ARM modes). That means the size of the processor will also change based on the mode for that processors.
请注意,累加器寄存器的有效可用大小可能会根据操作模式(Thumb 和 ARM 模式)在某些处理器(如 ARM)中发生变化。这意味着处理器的大小也将根据该处理器的模式而变化。
It common in many architectures to have virtual address pointer size and integer size same as accumulator size. It is only to take advantage of Accumulator Register in different processor operations but it is not a hard rule.
在许多体系结构中,虚拟地址指针大小和整数大小与累加器大小相同是很常见的。只是在不同的处理器操作中利用累加器寄存器,但这不是硬性规定。

