64 位数学是如何在 32 位机器上完成的?

发布于 2024-09-08 21:21:58 字数 370 浏览 1 评论 0原文

如果 32 位处理器的长度确实只有 32 位,那么数学运算如何处理 64 位数字呢?例如:

long lngTemp1 = 123456789123;
long lngTemp2 = lngTemp1 * 123;

根据 MSDN,C# 中的 long 是有符号的 64 位数字: http://msdn.microsoft.com/en-us/library/ctetwysk(VS.71).aspx

32位Intel微处理器如何执行上面的代码没有溢出?

If a 32-bit processor is, indeed, really only 32 bits in length, then how can math operations work on 64-bit numbers? For example:

long lngTemp1 = 123456789123;
long lngTemp2 = lngTemp1 * 123;

According to MSDN, a long in C# is a signed 64-bit number: http://msdn.microsoft.com/en-us/library/ctetwysk(VS.71).aspx

How is it that a 32-bit Intel Microprocessor can execute code, like the above without getting an overflow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

污味仙女 2024-09-15 21:21:58

他们使用进位位进行加法和减法。 “带进位加法”和“带进位减法”(或“借位”)的汇编器操作可用于任意位长度扩展精度加法和减法。

对于乘法,如果只有 32 位乘法结果,则可以将其分解为 16 位值对并进行乘法,然后进行移位和加法(带进位),以从 32 位乘法获得完整的 64 位结果。基本上,执行长手版本(任何两个 16 位乘法都适合 32 位结果)可用于使用更有限的精度生成任意位长度乘法。

FWIW,Intel 32位asm“mul”指令可以将64位结果放入EDX:EAX中,因此您实际上可以在32位块中进行乘法(要添加64位值)而不是16位块(使用 32 位值进行移位和相加)。

They use the carry bit for add and subtract. The assembler ops for "add with carry" and "subtract with carry" (or "borrow") can be used for arbitrary bit length extended precision addition and subtraction.

For multiply, if you only have a 32-bit result from multiply, you can break it into 16-bit value pairs and multiply and then shift and add (with carry) to get a full 64-bit result from 32-bit multiply. Basically, doing the long-hand version (any two 16-bit multiplies fit in a 32-bit result) can be used to generate arbitrary bit-length multiplies using a more limited precision.

FWIW, the Intel 32-bit asm "mul" instruction can put a 64-bit result in EDX:EAX so you can actually do multiplies in 32-bit chunks (with 64-bit values to add) rather than 16-bit chunks (with 32-bit values to shift and add).

娇纵 2024-09-15 21:21:58

x86的32位指令集可用于64位算术,参见扩展精度乘法

32 bit instruction set of x86 can be used for 64 bit arithmetic, see Extended Precision Multiplication.

七七 2024-09-15 21:21:58

即使 32 位处理器也通常配备 64 位浮点单元 - 但数据一次只能以 32 位总线传输。

然而,更一般地说,即使底层处理器仅允许 8 位整数运算,也可以实现 64 位浮点。然而,编译器或程序员必须插入足够的代码才能虚拟化效果。

Even 32-bit processors often came with a 64-bit floating point unit - but the data could only be bussed in 32-bits at a time.

More generally, however, 64-bit floating point could be accomplished even if the underlying processor only allows 8-bit integer operations. However, the compiler or programmer would have to insert sufficient code in order to virtualize the effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文