xref: /openbmc/linux/include/asm-generic/div64.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _ASM_GENERIC_DIV64_H
31da177e4SLinus Torvalds #define _ASM_GENERIC_DIV64_H
41da177e4SLinus Torvalds /*
51da177e4SLinus Torvalds  * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
61da177e4SLinus Torvalds  * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
71da177e4SLinus Torvalds  *
8461a5e51SNicolas Pitre  * Optimization for constant divisors on 32-bit machines:
9461a5e51SNicolas Pitre  * Copyright (C) 2006-2015 Nicolas Pitre
10461a5e51SNicolas Pitre  *
111da177e4SLinus Torvalds  * The semantics of do_div() are:
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * uint32_t do_div(uint64_t *n, uint32_t base)
141da177e4SLinus Torvalds  * {
151da177e4SLinus Torvalds  * 	uint32_t remainder = *n % base;
161da177e4SLinus Torvalds  * 	*n = *n / base;
171da177e4SLinus Torvalds  * 	return remainder;
181da177e4SLinus Torvalds  * }
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * NOTE: macro parameter n is evaluated multiple times,
211da177e4SLinus Torvalds  *       beware of side effects!
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/types.h>
251da177e4SLinus Torvalds #include <linux/compiler.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #if BITS_PER_LONG == 64
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds # define do_div(n,base) ({					\
301da177e4SLinus Torvalds 	uint32_t __base = (base);				\
311da177e4SLinus Torvalds 	uint32_t __rem;						\
321da177e4SLinus Torvalds 	__rem = ((uint64_t)(n)) % __base;			\
331da177e4SLinus Torvalds 	(n) = ((uint64_t)(n)) / __base;				\
341da177e4SLinus Torvalds 	__rem;							\
351da177e4SLinus Torvalds  })
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #elif BITS_PER_LONG == 32
381da177e4SLinus Torvalds 
39911918aaSNicolas Pitre #include <linux/log2.h>
40911918aaSNicolas Pitre 
41461a5e51SNicolas Pitre /*
42461a5e51SNicolas Pitre  * If the divisor happens to be constant, we determine the appropriate
43461a5e51SNicolas Pitre  * inverse at compile time to turn the division into a few inline
44461a5e51SNicolas Pitre  * multiplications which ought to be much faster. And yet only if compiling
45461a5e51SNicolas Pitre  * with a sufficiently recent gcc version to perform proper 64-bit constant
46461a5e51SNicolas Pitre  * propagation.
47461a5e51SNicolas Pitre  *
48461a5e51SNicolas Pitre  * (It is unfortunate that gcc doesn't perform all this internally.)
49461a5e51SNicolas Pitre  */
50461a5e51SNicolas Pitre 
51461a5e51SNicolas Pitre #ifndef __div64_const32_is_OK
52461a5e51SNicolas Pitre #define __div64_const32_is_OK (__GNUC__ >= 4)
53461a5e51SNicolas Pitre #endif
54461a5e51SNicolas Pitre 
55461a5e51SNicolas Pitre #define __div64_const32(n, ___b)					\
56461a5e51SNicolas Pitre ({									\
57461a5e51SNicolas Pitre 	/*								\
58461a5e51SNicolas Pitre 	 * Multiplication by reciprocal of b: n / b = n * (p / b) / p	\
59461a5e51SNicolas Pitre 	 *								\
60461a5e51SNicolas Pitre 	 * We rely on the fact that most of this code gets optimized	\
61461a5e51SNicolas Pitre 	 * away at compile time due to constant propagation and only	\
62461a5e51SNicolas Pitre 	 * a few multiplication instructions should remain.		\
63461a5e51SNicolas Pitre 	 * Hence this monstrous macro (static inline doesn't always	\
64461a5e51SNicolas Pitre 	 * do the trick here).						\
65461a5e51SNicolas Pitre 	 */								\
66461a5e51SNicolas Pitre 	uint64_t ___res, ___x, ___t, ___m, ___n = (n);			\
67f682b27cSNicolas Pitre 	uint32_t ___p, ___bias;						\
68461a5e51SNicolas Pitre 									\
69461a5e51SNicolas Pitre 	/* determine MSB of b */					\
70461a5e51SNicolas Pitre 	___p = 1 << ilog2(___b);					\
71461a5e51SNicolas Pitre 									\
72461a5e51SNicolas Pitre 	/* compute m = ((p << 64) + b - 1) / b */			\
73461a5e51SNicolas Pitre 	___m = (~0ULL / ___b) * ___p;					\
74461a5e51SNicolas Pitre 	___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b;	\
75461a5e51SNicolas Pitre 									\
76461a5e51SNicolas Pitre 	/* one less than the dividend with highest result */		\
77461a5e51SNicolas Pitre 	___x = ~0ULL / ___b * ___b - 1;					\
78461a5e51SNicolas Pitre 									\
79461a5e51SNicolas Pitre 	/* test our ___m with res = m * x / (p << 64) */		\
80461a5e51SNicolas Pitre 	___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32;	\
81461a5e51SNicolas Pitre 	___t = ___res += (___m & 0xffffffff) * (___x >> 32);		\
82461a5e51SNicolas Pitre 	___res += (___x & 0xffffffff) * (___m >> 32);			\
83461a5e51SNicolas Pitre 	___t = (___res < ___t) ? (1ULL << 32) : 0;			\
84461a5e51SNicolas Pitre 	___res = (___res >> 32) + ___t;					\
85461a5e51SNicolas Pitre 	___res += (___m >> 32) * (___x >> 32);				\
86461a5e51SNicolas Pitre 	___res /= ___p;							\
87461a5e51SNicolas Pitre 									\
88461a5e51SNicolas Pitre 	/* Now sanitize and optimize what we've got. */			\
89461a5e51SNicolas Pitre 	if (~0ULL % (___b / (___b & -___b)) == 0) {			\
90461a5e51SNicolas Pitre 		/* special case, can be simplified to ... */		\
91461a5e51SNicolas Pitre 		___n /= (___b & -___b);					\
92461a5e51SNicolas Pitre 		___m = ~0ULL / (___b / (___b & -___b));			\
93461a5e51SNicolas Pitre 		___p = 1;						\
94461a5e51SNicolas Pitre 		___bias = 1;						\
95461a5e51SNicolas Pitre 	} else if (___res != ___x / ___b) {				\
96461a5e51SNicolas Pitre 		/*							\
97461a5e51SNicolas Pitre 		 * We can't get away without a bias to compensate	\
98461a5e51SNicolas Pitre 		 * for bit truncation errors.  To avoid it we'd need an	\
99461a5e51SNicolas Pitre 		 * additional bit to represent m which would overflow	\
100461a5e51SNicolas Pitre 		 * a 64-bit variable.					\
101461a5e51SNicolas Pitre 		 *							\
102461a5e51SNicolas Pitre 		 * Instead we do m = p / b and n / b = (n * m + m) / p.	\
103461a5e51SNicolas Pitre 		 */							\
104461a5e51SNicolas Pitre 		___bias = 1;						\
105461a5e51SNicolas Pitre 		/* Compute m = (p << 64) / b */				\
106461a5e51SNicolas Pitre 		___m = (~0ULL / ___b) * ___p;				\
107461a5e51SNicolas Pitre 		___m += ((~0ULL % ___b + 1) * ___p) / ___b;		\
108461a5e51SNicolas Pitre 	} else {							\
109461a5e51SNicolas Pitre 		/*							\
110461a5e51SNicolas Pitre 		 * Reduce m / p, and try to clear bit 31 of m when	\
111461a5e51SNicolas Pitre 		 * possible, otherwise that'll need extra overflow	\
112461a5e51SNicolas Pitre 		 * handling later.					\
113461a5e51SNicolas Pitre 		 */							\
114461a5e51SNicolas Pitre 		uint32_t ___bits = -(___m & -___m);			\
115461a5e51SNicolas Pitre 		___bits |= ___m >> 32;					\
116461a5e51SNicolas Pitre 		___bits = (~___bits) << 1;				\
117461a5e51SNicolas Pitre 		/*							\
118461a5e51SNicolas Pitre 		 * If ___bits == 0 then setting bit 31 is  unavoidable.	\
119461a5e51SNicolas Pitre 		 * Simply apply the maximum possible reduction in that	\
120461a5e51SNicolas Pitre 		 * case. Otherwise the MSB of ___bits indicates the	\
121461a5e51SNicolas Pitre 		 * best reduction we should apply.			\
122461a5e51SNicolas Pitre 		 */							\
123461a5e51SNicolas Pitre 		if (!___bits) {						\
124461a5e51SNicolas Pitre 			___p /= (___m & -___m);				\
125461a5e51SNicolas Pitre 			___m /= (___m & -___m);				\
126461a5e51SNicolas Pitre 		} else {						\
127461a5e51SNicolas Pitre 			___p >>= ilog2(___bits);			\
128461a5e51SNicolas Pitre 			___m >>= ilog2(___bits);			\
129461a5e51SNicolas Pitre 		}							\
130461a5e51SNicolas Pitre 		/* No bias needed. */					\
131461a5e51SNicolas Pitre 		___bias = 0;						\
132461a5e51SNicolas Pitre 	}								\
133461a5e51SNicolas Pitre 									\
134461a5e51SNicolas Pitre 	/*								\
135461a5e51SNicolas Pitre 	 * Now we have a combination of 2 conditions:			\
136461a5e51SNicolas Pitre 	 *								\
137461a5e51SNicolas Pitre 	 * 1) whether or not we need to apply a bias, and		\
138461a5e51SNicolas Pitre 	 *								\
139461a5e51SNicolas Pitre 	 * 2) whether or not there might be an overflow in the cross	\
140461a5e51SNicolas Pitre 	 *    product determined by (___m & ((1 << 63) | (1 << 31))).	\
141461a5e51SNicolas Pitre 	 *								\
142f682b27cSNicolas Pitre 	 * Select the best way to do (m_bias + m * n) / (1 << 64).	\
143461a5e51SNicolas Pitre 	 * From now on there will be actual runtime code generated.	\
144461a5e51SNicolas Pitre 	 */								\
145f682b27cSNicolas Pitre 	___res = __arch_xprod_64(___m, ___n, ___bias);			\
146461a5e51SNicolas Pitre 									\
147461a5e51SNicolas Pitre 	___res /= ___p;							\
148461a5e51SNicolas Pitre })
149461a5e51SNicolas Pitre 
150f682b27cSNicolas Pitre #ifndef __arch_xprod_64
151f682b27cSNicolas Pitre /*
152f682b27cSNicolas Pitre  * Default C implementation for __arch_xprod_64()
153f682b27cSNicolas Pitre  *
154f682b27cSNicolas Pitre  * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
155f682b27cSNicolas Pitre  * Semantic:  retval = ((bias ? m : 0) + m * n) >> 64
156f682b27cSNicolas Pitre  *
157f682b27cSNicolas Pitre  * The product is a 128-bit value, scaled down to 64 bits.
158f682b27cSNicolas Pitre  * Assuming constant propagation to optimize away unused conditional code.
159f682b27cSNicolas Pitre  * Architectures may provide their own optimized assembly implementation.
160f682b27cSNicolas Pitre  */
161f682b27cSNicolas Pitre static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
162f682b27cSNicolas Pitre {
163f682b27cSNicolas Pitre 	uint32_t m_lo = m;
164f682b27cSNicolas Pitre 	uint32_t m_hi = m >> 32;
165f682b27cSNicolas Pitre 	uint32_t n_lo = n;
166f682b27cSNicolas Pitre 	uint32_t n_hi = n >> 32;
167f682b27cSNicolas Pitre 	uint64_t res, tmp;
168f682b27cSNicolas Pitre 
169f682b27cSNicolas Pitre 	if (!bias) {
170f682b27cSNicolas Pitre 		res = ((uint64_t)m_lo * n_lo) >> 32;
171f682b27cSNicolas Pitre 	} else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
172f682b27cSNicolas Pitre 		/* there can't be any overflow here */
173f682b27cSNicolas Pitre 		res = (m + (uint64_t)m_lo * n_lo) >> 32;
174f682b27cSNicolas Pitre 	} else {
175f682b27cSNicolas Pitre 		res = m + (uint64_t)m_lo * n_lo;
176f682b27cSNicolas Pitre 		tmp = (res < m) ? (1ULL << 32) : 0;
177f682b27cSNicolas Pitre 		res = (res >> 32) + tmp;
178f682b27cSNicolas Pitre 	}
179f682b27cSNicolas Pitre 
180f682b27cSNicolas Pitre 	if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
181f682b27cSNicolas Pitre 		/* there can't be any overflow here */
182f682b27cSNicolas Pitre 		res += (uint64_t)m_lo * n_hi;
183f682b27cSNicolas Pitre 		res += (uint64_t)m_hi * n_lo;
184f682b27cSNicolas Pitre 		res >>= 32;
185f682b27cSNicolas Pitre 	} else {
186f682b27cSNicolas Pitre 		tmp = res += (uint64_t)m_lo * n_hi;
187f682b27cSNicolas Pitre 		res += (uint64_t)m_hi * n_lo;
188f682b27cSNicolas Pitre 		tmp = (res < tmp) ? (1ULL << 32) : 0;
189f682b27cSNicolas Pitre 		res = (res >> 32) + tmp;
190f682b27cSNicolas Pitre 	}
191f682b27cSNicolas Pitre 
192f682b27cSNicolas Pitre 	res += (uint64_t)m_hi * n_hi;
193f682b27cSNicolas Pitre 
194f682b27cSNicolas Pitre 	return res;
195f682b27cSNicolas Pitre }
196f682b27cSNicolas Pitre #endif
197f682b27cSNicolas Pitre 
198dce1eb93SNicolas Pitre #ifndef __div64_32
1991da177e4SLinus Torvalds extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
200dce1eb93SNicolas Pitre #endif
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /* The unnecessary pointer compare is there
2031da177e4SLinus Torvalds  * to check for type safety (n must be 64bit)
2041da177e4SLinus Torvalds  */
2051da177e4SLinus Torvalds # define do_div(n,base) ({				\
2061da177e4SLinus Torvalds 	uint32_t __base = (base);			\
2071da177e4SLinus Torvalds 	uint32_t __rem;					\
2081da177e4SLinus Torvalds 	(void)(((typeof((n)) *)0) == ((uint64_t *)0));	\
209911918aaSNicolas Pitre 	if (__builtin_constant_p(__base) &&		\
210911918aaSNicolas Pitre 	    is_power_of_2(__base)) {			\
211911918aaSNicolas Pitre 		__rem = (n) & (__base - 1);		\
212911918aaSNicolas Pitre 		(n) >>= ilog2(__base);			\
213461a5e51SNicolas Pitre 	} else if (__div64_const32_is_OK &&		\
214461a5e51SNicolas Pitre 		   __builtin_constant_p(__base) &&	\
215461a5e51SNicolas Pitre 		   __base != 0) {			\
216461a5e51SNicolas Pitre 		uint32_t __res_lo, __n_lo = (n);	\
217461a5e51SNicolas Pitre 		(n) = __div64_const32(n, __base);	\
218461a5e51SNicolas Pitre 		/* the remainder can be computed with 32-bit regs */ \
219461a5e51SNicolas Pitre 		__res_lo = (n);				\
220461a5e51SNicolas Pitre 		__rem = __n_lo - __res_lo * __base;	\
221911918aaSNicolas Pitre 	} else if (likely(((n) >> 32) == 0)) {		\
2221da177e4SLinus Torvalds 		__rem = (uint32_t)(n) % __base;		\
2231da177e4SLinus Torvalds 		(n) = (uint32_t)(n) / __base;		\
2241da177e4SLinus Torvalds 	} else 						\
2251da177e4SLinus Torvalds 		__rem = __div64_32(&(n), __base);	\
2261da177e4SLinus Torvalds 	__rem;						\
2271da177e4SLinus Torvalds  })
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #else /* BITS_PER_LONG == ?? */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds # error do_div() does not yet support the C64
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds #endif /* BITS_PER_LONG */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds #endif /* _ASM_GENERIC_DIV64_H */
236