udivsi3.S 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(__udivsi3)
  6. abi_entry_default
  7. #if XCHAL_HAVE_DIV32
  8. quou a2, a2, a3
  9. #else
  10. bltui a3, 2, .Lle_one /* check if the divisor <= 1 */
  11. mov a6, a2 /* keep dividend in a6 */
  12. do_nsau a5, a6, a2, a7 /* dividend_shift = nsau (dividend) */
  13. do_nsau a4, a3, a2, a7 /* divisor_shift = nsau (divisor) */
  14. bgeu a5, a4, .Lspecial
  15. sub a4, a4, a5 /* count = divisor_shift - dividend_shift */
  16. ssl a4
  17. sll a3, a3 /* divisor <<= count */
  18. movi a2, 0 /* quotient = 0 */
  19. /* test-subtract-and-shift loop; one quotient bit on each iteration */
  20. #if XCHAL_HAVE_LOOPS
  21. loopnez a4, .Lloopend
  22. #endif /* XCHAL_HAVE_LOOPS */
  23. .Lloop:
  24. bltu a6, a3, .Lzerobit
  25. sub a6, a6, a3
  26. addi a2, a2, 1
  27. .Lzerobit:
  28. slli a2, a2, 1
  29. srli a3, a3, 1
  30. #if !XCHAL_HAVE_LOOPS
  31. addi a4, a4, -1
  32. bnez a4, .Lloop
  33. #endif /* !XCHAL_HAVE_LOOPS */
  34. .Lloopend:
  35. bltu a6, a3, .Lreturn
  36. addi a2, a2, 1 /* increment quotient if dividend >= divisor */
  37. .Lreturn:
  38. abi_ret_default
  39. .Lle_one:
  40. beqz a3, .Lerror /* if divisor == 1, return the dividend */
  41. abi_ret_default
  42. .Lspecial:
  43. /* return dividend >= divisor */
  44. bltu a6, a3, .Lreturn0
  45. movi a2, 1
  46. abi_ret_default
  47. .Lerror:
  48. /* Divide by zero: Use an illegal instruction to force an exception.
  49. The subsequent "DIV0" string can be recognized by the exception
  50. handler to identify the real cause of the exception. */
  51. ill
  52. .ascii "DIV0"
  53. .Lreturn0:
  54. movi a2, 0
  55. #endif /* XCHAL_HAVE_DIV32 */
  56. abi_ret_default
  57. ENDPROC(__udivsi3)