divqu.S 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 unsigned 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 be
  17. 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 __divqu
  35. .type __divqu, @funcnoplt
  36. .usepv __divqu, no
  37. cfi_startproc
  38. cfi_return_column (RA)
  39. __divqu:
  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. beq Y, DIVBYZERO
  48. stt $f0, 0(sp)
  49. excb
  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. blt X, $x_is_neg
  60. divt/c $f0, $f1, $f0
  61. /* Check to see if Y was mis-converted as signed value. */
  62. ldt $f1, 8(sp)
  63. blt Y, $y_is_neg
  64. /* Check to see if X fit in the double as an exact value. */
  65. srl X, 53, AT
  66. bne AT, $x_big
  67. /* If we get here, we're expecting exact results from the division.
  68. Do nothing else besides convert and clean up. */
  69. cvttq/c $f0, $f0
  70. excb
  71. mt_fpcr $f3
  72. _FTOIT $f0, RV, 16
  73. ldt $f0, 0(sp)
  74. ldt $f3, 48(sp)
  75. lda sp, FRAME(sp)
  76. cfi_remember_state
  77. cfi_restore ($f0)
  78. cfi_restore ($f1)
  79. cfi_restore ($f3)
  80. cfi_def_cfa_offset (0)
  81. ret $31, (RA), 1
  82. .align 4
  83. cfi_restore_state
  84. $x_is_neg:
  85. /* If we get here, X is so big that bit 63 is set, which made the
  86. conversion come out negative. Fix it up lest we not even get
  87. a good estimate. */
  88. ldah AT, 0x5f80 /* 2**64 as float. */
  89. stt $f2, 24(sp)
  90. cfi_rel_offset ($f2, 24)
  91. _ITOFS AT, $f2, 16
  92. .align 4
  93. addt $f0, $f2, $f0
  94. unop
  95. divt/c $f0, $f1, $f0
  96. unop
  97. /* Ok, we've now the divide issued. Continue with other checks. */
  98. ldt $f1, 8(sp)
  99. unop
  100. ldt $f2, 24(sp)
  101. blt Y, $y_is_neg
  102. cfi_restore ($f1)
  103. cfi_restore ($f2)
  104. cfi_remember_state /* for y_is_neg */
  105. .align 4
  106. $x_big:
  107. /* If we get here, X is large enough that we don't expect exact
  108. results, and neither X nor Y got mis-translated for the fp
  109. division. Our task is to take the fp result, figure out how
  110. far it's off from the correct result and compute a fixup. */
  111. stq t0, 16(sp)
  112. stq t1, 24(sp)
  113. stq t2, 32(sp)
  114. stq t3, 40(sp)
  115. cfi_rel_offset (t0, 16)
  116. cfi_rel_offset (t1, 24)
  117. cfi_rel_offset (t2, 32)
  118. cfi_rel_offset (t3, 40)
  119. #define Q RV /* quotient */
  120. #define R t0 /* remainder */
  121. #define SY t1 /* scaled Y */
  122. #define S t2 /* scalar */
  123. #define QY t3 /* Q*Y */
  124. cvttq/c $f0, $f0
  125. _FTOIT $f0, Q, 8
  126. mulq Q, Y, QY
  127. .align 4
  128. stq t4, 8(sp)
  129. excb
  130. ldt $f0, 0(sp)
  131. mt_fpcr $f3
  132. cfi_rel_offset (t4, 8)
  133. cfi_restore ($f0)
  134. subq QY, X, R
  135. mov Y, SY
  136. mov 1, S
  137. bgt R, $q_high
  138. $q_high_ret:
  139. subq X, QY, R
  140. mov Y, SY
  141. mov 1, S
  142. bgt R, $q_low
  143. $q_low_ret:
  144. ldq t4, 8(sp)
  145. ldq t0, 16(sp)
  146. ldq t1, 24(sp)
  147. ldq t2, 32(sp)
  148. ldq t3, 40(sp)
  149. ldt $f3, 48(sp)
  150. lda sp, FRAME(sp)
  151. cfi_remember_state
  152. cfi_restore (t0)
  153. cfi_restore (t1)
  154. cfi_restore (t2)
  155. cfi_restore (t3)
  156. cfi_restore (t4)
  157. cfi_restore ($f3)
  158. cfi_def_cfa_offset (0)
  159. ret $31, (RA), 1
  160. .align 4
  161. cfi_restore_state
  162. /* The quotient that we computed was too large. We need to reduce
  163. it by S such that Y*S >= R. Obviously the closer we get to the
  164. correct value the better, but overshooting high is ok, as we'll
  165. fix that up later. */
  166. 0:
  167. addq SY, SY, SY
  168. addq S, S, S
  169. $q_high:
  170. cmpult SY, R, AT
  171. bne AT, 0b
  172. subq Q, S, Q
  173. unop
  174. subq QY, SY, QY
  175. br $q_high_ret
  176. .align 4
  177. /* The quotient that we computed was too small. Divide Y by the
  178. current remainder (R) and add that to the existing quotient (Q).
  179. The expectation, of course, is that R is much smaller than X. */
  180. /* Begin with a shift-up loop. Compute S such that Y*S >= R. We
  181. already have a copy of Y in SY and the value 1 in S. */
  182. 0:
  183. addq SY, SY, SY
  184. addq S, S, S
  185. $q_low:
  186. cmpult SY, R, AT
  187. bne AT, 0b
  188. /* Shift-down and subtract loop. Each iteration compares our scaled
  189. Y (SY) with the remainder (R); if SY <= R then X is divisible by
  190. Y's scalar (S) so add it to the quotient (Q). */
  191. 2: addq Q, S, t3
  192. srl S, 1, S
  193. cmpule SY, R, AT
  194. subq R, SY, t4
  195. cmovne AT, t3, Q
  196. cmovne AT, t4, R
  197. srl SY, 1, SY
  198. bne S, 2b
  199. br $q_low_ret
  200. .align 4
  201. cfi_restore_state
  202. $y_is_neg:
  203. /* If we get here, Y is so big that bit 63 is set. The results
  204. from the divide will be completely wrong. Fortunately, the
  205. quotient must be either 0 or 1, so just compute it directly. */
  206. cmpule Y, X, RV
  207. excb
  208. mt_fpcr $f3
  209. ldt $f0, 0(sp)
  210. ldt $f3, 48(sp)
  211. lda sp, FRAME(sp)
  212. cfi_restore ($f0)
  213. cfi_restore ($f3)
  214. cfi_def_cfa_offset (0)
  215. ret $31, (RA), 1
  216. cfi_endproc
  217. .size __divqu, .-__divqu
  218. DO_DIVBYZERO