divq.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (C) 2004-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include "div_libc.h"
  15. /* 64-bit signed long divide. These are not normal C functions. Argument
  16. registers are t10 and t11, the result goes in t12. Only t12 and AT may
  17. be clobbered.
  18. Theory of operation here is that we can use the FPU divider for virtually
  19. all operands that we see: all dividend values between -2**53 and 2**53-1
  20. can be computed directly. Note that divisor values need not be checked
  21. against that range because the rounded fp value will be close enough such
  22. that the quotient is < 1, which will properly be truncated to zero when we
  23. convert back to integer.
  24. When the dividend is outside the range for which we can compute exact
  25. results, we use the fp quotient as an estimate from which we begin refining
  26. an exact integral value. This reduces the number of iterations in the
  27. shift-and-subtract loop significantly.
  28. The FPCR save/restore is due to the fact that the EV6 _will_ set FPCR_INE
  29. for cvttq/c even without /sui being set. It will not, however, properly
  30. raise the exception, so we don't have to worry about FPCR_INED being clear
  31. and so dying by SIGFPE. */
  32. .text
  33. .align 4
  34. .globl __divq
  35. .type __divq, @funcnoplt
  36. .usepv __divq, no
  37. cfi_startproc
  38. cfi_return_column (RA)
  39. __divq:
  40. lda sp, -FRAME(sp)
  41. cfi_def_cfa_offset (FRAME)
  42. CALL_MCOUNT
  43. /* Get the fp divide insn issued as quickly as possible. After
  44. that's done, we have at least 22 cycles until its results are
  45. ready -- all the time in the world to figure out how we're
  46. going to use the results. */
  47. stt $f0, 0(sp)
  48. excb
  49. beq Y, DIVBYZERO
  50. stt $f1, 8(sp)
  51. stt $f3, 48(sp)
  52. cfi_rel_offset ($f0, 0)
  53. cfi_rel_offset ($f1, 8)
  54. cfi_rel_offset ($f3, 48)
  55. mf_fpcr $f3
  56. _ITOFT2 X, $f0, 16, Y, $f1, 24
  57. cvtqt $f0, $f0
  58. cvtqt $f1, $f1
  59. divt/c $f0, $f1, $f0
  60. /* Check to see if X fit in the double as an exact value. */
  61. sll X, (64-53), AT
  62. ldt $f1, 8(sp)
  63. sra AT, (64-53), AT
  64. cmpeq X, AT, AT
  65. beq AT, $x_big
  66. /* If we get here, we're expecting exact results from the division.
  67. Do nothing else besides convert and clean up. */
  68. cvttq/c $f0, $f0
  69. excb
  70. mt_fpcr $f3
  71. _FTOIT $f0, RV, 16
  72. ldt $f0, 0(sp)
  73. ldt $f3, 48(sp)
  74. cfi_restore ($f1)
  75. cfi_remember_state
  76. cfi_restore ($f0)
  77. cfi_restore ($f3)
  78. cfi_def_cfa_offset (0)
  79. lda sp, FRAME(sp)
  80. ret $31, (RA), 1
  81. .align 4
  82. cfi_restore_state
  83. $x_big:
  84. /* If we get here, X is large enough that we don't expect exact
  85. results, and neither X nor Y got mis-translated for the fp
  86. division. Our task is to take the fp result, figure out how
  87. far it's off from the correct result and compute a fixup. */
  88. stq t0, 16(sp)
  89. stq t1, 24(sp)
  90. stq t2, 32(sp)
  91. stq t5, 40(sp)
  92. cfi_rel_offset (t0, 16)
  93. cfi_rel_offset (t1, 24)
  94. cfi_rel_offset (t2, 32)
  95. cfi_rel_offset (t5, 40)
  96. #define Q RV /* quotient */
  97. #define R t0 /* remainder */
  98. #define SY t1 /* scaled Y */
  99. #define S t2 /* scalar */
  100. #define QY t3 /* Q*Y */
  101. /* The fixup code below can only handle unsigned values. */
  102. or X, Y, AT
  103. mov $31, t5
  104. blt AT, $fix_sign_in
  105. $fix_sign_in_ret1:
  106. cvttq/c $f0, $f0
  107. _FTOIT $f0, Q, 8
  108. .align 3
  109. $fix_sign_in_ret2:
  110. ldt $f0, 0(sp)
  111. stq t3, 0(sp)
  112. cfi_restore ($f0)
  113. cfi_rel_offset (t3, 0)
  114. mulq Q, Y, QY
  115. excb
  116. stq t4, 8(sp)
  117. mt_fpcr $f3
  118. cfi_rel_offset (t4, 8)
  119. subq QY, X, R
  120. mov Y, SY
  121. mov 1, S
  122. bgt R, $q_high
  123. $q_high_ret:
  124. subq X, QY, R
  125. mov Y, SY
  126. mov 1, S
  127. bgt R, $q_low
  128. $q_low_ret:
  129. ldq t0, 16(sp)
  130. ldq t1, 24(sp)
  131. ldq t2, 32(sp)
  132. bne t5, $fix_sign_out
  133. $fix_sign_out_ret:
  134. ldq t3, 0(sp)
  135. ldq t4, 8(sp)
  136. ldq t5, 40(sp)
  137. ldt $f3, 48(sp)
  138. lda sp, FRAME(sp)
  139. cfi_remember_state
  140. cfi_restore (t0)
  141. cfi_restore (t1)
  142. cfi_restore (t2)
  143. cfi_restore (t3)
  144. cfi_restore (t4)
  145. cfi_restore (t5)
  146. cfi_restore ($f3)
  147. cfi_def_cfa_offset (0)
  148. ret $31, (RA), 1
  149. .align 4
  150. cfi_restore_state
  151. /* The quotient that we computed was too large. We need to reduce
  152. it by S such that Y*S >= R. Obviously the closer we get to the
  153. correct value the better, but overshooting high is ok, as we'll
  154. fix that up later. */
  155. 0:
  156. addq SY, SY, SY
  157. addq S, S, S
  158. $q_high:
  159. cmpult SY, R, AT
  160. bne AT, 0b
  161. subq Q, S, Q
  162. unop
  163. subq QY, SY, QY
  164. br $q_high_ret
  165. .align 4
  166. /* The quotient that we computed was too small. Divide Y by the
  167. current remainder (R) and add that to the existing quotient (Q).
  168. The expectation, of course, is that R is much smaller than X. */
  169. /* Begin with a shift-up loop. Compute S such that Y*S >= R. We
  170. already have a copy of Y in SY and the value 1 in S. */
  171. 0:
  172. addq SY, SY, SY
  173. addq S, S, S
  174. $q_low:
  175. cmpult SY, R, AT
  176. bne AT, 0b
  177. /* Shift-down and subtract loop. Each iteration compares our scaled
  178. Y (SY) with the remainder (R); if SY <= R then X is divisible by
  179. Y's scalar (S) so add it to the quotient (Q). */
  180. 2: addq Q, S, t3
  181. srl S, 1, S
  182. cmpule SY, R, AT
  183. subq R, SY, t4
  184. cmovne AT, t3, Q
  185. cmovne AT, t4, R
  186. srl SY, 1, SY
  187. bne S, 2b
  188. br $q_low_ret
  189. .align 4
  190. $fix_sign_in:
  191. /* If we got here, then X|Y is negative. Need to adjust everything
  192. such that we're doing unsigned division in the fixup loop. */
  193. /* T5 records the changes we had to make:
  194. bit 0: set if result should be negative.
  195. bit 2: set if X was negated.
  196. bit 3: set if Y was negated.
  197. */
  198. xor X, Y, AT
  199. cmplt AT, 0, t5
  200. cmplt X, 0, AT
  201. negq X, t0
  202. s4addq AT, t5, t5
  203. cmovne AT, t0, X
  204. cmplt Y, 0, AT
  205. negq Y, t0
  206. s8addq AT, t5, t5
  207. cmovne AT, t0, Y
  208. unop
  209. blbc t5, $fix_sign_in_ret1
  210. cvttq/c $f0, $f0
  211. _FTOIT $f0, Q, 8
  212. .align 3
  213. negq Q, Q
  214. br $fix_sign_in_ret2
  215. .align 4
  216. $fix_sign_out:
  217. /* Now we get to undo what we did above. */
  218. /* ??? Is this really faster than just increasing the size of
  219. the stack frame and storing X and Y in memory? */
  220. and t5, 8, AT
  221. negq Y, t4
  222. cmovne AT, t4, Y
  223. and t5, 4, AT
  224. negq X, t4
  225. cmovne AT, t4, X
  226. negq RV, t4
  227. cmovlbs t5, t4, RV
  228. br $fix_sign_out_ret
  229. cfi_endproc
  230. .size __divq, .-__divq
  231. DO_DIVBYZERO