qrnnd.S 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # S/390 __udiv_qrnnd
  2. # r2 : &__r
  3. # r3 : upper half of 64 bit word n
  4. # r4 : lower half of 64 bit word n
  5. # r5 : divisor d
  6. # the reminder r of the division is to be stored to &__r and
  7. # the quotient q is to be returned
  8. .text
  9. .globl __udiv_qrnnd
  10. __udiv_qrnnd:
  11. st %r2,24(%r15) # store pointer to reminder for later
  12. lr %r0,%r3 # reload n
  13. lr %r1,%r4
  14. ltr %r2,%r5 # reload and test divisor
  15. jp 5f
  16. # divisor >= 0x80000000
  17. srdl %r0,2 # n/4
  18. srl %r2,1 # d/2
  19. slr %r1,%r2 # special case if last bit of d is set
  20. brc 3,0f # (n/4) div (n/2) can overflow by 1
  21. ahi %r0,-1 # trick: subtract n/2, then divide
  22. 0: dr %r0,%r2 # signed division
  23. ahi %r1,1 # trick part 2: add 1 to the quotient
  24. # now (n >> 2) = (d >> 1) * %r1 + %r0
  25. lhi %r3,1
  26. nr %r3,%r1 # test last bit of q
  27. jz 1f
  28. alr %r0,%r2 # add (d>>1) to r
  29. 1: srl %r1,1 # q >>= 1
  30. # now (n >> 2) = (d&-2) * %r1 + %r0
  31. lhi %r3,1
  32. nr %r3,%r5 # test last bit of d
  33. jz 2f
  34. slr %r0,%r1 # r -= q
  35. brc 3,2f # borrow ?
  36. alr %r0,%r5 # r += d
  37. ahi %r1,-1
  38. 2: # now (n >> 2) = d * %r1 + %r0
  39. alr %r1,%r1 # q <<= 1
  40. alr %r0,%r0 # r <<= 1
  41. brc 12,3f # overflow on r ?
  42. slr %r0,%r5 # r -= d
  43. ahi %r1,1 # q += 1
  44. 3: lhi %r3,2
  45. nr %r3,%r4 # test next to last bit of n
  46. jz 4f
  47. ahi %r0,1 # r += 1
  48. 4: clr %r0,%r5 # r >= d ?
  49. jl 6f
  50. slr %r0,%r5 # r -= d
  51. ahi %r1,1 # q += 1
  52. # now (n >> 1) = d * %r1 + %r0
  53. j 6f
  54. 5: # divisor < 0x80000000
  55. srdl %r0,1
  56. dr %r0,%r2 # signed division
  57. # now (n >> 1) = d * %r1 + %r0
  58. 6: alr %r1,%r1 # q <<= 1
  59. alr %r0,%r0 # r <<= 1
  60. brc 12,7f # overflow on r ?
  61. slr %r0,%r5 # r -= d
  62. ahi %r1,1 # q += 1
  63. 7: lhi %r3,1
  64. nr %r3,%r4 # isolate last bit of n
  65. alr %r0,%r3 # r += (n & 1)
  66. clr %r0,%r5 # r >= d ?
  67. jl 8f
  68. slr %r0,%r5 # r -= d
  69. ahi %r1,1 # q += 1
  70. 8: # now n = d * %r1 + %r0
  71. l %r2,24(%r15)
  72. st %r0,0(%r2)
  73. lr %r2,%r1
  74. br %r14
  75. .end __udiv_qrnnd