Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

divide.S

Go to the documentation of this file.
00001 /*
00002  * arch/alpha/lib/divide.S
00003  *
00004  * (C) 1995 Linus Torvalds
00005  *
00006  * Alpha division..
00007  */
00008 
00009 /*
00010  * The alpha chip doesn't provide hardware division, so we have to do it
00011  * by hand.  The compiler expects the functions
00012  *
00013  *      __divqu: 64-bit unsigned long divide
00014  *      __remqu: 64-bit unsigned long remainder
00015  *      __divqs/__remqs: signed 64-bit
00016  *      __divlu/__remlu: unsigned 32-bit
00017  *      __divls/__remls: signed 32-bit
00018  *
00019  * These are not normal C functions: instead of the normal
00020  * calling sequence, these expect their arguments in registers
00021  * $24 and $25, and return the result in $27. Register $28 may
00022  * be clobbered (assembly temporary), anything else must be saved. 
00023  *
00024  * In short: painful.
00025  *
00026  * This is a rather simple bit-at-a-time algorithm: it's very good
00027  * at dividing random 64-bit numbers, but the more usual case where
00028  * the divisor is small is handled better by the DEC algorithm
00029  * using lookup tables. This uses much less memory, though, and is
00030  * nicer on the cache.. Besides, I don't know the copyright status
00031  * of the DEC code.
00032  */
00033 
00034 /*
00035  * My temporaries:
00036  *      $0 - current bit
00037  *      $1 - shifted divisor
00038  *      $2 - modulus/quotient
00039  *
00040  *      $23 - return address
00041  *      $24 - dividend
00042  *      $25 - divisor
00043  *
00044  *      $27 - quotient/modulus
00045  *      $28 - compare status
00046  */
00047 
00048 #define halt .long 0
00049 
00050 /*
00051  * Select function type and registers
00052  */
00053 #define mask    $0
00054 #define divisor $1
00055 #define compare $28
00056 #define tmp1    $3
00057 #define tmp2    $4
00058 
00059 #ifdef DIV
00060 #define DIV_ONLY(x,y...) x,##y
00061 #define MOD_ONLY(x,y...)
00062 #define func(x) __div##x
00063 #define modulus $2
00064 #define quotient $27
00065 #define GETSIGN(x) xor $24,$25,x
00066 #define STACK 48
00067 #else
00068 #define DIV_ONLY(x,y...)
00069 #define MOD_ONLY(x,y...) x,##y
00070 #define func(x) __rem##x
00071 #define modulus $27
00072 #define quotient $2
00073 #define GETSIGN(x) bis $24,$24,x
00074 #define STACK 32
00075 #endif
00076 
00077 /*
00078  * For 32-bit operations, we need to extend to 64-bit
00079  */
00080 #ifdef INTSIZE
00081 #define ufunction func(lu)
00082 #define sfunction func(l)
00083 #define LONGIFY(x) zapnot x,15,x
00084 #define SLONGIFY(x) addl x,0,x
00085 #else
00086 #define ufunction func(qu)
00087 #define sfunction func(q)
00088 #define LONGIFY(x)
00089 #define SLONGIFY(x)
00090 #endif
00091 
00092 .set noat
00093 .align  3
00094 .globl  ufunction
00095 .ent    ufunction
00096 ufunction:
00097         subq    $30,STACK,$30
00098         .frame  $30,STACK,$23
00099         .prologue 0
00100 
00101 7:      stq     $1, 0($30)
00102         bis     $25,$25,divisor
00103         stq     $2, 8($30)
00104         bis     $24,$24,modulus
00105         stq     $0,16($30)
00106         bis     $31,$31,quotient
00107         LONGIFY(divisor)
00108         stq     tmp1,24($30)
00109         LONGIFY(modulus)
00110         bis     $31,1,mask
00111         DIV_ONLY(stq tmp2,32($30))
00112         beq     divisor, 9f                     /* div by zero */
00113 
00114 #ifdef INTSIZE
00115         /*
00116          * shift divisor left, using 3-bit shifts for
00117          * 32-bit divides as we can't overflow. Three-bit
00118          * shifts will result in looping three times less
00119          * here, but can result in two loops more later.
00120          * Thus using a large shift isn't worth it (and
00121          * s8add pairs better than a sll..)
00122          */
00123 1:      cmpult  divisor,modulus,compare
00124         s8addq  divisor,$31,divisor
00125         s8addq  mask,$31,mask
00126         bne     compare,1b
00127 #else
00128 1:      cmpult  divisor,modulus,compare
00129         blt     divisor, 2f
00130         addq    divisor,divisor,divisor
00131         addq    mask,mask,mask
00132         bne     compare,1b
00133         unop
00134 #endif
00135 
00136         /* ok, start to go right again.. */
00137 2:      DIV_ONLY(addq quotient,mask,tmp2)
00138         srl     mask,1,mask
00139         cmpule  divisor,modulus,compare
00140         subq    modulus,divisor,tmp1
00141         DIV_ONLY(cmovne compare,tmp2,quotient)
00142         srl     divisor,1,divisor
00143         cmovne  compare,tmp1,modulus
00144         bne     mask,2b
00145 
00146 9:      ldq     $1, 0($30)
00147         ldq     $2, 8($30)
00148         ldq     $0,16($30)
00149         ldq     tmp1,24($30)
00150         DIV_ONLY(ldq tmp2,32($30))
00151         addq    $30,STACK,$30
00152         ret     $31,($23),1
00153         .end    ufunction
00154 
00155 /*
00156  * Uhh.. Ugly signed division. I'd rather not have it at all, but
00157  * it's needed in some circumstances. There are different ways to
00158  * handle this, really. This does:
00159  *      -a / b = a / -b = -(a / b)
00160  *      -a % b = -(a % b)
00161  *      a % -b = a % b
00162  * which is probably not the best solution, but at least should
00163  * have the property that (x/y)*y + (x%y) = x.
00164  */
00165 .align 3
00166 .globl  sfunction
00167 .ent    sfunction
00168 sfunction:
00169         subq    $30,STACK,$30
00170         .frame  $30,STACK,$23
00171         .prologue 0
00172         bis     $24,$25,$28
00173         SLONGIFY($28)
00174         bge     $28,7b
00175         stq     $24,0($30)
00176         subq    $31,$24,$28
00177         stq     $25,8($30)
00178         cmovlt  $24,$28,$24     /* abs($24) */
00179         stq     $23,16($30)
00180         subq    $31,$25,$28
00181         stq     tmp1,24($30)
00182         cmovlt  $25,$28,$25     /* abs($25) */
00183         unop
00184         bsr     $23,ufunction
00185         ldq     $24,0($30)
00186         ldq     $25,8($30)
00187         GETSIGN($28)
00188         subq    $31,$27,tmp1
00189         SLONGIFY($28)
00190         ldq     $23,16($30)
00191         cmovlt  $28,tmp1,$27
00192         ldq     tmp1,24($30)
00193         addq    $30,STACK,$30
00194         ret     $31,($23),1
00195         .end    sfunction