umodsi3.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===-- umodsi3.S - 32-bit unsigned integer 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 __umodsi3 (32-bit unsigned integer modulus)
  10. // 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 __umodsi3(unsigned int divident, unsigned int divisor)
  18. @ Calculate and return the remainder of the (unsigned) division.
  19. .p2align 2
  20. DEFINE_COMPILERRT_FUNCTION(__umodsi3)
  21. #if __ARM_ARCH_EXT_IDIV__
  22. tst r1, r1
  23. beq LOCAL_LABEL(divby0)
  24. udiv r2, r0, r1
  25. mls r0, r2, r1, r0
  26. bx lr
  27. #else
  28. cmp r1, #1
  29. bcc LOCAL_LABEL(divby0)
  30. ITT(eq)
  31. moveq r0, #0
  32. JMPc(lr, eq)
  33. cmp r0, r1
  34. IT(cc)
  35. JMPc(lr, cc)
  36. // Implement division using binary long division algorithm.
  37. //
  38. // r0 is the numerator, r1 the denominator.
  39. //
  40. // The code before JMP computes the correct shift I, so that
  41. // r0 and (r1 << I) have the highest bit set in the same position.
  42. // At the time of JMP, ip := .Ldiv0block - 8 * I.
  43. // This depends on the fixed instruction size of block.
  44. // For ARM mode, this is 8 Bytes, for THUMB mode 10 Bytes.
  45. //
  46. // block(shift) implements the test-and-update-quotient core.
  47. // It assumes (r0 << shift) can be computed without overflow and
  48. // that (r0 << shift) < 2 * r1. The quotient is stored in r3.
  49. # ifdef __ARM_FEATURE_CLZ
  50. clz ip, r0
  51. clz r3, r1
  52. // r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3.
  53. sub r3, r3, ip
  54. # if defined(USE_THUMB_2)
  55. adr ip, LOCAL_LABEL(div0block) + 1
  56. sub ip, ip, r3, lsl #1
  57. # else
  58. adr ip, LOCAL_LABEL(div0block)
  59. # endif
  60. sub ip, ip, r3, lsl #3
  61. bx ip
  62. # else
  63. # if defined(USE_THUMB_2)
  64. # error THUMB mode requires CLZ or UDIV
  65. # endif
  66. mov r2, r0
  67. adr ip, LOCAL_LABEL(div0block)
  68. lsr r3, r2, #16
  69. cmp r3, r1
  70. movhs r2, r3
  71. subhs ip, ip, #(16 * 8)
  72. lsr r3, r2, #8
  73. cmp r3, r1
  74. movhs r2, r3
  75. subhs ip, ip, #(8 * 8)
  76. lsr r3, r2, #4
  77. cmp r3, r1
  78. movhs r2, r3
  79. subhs ip, #(4 * 8)
  80. lsr r3, r2, #2
  81. cmp r3, r1
  82. movhs r2, r3
  83. subhs ip, ip, #(2 * 8)
  84. // Last block, no need to update r2 or r3.
  85. cmp r1, r2, lsr #1
  86. subls ip, ip, #(1 * 8)
  87. JMP(ip)
  88. # endif
  89. #define IMM #
  90. #define block(shift) \
  91. cmp r0, r1, lsl IMM shift; \
  92. IT(hs); \
  93. WIDE(subhs) r0, r0, r1, lsl IMM shift
  94. block(31)
  95. block(30)
  96. block(29)
  97. block(28)
  98. block(27)
  99. block(26)
  100. block(25)
  101. block(24)
  102. block(23)
  103. block(22)
  104. block(21)
  105. block(20)
  106. block(19)
  107. block(18)
  108. block(17)
  109. block(16)
  110. block(15)
  111. block(14)
  112. block(13)
  113. block(12)
  114. block(11)
  115. block(10)
  116. block(9)
  117. block(8)
  118. block(7)
  119. block(6)
  120. block(5)
  121. block(4)
  122. block(3)
  123. block(2)
  124. block(1)
  125. LOCAL_LABEL(div0block):
  126. block(0)
  127. JMP(lr)
  128. #endif // __ARM_ARCH_EXT_IDIV__
  129. LOCAL_LABEL(divby0):
  130. mov r0, #0
  131. #ifdef __ARM_EABI__
  132. b __aeabi_idiv0
  133. #else
  134. JMP(lr)
  135. #endif
  136. END_COMPILERRT_FUNCTION(__umodsi3)
  137. NO_EXEC_STACK_DIRECTIVE