udivsi3.S 8.3 KB

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