179 lines · plain
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the __udivmodsi4 (32-bit unsigned integer divide and10// modulus) function for the ARM 32-bit architecture.11//12//===----------------------------------------------------------------------===//13 14#include "../assembly.h"15 16 .syntax unified17 .text18 DEFINE_CODE_STATE19 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 return23@ value is the quotient, the remainder is placed in the variable.24 25 .p2align 226DEFINE_COMPILERRT_FUNCTION(__udivmodsi4)27#if __ARM_ARCH_EXT_IDIV__28 tst r1, r129 beq LOCAL_LABEL(divby0)30 mov r3, r031 udiv r0, r3, r132 mls r1, r0, r1, r333 str r1, [r2]34 bx lr35#else36 cmp r1, #137 bcc LOCAL_LABEL(divby0)38 beq LOCAL_LABEL(divby1)39 cmp r0, r140 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 that47 // 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 and54 // that (r0 << shift) < 2 * r1. The quotient is stored in r3.55 56# ifdef __ARM_FEATURE_CLZ57 clz ip, r058 clz r3, r159 // r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3.60 sub r3, r3, ip61# if defined(USE_THUMB_2)62 adr ip, LOCAL_LABEL(div0block) + 163 sub ip, ip, r3, lsl #164# else65 adr ip, LOCAL_LABEL(div0block)66# endif67 sub ip, ip, r3, lsl #268 sub ip, ip, r3, lsl #369 mov r3, #070 bx ip71# else72# if defined(USE_THUMB_2)73# error THUMB mode requires CLZ or UDIV74# endif75 str r4, [sp, #-8]!76 77 mov r4, r078 adr ip, LOCAL_LABEL(div0block)79 80 lsr r3, r4, #1681 cmp r3, r182 movhs r4, r383 subhs ip, ip, #(16 * 12)84 85 lsr r3, r4, #886 cmp r3, r187 movhs r4, r388 subhs ip, ip, #(8 * 12)89 90 lsr r3, r4, #491 cmp r3, r192 movhs r4, r393 subhs ip, #(4 * 12)94 95 lsr r3, r4, #296 cmp r3, r197 movhs r4, r398 subhs ip, ip, #(2 * 12)99 100 // Last block, no need to update r3 or r4.101 cmp r1, r4, lsr #1102 subls ip, ip, #(1 * 12)103 104 ldr r4, [sp], #8 // restore r4, we are done with it.105 mov r3, #0106 107 JMP(ip)108# endif109 110#define IMM #111 112#define block(shift) \113 cmp r0, r1, lsl IMM shift; \114 ITT(hs); \115 WIDE(addhs) r3, r3, IMM (1 << shift); \116 WIDE(subhs) r0, r0, r1, lsl IMM shift117 118 block(31)119 block(30)120 block(29)121 block(28)122 block(27)123 block(26)124 block(25)125 block(24)126 block(23)127 block(22)128 block(21)129 block(20)130 block(19)131 block(18)132 block(17)133 block(16)134 block(15)135 block(14)136 block(13)137 block(12)138 block(11)139 block(10)140 block(9)141 block(8)142 block(7)143 block(6)144 block(5)145 block(4)146 block(3)147 block(2)148 block(1)149LOCAL_LABEL(div0block):150 block(0)151 152 str r0, [r2]153 mov r0, r3154 JMP(lr)155 156LOCAL_LABEL(quotient0):157 str r0, [r2]158 mov r0, #0159 JMP(lr)160 161LOCAL_LABEL(divby1):162 mov r3, #0163 str r3, [r2]164 JMP(lr)165#endif // __ARM_ARCH_EXT_IDIV__166 167LOCAL_LABEL(divby0):168 mov r0, #0169#ifdef __ARM_EABI__170 b __aeabi_idiv0171#else172 JMP(lr)173#endif174 175END_COMPILERRT_FUNCTION(__udivmodsi4)176 177NO_EXEC_STACK_DIRECTIVE178 179