[fpc-pascal] fast integer multiplication

Marcel Martin mm10 at ellipsa.net
Sat Jul 23 17:02:43 CEST 2005

Florian Klaempfl a écrit :
> Vincent Snijders wrote:
>>Suppose I have the following code:
>>  a,b: dword;
>>  c: qword;
>>  a := 10000000;
>>  b := 20000000;
>>  c := a * b;
>>  writeln(c);
>>Now, although c is large enough to contain the result only the lower
>>dword is filled. I can force correct results by using c := qword(a) * b,
>>but the slow fpc_mul_qword is used.
>>The code generated for the above sample is:
>># [16] c:=a*b;
>>    movl    U_P$PROJECT1_A,%edx
>>    movl    U_P$PROJECT1_B,%eax
>>    mull    %edx
>>    movl    $0,%edx
>>    movl    %eax,U_P$PROJECT1_C
>>    movl    %edx,U_P$PROJECT1_C+4
>>What I want is the above code, but without the "movl $0,%edx"
>>instruction. Is there a way to do this (wihtout using fpc_mul_qword).
> Only assembler for now. Any suggestions how the compiler could be told
> to generate such code?

A small function (like the one I asked for a few weeks ago :-)
that would be inlined by the compiler?

Currently I use this one

function UI32x32To64(A,B: Longword): QWord;
assembler; register; nostackframe;
     mull %edx

It is fast but certainly much less than if it were inlined.


More information about the fpc-pascal mailing list