udivdi3.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 __udivdi3(du_int a, du_int b);
  6. // result = 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(__udivdi3)
  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 [1] if the high word is larger than bhi
  33. cmpl %ebx, %edx // to avoid overflowing the upcoming divide.
  34. jae 1f
  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. sbbl $0, %edi // decrement q if remainder is negative
  51. xorl %edx, %edx
  52. movl %edi, %eax
  53. popl %edi
  54. popl %ebx
  55. retl
  56. 1: // High word of a is greater than or equal to (b >> (1 + i)) on this branch
  57. subl %ebx, %edx // subtract bhi from ahi so that divide will not
  58. divl %ebx // overflow, and find q and r such that
  59. //
  60. // ahi:alo = (1:q)*bhi + r
  61. //
  62. // Note that q is a number in (31-i).(1+i)
  63. // fix point.
  64. pushl %edi
  65. notl %ecx
  66. shrl %eax
  67. orl $0x80000000, %eax
  68. shrl %cl, %eax // q = (1:qs) >> (1 + i)
  69. movl %eax, %edi
  70. mull 20(%esp) // q*blo
  71. movl 12(%esp), %ebx
  72. movl 16(%esp), %ecx // ECX:EBX = a
  73. subl %eax, %ebx
  74. sbbl %edx, %ecx // ECX:EBX = a - q*blo
  75. movl 24(%esp), %eax
  76. imull %edi, %eax // q*bhi
  77. subl %eax, %ecx // ECX:EBX = a - q*b
  78. sbbl $0, %edi // decrement q if remainder is negative
  79. xorl %edx, %edx
  80. movl %edi, %eax
  81. popl %edi
  82. popl %ebx
  83. retl
  84. 9: // High word of b is zero on this branch
  85. movl 12(%esp), %eax // Find qhi and rhi such that
  86. movl 16(%esp), %ecx //
  87. xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b
  88. divl %ecx //
  89. movl %eax, %ebx //
  90. movl 8(%esp), %eax // Find qlo such that
  91. divl %ecx //
  92. movl %ebx, %edx // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b
  93. popl %ebx //
  94. retl // and return qhi:qlo
  95. END_COMPILERRT_FUNCTION(__udivdi3)
  96. #endif // __i386__
  97. NO_EXEC_STACK_DIRECTIVE