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