moddi3.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 __moddi3(di_int a, di_int b);
  6. // result = remainder of 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(__moddi3)
  20. // This is currently implemented by wrapping the unsigned modulus up in an absolute
  21. // value. This could certainly be improved upon.
  22. pushl %esi
  23. movl 20(%esp), %edx // high word of b
  24. movl 16(%esp), %eax // low word of b
  25. movl %edx, %ecx
  26. sarl $31, %ecx // (b < 0) ? -1 : 0
  27. xorl %ecx, %eax
  28. xorl %ecx, %edx // EDX:EAX = (b < 0) ? not(b) : b
  29. subl %ecx, %eax
  30. sbbl %ecx, %edx // EDX:EAX = abs(b)
  31. movl %edx, 20(%esp)
  32. movl %eax, 16(%esp) // store abs(b) back to stack
  33. movl 12(%esp), %edx // high word of b
  34. movl 8(%esp), %eax // low word of b
  35. movl %edx, %ecx
  36. sarl $31, %ecx // (a < 0) ? -1 : 0
  37. xorl %ecx, %eax
  38. xorl %ecx, %edx // EDX:EAX = (a < 0) ? not(a) : a
  39. subl %ecx, %eax
  40. sbbl %ecx, %edx // EDX:EAX = abs(a)
  41. movl %edx, 12(%esp)
  42. movl %eax, 8(%esp) // store abs(a) back to stack
  43. movl %ecx, %esi // set aside sign of a
  44. pushl %ebx
  45. movl 24(%esp), %ebx // Find the index i of the leading bit in b.
  46. bsrl %ebx, %ecx // If the high word of b is zero, jump to
  47. jz 9f // the code to handle that special case [9].
  48. // High word of b is known to be non-zero on this branch
  49. movl 20(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b
  50. shrl %cl, %eax // Practically, this means that bhi is given by:
  51. shrl %eax //
  52. notl %ecx // bhi = (high word of b) << (31 - i) |
  53. shll %cl, %ebx // (low word of b) >> (1 + i)
  54. orl %eax, %ebx //
  55. movl 16(%esp), %edx // Load the high and low words of a, and jump
  56. movl 12(%esp), %eax // to [2] if the high word is larger than bhi
  57. cmpl %ebx, %edx // to avoid overflowing the upcoming divide.
  58. jae 2f
  59. // High word of a is greater than or equal to (b >> (1 + i)) on this branch
  60. divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
  61. pushl %edi
  62. notl %ecx
  63. shrl %eax
  64. shrl %cl, %eax // q = qs >> (1 + i)
  65. movl %eax, %edi
  66. mull 24(%esp) // q*blo
  67. movl 16(%esp), %ebx
  68. movl 20(%esp), %ecx // ECX:EBX = a
  69. subl %eax, %ebx
  70. sbbl %edx, %ecx // ECX:EBX = a - q*blo
  71. movl 28(%esp), %eax
  72. imull %edi, %eax // q*bhi
  73. subl %eax, %ecx // ECX:EBX = a - q*b
  74. jnc 1f // if positive, this is the result.
  75. addl 24(%esp), %ebx // otherwise
  76. adcl 28(%esp), %ecx // ECX:EBX = a - (q-1)*b = result
  77. 1: movl %ebx, %eax
  78. movl %ecx, %edx
  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. 2: // 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. jnc 3f // if positive, this is the result.
  110. addl 24(%esp), %ebx // otherwise
  111. adcl 28(%esp), %ecx // ECX:EBX = a - (q-1)*b = result
  112. 3: movl %ebx, %eax
  113. movl %ecx, %edx
  114. addl %esi, %eax // Restore correct sign to result
  115. adcl %esi, %edx
  116. xorl %esi, %eax
  117. xorl %esi, %edx
  118. popl %edi // Restore callee-save registers
  119. popl %ebx
  120. popl %esi
  121. retl // Return
  122. 9: // High word of b is zero on this branch
  123. movl 16(%esp), %eax // Find qhi and rhi such that
  124. movl 20(%esp), %ecx //
  125. xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b
  126. divl %ecx //
  127. movl %eax, %ebx //
  128. movl 12(%esp), %eax // Find rlo such that
  129. divl %ecx //
  130. movl %edx, %eax // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b
  131. popl %ebx //
  132. xorl %edx, %edx // and return 0:rlo
  133. addl %esi, %eax // Restore correct sign to result
  134. adcl %esi, %edx
  135. xorl %esi, %eax
  136. xorl %esi, %edx
  137. popl %esi
  138. retl // Return
  139. END_COMPILERRT_FUNCTION(__moddi3)
  140. #endif // __i386__
  141. NO_EXEC_STACK_DIRECTIVE