xref: /openbmc/linux/arch/x86/include/asm/div64.h (revision 65412c8d)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_DIV64_H
31965aae3SH. Peter Anvin #define _ASM_X86_DIV64_H
4bb898558SAl Viro 
5bb898558SAl Viro #ifdef CONFIG_X86_32
6bb898558SAl Viro 
7bb898558SAl Viro #include <linux/types.h>
8668b4484SSebastian Andrzej Siewior #include <linux/log2.h>
9bb898558SAl Viro 
10bb898558SAl Viro /*
11bb898558SAl Viro  * do_div() is NOT a C function. It wants to return
12bb898558SAl Viro  * two values (the quotient and the remainder), but
13bb898558SAl Viro  * since that doesn't work very well in C, what it
14bb898558SAl Viro  * does is:
15bb898558SAl Viro  *
16bb898558SAl Viro  * - modifies the 64-bit dividend _in_place_
17bb898558SAl Viro  * - returns the 32-bit remainder
18bb898558SAl Viro  *
19bb898558SAl Viro  * This ends up being the most efficient "calling
20bb898558SAl Viro  * convention" on x86.
21bb898558SAl Viro  */
22bb898558SAl Viro #define do_div(n, base)						\
23bb898558SAl Viro ({								\
24bb898558SAl Viro 	unsigned long __upper, __low, __high, __mod, __base;	\
25bb898558SAl Viro 	__base = (base);					\
26668b4484SSebastian Andrzej Siewior 	if (__builtin_constant_p(__base) && is_power_of_2(__base)) { \
27668b4484SSebastian Andrzej Siewior 		__mod = n & (__base - 1);			\
28668b4484SSebastian Andrzej Siewior 		n >>= ilog2(__base);				\
29668b4484SSebastian Andrzej Siewior 	} else {						\
30bb898558SAl Viro 		asm("" : "=a" (__low), "=d" (__high) : "A" (n));\
31bb898558SAl Viro 		__upper = __high;				\
32bb898558SAl Viro 		if (__high) {					\
33bb898558SAl Viro 			__upper = __high % (__base);		\
34bb898558SAl Viro 			__high = __high / (__base);		\
35bb898558SAl Viro 		}						\
36bb898558SAl Viro 		asm("divl %2" : "=a" (__low), "=d" (__mod)	\
37bb898558SAl Viro 			: "rm" (__base), "0" (__low), "1" (__upper));	\
38bb898558SAl Viro 		asm("" : "=A" (n) : "a" (__low), "d" (__high));	\
39668b4484SSebastian Andrzej Siewior 	}							\
40bb898558SAl Viro 	__mod;							\
41bb898558SAl Viro })
42bb898558SAl Viro 
div_u64_rem(u64 dividend,u32 divisor,u32 * remainder)43bb898558SAl Viro static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
44bb898558SAl Viro {
45bb898558SAl Viro 	union {
46bb898558SAl Viro 		u64 v64;
47bb898558SAl Viro 		u32 v32[2];
48bb898558SAl Viro 	} d = { dividend };
49bb898558SAl Viro 	u32 upper;
50bb898558SAl Viro 
51bb898558SAl Viro 	upper = d.v32[1];
52bb898558SAl Viro 	d.v32[1] = 0;
53bb898558SAl Viro 	if (upper >= divisor) {
54bb898558SAl Viro 		d.v32[1] = upper / divisor;
55bb898558SAl Viro 		upper %= divisor;
56bb898558SAl Viro 	}
57bb898558SAl Viro 	asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
58bb898558SAl Viro 		"rm" (divisor), "0" (d.v32[0]), "1" (upper));
59bb898558SAl Viro 	return d.v64;
60bb898558SAl Viro }
61bb898558SAl Viro #define div_u64_rem	div_u64_rem
62bb898558SAl Viro 
mul_u32_u32(u32 a,u32 b)639e3d6223SPeter Zijlstra static inline u64 mul_u32_u32(u32 a, u32 b)
649e3d6223SPeter Zijlstra {
659e3d6223SPeter Zijlstra 	u32 high, low;
669e3d6223SPeter Zijlstra 
679e3d6223SPeter Zijlstra 	asm ("mull %[b]" : "=a" (low), "=d" (high)
689e3d6223SPeter Zijlstra 			 : [a] "a" (a), [b] "rm" (b) );
699e3d6223SPeter Zijlstra 
709e3d6223SPeter Zijlstra 	return low | ((u64)high) << 32;
719e3d6223SPeter Zijlstra }
729e3d6223SPeter Zijlstra #define mul_u32_u32 mul_u32_u32
739e3d6223SPeter Zijlstra 
74*65412c8dSArnd Bergmann /*
75*65412c8dSArnd Bergmann  * __div64_32() is never called on x86, so prevent the
76*65412c8dSArnd Bergmann  * generic definition from getting built.
77*65412c8dSArnd Bergmann  */
78*65412c8dSArnd Bergmann #define __div64_32
79*65412c8dSArnd Bergmann 
80bb898558SAl Viro #else
81bb898558SAl Viro # include <asm-generic/div64.h>
82db4e919dSPeter Zijlstra 
833dc167baSOleg Nesterov /*
843dc167baSOleg Nesterov  * Will generate an #DE when the result doesn't fit u64, could fix with an
853dc167baSOleg Nesterov  * __ex_table[] entry when it becomes an issue.
863dc167baSOleg Nesterov  */
mul_u64_u64_div_u64(u64 a,u64 mul,u64 div)873dc167baSOleg Nesterov static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
88db4e919dSPeter Zijlstra {
89db4e919dSPeter Zijlstra 	u64 q;
90db4e919dSPeter Zijlstra 
91db4e919dSPeter Zijlstra 	asm ("mulq %2; divq %3" : "=a" (q)
923dc167baSOleg Nesterov 				: "a" (a), "rm" (mul), "rm" (div)
93db4e919dSPeter Zijlstra 				: "rdx");
94db4e919dSPeter Zijlstra 
95db4e919dSPeter Zijlstra 	return q;
96db4e919dSPeter Zijlstra }
973dc167baSOleg Nesterov #define mul_u64_u64_div_u64 mul_u64_u64_div_u64
983dc167baSOleg Nesterov 
mul_u64_u32_div(u64 a,u32 mul,u32 div)993dc167baSOleg Nesterov static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 div)
1003dc167baSOleg Nesterov {
1013dc167baSOleg Nesterov 	return mul_u64_u64_div_u64(a, mul, div);
1023dc167baSOleg Nesterov }
103db4e919dSPeter Zijlstra #define mul_u64_u32_div	mul_u64_u32_div
104db4e919dSPeter Zijlstra 
105bb898558SAl Viro #endif /* CONFIG_X86_32 */
106bb898558SAl Viro 
1071965aae3SH. Peter Anvin #endif /* _ASM_X86_DIV64_H */
108