32位处理器如何支持64位整数? [英] How does a 32 bit processor support 64 bit integers?

查看:1590
本文介绍了32位处理器如何支持64位整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,您可以使用通常为4个字节的 int long long 整数通常为8个字节。如果cpu是32位,不会限制到32位数吗?如果不支持64位,如何使用 long long 整数?

In C++, you can use an int which is usually 4 bytes. A long long integer is usually 8 bytes. If the cpu was 32 bit, wouldn't that limit it to 32 bit numbers? How come I can use a long long integer if it doesn't support 64 bits? Can the alu add larger integers or something?

推荐答案

大多数处理器都包含一个进位标志和一个溢出标志,字整数。进位标志用于无符号数学运算,以及用于有符号数学的溢出标志。

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.

例如,在x86上可以添加两个无符号64位数将假定在EDX:EAX和EBX:ECX)类似这样:

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:

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

有可能在C ++等高级语言中实现这种东西,但是他们做的很少支持它,所以代码通常会比用汇编语言编写的慢得多。

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.

大多数操作基本上是串行的。当在二进制电平上进行加法运算时,您需要两个输入位,并产生一个结果位和一个进位位。然后当将下一个最低有效位加入时,进位位用作输入,以此类推,在字(称为波纹加法器,因为在该字上添加波纹)。

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.

然而,在最坏的情况下,将 1 添加到已经是给定字大小支持的最大值的数字将导致产生一个进位

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.

这意味着(至少在某种程度上)CPU支持的字宽限制了它可以运行的最大时钟速度。如果有人想要足够糟糕,他们可以构建一个与1024位操作数一起工作的CPU。但是,如果他们这样做,他们有两个选择:以较低的时钟速度运行它,或者花费多个时钟来添加一对操作数。

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. If somebody wanted to badly enough, they could build a CPU that worked with, say, 1024-bit operands. 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 (e.g., larger cache) to store as many operands, more gates to carry out each individual operation, and so on.

因此,如果采用相同的技术,您可以拥有一个运行速度为4 GHz,有4兆字节缓存的64位处理器或运行速度约为250 MHz的1024位处理器, 2 MB的缓存。

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.

如果大部分工作都在1024位(或更大)的操作数,后者可能会赢。大多数人不会经常在1024位操作数上做数学。事实上,64位数对于大多数用途都足够大。因此,支持更广泛的操作数在大多数时间可能会成为大多数人的净亏损。

The latter would probably be a win if most of your work was on 1024-bit (or larger) operands. Most people don't do math on 1024-bit operands very often at all though. In fact, 64-bit numbers are large enough for most purposes. As such, supporting wider operands would probably turn out to be a net loss for most people most of the time.

这篇关于32位处理器如何支持64位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆