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 #else
  62 #define func(x) __rem##x
  63 #define modulus $27
  64 #define quotient $2
  65 #endif
  66 
  67 /*
  68  * For 32-bit operations, we need to extend to 64-bit
  69  */
  70 #ifdef INTSIZE
  71 #define ufunction func(lu)
  72 #define sfunction func(l)
  73 #define LONGIFY(x) zapnot x,15,x
  74 #define SLONGIFY(x) addl x,0,x
  75 #else
  76 #define ufunction func(qu)
  77 #define sfunction func(q)
  78 #define LONGIFY(x)
  79 #define SLONGIFY(x)
  80 #endif
  81 
  82 .set noat
  83 .globl  ufunction
  84 .ent    ufunction
  85 ufunction:
  86         subq    $30,32,$30
  87         stq     $0, 0($30)
  88         stq     $1, 8($30)
  89         stq     $2,16($30)
  90 
  91         bis     $25,$25,divisor
  92         bis     $24,$24,modulus
  93         bis     $31,$31,quotient
  94         LONGIFY(divisor)
  95         LONGIFY(modulus)
  96         beq     divisor, 9f                     /* div by zero */
  97         bis     $31,1,mask
  98 
  99         /* shift divisor left */
 100 1:      cmpult  divisor,modulus,compare
 101         blt     divisor, 3f
 102         addq    divisor,divisor,divisor
 103         addq    mask,mask,mask
 104         bne     compare,1b
 105 
 106         /* ok, start to go right again.. */
 107 2:      srl     divisor,1,divisor
 108         beq     mask,9f
 109         srl     mask,1,mask
 110 3:      cmpule  divisor,modulus,compare
 111         beq     compare,2b
 112         addq    quotient,mask,quotient
 113         beq     mask,9f
 114         subq    modulus,divisor,modulus
 115         br      2b
 116 
 117 9:      ldq     $0, 0($30)
 118         ldq     $1, 8($30)
 119         ldq     $2, 16($30)
 120         addq    $30,32,$30
 121         ret     $31,($23),1
 122         .end    ufunction
 123 
 124 /*
 125  * The "signed" version just does a halt if either of the value is
 126  * signed: the kernel shouldn't mess with signed divides anyway (who
 127  * knows what way they'll round..)
 128  */
 129 .globl  sfunction
 130 .ent    sfunction
 131 sfunction:
 132         bis     $24,$25,$28
 133         SLONGIFY($28)
 134         bge     $28,ufunction
 135         halt
 136         .end    sfunction

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