1 /* SPDX-License-Identifier: GPL-2.0 */
3 * arch/alpha/lib/divide.S
5 * (C) 1995 Linus Torvalds
11 * The alpha chip doesn't provide hardware division, so we have to do it
12 * by hand. The compiler expects the functions
14 * __divqu: 64-bit unsigned long divide
15 * __remqu: 64-bit unsigned long remainder
16 * __divqs/__remqs: signed 64-bit
17 * __divlu/__remlu: unsigned 32-bit
18 * __divls/__remls: signed 32-bit
20 * These are not normal C functions: instead of the normal
21 * calling sequence, these expect their arguments in registers
22 * $24 and $25, and return the result in $27. Register $28 may
23 * be clobbered (assembly temporary), anything else must be saved.
27 * This is a rather simple bit-at-a-time algorithm: it's very good
28 * at dividing random 64-bit numbers, but the more usual case where
29 * the divisor is small is handled better by the DEC algorithm
30 * using lookup tables. This uses much less memory, though, and is
31 * nicer on the cache.. Besides, I don't know the copyright status
38 * $1 - shifted divisor
39 * $2 - modulus/quotient
41 * $23 - return address
45 * $27 - quotient/modulus
46 * $28 - compare status
49 #include <asm/export.h>
53 * Select function type and registers
62 #define DIV_ONLY(x,y...) x,##y
63 #define MOD_ONLY(x,y...)
64 #define func(x) __div##x
67 #define GETSIGN(x) xor $24,$25,x
70 #define DIV_ONLY(x,y...)
71 #define MOD_ONLY(x,y...) x,##y
72 #define func(x) __rem##x
75 #define GETSIGN(x) bis $24,$24,x
80 * For 32-bit operations, we need to extend to 64-bit
83 #define ufunction func(lu)
84 #define sfunction func(l)
85 #define LONGIFY(x) zapnot x,15,x
86 #define SLONGIFY(x) addl x,0,x
88 #define ufunction func(qu)
89 #define sfunction func(q)
113 DIV_ONLY(stq tmp2,32($30))
114 beq divisor, 9f /* div by zero */
118 * shift divisor left, using 3-bit shifts for
119 * 32-bit divides as we can't overflow. Three-bit
120 * shifts will result in looping three times less
121 * here, but can result in two loops more later.
122 * Thus using a large shift isn't worth it (and
123 * s8add pairs better than a sll..)
125 1: cmpult divisor,modulus,compare
126 s8addq divisor,$31,divisor
130 1: cmpult divisor,modulus,compare
132 addq divisor,divisor,divisor
138 /* ok, start to go right again.. */
139 2: DIV_ONLY(addq quotient,mask,tmp2)
141 cmpule divisor,modulus,compare
142 subq modulus,divisor,tmp1
143 DIV_ONLY(cmovne compare,tmp2,quotient)
144 srl divisor,1,divisor
145 cmovne compare,tmp1,modulus
152 DIV_ONLY(ldq tmp2,32($30))
156 EXPORT_SYMBOL(ufunction)
159 * Uhh.. Ugly signed division. I'd rather not have it at all, but
160 * it's needed in some circumstances. There are different ways to
161 * handle this, really. This does:
162 * -a / b = a / -b = -(a / b)
165 * which is probably not the best solution, but at least should
166 * have the property that (x/y)*y + (x%y) = x.
181 cmovlt $24,$28,$24 /* abs($24) */
185 cmovlt $25,$28,$25 /* abs($25) */
199 EXPORT_SYMBOL(sfunction)