[fpc-pascal] fast integer multiplication

Florian Klaempfl fpk at gmx.org
Fri Jul 29 08:58:49 CEST 2005


Peter Vreman wrote:

>>Florian Klaempfl a écrit :
>>
>>>Vincent Snijders wrote:
>>>
>>>
>>>
>>>>Hi,
>>>>
>>>>Suppose I have the following code:
>>>>
>>>>var
>>>> a,b: dword;
>>>> c: qword;
>>>>
>>>>begin
>>>> a := 10000000;
>>>> b := 20000000;
>>>> c := a * b;
>>>> writeln(c);
>>>>end.
>>>>
>>>>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).
> 
> 
> No. At the time a*b is calculated there is nothing known about the size of
> the result.

a*b knows that it should do a qword multiplication and it can check if both
operands are dword. It could remove the type casts then and do a mulll.

> 
> 
> 
>>>Only assembler for now. Any suggestions how the compiler could be told
>>>to generate such code?
>>
>>Since you ask the question, I suppose you cannot simply
>>suppress the "movl $0,%edx" line generation [*]. What about
>>using a compiler directive, something like {$EXTENDEDMUL ON/OFF}
>>for instance?
> 
> 
> I don't like this. This means we have to add directives for all kind of
> special cpu features to improve some special corner cases.

Me neither.





More information about the fpc-pascal mailing list