udivsi3.S 1.5 KB

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