udivmodsi4.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //===-- udivmodsi4.S - 32-bit unsigned integer divide and modulus ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the __udivmodsi4 (32-bit unsigned integer divide and
  10. // modulus) function for the ARM 32-bit architecture.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "../assembly.h"
  14. .syntax unified
  15. .text
  16. DEFINE_CODE_STATE
  17. @ unsigned int __udivmodsi4(unsigned int divident, unsigned int divisor,
  18. @ unsigned int *remainder)
  19. @ Calculate the quotient and remainder of the (unsigned) division. The return
  20. @ value is the quotient, the remainder is placed in the variable.
  21. .p2align 2
  22. DEFINE_COMPILERRT_FUNCTION(__udivmodsi4)
  23. #if __ARM_ARCH_EXT_IDIV__
  24. tst r1, r1
  25. beq LOCAL_LABEL(divby0)
  26. mov r3, r0
  27. udiv r0, r3, r1
  28. mls r1, r0, r1, r3
  29. str r1, [r2]
  30. bx lr
  31. #else
  32. cmp r1, #1
  33. bcc LOCAL_LABEL(divby0)
  34. beq LOCAL_LABEL(divby1)
  35. cmp r0, r1
  36. bcc LOCAL_LABEL(quotient0)
  37. // Implement division using binary long division algorithm.
  38. //
  39. // r0 is the numerator, r1 the denominator.
  40. //
  41. // The code before JMP computes the correct shift I, so that
  42. // r0 and (r1 << I) have the highest bit set in the same position.
  43. // At the time of JMP, ip := .Ldiv0block - 12 * I.
  44. // This depends on the fixed instruction size of block.
  45. // For ARM mode, this is 12 Bytes, for THUMB mode 14 Bytes.
  46. //
  47. // block(shift) implements the test-and-update-quotient core.
  48. // It assumes (r0 << shift) can be computed without overflow and
  49. // that (r0 << shift) < 2 * r1. The quotient is stored in r3.
  50. # ifdef __ARM_FEATURE_CLZ
  51. clz ip, r0
  52. clz r3, r1
  53. // r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3.
  54. sub r3, r3, ip
  55. # if defined(USE_THUMB_2)
  56. adr ip, LOCAL_LABEL(div0block) + 1
  57. sub ip, ip, r3, lsl #1
  58. # else
  59. adr ip, LOCAL_LABEL(div0block)
  60. # endif
  61. sub ip, ip, r3, lsl #2
  62. sub ip, ip, r3, lsl #3
  63. mov r3, #0
  64. bx ip
  65. # else
  66. # if defined(USE_THUMB_2)
  67. # error THUMB mode requires CLZ or UDIV
  68. # endif
  69. str r4, [sp, #-8]!
  70. mov r4, r0
  71. adr ip, LOCAL_LABEL(div0block)
  72. lsr r3, r4, #16
  73. cmp r3, r1
  74. movhs r4, r3
  75. subhs ip, ip, #(16 * 12)
  76. lsr r3, r4, #8
  77. cmp r3, r1
  78. movhs r4, r3
  79. subhs ip, ip, #(8 * 12)
  80. lsr r3, r4, #4
  81. cmp r3, r1
  82. movhs r4, r3
  83. subhs ip, #(4 * 12)
  84. lsr r3, r4, #2
  85. cmp r3, r1
  86. movhs r4, r3
  87. subhs ip, ip, #(2 * 12)
  88. // Last block, no need to update r3 or r4.
  89. cmp r1, r4, lsr #1
  90. subls ip, ip, #(1 * 12)
  91. ldr r4, [sp], #8 // restore r4, we are done with it.
  92. mov r3, #0
  93. JMP(ip)
  94. # endif
  95. #define IMM #
  96. #define block(shift) \
  97. cmp r0, r1, lsl IMM shift; \
  98. ITT(hs); \
  99. WIDE(addhs) r3, r3, IMM (1 << shift); \
  100. WIDE(subhs) r0, r0, r1, lsl IMM shift
  101. block(31)
  102. block(30)
  103. block(29)
  104. block(28)
  105. block(27)
  106. block(26)
  107. block(25)
  108. block(24)
  109. block(23)
  110. block(22)
  111. block(21)
  112. block(20)
  113. block(19)
  114. block(18)
  115. block(17)
  116. block(16)
  117. block(15)
  118. block(14)
  119. block(13)
  120. block(12)
  121. block(11)
  122. block(10)
  123. block(9)
  124. block(8)
  125. block(7)
  126. block(6)
  127. block(5)
  128. block(4)
  129. block(3)
  130. block(2)
  131. block(1)
  132. LOCAL_LABEL(div0block):
  133. block(0)
  134. str r0, [r2]
  135. mov r0, r3
  136. JMP(lr)
  137. LOCAL_LABEL(quotient0):
  138. str r0, [r2]
  139. mov r0, #0
  140. JMP(lr)
  141. LOCAL_LABEL(divby1):
  142. mov r3, #0
  143. str r3, [r2]
  144. JMP(lr)
  145. #endif // __ARM_ARCH_EXT_IDIV__
  146. LOCAL_LABEL(divby0):
  147. mov r0, #0
  148. #ifdef __ARM_EABI__
  149. b __aeabi_idiv0
  150. #else
  151. JMP(lr)
  152. #endif
  153. END_COMPILERRT_FUNCTION(__udivmodsi4)
  154. NO_EXEC_STACK_DIRECTIVE