[Python-Dev] Optionally using GMP to implement long if available

Nick Craig-Wood nick at craig-wood.com
Mon Nov 10 17:26:30 CET 2008

Tim Peters <tim.peters at gmail.com> wrote:
> > And for 32x32 -> 64, can't this simply be replaced by "(uint64_t)i * j",
> > where uint64_t is as in C99?  I'd hope that most compilers would
> > manage to turn this into the appropriate 32x32-bit hardware multiply.
>  1. That's C99, not C89, and therefore less portable.
>  2. On platforms that support it, this is at least 64x64->64 multiplication,
>     potentially much more expensive than the 32x32->64 (or 31x31->62?)
>     flavor you /intend/ to move to.
>  3. There's no way to know exactly what compilers will do with this short of
>     staring at generated code.

I've relied in the past for the compiler generating a 32*32->64 bit
multiply for this code

#include <stdint.h>

uint64_t mul(uint32_t a, uint32_t b)
    return a*b;

Looking at the assembler it produces (x86)

        pushl   %ebp
        xorl    %edx, %edx
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        imull   8(%ebp), %eax
        popl    %ebp

Which I'm pretty sure is a 32x32->64 bit mul (though my x86 assembler
foo is weak).

I think a compiler would have to be pretty stupid not to take this
optimisation... But then there are some pretty stupid compilers out

Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick

More information about the Python-Dev mailing list