简体   繁体   English

C ++ int vs 64位机器中的long long

[英]C++ int vs long long in 64 bit machine

My computer has 64 bit processor and when I look for sizeof(int) , sizeof(long) , and sizeof(long long) , it turns out that int and long are 32 bits, and long long is 64 bit. 我的计算机有64位处理器,当我查找sizeof(int)sizeof(long)sizeof(long long) ,结果是intlong是32位, long long是64位。 I researched the reason, and it appears that popular assumption telling that int in C++ fits machine's word size is wrong. 我研究了原因,看来普遍的假设是说C ++中的int符合机器的字大小是错误的。 As I understood it is up to compiler to define what will be the size, and mine is Mingw-w64. 据我所知,编译器需要定义大小,我的是Mingw-w64。 The reason for my research was understanding that if the usage of types smaller than word size is beneficial for speed(for instance, short vs int ) or if it has negative effect. 我研究的原因是理解如果小于字大小的类型的使用有利于速度(例如, int )或它是否具有负面影响。 In 32 bit system, one popular opinion is: due to the fact that word size is int , short will be converted into int and it would cause additional bit shifts and etc, thus leading to worse performance. 在32位系统中,一种流行的观点是:由于字大小为intshort将被转换为int ,这将导致额外的位移等,从而导致更差的性能。 Opposing opinion is that there will be benefit in cache level(I didn't go deep into it), and using short would be usefull for virtual memory economy. 反对意见是缓存级别会有好处(我没有深入研究),使用short会对虚拟内存经济有用。 So, in addition to confusion between this dilemma, I also face another problem. 所以,除了这种困境之间的混淆之外,我还面临着另一个问题。 My system is 64 bit, and it doesn't matter if I use int or short , it still will be less than the word size, and I start thinking that wouldn't it be efficient to use 64 bit long long because it is at the level the system is designed to. 我的系统是64位,如果我使用intshort并不重要,它仍然会小于字大小,我开始认为使用64位长的长度不是很有效,因为它是在系统设计的级别。 Also I read that there is another constraint, which is library(ILP64, LP64) of OS that defines the type sizes. 另外我读到还有另一个约束,即OS的库(ILP64,LP64),它定义了类型大小。 In ILP64 default int is 64 bit in contrast to LP64, would it speed up the program if I use OS with ILP64 support? 在ILP64中,与LP64相比,默认int为64位,如果我使用支持ILP64的操作系统,它会加速程序吗? Once I started to ask which type should I use for speeding up my C++ program, I faced more deep topics in which I have no expertise and some explanations seems to contradict to each other. 一旦我开始询问我应该使用哪种类型来加速我的C ++程序,我就面临更深层次的主题,其中我没有专业知识,而且一些解释似乎相互矛盾。 Can you please explain: 你能解释一下:

1) If it is best practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data? 1)如果最好在x64中使用long long来实现最大性能,即使对于1-4字节数据也是如此?

2) Trade-off in using a type less than word size(memory win vs additional operations) 2)使用小于字大小的类型(内存胜利与附加操作)的权衡

3) Does a x64 computer where word&int size is 64 bits, has possibility of processing a short, using 16 bit word size by using so called backward compatibility? 3)word和int size是64位的x64计算机是否有可能通过使用所谓的向后兼容性使用16位字大小来处理短路? Or it must put the 16bit file into 64 bit file, and the fact that it can be done defines the system as backward compatible. 或者它必须将16位文件放入64位文件中,并且可以完成的事实将系统定​​义为向后兼容。

4) Can we force the compiler to make the int 64 bit? 4)我们可以强制编译器使int 64位?

5) How to incorporate ILP64 into PC that uses LP64? 5)如何将ILP64整合到使用LP64的PC中?

6) What are possible problems of using code adapted to above issues with other compilers, OS's, and architectures(32 bit processor)? 6)使用适用于其他编译器,操作系统和体系结构(32位处理器)的上述问题的代码可能存在哪些问题?

1) If it is best practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data? 1)如果最好在x64中使用long long来实现最大性能,即使对于1-4字节数据也是如此?

No- and it will probably in fact make your performance worse. 不 - 它实际上可能会使你的表现更糟。 For example, if you use 64-bit integers where you could have gotten away with 32-bit integers then you have just doubled the amount of data that must be sent between the processor and memory and the memory is orders of magnitude slower. 例如,如果你使用64位整数,你可以用32位整数,那么你只需要在处理器和内存之间发送的数据量增加一倍,内存就会慢几个数量级。 All of your caches and memory buses will crap out twice as fast. 所有缓存和内存总线的速度都会快两倍。

2) Trade-off in using a type less than word size(memory win vs additional operations) 2)使用小于字大小的类型(内存胜利与附加操作)的权衡

Generally, the dominant driver of performance in a modern machine is going to be how much data needs to be stored in order to run a program. 通常,现代机器中性能的主要驱动因素是需要存储多少数据才能运行程序。 You are going to see significant performance cliffs once the working set size of your program exceeds the capacity of your registers, L1 cache, L2 cache, L3 cache, and RAM, in that order. 一旦程序的工作集大小超过了寄存器,L1缓存,L2缓存,L3缓存和RAM的容量,您将看到重要的性能悬崖。

In addition, using a smaller data type can be a win if your compiler is smart enough to figure out how to use your processor's vector instructions (aka SSE instructions). 此外,如果您的编译器足够智能以找出如何使用处理器的向量指令(也称为SSE指令),则使用较小的数据类型可能是一种胜利。 Modern vector processing units are smart enough to cram eight 16-bit short integers into the same space as two 64-bit long long integers, so you can do four times as many operations at once. 现代矢量处理单元足够聪明,可以将8个16位短整数填充到与两个64位长整数相同的空间中,因此一次可以执行四次操作。

3) Does a x64 computer where word&int size is 64 bits, has possibility of processing a short, using 16 bit word size by using so called backward compatibility? 3)word和int size是64位的x64计算机是否有可能通过使用所谓的向后兼容性使用16位字大小来处理短路? Or it must put the 16bit file into 64 bit file, and the fact that it can be done defines the system as backward compatible. 或者它必须将16位文件放入64位文件中,并且可以完成的事实将系统定​​义为向后兼容。

I'm not sure what you're asking here. 我不确定你在这里问什么。 In general, 64-bit machines are capable of executing 32-bit and 16-bit executable files because those earlier executable files use a subset of the 64-bit machine's potential. 通常,64位计算机能够执行32位和16位可执行文件,因为早期的可执行文件使用64位计算机潜在的子集。

Hardware instruction sets are generally backwards compatible, meaning that processor designers tend to add capabilities, but rarely if ever remove capabilities. 硬件指令集通常是向后兼容的,这意味着处理器设计者倾向于添加功能,但很少删除功能。

4) Can we force the compiler to make the int 64 bit? 4)我们可以强制编译器使int 64位?

There are fairly standard extensions for all compilers that allow you to work with fixed-bit-size data. 所有编译器都有相当标准的扩展,允许您使用固定位大小的数据。 For example, the header file stdint.h declares types such as int64_t , uint64_t , etc. 例如,头文件stdint.h声明类型,如int64_tuint64_t等。

5) How to incorporate ILP64 into PC that uses LP64? 5)如何将ILP64整合到使用LP64的PC中?

https://software.intel.com/en-us/node/528682 https://software.intel.com/en-us/node/528682

6) What are possible problems of using code adapted to above issues with other compilers, OS's, and architectures(32 bit processor)? 6)使用适用于其他编译器,操作系统和体系结构(32位处理器)的上述问题的代码可能存在哪些问题?

Generally the compilers and systems are smart enough to figure out how to execute your code on any given system. 通常,编译器和系统足够聪明,可以弄清楚如何在任何给定系统上执行代码。 However, 32-bit processors are going to have to do extra work to operate on 64-bit data. 但是,32位处理器将不得不做额外的工作来操作64位数据。 In other words, correctness should not be an issue, but performance will be. 换句话说,正确性不应该是一个问题,但性能将是。

But it's generally the case that if performance is really critical to you, then you need to program for a specific architecture and platform anyway. 但通常的情况是,如果性能对您来说非常关键,那么无论如何您都需要针对特定​​的架构和平台进行编程。

Clarification Request: Thanks alot! 澄清请求:非常感谢! I wanted to clarify question no:1. 我想澄清问题:1。 You say that it is bad for memory. 你说它对记忆有害。 Lets take an example of 32 bit int. 让我们举一个32位int的例子。 When you send it to memory, because it is 64 bit system, for a desired integer 0xee ee ee ee, when we send it won't it become 0x ee ee ee ee+ 32 other bits? 当你把它发送到内存,因为它是64位系统,对于一个所需的整数0xee ee ee,当我们发送它不会变成0xee ee ee ee + 32个其他位? How can a processor send 32 bits when the word size is 64 bits? 当字长为64位时,处理器如何发送32位? 32 bits are the desired values, but won't it be combined with 32 unused bits and sent this way? 32位是所需的值,但不会与32个未使用的位组合并以这种方式发送? If my assumption is true, then there is no difference for memory. 如果我的假设是正确的,那么记忆就没有区别了。

There are two things to discuss here. 这里有两件事要讨论。

First, the situation you discuss does not occur. 首先,您讨论的情况不会发生。 A processor does not need to "promote" a 32-bit value into a 64-bit value in order to use it appropriately. 处理器不需要将32位值“提升”为64位值,以便适当地使用它。 This is because modern processors have different accessing modes that are capable of dealing with different size data appropriately. 这是因为现代处理器具有不同的访问模式,能够适当地处理不同大小的数据。

For example, a 64-bit Intel processor has a 64-bit register named RAX. 例如,64位Intel处理器具有名为RAX的64位寄存器。 However, this same register can be used in 32-bit mode by referring to it as EAX, and even in 16-bit and 8-bit modes. 但是,通过将其称为EAX,甚至在16位和8位模式下,可以在32位模式下使用该相同的寄存器。 I stole a diagram from here: 我从这里偷了一张图:

x86_64 registers rax/eax/ax/al overwriting full register contents x86_64寄存器rax / eax / ax / al覆盖完整寄存器内容

1122334455667788
================ rax (64 bits)
        ======== eax (32 bits)
            ====  ax (16 bits)
            ==    ah (8 bits)
              ==  al (8 bits)

Between the compiler and assembler, the correct code is generated so that a 32-bit value is handled appropriately. 在编译器和汇编器之间,生成正确的代码,以便正确处理32位值。

Second, when we're talking about memory overhead and performance we should be more specific. 其次,当我们谈论内存开销和性能时,我们应该更具体。 Modern memory systems are composed of a disk, then main memory (RAM) and typically two or three caches (eg L3, L2, and L1). 现代存储器系统由磁盘,主存储器(RAM)和通常两个或三个高速缓存(例如L3,L2和L1)组成。 The smallest quantity of data that can be addressed on the disk is called a page, and page sizes are usually 4096 bytes (though they don't have to be). 可以在磁盘上寻址的最小数据量称为页面,页面大小通常为4096字节(尽管它们不必是)。 Then, the smallest quantity of data that can be addressed in memory is called a cache line, which is usually much larger than 32 or 64 bits. 然后,可在内存中寻址的最小数据量称为缓存行,通常远大于32或64位。 On my computer the cache line size is 64 bytes. 在我的计算机上,缓存行大小为64字节。 The processor is the only place where data is actually transferred and addressed at the word level and below. 处理器是唯一一个在字级及以下实际传输和寻址数据的地方。

So if you want to change one 64-bit word in a file that resides on disk, then, on my computer, this actually requires that you load 4096 bytes from the disk into memory, and then 64 bytes from memory into the L3, L2, and L1 caches, and then the processor takes a single 64-bit word from the L1 cache. 因此,如果要更改驻留在磁盘上的文件中的一个64位字,那么,在我的计算机上,这实际上要求您将4096个字节从磁盘加载到内存中,然后从内存加载64个字节到L3,L2和L1缓存,然后处理器从L1缓存中获取一个64位字。

The result is that the word size means nothing for memory bandwidth. 结果是字大小对内存带宽毫无意义。 However, you can fit 16 of those 32-bit integers in the same space you can pack 8 of those 64-bit integers. 但是,您可以在同一空间中容纳16个32位整数,这些空间可以打包8个64位整数。 Or you could even fit 32 16-bit values or 64 8-bit values in the same space. 或者您甚至可以在同一空间中容纳32个16位值或64个8位值。 If your program uses a lot of different data values you can significantly improve performance by using the smallest data type necessary. 如果您的程序使用大量不同的数据值,则可以使用所需的最小数据类型来显着提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM