root/arch/alpha/lib/divide.S

/* [previous][next][first][last][top][bottom][index][help] */
   1 /*
   2  * arch/alpha/lib/divide.S
   3  *
   4  * (C) 1995 Linus Torvalds
   5  *
   6  * Alpha division..
   7  */
   8 
   9 /*
  10  * The alpha chip doesn't provide hardware division, so we have to do it
  11  * by hand.  The compiler expects the functions
  12  *
  13  *      __divqu: 64-bit unsigned long divide
  14  *      __remqu: 64-bit unsigned long remainder
  15  *      __divqs/__remqs: signed 64-bit
  16  *      __divlu/__remlu: unsigned 32-bit
  17  *      __divls/__remls: signed 32-bit
  18  *
  19  * These are not normal C functions: instead of the normal
  20  * calling sequence, these expect their arguments in registers
  21  * $24 and $25, and return the result in $27. Register $28 may
  22  * be clobbered (assembly temporary), anything else must be saved. 
  23  *
  24  * In short: painful.
  25  *
  26  * This is a rather simple bit-at-a-time algorithm: it's very good
  27  * at dividing random 64-bit numbers, but the more usual case where
  28  * the divisor is small is handled better by the DEC algorithm
  29  * using lookup tables. This uses much less memory, though, and is
  30  * nicer on the cache.. Besides, I don't know the copyright status
  31  * of the DEC code.
  32  */
  33 
  34 /*
  35  * My temporaries:
  36  *      $0 - current bit
  37  *      $1 - shifted divisor
  38  *      $2 - modulus/quotient
  39  *
  40  *      $23 - return address
  41  *      $24 - dividend
  42  *      $25 - divisor
  43  *
  44  *      $27 - quotient/modulus
  45  *      $28 - compare status
  46  */
  47 
  48 #define halt .long 0
  49 
  50 /*
  51  * Select function type and registers
  52  */
  53 #define mask    $0
  54 #define divisor $1
  55 #define compare $28
  56 
  57 #ifdef DIV
  58 #define func(x) __div##x
  59 #define modulus $2
  60 #define quotient $27
  61 #define GETSIGN(x) xor $24,$25,x
  62 #else
  63 #define func(x) __rem##x
  64 #define modulus $27
  65 #define quotient $2
  66 #define GETSIGN(x) bis $24,$24,x
  67 #endif
  68 
  69 /*
  70  * For 32-bit operations, we need to extend to 64-bit
  71  */
  72 #ifdef INTSIZE
  73 #define ufunction func(lu)
  74 #define sfunction func(l)
  75 #define LONGIFY(x) zapnot x,15,x
  76 #define SLONGIFY(x) addl x,0,x
  77 #else
  78 #define ufunction func(qu)
  79 #define sfunction func(q)
  80 #define LONGIFY(x)
  81 #define SLONGIFY(x)
  82 #endif
  83 
  84 .set noat
  85 .globl  ufunction
  86 .ent    ufunction
  87 ufunction:
  88         subq    $30,32,$30
  89         stq     $0, 0($30)
  90         stq     $1, 8($30)
  91         stq     $2,16($30)
  92 
  93         bis     $25,$25,divisor
  94         bis     $24,$24,modulus
  95         bis     $31,$31,quotient
  96         LONGIFY(divisor)
  97         LONGIFY(modulus)
  98         beq     divisor, 9f                     /* div by zero */
  99         bis     $31,1,mask
 100 
 101         /* shift divisor left */
 102 1:      cmpult  divisor,modulus,compare
 103         blt     divisor, 3f
 104         addq    divisor,divisor,divisor
 105         addq    mask,mask,mask
 106         bne     compare,1b
 107 
 108         /* ok, start to go right again.. */
 109 2:      srl     divisor,1,divisor
 110         beq     mask,9f
 111         srl     mask,1,mask
 112 3:      cmpule  divisor,modulus,compare
 113         beq     compare,2b
 114         addq    quotient,mask,quotient
 115         beq     mask,9f
 116         subq    modulus,divisor,modulus
 117         br      2b
 118 
 119 9:      ldq     $0, 0($30)
 120         ldq     $1, 8($30)
 121         ldq     $2, 16($30)
 122         addq    $30,32,$30
 123         ret     $31,($23),1
 124         .end    ufunction
 125 
 126 /*
 127  * Uhh.. Ugly signed division. I'd rather not have it at all, but
 128  * it's needed in some circumstances. There are different ways to
 129  * handle this, really. This does:
 130  *      -a / b = a / -b = -(a / b)
 131  *      -a % b = -(a % b)
 132  *      a % -b = a % b
 133  * which is probably not the best solution, but at least should
 134  * have the property that (x/y)*y + (x%y) = x.
 135  */
 136 .globl  sfunction
 137 .ent    sfunction
 138 sfunction:
 139         bis     $24,$25,$28
 140         SLONGIFY($28)
 141         bge     $28,ufunction
 142         subq    $30,32,$30
 143         stq     $23,0($30)
 144         stq     $24,8($30)
 145         stq     $25,16($30)
 146         subq    $31,$24,$28
 147         cmovlt  $24,$28,$24     /* abs($24) */
 148         subq    $31,$25,$28
 149         cmovlt  $25,$28,$25     /* abs($25) */
 150         bsr     $23,ufunction
 151         ldq     $23,0($30)
 152         ldq     $24,8($30)
 153         ldq     $25,16($30)
 154         addq    $30,32,$30
 155         GETSIGN($28)
 156         SLONGIFY($28)
 157         bge     $28,1f
 158         subq    $31,$27,$27
 159 1:      ret     $31,($23),1
 160         .end    sfunction

/* [previous][next][first][last][top][bottom][index][help] */