How is 64-bit math accomplished on a 32-bit machin

2019-02-17 06:45发布

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?

3条回答
倾城 Initia
2楼-- · 2019-02-17 07:35

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.

查看更多
贪生不怕死
3楼-- · 2019-02-17 07:36

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).

查看更多
我命由我不由天
4楼-- · 2019-02-17 07:45

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

查看更多
登录 后发表回答