Multi-Precision Arithmetic

These pages relate my experiments on 64-bit basic multi-precision arithmetic.

I describe a set of basic memory, arithmetic, and logic operations on (long) vectors of 64-bit words. The vector (A,N) is a vector of N consecutive 64-bit words A0, A1,..., AN-1. Such a vector represents an unsigned integer A0+A1.264+...+AN-1.264(N-1). All integers in the interval 0..264N-1 are representable, and have an unique representation.

To simplify the functions, I impose N to be a multiple of 8: the size of a vector is a multiple of 64 bytes (cache line). Vectors must be 8-byte aligned on the AMD64, and 16-byte aligned on the Intel64, where 128-bit SSE instructions are used.

I used information from the following sources:

The GMP library already provides a very fast and comprehensive set of low-level multi-precision functions (mpn). I will often use it as a starting point for further optimizations, along with some optimized memory function of the GNU C Library.

P. Gaudry distributes a patch to GMP 4.2.1 for the AMD64 processors, and Jason W. Martin distributes a similar patch for the Intel Core 2 Duo processors.

Execution time for each function is evaluated for 8KB vectors (N=1024 words), using the rdtsc instruction surrounded by two cpuid instructions to ensure all instructions are counted (because rdtsc may be executed out-of-order, but cpuid can't be). With this size we can get a very precise measure of the number of required cycles per word. Each function is called a large number of times, and we take the average number of cycles.

So far, I explored two processors: