udivsi3.S 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include "libgcc.h"
  3. ;; This function also computes the remainder and stores it in er3.
  4. .global __udivsi3
  5. __udivsi3:
  6. mov.w A1E,A1E ; denominator top word 0?
  7. bne DenHighNonZero
  8. ; do it the easy way, see page 107 in manual
  9. mov.w A0E,A2
  10. extu.l A2P
  11. divxu.w A1,A2P
  12. mov.w A2E,A0E
  13. divxu.w A1,A0P
  14. mov.w A0E,A3
  15. mov.w A2,A0E
  16. extu.l A3P
  17. rts
  18. ; er0 = er0 / er1
  19. ; er3 = er0 % er1
  20. ; trashes er1 er2
  21. ; expects er1 >= 2^16
  22. DenHighNonZero:
  23. mov.l er0,er3
  24. mov.l er1,er2
  25. #ifdef CONFIG_CPU_H8300H
  26. divmod_L21:
  27. shlr.l er0
  28. shlr.l er2 ; make divisor < 2^16
  29. mov.w e2,e2
  30. bne divmod_L21
  31. #else
  32. shlr.l #2,er2 ; make divisor < 2^16
  33. mov.w e2,e2
  34. beq divmod_L22A
  35. divmod_L21:
  36. shlr.l #2,er0
  37. divmod_L22:
  38. shlr.l #2,er2 ; make divisor < 2^16
  39. mov.w e2,e2
  40. bne divmod_L21
  41. divmod_L22A:
  42. rotxl.w r2
  43. bcs divmod_L23
  44. shlr.l er0
  45. bra divmod_L24
  46. divmod_L23:
  47. rotxr.w r2
  48. shlr.l #2,er0
  49. divmod_L24:
  50. #endif
  51. ;; At this point,
  52. ;; er0 contains shifted dividend
  53. ;; er1 contains divisor
  54. ;; er2 contains shifted divisor
  55. ;; er3 contains dividend, later remainder
  56. divxu.w r2,er0 ; r0 now contains the approximate quotient (AQ)
  57. extu.l er0
  58. beq divmod_L25
  59. subs #1,er0 ; er0 = AQ - 1
  60. mov.w e1,r2
  61. mulxu.w r0,er2 ; er2 = upper (AQ - 1) * divisor
  62. sub.w r2,e3 ; dividend - 65536 * er2
  63. mov.w r1,r2
  64. mulxu.w r0,er2 ; compute er3 = remainder (tentative)
  65. sub.l er2,er3 ; er3 = dividend - (AQ - 1) * divisor
  66. divmod_L25:
  67. cmp.l er1,er3 ; is divisor < remainder?
  68. blo divmod_L26
  69. adds #1,er0
  70. sub.l er1,er3 ; correct the remainder
  71. divmod_L26:
  72. rts
  73. .end