div64.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Divide a 64-bit unsigned number by a 32-bit unsigned number.
  3. * This routine assumes that the top 32 bits of the dividend are
  4. * non-zero to start with.
  5. * On entry, r3 points to the dividend, which get overwritten with
  6. * the 64-bit quotient, and r4 contains the divisor.
  7. * On exit, r3 contains the remainder.
  8. *
  9. * Copyright (C) 2002 Paul Mackerras, IBM Corp.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include "ppc_asm.h"
  17. .globl __div64_32
  18. __div64_32:
  19. lwz r5,0(r3) # get the dividend into r5/r6
  20. lwz r6,4(r3)
  21. cmplw r5,r4
  22. li r7,0
  23. li r8,0
  24. blt 1f
  25. divwu r7,r5,r4 # if dividend.hi >= divisor,
  26. mullw r0,r7,r4 # quotient.hi = dividend.hi / divisor
  27. subf. r5,r0,r5 # dividend.hi %= divisor
  28. beq 3f
  29. 1: mr r11,r5 # here dividend.hi != 0
  30. andis. r0,r5,0xc000
  31. bne 2f
  32. cntlzw r0,r5 # we are shifting the dividend right
  33. li r10,-1 # to make it < 2^32, and shifting
  34. srw r10,r10,r0 # the divisor right the same amount,
  35. addc r9,r4,r10 # rounding up (so the estimate cannot
  36. andc r11,r6,r10 # ever be too large, only too small)
  37. andc r9,r9,r10
  38. addze r9,r9
  39. or r11,r5,r11
  40. rotlw r9,r9,r0
  41. rotlw r11,r11,r0
  42. divwu r11,r11,r9 # then we divide the shifted quantities
  43. 2: mullw r10,r11,r4 # to get an estimate of the quotient,
  44. mulhwu r9,r11,r4 # multiply the estimate by the divisor,
  45. subfc r6,r10,r6 # take the product from the divisor,
  46. add r8,r8,r11 # and add the estimate to the accumulated
  47. subfe. r5,r9,r5 # quotient
  48. bne 1b
  49. 3: cmplw r6,r4
  50. blt 4f
  51. divwu r0,r6,r4 # perform the remaining 32-bit division
  52. mullw r10,r0,r4 # and get the remainder
  53. add r8,r8,r0
  54. subf r6,r10,r6
  55. 4: stw r7,0(r3) # return the quotient in *r3
  56. stw r8,4(r3)
  57. mr r3,r6 # return the remainder in r3
  58. blr
  59. /*
  60. * Extended precision shifts.
  61. *
  62. * Updated to be valid for shift counts from 0 to 63 inclusive.
  63. * -- Gabriel
  64. *
  65. * R3/R4 has 64 bit value
  66. * R5 has shift count
  67. * result in R3/R4
  68. *
  69. * ashrdi3: arithmetic right shift (sign propagation)
  70. * lshrdi3: logical right shift
  71. * ashldi3: left shift
  72. */
  73. .globl __ashrdi3
  74. __ashrdi3:
  75. subfic r6,r5,32
  76. srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
  77. addi r7,r5,32 # could be xori, or addi with -32
  78. slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
  79. rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
  80. sraw r7,r3,r7 # t2 = MSW >> (count-32)
  81. or r4,r4,r6 # LSW |= t1
  82. slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
  83. sraw r3,r3,r5 # MSW = MSW >> count
  84. or r4,r4,r7 # LSW |= t2
  85. blr
  86. .globl __ashldi3
  87. __ashldi3:
  88. subfic r6,r5,32
  89. slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
  90. addi r7,r5,32 # could be xori, or addi with -32
  91. srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
  92. slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
  93. or r3,r3,r6 # MSW |= t1
  94. slw r4,r4,r5 # LSW = LSW << count
  95. or r3,r3,r7 # MSW |= t2
  96. blr
  97. .globl __lshrdi3
  98. __lshrdi3:
  99. subfic r6,r5,32
  100. srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
  101. addi r7,r5,32 # could be xori, or addi with -32
  102. slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
  103. srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
  104. or r4,r4,r6 # LSW |= t1
  105. srw r3,r3,r5 # MSW = MSW >> count
  106. or r4,r4,r7 # LSW |= t2
  107. blr