简体   繁体   English

32位处理器如何支持64位整数?

[英]How does a 32 bit processor support 64 bit integers?

In C++, you can use an int which is usually 4 bytes. 在C ++中,您可以使用通常为4个字节的int A long long integer is usually 8 bytes. long long整数通常是8个字节。 If the cpu was 32 bit, wouldn't that limit it to 32 bit numbers? 如果cpu是32位,那不会将它限制为32位数吗? How come I can use a long long integer if it doesn't support 64 bits? 如果它不支持64位,为什么我可以使用long long整数? Can the alu add larger integers or something? alu可以添加更大的整数或其他东西吗?

Most processors include a carry flag and an overflow flag to support operations on multi-word integers. 大多数处理器包括进位标志和溢出标志,以支持对多字整数的操作。 The carry flag is used for unsigned math, and the overflow flag for signed math. 进位标志用于无符号数学运算,溢出标志用于有符号数学运算。

For example, on an x86 you could add two unsigned 64-bit numbers (which we'll assume are in EDX:EAX and EBX:ECX) something like this: 例如,在x86上你可以添加两个无符号的64位数字(我们假设它们在EDX中:EAX和EBX:ECX),如下所示:

add eax, ecx  ; this does an add, ignoring the carry flag
adc edx, ebx  ; this adds the carry flag along with the numbers
; sum in edx:eax

It's possible to implement this sort of thing in higher level languages like C++ as well, but they do a lot less to support it, so the code typically ends up substantially slower than when it's written in assembly language. 也可以在更高级别的语言(如C ++)中实现此类功能,但是它们支持它的功能要少得多,因此代码通常比使用汇编语言编写时要慢得多。

Most operations are basically serial in nature. 大多数操作基本上都是串行的。 When you're doing addition at the binary level, you take two input bits and produce one result bit and one carry bit. 当您在二进制级别进行加法时,您需要两个输入位并产生一个结果位和一个进位。 The carry bit is then used as an input when adding the next least significant bit, and so on across the word (known as a "ripple adder", because the addition "ripples" across the word). 然后,在添加下一个最低有效位时,进位位用作输入,等等在整个字上(称为“纹波加法器”,因为在单词上添加“涟漪”)。

There are more sophisticated ways to do addition that can reduce that dependency between one bit and another when a particular addition doesn't produce a dependency, and most current hardware uses such things. 有一些更复杂的添加方法,当特定的添加不产生依赖性时,可以减少一位与另一位之间的依赖性,并且大多数当前硬件使用这样的东西。

In the worst case, however, adding 1 to a number that's already the largest a given word size supports will result in generating a carry from every bit to the next, all the way across the word. 然而,在最坏的情况下,将1加到已经是给定字大小支持的最大数字将导致从每个位到下一个位生成进位,一直到整个单词。

That means that (to at least some extent) the word width a CPU supports imposes a limit on the maximum clock speed at which it can run. 这意味着(至少在某种程度上)CPU支持的字宽度限制了它可以运行的最大时钟速度。 If somebody wanted to badly enough, they could build a CPU that worked with, say, 1024-bit operands. 如果有人想要足够严重,他们可以构建一个与1024位操作数一起使用的CPU。 If they did that, however, they'd have two choices: either run it at a lower clock speed, or else take multiple clocks to add a single pair of operands. 但是,如果他们这样做,他们有两个选择:要么以较低的时钟速度运行,要么采用多个时钟来添加一对操作数。

Also note that as you widen operands like that, you need more storage (eg, larger cache) to store as many operands, more gates to carry out each individual operation, and so on. 还要注意,当你扩展这样的操作数时,你需要更多的存储空间(例如,更大的缓存)来存储尽可能多的操作数,更多的门来执行每个单独的操作,等等。

So given identical technology, you could have a 64-bit processor that ran at 4 GHz and had, say, 4 megabytes of cache, or a 1024-bit processor that ran at about 250 MHz and had, perhaps, 2 megabytes of cache. 因此,如果使用相同的技术,您可以拥有一个运行速度为4 GHz的64位处理器,例如4兆字节的缓存,或者运行速度大约为250 MHz且可能具有2兆字节缓存的1024位处理器。

The latter would probably be a win if most of your work was on 1024-bit (or larger) operands. 如果你的大部分工作都是在1024位(或更大)的操作数上,后者可能会获胜。 Most people don't do math on 1024-bit operands very often at all though. 大多数人不会经常在1024位操作数上进行数学运算。 In fact, 64-bit numbers are large enough for most purposes. 事实上,64位数字足以满足大多数用途。 As such, supporting wider operands would probably turn out to be a net loss for most people most of the time. 因此,支持更广泛的操作数可能会在大多数时间成为大多数人的净损失。

It's possible to support arbitrarily wide integers (through software implementation), even if the underlying hardware only supports less bits directly. 即使底层硬件仅直接支持较少的位,也可以支持任意宽的整数(通过软件实现)。 If a 32-bit integer is added to another 32-bit integer, it could overflow and require 33 bits to store the answer. 如果将32位整数添加到另一个32位整数,它可能会溢出并需要33位来存储答案。 Software can detect that this overflow occurred (the processor has a carry flag that can be checked), and another 32-bit word that represents the most significant bits of the 64-bit number can be incremented by 1. 软件可以检测到发生了这种溢出(处理器有一个可以检查的进位标志 ),另一个代表64位数字最高有效位的32位字可以递增1。

Here's a little more on the carry flag and how it's used. 这里有一些关于进位标志及其使用方式的更多信息。

Essentially the normally single instruction add is broken into two (or three) steps: 基本上,通常单指令添加分为两个(或三个)步骤:

1) Add the low-order 32 bits using the usual add instruction. 1)使用通常的add指令添加低32位。 Note whether this addition would generate a "carry out" bit (that is, if the result would actually require 33 bits to represent). 注意这个加法是否会产生“进位”位(也就是说,如果结果实际上需要33位来表示)。

2) Add the high order 32 bits the same way. 2)以相同的方式添加高位32位。 If there was a carry-out from the lower order bits, set the carry in bit here (or, alternatively, add one to the result after adding). 如果从低位开始有进位,则在此处设置进位(或者,在添加后将结果加1)。

You use two memory locations to store the number. 您使用两个内存位置来存储该号码。 Half of the number is stored at one location in memory, and the other half in the adjacent memory location. 数字的一半存储在存储器中的一个位置,另一半存储在相邻存储器位置。

You might also consider that we used to cope with 16 or even 32 bit sized integers back in the days of 8-bit CPUs. 您可能还会考虑在8位CPU的时代我们曾经处理过16位甚至32位大小的整数。 There's nothing that restricts any particular alu from handling arbitrary size numbers other than memory space and ultimately I suppose the patience of the user. 没有什么能限制任何特定的alu处理除内存空间之外的任意大小数字,最终我认为用户的耐心。

Smalltalk for example has always provided arbitrary length integers since the original Dorados and Altos - that takes us back to 1970. Want the exact value of 963! 例如,Smalltalk总是提供任意长度的整数,因为最初的Dorados和Altos - 将我们带回到1970年。想要963的确切值! - just do it. - 去做就对了。 It'll take a while to format it to print though. 它需要一段时间才能将其格式化以进行打印。

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

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