divsi3.S 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0 */
  2. #include <linux/linkage.h>
  3. #include <asm/asmmacro.h>
  4. #include <asm/core.h>
  5. ENTRY(__divsi3)
  6. abi_entry_default
  7. #if XCHAL_HAVE_DIV32
  8. quos a2, a2, a3
  9. #else
  10. xor a7, a2, a3 /* sign = dividend ^ divisor */
  11. do_abs a6, a2, a4 /* udividend = abs (dividend) */
  12. do_abs a3, a3, a4 /* udivisor = abs (divisor) */
  13. bltui a3, 2, .Lle_one /* check if udivisor <= 1 */
  14. do_nsau a5, a6, a2, a8 /* udividend_shift = nsau (udividend) */
  15. do_nsau a4, a3, a2, a8 /* udivisor_shift = nsau (udivisor) */
  16. bgeu a5, a4, .Lspecial
  17. sub a4, a4, a5 /* count = udivisor_shift - udividend_shift */
  18. ssl a4
  19. sll a3, a3 /* udivisor <<= count */
  20. movi a2, 0 /* quotient = 0 */
  21. /* test-subtract-and-shift loop; one quotient bit on each iteration */
  22. #if XCHAL_HAVE_LOOPS
  23. loopnez a4, .Lloopend
  24. #endif /* XCHAL_HAVE_LOOPS */
  25. .Lloop:
  26. bltu a6, a3, .Lzerobit
  27. sub a6, a6, a3
  28. addi a2, a2, 1
  29. .Lzerobit:
  30. slli a2, a2, 1
  31. srli a3, a3, 1
  32. #if !XCHAL_HAVE_LOOPS
  33. addi a4, a4, -1
  34. bnez a4, .Lloop
  35. #endif /* !XCHAL_HAVE_LOOPS */
  36. .Lloopend:
  37. bltu a6, a3, .Lreturn
  38. addi a2, a2, 1 /* increment if udividend >= udivisor */
  39. .Lreturn:
  40. neg a5, a2
  41. movltz a2, a5, a7 /* return (sign < 0) ? -quotient : quotient */
  42. abi_ret_default
  43. .Lle_one:
  44. beqz a3, .Lerror
  45. neg a2, a6 /* if udivisor == 1, then return... */
  46. movgez a2, a6, a7 /* (sign < 0) ? -udividend : udividend */
  47. abi_ret_default
  48. .Lspecial:
  49. bltu a6, a3, .Lreturn0 /* if dividend < divisor, return 0 */
  50. movi a2, 1
  51. movi a4, -1
  52. movltz a2, a4, a7 /* else return (sign < 0) ? -1 : 1 */
  53. abi_ret_default
  54. .Lerror:
  55. /* Divide by zero: Use an illegal instruction to force an exception.
  56. The subsequent "DIV0" string can be recognized by the exception
  57. handler to identify the real cause of the exception. */
  58. ill
  59. .ascii "DIV0"
  60. .Lreturn0:
  61. movi a2, 0
  62. #endif /* XCHAL_HAVE_DIV32 */
  63. abi_ret_default
  64. ENDPROC(__divsi3)