divsi3.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * File: arch/blackfin/lib/divsi3.S
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description: 16 / 32 bit signed division.
  8. * Special cases :
  9. * 1) If(numerator == 0)
  10. * return 0
  11. * 2) If(denominator ==0)
  12. * return positive max = 0x7fffffff
  13. * 3) If(numerator == denominator)
  14. * return 1
  15. * 4) If(denominator ==1)
  16. * return numerator
  17. * 5) If(denominator == -1)
  18. * return -numerator
  19. *
  20. * Operand : R0 - Numerator (i)
  21. * R1 - Denominator (i)
  22. * R0 - Quotient (o)
  23. * Registers Used : R2-R7,P0-P2
  24. *
  25. * Modified:
  26. * Copyright 2004-2006 Analog Devices Inc.
  27. *
  28. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, see the file COPYING, or write
  42. * to the Free Software Foundation, Inc.,
  43. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  44. */
  45. .global ___divsi3;
  46. .type ___divsi3, STT_FUNC;
  47. #ifdef CONFIG_ARITHMETIC_OPS_L1
  48. .section .l1.text
  49. #else
  50. .text
  51. #endif
  52. .align 2;
  53. ___divsi3 :
  54. R3 = R0 ^ R1;
  55. R0 = ABS R0;
  56. CC = V;
  57. r3 = rot r3 by -1;
  58. r1 = abs r1; /* now both positive, r3.30 means "negate result",
  59. ** r3.31 means overflow, add one to result
  60. */
  61. cc = r0 < r1;
  62. if cc jump .Lret_zero;
  63. r2 = r1 >> 15;
  64. cc = r2;
  65. if cc jump .Lidents;
  66. r2 = r1 << 16;
  67. cc = r2 <= r0;
  68. if cc jump .Lidents;
  69. DIVS(R0, R1);
  70. DIVQ(R0, R1);
  71. DIVQ(R0, R1);
  72. DIVQ(R0, R1);
  73. DIVQ(R0, R1);
  74. DIVQ(R0, R1);
  75. DIVQ(R0, R1);
  76. DIVQ(R0, R1);
  77. DIVQ(R0, R1);
  78. DIVQ(R0, R1);
  79. DIVQ(R0, R1);
  80. DIVQ(R0, R1);
  81. DIVQ(R0, R1);
  82. DIVQ(R0, R1);
  83. DIVQ(R0, R1);
  84. DIVQ(R0, R1);
  85. DIVQ(R0, R1);
  86. R0 = R0.L (Z);
  87. r1 = r3 >> 31; /* add overflow issue back in */
  88. r0 = r0 + r1;
  89. r1 = -r0;
  90. cc = bittst(r3, 30);
  91. if cc r0 = r1;
  92. RTS;
  93. /* Can't use the primitives. Test common identities.
  94. ** If the identity is true, return the value in R2.
  95. */
  96. .Lidents:
  97. CC = R1 == 0; /* check for divide by zero */
  98. IF CC JUMP .Lident_return;
  99. CC = R0 == 0; /* check for division of zero */
  100. IF CC JUMP .Lzero_return;
  101. CC = R0 == R1; /* check for identical operands */
  102. IF CC JUMP .Lident_return;
  103. CC = R1 == 1; /* check for divide by 1 */
  104. IF CC JUMP .Lident_return;
  105. R2.L = ONES R1;
  106. R2 = R2.L (Z);
  107. CC = R2 == 1;
  108. IF CC JUMP .Lpower_of_two;
  109. /* Identities haven't helped either.
  110. ** Perform the full division process.
  111. */
  112. P1 = 31; /* Set loop counter */
  113. [--SP] = (R7:5); /* Push registers R5-R7 */
  114. R2 = -R1;
  115. [--SP] = R2;
  116. R2 = R0 << 1; /* R2 lsw of dividend */
  117. R6 = R0 ^ R1; /* Get sign */
  118. R5 = R6 >> 31; /* Shift sign to LSB */
  119. R0 = 0 ; /* Clear msw partial remainder */
  120. R2 = R2 | R5; /* Shift quotient bit */
  121. R6 = R0 ^ R1; /* Get new quotient bit */
  122. LSETUP(.Llst,.Llend) LC0 = P1; /* Setup loop */
  123. .Llst: R7 = R2 >> 31; /* record copy of carry from R2 */
  124. R2 = R2 << 1; /* Shift 64 bit dividend up by 1 bit */
  125. R0 = R0 << 1 || R5 = [SP];
  126. R0 = R0 | R7; /* and add carry */
  127. CC = R6 < 0; /* Check quotient(AQ) */
  128. /* we might be subtracting divisor (AQ==0) */
  129. IF CC R5 = R1; /* or we might be adding divisor (AQ==1)*/
  130. R0 = R0 + R5; /* do add or subtract, as indicated by AQ */
  131. R6 = R0 ^ R1; /* Generate next quotient bit */
  132. R5 = R6 >> 31;
  133. /* Assume AQ==1, shift in zero */
  134. BITTGL(R5,0); /* tweak AQ to be what we want to shift in */
  135. .Llend: R2 = R2 + R5; /* and then set shifted-in value to
  136. ** tweaked AQ.
  137. */
  138. r1 = r3 >> 31;
  139. r2 = r2 + r1;
  140. cc = bittst(r3,30);
  141. r0 = -r2;
  142. if !cc r0 = r2;
  143. SP += 4;
  144. (R7:5)= [SP++]; /* Pop registers R6-R7 */
  145. RTS;
  146. .Lident_return:
  147. CC = R1 == 0; /* check for divide by zero => 0x7fffffff */
  148. R2 = -1 (X);
  149. R2 >>= 1;
  150. IF CC JUMP .Ltrue_ident_return;
  151. CC = R0 == R1; /* check for identical operands => 1 */
  152. R2 = 1 (Z);
  153. IF CC JUMP .Ltrue_ident_return;
  154. R2 = R0; /* assume divide by 1 => numerator */
  155. /*FALLTHRU*/
  156. .Ltrue_ident_return:
  157. R0 = R2; /* Return an identity value */
  158. R2 = -R2;
  159. CC = bittst(R3,30);
  160. IF CC R0 = R2;
  161. .Lzero_return:
  162. RTS; /* ...including zero */
  163. .Lpower_of_two:
  164. /* Y has a single bit set, which means it's a power of two.
  165. ** That means we can perform the division just by shifting
  166. ** X to the right the appropriate number of bits
  167. */
  168. /* signbits returns the number of sign bits, minus one.
  169. ** 1=>30, 2=>29, ..., 0x40000000=>0. Which means we need
  170. ** to shift right n-signbits spaces. It also means 0x80000000
  171. ** is a special case, because that *also* gives a signbits of 0
  172. */
  173. R2 = R0 >> 31;
  174. CC = R1 < 0;
  175. IF CC JUMP .Ltrue_ident_return;
  176. R1.l = SIGNBITS R1;
  177. R1 = R1.L (Z);
  178. R1 += -30;
  179. R0 = LSHIFT R0 by R1.L;
  180. r1 = r3 >> 31;
  181. r0 = r0 + r1;
  182. R2 = -R0; // negate result if necessary
  183. CC = bittst(R3,30);
  184. IF CC R0 = R2;
  185. RTS;
  186. .Lret_zero:
  187. R0 = 0;
  188. RTS;
  189. .size ___divsi3, .-___divsi3