umoddi3.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // du_int __umoddi3(du_int a, du_int b);
  6. // result = remainder of a / b.
  7. // both inputs and the output are 64-bit unsigned 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(__umoddi3)
  20. pushl %ebx
  21. movl 20(%esp), %ebx // Find the index i of the leading bit in b.
  22. bsrl %ebx, %ecx // If the high word of b is zero, jump to
  23. jz 9f // the code to handle that special case [9].
  24. // High word of b is known to be non-zero on this branch
  25. movl 16(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b
  26. shrl %cl, %eax // Practically, this means that bhi is given by:
  27. shrl %eax //
  28. notl %ecx // bhi = (high word of b) << (31 - i) |
  29. shll %cl, %ebx // (low word of b) >> (1 + i)
  30. orl %eax, %ebx //
  31. movl 12(%esp), %edx // Load the high and low words of a, and jump
  32. movl 8(%esp), %eax // to [2] if the high word is larger than bhi
  33. cmpl %ebx, %edx // to avoid overflowing the upcoming divide.
  34. jae 2f
  35. // High word of a is greater than or equal to (b >> (1 + i)) on this branch
  36. divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
  37. pushl %edi
  38. notl %ecx
  39. shrl %eax
  40. shrl %cl, %eax // q = qs >> (1 + i)
  41. movl %eax, %edi
  42. mull 20(%esp) // q*blo
  43. movl 12(%esp), %ebx
  44. movl 16(%esp), %ecx // ECX:EBX = a
  45. subl %eax, %ebx
  46. sbbl %edx, %ecx // ECX:EBX = a - q*blo
  47. movl 24(%esp), %eax
  48. imull %edi, %eax // q*bhi
  49. subl %eax, %ecx // ECX:EBX = a - q*b
  50. jnc 1f // if positive, this is the result.
  51. addl 20(%esp), %ebx // otherwise
  52. adcl 24(%esp), %ecx // ECX:EBX = a - (q-1)*b = result
  53. 1: movl %ebx, %eax
  54. movl %ecx, %edx
  55. popl %edi
  56. popl %ebx
  57. retl
  58. 2: // High word of a is greater than or equal to (b >> (1 + i)) on this branch
  59. subl %ebx, %edx // subtract bhi from ahi so that divide will not
  60. divl %ebx // overflow, and find q and r such that
  61. //
  62. // ahi:alo = (1:q)*bhi + r
  63. //
  64. // Note that q is a number in (31-i).(1+i)
  65. // fix point.
  66. pushl %edi
  67. notl %ecx
  68. shrl %eax
  69. orl $0x80000000, %eax
  70. shrl %cl, %eax // q = (1:qs) >> (1 + i)
  71. movl %eax, %edi
  72. mull 20(%esp) // q*blo
  73. movl 12(%esp), %ebx
  74. movl 16(%esp), %ecx // ECX:EBX = a
  75. subl %eax, %ebx
  76. sbbl %edx, %ecx // ECX:EBX = a - q*blo
  77. movl 24(%esp), %eax
  78. imull %edi, %eax // q*bhi
  79. subl %eax, %ecx // ECX:EBX = a - q*b
  80. jnc 3f // if positive, this is the result.
  81. addl 20(%esp), %ebx // otherwise
  82. adcl 24(%esp), %ecx // ECX:EBX = a - (q-1)*b = result
  83. 3: movl %ebx, %eax
  84. movl %ecx, %edx
  85. popl %edi
  86. popl %ebx
  87. retl
  88. 9: // High word of b is zero on this branch
  89. movl 12(%esp), %eax // Find qhi and rhi such that
  90. movl 16(%esp), %ecx //
  91. xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b
  92. divl %ecx //
  93. movl %eax, %ebx //
  94. movl 8(%esp), %eax // Find rlo such that
  95. divl %ecx //
  96. movl %edx, %eax // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b
  97. popl %ebx //
  98. xorl %edx, %edx // and return 0:rlo
  99. retl //
  100. END_COMPILERRT_FUNCTION(__umoddi3)
  101. #endif // __i386__
  102. NO_EXEC_STACK_DIRECTIVE