Re: NAFCQY [Not A FastCode Question, Yay!]: Int64 DivMod



On Tue, 26 Jul 2005 22:55:58 +0200, "Kristofer Skaug"
<nospam@xxxxxxxxx> wrote:

>does anyone have an Int64-based DivMod they would be willing to share?

Not heavily tested:

procedure DivMod64(Dividend: Int64; Divisor: LongWord; var Result,
Remainder: Int64);
asm
push ebx
push edi
push esi

mov esi, edx // esi = Result
mov edi, ecx // edi = Remainder

mov ecx, eax // Divisor

or ecx, ecx
jz @@regular // use regular to force division by zero

mov eax, [ebp+$08]
mov edx, [ebp+$0C]

or edx, edx
jns @@noneg
// force positive Dividend
neg eax
adc edx, 0
neg edx

@@noneg:
// if highword is zero, use regular division
cmp edx, 0
je @@regular

push edi
push esi

mov ebp, 64

xor esi, esi
xor edi, edi

@@loop:
shl eax, 1
rcl edx, 1
rcl esi, 1
rcl edi, 1

cmp edi, 0 // check high words
jb @@nosub
ja @@sub
cmp esi, ecx // check low words
jb @@nosub

@@sub:
sub esi, ecx // subtract divisor
sbb edi, 0
inc eax // increment result

@@nosub:
dec ebp
jnz @@loop

pop ecx
pop ebx

mov [ebx], esi
mov [ebx+4], edi
mov [ecx], eax
mov [ecx+4], edx

jmp @@exit

@@regular:
div ecx
mov [esi], eax
mov [esi+4], 0
mov [edi], edx
mov [edi+4], 0

@@exit:
pop esi
pop edi
pop ebx
end;


Shouldn't be hard to modify to accept int64 Divisor. replace the 0's
in "cmp edi, 0" and "sbb edi, 0" with the register containing the high
word of the divisor, and you should be set.

Yes my asm sucks :)

- Asbjørn
.



Relevant Pages