div.s 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Q = 0
  2. N = 1
  3. D = 2
  4. CC = 3
  5. TMP = 11
  6. TEXT save<>(SB), 1, $0
  7. MOVW R(Q), 0(FP)
  8. MOVW R(N), 4(FP)
  9. MOVW R(D), 8(FP)
  10. MOVW R(CC), 12(FP)
  11. MOVW R(TMP), R(Q) /* numerator */
  12. MOVW 20(FP), R(D) /* denominator */
  13. CMP $0, R(D)
  14. BNE s1
  15. MOVW -1(R(D)), R(TMP) /* divide by zero fault */
  16. s1: RET
  17. TEXT rest<>(SB), 1, $0
  18. MOVW 0(FP), R(Q)
  19. MOVW 4(FP), R(N)
  20. MOVW 8(FP), R(D)
  21. MOVW 12(FP), R(CC)
  22. /*
  23. * return to caller
  24. * of rest<>
  25. */
  26. MOVW 0(R13), R14
  27. ADD $20, R13
  28. B (R14)
  29. TEXT div<>(SB), 1, $0
  30. MOVW $32, R(CC)
  31. /*
  32. * skip zeros 8-at-a-time
  33. */
  34. e1:
  35. AND.S $(0xff<<24),R(Q), R(N)
  36. BNE e2
  37. SLL $8, R(Q)
  38. SUB.S $8, R(CC)
  39. BNE e1
  40. RET
  41. e2:
  42. MOVW $0, R(N)
  43. loop:
  44. /*
  45. * shift R(N||Q) left one
  46. */
  47. SLL $1, R(N)
  48. CMP $0, R(Q)
  49. ORR.LT $1, R(N)
  50. SLL $1, R(Q)
  51. /*
  52. * compare numerator to denominator
  53. * if less, subtract and set quotent bit
  54. */
  55. CMP R(D), R(N)
  56. ORR.HS $1, R(Q)
  57. SUB.HS R(D), R(N)
  58. SUB.S $1, R(CC)
  59. BNE loop
  60. RET
  61. TEXT _div(SB), 1, $16
  62. BL save<>(SB)
  63. CMP $0, R(Q)
  64. BGE d1
  65. RSB $0, R(Q), R(Q)
  66. CMP $0, R(D)
  67. BGE d2
  68. RSB $0, R(D), R(D)
  69. d0:
  70. BL div<>(SB) /* none/both neg */
  71. MOVW R(Q), R(TMP)
  72. B out
  73. d1:
  74. CMP $0, R(D)
  75. BGE d0
  76. RSB $0, R(D), R(D)
  77. d2:
  78. BL div<>(SB) /* one neg */
  79. RSB $0, R(Q), R(TMP)
  80. B out
  81. TEXT _mod(SB), 1, $16
  82. BL save<>(SB)
  83. CMP $0, R(D)
  84. RSB.LT $0, R(D), R(D)
  85. CMP $0, R(Q)
  86. BGE m1
  87. RSB $0, R(Q), R(Q)
  88. BL div<>(SB) /* neg numerator */
  89. RSB $0, R(N), R(TMP)
  90. B out
  91. m1:
  92. BL div<>(SB) /* pos numerator */
  93. MOVW R(N), R(TMP)
  94. B out
  95. TEXT _divu(SB), 1, $16
  96. BL save<>(SB)
  97. BL div<>(SB)
  98. MOVW R(Q), R(TMP)
  99. B out
  100. TEXT _modu(SB), 1, $16
  101. BL save<>(SB)
  102. BL div<>(SB)
  103. MOVW R(N), R(TMP)
  104. B out
  105. out:
  106. BL rest<>(SB)
  107. B out