divide.S 4.3 KB

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