remq.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 remainder. 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 __remq
  35. .type __remq, @funcnoplt
  36. .usepv __remq, no
  37. cfi_startproc
  38. cfi_return_column (RA)
  39. __remq:
  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, compute remainder, clean up. */
  68. cvttq/c $f0, $f0
  69. excb
  70. mt_fpcr $f3
  71. _FTOIT $f0, AT, 16
  72. mulq AT, Y, AT
  73. ldt $f0, 0(sp)
  74. ldt $f3, 48(sp)
  75. cfi_restore ($f1)
  76. cfi_remember_state
  77. cfi_restore ($f0)
  78. cfi_restore ($f3)
  79. cfi_def_cfa_offset (0)
  80. lda sp, FRAME(sp)
  81. subq X, AT, RV
  82. ret $31, (RA), 1
  83. .align 4
  84. cfi_restore_state
  85. $x_big:
  86. /* If we get here, X is large enough that we don't expect exact
  87. results, and neither X nor Y got mis-translated for the fp
  88. division. Our task is to take the fp result, figure out how
  89. far it's off from the correct result and compute a fixup. */
  90. stq t0, 16(sp)
  91. stq t1, 24(sp)
  92. stq t2, 32(sp)
  93. stq t5, 40(sp)
  94. cfi_rel_offset (t0, 16)
  95. cfi_rel_offset (t1, 24)
  96. cfi_rel_offset (t2, 32)
  97. cfi_rel_offset (t5, 40)
  98. #define Q t0 /* quotient */
  99. #define R RV /* remainder */
  100. #define SY t1 /* scaled Y */
  101. #define S t2 /* scalar */
  102. #define QY t3 /* Q*Y */
  103. /* The fixup code below can only handle unsigned values. */
  104. or X, Y, AT
  105. mov $31, t5
  106. blt AT, $fix_sign_in
  107. $fix_sign_in_ret1:
  108. cvttq/c $f0, $f0
  109. _FTOIT $f0, Q, 8
  110. .align 3
  111. $fix_sign_in_ret2:
  112. ldt $f0, 0(sp)
  113. stq t3, 0(sp)
  114. cfi_restore ($f0)
  115. cfi_rel_offset (t3, 0)
  116. mulq Q, Y, QY
  117. excb
  118. stq t4, 8(sp)
  119. mt_fpcr $f3
  120. cfi_rel_offset (t4, 8)
  121. subq QY, X, R
  122. mov Y, SY
  123. mov 1, S
  124. bgt R, $q_high
  125. $q_high_ret:
  126. subq X, QY, R
  127. mov Y, SY
  128. mov 1, S
  129. bgt R, $q_low
  130. $q_low_ret:
  131. ldq t0, 16(sp)
  132. ldq t1, 24(sp)
  133. ldq t2, 32(sp)
  134. bne t5, $fix_sign_out
  135. $fix_sign_out_ret:
  136. ldq t3, 0(sp)
  137. ldq t4, 8(sp)
  138. ldq t5, 40(sp)
  139. ldt $f3, 48(sp)
  140. lda sp, FRAME(sp)
  141. cfi_remember_state
  142. cfi_restore (t0)
  143. cfi_restore (t1)
  144. cfi_restore (t2)
  145. cfi_restore (t3)
  146. cfi_restore (t4)
  147. cfi_restore (t5)
  148. cfi_restore ($f3)
  149. cfi_def_cfa_offset (0)
  150. ret $31, (RA), 1
  151. .align 4
  152. cfi_restore_state
  153. /* The quotient that we computed was too large. We need to reduce
  154. it by S such that Y*S >= R. Obviously the closer we get to the
  155. correct value the better, but overshooting high is ok, as we'll
  156. fix that up later. */
  157. 0:
  158. addq SY, SY, SY
  159. addq S, S, S
  160. $q_high:
  161. cmpult SY, R, AT
  162. bne AT, 0b
  163. subq Q, S, Q
  164. unop
  165. subq QY, SY, QY
  166. br $q_high_ret
  167. .align 4
  168. /* The quotient that we computed was too small. Divide Y by the
  169. current remainder (R) and add that to the existing quotient (Q).
  170. The expectation, of course, is that R is much smaller than X. */
  171. /* Begin with a shift-up loop. Compute S such that Y*S >= R. We
  172. already have a copy of Y in SY and the value 1 in S. */
  173. 0:
  174. addq SY, SY, SY
  175. addq S, S, S
  176. $q_low:
  177. cmpult SY, R, AT
  178. bne AT, 0b
  179. /* Shift-down and subtract loop. Each iteration compares our scaled
  180. Y (SY) with the remainder (R); if SY <= R then X is divisible by
  181. Y's scalar (S) so add it to the quotient (Q). */
  182. 2: addq Q, S, t3
  183. srl S, 1, S
  184. cmpule SY, R, AT
  185. subq R, SY, t4
  186. cmovne AT, t3, Q
  187. cmovne AT, t4, R
  188. srl SY, 1, SY
  189. bne S, 2b
  190. br $q_low_ret
  191. .align 4
  192. $fix_sign_in:
  193. /* If we got here, then X|Y is negative. Need to adjust everything
  194. such that we're doing unsigned division in the fixup loop. */
  195. /* T5 records the changes we had to make:
  196. bit 0: set if X was negated. Note that the sign of the
  197. remainder follows the sign of the divisor.
  198. bit 2: set if Y was negated.
  199. */
  200. xor X, Y, t1
  201. cmplt X, 0, t5
  202. negq X, t0
  203. cmovne t5, t0, X
  204. cmplt Y, 0, AT
  205. negq Y, t0
  206. s4addq AT, t5, t5
  207. cmovne AT, t0, Y
  208. bge t1, $fix_sign_in_ret1
  209. cvttq/c $f0, $f0
  210. _FTOIT $f0, Q, 8
  211. .align 3
  212. negq Q, Q
  213. br $fix_sign_in_ret2
  214. .align 4
  215. $fix_sign_out:
  216. /* Now we get to undo what we did above. */
  217. /* ??? Is this really faster than just increasing the size of
  218. the stack frame and storing X and Y in memory? */
  219. and t5, 4, AT
  220. negq Y, t4
  221. cmovne AT, t4, Y
  222. negq X, t4
  223. cmovlbs t5, t4, X
  224. negq RV, t4
  225. cmovlbs t5, t4, RV
  226. br $fix_sign_out_ret
  227. cfi_endproc
  228. .size __remq, .-__remq
  229. DO_DIVBYZERO