Date : May 06 '96, 22:35                                                       
From : Jon Merkel                                             1:124/1301
To   : Mel Mcbain                          
Subj : 64-bit math                                                           


 MM> If anyone could shed some light on a speedy 64/64 bit algo, well
 MM> I'd be so grateful I'd name my first born after their pet pooch.

Ok, here's some code I've written.  It uses 32 bit registers, though, and
divides a 128 bit number by a 64 bit one:  (untested!!!, but should work)

; In:
;    edx:ecx:ebx:eax = 128-bit dividend
;    edi:esi = 64-bit divisor
; Out:
;   ebx:eax = quotient
;   edx:ecx = remainder
; Modifies:
;   edi,esi,ebp
 
        mov     ebp,64          ; 64 bits to process
    @1: add     eax,eax         ; shift left edx:ecx:ebx:eax 1 bit 
        adc     ebx,ebx
        adc     ecx,ecx
        adc     edx,edx
        inc     eax
        sub     ecx,esi         ; edx:ecx -= edi:esi
        sbb     edx,edi
        jnb     @2
        dec     eax
        add     ecx,esi         ; edx:ecx += edi:esi (to restore)
        adc     edx,edi
    @2: dec     ebp
        jnz     @1

I'm not sure how you'd do this with only 16-bit registers; you'll probably need
to use some variables in memory. 

 MM> p.s. What div algo do the chip makers use?  Is it the sub loop algo too?

No way...  This above code is a restoring division algorithm based on one
from my hardware design course.  The fastest division algorithms (non-
restoring) require 1 shift and 1 add/subtract per bit in the divisor.


--- Maximus 3.00
 * Origin: The MOCHINE BBS * Irving, TX * 214/399-8414 * HST DS * (1:124/1301)
