udivsi3.S 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * File: arch/blackfin/lib/udivsi3.S
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description:
  8. *
  9. * Modified:
  10. * Copyright 2004-2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/linkage.h>
  30. #define CARRY AC0
  31. #ifdef CONFIG_ARITHMETIC_OPS_L1
  32. .section .l1.text
  33. #else
  34. .text
  35. #endif
  36. ENTRY(___udivsi3)
  37. CC = R0 < R1 (IU); /* If X < Y, always return 0 */
  38. IF CC JUMP .Lreturn_ident;
  39. R2 = R1 << 16;
  40. CC = R2 <= R0 (IU);
  41. IF CC JUMP .Lidents;
  42. R2 = R0 >> 31; /* if X is a 31-bit number */
  43. R3 = R1 >> 15; /* and Y is a 15-bit number */
  44. R2 = R2 | R3; /* then it's okay to use the DIVQ builtins (fallthrough to fast)*/
  45. CC = R2;
  46. IF CC JUMP .Ly_16bit;
  47. /* METHOD 1: FAST DIVQ
  48. We know we have a 31-bit dividend, and 15-bit divisor so we can use the
  49. simple divq approach (first setting AQ to 0 - implying unsigned division,
  50. then 16 DIVQ's).
  51. */
  52. AQ = CC; /* Clear AQ (CC==0) */
  53. /* ISR States: When dividing two integers (32.0/16.0) using divide primitives,
  54. we need to shift the dividend one bit to the left.
  55. We have already checked that we have a 31-bit number so we are safe to do
  56. that.
  57. */
  58. R0 <<= 1;
  59. DIVQ(R0, R1); // 1
  60. DIVQ(R0, R1); // 2
  61. DIVQ(R0, R1); // 3
  62. DIVQ(R0, R1); // 4
  63. DIVQ(R0, R1); // 5
  64. DIVQ(R0, R1); // 6
  65. DIVQ(R0, R1); // 7
  66. DIVQ(R0, R1); // 8
  67. DIVQ(R0, R1); // 9
  68. DIVQ(R0, R1); // 10
  69. DIVQ(R0, R1); // 11
  70. DIVQ(R0, R1); // 12
  71. DIVQ(R0, R1); // 13
  72. DIVQ(R0, R1); // 14
  73. DIVQ(R0, R1); // 15
  74. DIVQ(R0, R1); // 16
  75. R0 = R0.L (Z);
  76. RTS;
  77. .Ly_16bit:
  78. /* We know that the upper 17 bits of Y might have bits set,
  79. ** or that the sign bit of X might have a bit. If Y is a
  80. ** 16-bit number, but not bigger, then we can use the builtins
  81. ** with a post-divide correction.
  82. ** R3 currently holds Y>>15, which means R3's LSB is the
  83. ** bit we're interested in.
  84. */
  85. /* According to the ISR, to use the Divide primitives for
  86. ** unsigned integer divide, the useable range is 31 bits
  87. */
  88. CC = ! BITTST(R0, 31);
  89. /* IF condition is true we can scale our inputs and use the divide primitives,
  90. ** with some post-adjustment
  91. */
  92. R3 += -1; /* if so, Y is 0x00008nnn */
  93. CC &= AZ;
  94. /* If condition is true we can scale our inputs and use the divide primitives,
  95. ** with some post-adjustment
  96. */
  97. R3 = R1 >> 1; /* Pre-scaled divisor for primitive case */
  98. R2 = R0 >> 16;
  99. R2 = R3 - R2; /* shifted divisor < upper 16 bits of dividend */
  100. CC &= CARRY;
  101. IF CC JUMP .Lshift_and_correct;
  102. /* Fall through to the identities */
  103. /* METHOD 2: identities and manual calculation
  104. We are not able to use the divide primites, but may still catch some special
  105. cases.
  106. */
  107. .Lidents:
  108. /* Test for common identities. Value to be returned is placed in R2. */
  109. CC = R0 == 0; /* 0/Y => 0 */
  110. IF CC JUMP .Lreturn_r0;
  111. CC = R0 == R1; /* X==Y => 1 */
  112. IF CC JUMP .Lreturn_ident;
  113. CC = R1 == 1; /* X/1 => X */
  114. IF CC JUMP .Lreturn_ident;
  115. R2.L = ONES R1;
  116. R2 = R2.L (Z);
  117. CC = R2 == 1;
  118. IF CC JUMP .Lpower_of_two;
  119. [--SP] = (R7:5); /* Push registers R5-R7 */
  120. /* Idents don't match. Go for the full operation. */
  121. R6 = 2; /* assume we'll shift two */
  122. R3 = 1;
  123. P2 = R1;
  124. /* If either R0 or R1 have sign set, */
  125. /* divide them by two, and note it's */
  126. /* been done. */
  127. CC = R1 < 0;
  128. R2 = R1 >> 1;
  129. IF CC R1 = R2; /* Possibly-shifted R1 */
  130. IF !CC R6 = R3; /* R1 doesn't, so at most 1 shifted */
  131. P0 = 0;
  132. R3 = -R1;
  133. [--SP] = R3;
  134. R2 = R0 >> 1;
  135. R2 = R0 >> 1;
  136. CC = R0 < 0;
  137. IF CC P0 = R6; /* Number of values divided */
  138. IF !CC R2 = R0; /* Shifted R0 */
  139. /* P0 is 0, 1 (NR/=2) or 2 (NR/=2, DR/=2) */
  140. /* r2 holds Copy dividend */
  141. R3 = 0; /* Clear partial remainder */
  142. R7 = 0; /* Initialise quotient bit */
  143. P1 = 32; /* Set loop counter */
  144. LSETUP(.Lulst, .Lulend) LC0 = P1; /* Set loop counter */
  145. .Lulst: R6 = R2 >> 31; /* R6 = sign bit of R2, for carry */
  146. R2 = R2 << 1; /* Shift 64 bit dividend up by 1 bit */
  147. R3 = R3 << 1 || R5 = [SP];
  148. R3 = R3 | R6; /* Include any carry */
  149. CC = R7 < 0; /* Check quotient(AQ) */
  150. /* If AQ==0, we'll sub divisor */
  151. IF CC R5 = R1; /* and if AQ==1, we'll add it. */
  152. R3 = R3 + R5; /* Add/sub divsor to partial remainder */
  153. R7 = R3 ^ R1; /* Generate next quotient bit */
  154. R5 = R7 >> 31; /* Get AQ */
  155. BITTGL(R5, 0); /* Invert it, to get what we'll shift */
  156. .Lulend: R2 = R2 + R5; /* and "shift" it in. */
  157. CC = P0 == 0; /* Check how many inputs we shifted */
  158. IF CC JUMP .Lno_mult; /* if none... */
  159. R6 = R2 << 1;
  160. CC = P0 == 1;
  161. IF CC R2 = R6; /* if 1, Q = Q*2 */
  162. IF !CC R1 = P2; /* if 2, restore stored divisor */
  163. R3 = R2; /* Copy of R2 */
  164. R3 *= R1; /* Q * divisor */
  165. R5 = R0 - R3; /* Z = (dividend - Q * divisor) */
  166. CC = R1 <= R5 (IU); /* Check if divisor <= Z? */
  167. R6 = CC; /* if yes, R6 = 1 */
  168. R2 = R2 + R6; /* if yes, add one to quotient(Q) */
  169. .Lno_mult:
  170. SP += 4;
  171. (R7:5) = [SP++]; /* Pop registers R5-R7 */
  172. R0 = R2; /* Store quotient */
  173. RTS;
  174. .Lreturn_ident:
  175. CC = R0 < R1 (IU); /* If X < Y, always return 0 */
  176. R2 = 0;
  177. IF CC JUMP .Ltrue_return_ident;
  178. R2 = -1 (X); /* X/0 => 0xFFFFFFFF */
  179. CC = R1 == 0;
  180. IF CC JUMP .Ltrue_return_ident;
  181. R2 = -R2; /* R2 now 1 */
  182. CC = R0 == R1; /* X==Y => 1 */
  183. IF CC JUMP .Ltrue_return_ident;
  184. R2 = R0; /* X/1 => X */
  185. /*FALLTHRU*/
  186. .Ltrue_return_ident:
  187. R0 = R2;
  188. .Lreturn_r0:
  189. RTS;
  190. .Lpower_of_two:
  191. /* Y has a single bit set, which means it's a power of two.
  192. ** That means we can perform the division just by shifting
  193. ** X to the right the appropriate number of bits
  194. */
  195. /* signbits returns the number of sign bits, minus one.
  196. ** 1=>30, 2=>29, ..., 0x40000000=>0. Which means we need
  197. ** to shift right n-signbits spaces. It also means 0x80000000
  198. ** is a special case, because that *also* gives a signbits of 0
  199. */
  200. R2 = R0 >> 31;
  201. CC = R1 < 0;
  202. IF CC JUMP .Ltrue_return_ident;
  203. R1.l = SIGNBITS R1;
  204. R1 = R1.L (Z);
  205. R1 += -30;
  206. R0 = LSHIFT R0 by R1.L;
  207. RTS;
  208. /* METHOD 3: PRESCALE AND USE THE DIVIDE PRIMITIVES WITH SOME POST-CORRECTION
  209. Two scaling operations are required to use the divide primitives with a
  210. divisor > 0x7FFFF.
  211. Firstly (as in method 1) we need to shift the dividend 1 to the left for
  212. integer division.
  213. Secondly we need to shift both the divisor and dividend 1 to the right so
  214. both are in range for the primitives.
  215. The left/right shift of the dividend does nothing so we can skip it.
  216. */
  217. .Lshift_and_correct:
  218. R2 = R0;
  219. // R3 is already R1 >> 1
  220. CC=!CC;
  221. AQ = CC; /* Clear AQ, got here with CC = 0 */
  222. DIVQ(R2, R3); // 1
  223. DIVQ(R2, R3); // 2
  224. DIVQ(R2, R3); // 3
  225. DIVQ(R2, R3); // 4
  226. DIVQ(R2, R3); // 5
  227. DIVQ(R2, R3); // 6
  228. DIVQ(R2, R3); // 7
  229. DIVQ(R2, R3); // 8
  230. DIVQ(R2, R3); // 9
  231. DIVQ(R2, R3); // 10
  232. DIVQ(R2, R3); // 11
  233. DIVQ(R2, R3); // 12
  234. DIVQ(R2, R3); // 13
  235. DIVQ(R2, R3); // 14
  236. DIVQ(R2, R3); // 15
  237. DIVQ(R2, R3); // 16
  238. /* According to the Instruction Set Reference:
  239. To divide by a divisor > 0x7FFF,
  240. 1. prescale and perform divide to obtain quotient (Q) (done above),
  241. 2. multiply quotient by unscaled divisor (result M)
  242. 3. subtract the product from the divident to get an error (E = X - M)
  243. 4. if E < divisor (Y) subtract 1, if E > divisor (Y) add 1, else return quotient (Q)
  244. */
  245. R3 = R2.L (Z); /* Q = X' / Y' */
  246. R2 = R3; /* Preserve Q */
  247. R2 *= R1; /* M = Q * Y */
  248. R2 = R0 - R2; /* E = X - M */
  249. R0 = R3; /* Copy Q into result reg */
  250. /* Correction: If result of the multiply is negative, we overflowed
  251. and need to correct the result by subtracting 1 from the result.*/
  252. R3 = 0xFFFF (Z);
  253. R2 = R2 >> 16; /* E >> 16 */
  254. CC = R2 == R3;
  255. R3 = 1 ;
  256. R1 = R0 - R3;
  257. IF CC R0 = R1;
  258. RTS;
  259. ENDPROC(___udivsi3)