1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/arch/arm/lib/div64.S
5 * Optimized computation of 64-bit dividend / 32-bit divisor
7 * Author: Nicolas Pitre
9 * Copyright: Monta Vista Software, Inc.
12 #include <linux/linkage.h>
13 #include <asm/assembler.h>
14 #include <asm/unwind.h>
29 * __do_div64: perform a division with 64-bit dividend and 32-bit divisor.
31 * Note: Calling convention is totally non standard for optimal code.
32 * This is meant to be used by do_div() from include/asm/div64.h only.
35 * xh-xl = dividend (clobbered)
36 * r4 = divisor (preserved)
42 * Clobbered regs: xl, ip
48 @ Test for easy paths first.
50 bls 9f @ divisor is 0 or 1
52 beq 8f @ divisor is power of 2
54 @ See if we need to handle upper 32-bit result.
59 @ Align divisor with upper part of dividend.
60 @ The aligned divisor is stored in yl preserving the original.
61 @ The bit position is stored in ip.
63 #if __LINUX_ARM_ARCH__ >= 5
76 1: cmp yl, #0x80000000
84 @ The division loop for needed upper bit positions.
85 @ Break out early if dividend reaches 0.
93 @ See if we need to handle lower 32-bit result.
100 @ The division loop for lower bit positions.
101 @ Here we shift remainer bits leftwards rather than moving the
102 @ divisor for comparisons, considering the carry-out bit as well.
104 4: movs xl, xl, lsl #1
114 @ The top part of remainder became zero. If carry is set
115 @ (the 33th bit) this is a false positive so resume the loop.
116 @ Otherwise, if lower part is also null then we are done.
121 @ We still have remainer bits in the low part. Bring them up.
123 #if __LINUX_ARM_ARCH__ >= 5
125 clz xh, xl @ we know xh is zero here so...
132 7: movs xl, xl, lsl #1
138 @ Current remainder is now 1. It is worthless to compare with
139 @ divisor at this point since divisor can not be smaller than 3 here.
140 @ If possible, branch for another shift in the division loop.
141 @ If no bit position left then we are done.
147 8: @ Division by a power of 2: determine what that divisor order is
148 @ then simply shift values around
150 #if __LINUX_ARM_ARCH__ >= 5
160 movhs yl, yl, lsr #16
173 addls ip, ip, yl, lsr #1
180 ARM( orr yl, yl, xh, lsl ip )
181 THUMB( lsl xh, xh, ip )
182 THUMB( orr yl, yl, xh )
187 @ eq -> division by 1: obvious enough...
202 @ as wrong as it could be...