64-bit integer on a 32-bit machine

Quote:

> I need to simulate 64-bit integers in C on a 32-bit platform. I'm sure

this

> problem has been solved before but I haven't found any literature on it.

I

> could work out a scheme using two 32-bit words and check for overflows,

but

> I was hoping for a more elegant solution. Thanks in advance.

The "Bible" for all such algorithms is D. Knuth's 'The Art of Computer

Programming' vol. 2. You can download the source code for an implementation

of multiple-precision integer arithmetic from Dr. Dobb's Journal

(http://www.ddj.com). Look for the source code for the August '92 issue.

Simplifying it to double-precision is left as an exercise for the reader.

:-)

Under C89, the only semi-portable solution is to combine two 32-bit

variables into one logical 64-bit variable, using standard

multiple-precision arithmetic. C99 mandates a 'long long' type which has at

least 64 bits of precision.

If you can accept a compiler-specific solution, many compilers have a 64-bit

integer type as an extension. See your compiler's documentation for details.

Daniel Pfeffer