2 * linux/arch/arm/lib/div64.S
4 * Optimized computation of 64-bit dividend / 32-bit divisor
6 * Author: Nicolas Pitre
8 * Copyright: Monta Vista Software, Inc.
10 * SPDX-License-Identifier: GPL-2.0
13 #include <linux/linkage.h>
14 #include <asm/assembler.h>
32 * __do_div64: perform a division with 64-bit dividend and 32-bit divisor.
34 * Note: Calling convention is totally non standard for optimal code.
35 * This is meant to be used by do_div() from include/asm/div64.h only.
38 * xh-xl = dividend (clobbered)
39 * r4 = divisor (preserved)
45 * Clobbered regs: xl, ip
48 .pushsection .text.__do_div64, "ax"
52 @ Test for easy paths first.
54 bls 9f @ divisor is 0 or 1
56 beq 8f @ divisor is power of 2
58 @ See if we need to handle upper 32-bit result.
63 @ Align divisor with upper part of dividend.
64 @ The aligned divisor is stored in yl preserving the original.
65 @ The bit position is stored in ip.
67 #if __LINUX_ARM_ARCH__ >= 5
80 1: cmp yl, #0x80000000
88 @ The division loop for needed upper bit positions.
89 @ Break out early if dividend reaches 0.
97 @ See if we need to handle lower 32-bit result.
104 @ The division loop for lower bit positions.
105 @ Here we shift remainer bits leftwards rather than moving the
106 @ divisor for comparisons, considering the carry-out bit as well.
108 4: movs xl, xl, lsl #1
118 @ The top part of remainder became zero. If carry is set
119 @ (the 33th bit) this is a false positive so resume the loop.
120 @ Otherwise, if lower part is also null then we are done.
125 @ We still have remainer bits in the low part. Bring them up.
127 #if __LINUX_ARM_ARCH__ >= 5
129 clz xh, xl @ we know xh is zero here so...
136 7: movs xl, xl, lsl #1
142 @ Current remainder is now 1. It is worthless to compare with
143 @ divisor at this point since divisor can not be smaller than 3 here.
144 @ If possible, branch for another shift in the division loop.
145 @ If no bit position left then we are done.
151 8: @ Division by a power of 2: determine what that divisor order is
152 @ then simply shift values around
154 #if __LINUX_ARM_ARCH__ >= 5
164 movhs yl, yl, lsr #16
177 addls ip, ip, yl, lsr #1
184 ARM( orr yl, yl, xh, lsl ip )
185 THUMB( lsl xh, xh, ip )
186 THUMB( orr yl, yl, xh )
191 @ eq -> division by 1: obvious enough...
206 @ as wrong as it could be...