views:

126

answers:

3

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?

A: 

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

Remus Rusanu
+3  A: 

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

Adisak
A: 

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.

csj