[fpc-devel] Math.DivMod results should be signed

Kai Olav Fredriksen k at i.fredriksen.net
Thu Nov 9 11:47:00 CET 2006


Thanks for CDQ info, I didn't know it existed :)

I just made the algotithm because I needed speedy div and mod, that could 
handle signed and unsigned Int32, also with a Int32 as divisor. And the one 
in Delphi6/7 doesn't do that.
I haven't used assembler since 1985 on a Dragon32, so I have a lot to catch 
up with.

It might be that it could be a lot better, I hope someone could tell me how. 
But as it is now, it runs about 90% faster than using div and then mod in 
Delphi code.

Kai

----- Original Message ----- 
From: "Florian Klaempfl" <florian at freepascal.org>
To: <k at i.fredriksen.net>; "FPC developers' list" 
<fpc-devel at lists.freepascal.org>
Sent: Thursday, November 09, 2006 8:42 AM
Subject: Re: [fpc-devel] Math.DivMod results should be signed


>> function DivWithRemainder(Value, Divisor: Integer; var Remainder: 
>> Integer): Integer;
>>   {
>>    In:
>>      EAX = Value
>>      ECX = @Remainder
>>      EDX = Divisor
>>
>>    Out:
>>      EAX = Result
>>      ECX = @Remainder
>>   }
>> asm
>>   PUSH EBX              { Save EBX - this register is not free to use }
>>   MOV  EBX  , EDX       { Copy Divisor to EBX, using it as a store }
>>   TEST EAX  , $80000000 { Test if Value is signed }
>>   JNZ  @Signed          { If Value is negative, prepare signed EDX }
>>   MOV  EDX  , 0         { If Value is positive, prepare unsigned EDX 
>> (Clear it) }
>>   @Continue:
>>   IDIV EBX              { Divides Value (actually EDX + EAX (64 bit)) by 
>> EBX. Result -> EAX, remainder -> EDX }
>>   MOV  [ECX], EDX       { Copies EDX to ECX^ (Remainder) }
>>   POP EBX               { Restore EBX }
>>   RET
>>   @Signed:
>>   MOV  EDX  , -1        { Fill EDX with $FFFFFFFF if Value was signed }
>>   JMP @Continue
>
> Is this really faster than using CDQ?
>
>
>
> -- 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.409 / Virus Database: 268.14.0/524 - Release Date: 08.11.2006
>
> 




More information about the fpc-devel mailing list