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

Florian Klaempfl florian at freepascal.org
Thu Nov 9 08:42:21 CET 2006


> 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?



More information about the fpc-devel mailing list