divdi3.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  2. // See https://llvm.org/LICENSE.txt for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "../assembly.h"
  5. // di_int __divdi3(di_int a, di_int b);
  6. // result = a / b.
  7. // both inputs and the output are 64-bit signed integers.
  8. // This will do whatever the underlying hardware is set to do on division by zero.
  9. // No other exceptions are generated, as the divide cannot overflow.
  10. //
  11. // This is targeted at 32-bit x86 *only*, as this can be done directly in hardware
  12. // on x86_64. The performance goal is ~40 cycles per divide, which is faster than
  13. // currently possible via simulation of integer divides on the x87 unit.
  14. //
  15. // Stephen Canon, December 2008
  16. #ifdef __i386__
  17. .text
  18. .balign 4
  19. DEFINE_COMPILERRT_FUNCTION(__divdi3)
  20. // This is currently implemented by wrapping the unsigned divide up in an absolute
  21. // value, then restoring the correct sign at the end of the computation. This could
  22. // certainly be improved upon.
  23. pushl %esi
  24. movl 20(%esp), %edx // high word of b
  25. movl 16(%esp), %eax // low word of b
  26. movl %edx, %ecx
  27. sarl $31, %ecx // (b < 0) ? -1 : 0
  28. xorl %ecx, %eax
  29. xorl %ecx, %edx // EDX:EAX = (b < 0) ? not(b) : b
  30. subl %ecx, %eax
  31. sbbl %ecx, %edx // EDX:EAX = abs(b)
  32. movl %edx, 20(%esp)
  33. movl %eax, 16(%esp) // store abs(b) back to stack
  34. movl %ecx, %esi // set aside sign of b
  35. movl 12(%esp), %edx // high word of b
  36. movl 8(%esp), %eax // low word of b
  37. movl %edx, %ecx
  38. sarl $31, %ecx // (a < 0) ? -1 : 0
  39. xorl %ecx, %eax
  40. xorl %ecx, %edx // EDX:EAX = (a < 0) ? not(a) : a
  41. subl %ecx, %eax
  42. sbbl %ecx, %edx // EDX:EAX = abs(a)
  43. movl %edx, 12(%esp)
  44. movl %eax, 8(%esp) // store abs(a) back to stack
  45. xorl %ecx, %esi // sign of result = (sign of a) ^ (sign of b)
  46. pushl %ebx
  47. movl 24(%esp), %ebx // Find the index i of the leading bit in b.
  48. bsrl %ebx, %ecx // If the high word of b is zero, jump to
  49. jz 9f // the code to handle that special case [9].
  50. // High word of b is known to be non-zero on this branch
  51. movl 20(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b
  52. shrl %cl, %eax // Practically, this means that bhi is given by:
  53. shrl %eax //
  54. notl %ecx // bhi = (high word of b) << (31 - i) |
  55. shll %cl, %ebx // (low word of b) >> (1 + i)
  56. orl %eax, %ebx //
  57. movl 16(%esp), %edx // Load the high and low words of a, and jump
  58. movl 12(%esp), %eax // to [1] if the high word is larger than bhi
  59. cmpl %ebx, %edx // to avoid overflowing the upcoming divide.
  60. jae 1f
  61. // High word of a is greater than or equal to (b >> (1 + i)) on this branch
  62. divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
  63. pushl %edi
  64. notl %ecx
  65. shrl %eax
  66. shrl %cl, %eax // q = qs >> (1 + i)
  67. movl %eax, %edi
  68. mull 24(%esp) // q*blo
  69. movl 16(%esp), %ebx
  70. movl 20(%esp), %ecx // ECX:EBX = a
  71. subl %eax, %ebx
  72. sbbl %edx, %ecx // ECX:EBX = a - q*blo
  73. movl 28(%esp), %eax
  74. imull %edi, %eax // q*bhi
  75. subl %eax, %ecx // ECX:EBX = a - q*b
  76. sbbl $0, %edi // decrement q if remainder is negative
  77. xorl %edx, %edx
  78. movl %edi, %eax
  79. addl %esi, %eax // Restore correct sign to result
  80. adcl %esi, %edx
  81. xorl %esi, %eax
  82. xorl %esi, %edx
  83. popl %edi // Restore callee-save registers
  84. popl %ebx
  85. popl %esi
  86. retl // Return
  87. 1: // High word of a is greater than or equal to (b >> (1 + i)) on this branch
  88. subl %ebx, %edx // subtract bhi from ahi so that divide will not
  89. divl %ebx // overflow, and find q and r such that
  90. //
  91. // ahi:alo = (1:q)*bhi + r
  92. //
  93. // Note that q is a number in (31-i).(1+i)
  94. // fix point.
  95. pushl %edi
  96. notl %ecx
  97. shrl %eax
  98. orl $0x80000000, %eax
  99. shrl %cl, %eax // q = (1:qs) >> (1 + i)
  100. movl %eax, %edi
  101. mull 24(%esp) // q*blo
  102. movl 16(%esp), %ebx
  103. movl 20(%esp), %ecx // ECX:EBX = a
  104. subl %eax, %ebx
  105. sbbl %edx, %ecx // ECX:EBX = a - q*blo
  106. movl 28(%esp), %eax
  107. imull %edi, %eax // q*bhi
  108. subl %eax, %ecx // ECX:EBX = a - q*b
  109. sbbl $0, %edi // decrement q if remainder is negative
  110. xorl %edx, %edx
  111. movl %edi, %eax
  112. addl %esi, %eax // Restore correct sign to result
  113. adcl %esi, %edx
  114. xorl %esi, %eax
  115. xorl %esi, %edx
  116. popl %edi // Restore callee-save registers
  117. popl %ebx
  118. popl %esi
  119. retl // Return
  120. 9: // High word of b is zero on this branch
  121. movl 16(%esp), %eax // Find qhi and rhi such that
  122. movl 20(%esp), %ecx //
  123. xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b
  124. divl %ecx //
  125. movl %eax, %ebx //
  126. movl 12(%esp), %eax // Find qlo such that
  127. divl %ecx //
  128. movl %ebx, %edx // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b
  129. addl %esi, %eax // Restore correct sign to result
  130. adcl %esi, %edx
  131. xorl %esi, %eax
  132. xorl %esi, %edx
  133. popl %ebx // Restore callee-save registers
  134. popl %esi
  135. retl // Return
  136. END_COMPILERRT_FUNCTION(__divdi3)
  137. #endif // __i386__
  138. NO_EXEC_STACK_DIRECTIVE