xref: /rk3399_rockchip-uboot/include/div64.h (revision 0342e335ba887817ed401e77be324e064ea7031e)
1f7c086e9SDirk Behme #ifndef _ASM_GENERIC_DIV64_H
2f7c086e9SDirk Behme #define _ASM_GENERIC_DIV64_H
3f7c086e9SDirk Behme /*
4f7c086e9SDirk Behme  * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
5f7c086e9SDirk Behme  * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
6f7c086e9SDirk Behme  *
7*0342e335SPeng Fan  * Optimization for constant divisors on 32-bit machines:
8*0342e335SPeng Fan  * Copyright (C) 2006-2015 Nicolas Pitre
9*0342e335SPeng Fan  *
10f7c086e9SDirk Behme  * The semantics of do_div() are:
11f7c086e9SDirk Behme  *
12f7c086e9SDirk Behme  * uint32_t do_div(uint64_t *n, uint32_t base)
13f7c086e9SDirk Behme  * {
14f7c086e9SDirk Behme  * 	uint32_t remainder = *n % base;
15f7c086e9SDirk Behme  * 	*n = *n / base;
16f7c086e9SDirk Behme  * 	return remainder;
17f7c086e9SDirk Behme  * }
18f7c086e9SDirk Behme  *
19f7c086e9SDirk Behme  * NOTE: macro parameter n is evaluated multiple times,
20f7c086e9SDirk Behme  *       beware of side effects!
21f7c086e9SDirk Behme  */
22f7c086e9SDirk Behme 
23f7c086e9SDirk Behme #include <linux/types.h>
24*0342e335SPeng Fan #include <linux/compiler.h>
25f7c086e9SDirk Behme 
26*0342e335SPeng Fan #if BITS_PER_LONG == 64
27*0342e335SPeng Fan 
28*0342e335SPeng Fan # define do_div(n,base) ({					\
29*0342e335SPeng Fan 	uint32_t __base = (base);				\
30*0342e335SPeng Fan 	uint32_t __rem;						\
31*0342e335SPeng Fan 	__rem = ((uint64_t)(n)) % __base;			\
32*0342e335SPeng Fan 	(n) = ((uint64_t)(n)) / __base;				\
33*0342e335SPeng Fan 	__rem;							\
34*0342e335SPeng Fan  })
35*0342e335SPeng Fan 
36*0342e335SPeng Fan #elif BITS_PER_LONG == 32
37*0342e335SPeng Fan 
38*0342e335SPeng Fan #include <linux/log2.h>
39*0342e335SPeng Fan 
40*0342e335SPeng Fan /*
41*0342e335SPeng Fan  * If the divisor happens to be constant, we determine the appropriate
42*0342e335SPeng Fan  * inverse at compile time to turn the division into a few inline
43*0342e335SPeng Fan  * multiplications which ought to be much faster. And yet only if compiling
44*0342e335SPeng Fan  * with a sufficiently recent gcc version to perform proper 64-bit constant
45*0342e335SPeng Fan  * propagation.
46*0342e335SPeng Fan  *
47*0342e335SPeng Fan  * (It is unfortunate that gcc doesn't perform all this internally.)
48*0342e335SPeng Fan  */
49*0342e335SPeng Fan 
50*0342e335SPeng Fan #ifndef __div64_const32_is_OK
51*0342e335SPeng Fan #define __div64_const32_is_OK (__GNUC__ >= 4)
52*0342e335SPeng Fan #endif
53*0342e335SPeng Fan 
54*0342e335SPeng Fan #define __div64_const32(n, ___b)					\
55*0342e335SPeng Fan ({									\
56*0342e335SPeng Fan 	/*								\
57*0342e335SPeng Fan 	 * Multiplication by reciprocal of b: n / b = n * (p / b) / p	\
58*0342e335SPeng Fan 	 *								\
59*0342e335SPeng Fan 	 * We rely on the fact that most of this code gets optimized	\
60*0342e335SPeng Fan 	 * away at compile time due to constant propagation and only	\
61*0342e335SPeng Fan 	 * a few multiplication instructions should remain.		\
62*0342e335SPeng Fan 	 * Hence this monstrous macro (static inline doesn't always	\
63*0342e335SPeng Fan 	 * do the trick here).						\
64*0342e335SPeng Fan 	 */								\
65*0342e335SPeng Fan 	uint64_t ___res, ___x, ___t, ___m, ___n = (n);			\
66*0342e335SPeng Fan 	uint32_t ___p, ___bias;						\
67*0342e335SPeng Fan 									\
68*0342e335SPeng Fan 	/* determine MSB of b */					\
69*0342e335SPeng Fan 	___p = 1 << ilog2(___b);					\
70*0342e335SPeng Fan 									\
71*0342e335SPeng Fan 	/* compute m = ((p << 64) + b - 1) / b */			\
72*0342e335SPeng Fan 	___m = (~0ULL / ___b) * ___p;					\
73*0342e335SPeng Fan 	___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b;	\
74*0342e335SPeng Fan 									\
75*0342e335SPeng Fan 	/* one less than the dividend with highest result */		\
76*0342e335SPeng Fan 	___x = ~0ULL / ___b * ___b - 1;					\
77*0342e335SPeng Fan 									\
78*0342e335SPeng Fan 	/* test our ___m with res = m * x / (p << 64) */		\
79*0342e335SPeng Fan 	___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32;	\
80*0342e335SPeng Fan 	___t = ___res += (___m & 0xffffffff) * (___x >> 32);		\
81*0342e335SPeng Fan 	___res += (___x & 0xffffffff) * (___m >> 32);			\
82*0342e335SPeng Fan 	___t = (___res < ___t) ? (1ULL << 32) : 0;			\
83*0342e335SPeng Fan 	___res = (___res >> 32) + ___t;					\
84*0342e335SPeng Fan 	___res += (___m >> 32) * (___x >> 32);				\
85*0342e335SPeng Fan 	___res /= ___p;							\
86*0342e335SPeng Fan 									\
87*0342e335SPeng Fan 	/* Now sanitize and optimize what we've got. */			\
88*0342e335SPeng Fan 	if (~0ULL % (___b / (___b & -___b)) == 0) {			\
89*0342e335SPeng Fan 		/* special case, can be simplified to ... */		\
90*0342e335SPeng Fan 		___n /= (___b & -___b);					\
91*0342e335SPeng Fan 		___m = ~0ULL / (___b / (___b & -___b));			\
92*0342e335SPeng Fan 		___p = 1;						\
93*0342e335SPeng Fan 		___bias = 1;						\
94*0342e335SPeng Fan 	} else if (___res != ___x / ___b) {				\
95*0342e335SPeng Fan 		/*							\
96*0342e335SPeng Fan 		 * We can't get away without a bias to compensate	\
97*0342e335SPeng Fan 		 * for bit truncation errors.  To avoid it we'd need an	\
98*0342e335SPeng Fan 		 * additional bit to represent m which would overflow	\
99*0342e335SPeng Fan 		 * a 64-bit variable.					\
100*0342e335SPeng Fan 		 *							\
101*0342e335SPeng Fan 		 * Instead we do m = p / b and n / b = (n * m + m) / p.	\
102*0342e335SPeng Fan 		 */							\
103*0342e335SPeng Fan 		___bias = 1;						\
104*0342e335SPeng Fan 		/* Compute m = (p << 64) / b */				\
105*0342e335SPeng Fan 		___m = (~0ULL / ___b) * ___p;				\
106*0342e335SPeng Fan 		___m += ((~0ULL % ___b + 1) * ___p) / ___b;		\
107*0342e335SPeng Fan 	} else {							\
108*0342e335SPeng Fan 		/*							\
109*0342e335SPeng Fan 		 * Reduce m / p, and try to clear bit 31 of m when	\
110*0342e335SPeng Fan 		 * possible, otherwise that'll need extra overflow	\
111*0342e335SPeng Fan 		 * handling later.					\
112*0342e335SPeng Fan 		 */							\
113*0342e335SPeng Fan 		uint32_t ___bits = -(___m & -___m);			\
114*0342e335SPeng Fan 		___bits |= ___m >> 32;					\
115*0342e335SPeng Fan 		___bits = (~___bits) << 1;				\
116*0342e335SPeng Fan 		/*							\
117*0342e335SPeng Fan 		 * If ___bits == 0 then setting bit 31 is  unavoidable.	\
118*0342e335SPeng Fan 		 * Simply apply the maximum possible reduction in that	\
119*0342e335SPeng Fan 		 * case. Otherwise the MSB of ___bits indicates the	\
120*0342e335SPeng Fan 		 * best reduction we should apply.			\
121*0342e335SPeng Fan 		 */							\
122*0342e335SPeng Fan 		if (!___bits) {						\
123*0342e335SPeng Fan 			___p /= (___m & -___m);				\
124*0342e335SPeng Fan 			___m /= (___m & -___m);				\
125*0342e335SPeng Fan 		} else {						\
126*0342e335SPeng Fan 			___p >>= ilog2(___bits);			\
127*0342e335SPeng Fan 			___m >>= ilog2(___bits);			\
128*0342e335SPeng Fan 		}							\
129*0342e335SPeng Fan 		/* No bias needed. */					\
130*0342e335SPeng Fan 		___bias = 0;						\
131*0342e335SPeng Fan 	}								\
132*0342e335SPeng Fan 									\
133*0342e335SPeng Fan 	/*								\
134*0342e335SPeng Fan 	 * Now we have a combination of 2 conditions:			\
135*0342e335SPeng Fan 	 *								\
136*0342e335SPeng Fan 	 * 1) whether or not we need to apply a bias, and		\
137*0342e335SPeng Fan 	 *								\
138*0342e335SPeng Fan 	 * 2) whether or not there might be an overflow in the cross	\
139*0342e335SPeng Fan 	 *    product determined by (___m & ((1 << 63) | (1 << 31))).	\
140*0342e335SPeng Fan 	 *								\
141*0342e335SPeng Fan 	 * Select the best way to do (m_bias + m * n) / (1 << 64).	\
142*0342e335SPeng Fan 	 * From now on there will be actual runtime code generated.	\
143*0342e335SPeng Fan 	 */								\
144*0342e335SPeng Fan 	___res = __arch_xprod_64(___m, ___n, ___bias);			\
145*0342e335SPeng Fan 									\
146*0342e335SPeng Fan 	___res /= ___p;							\
147*0342e335SPeng Fan })
148*0342e335SPeng Fan 
149*0342e335SPeng Fan #ifndef __arch_xprod_64
150*0342e335SPeng Fan /*
151*0342e335SPeng Fan  * Default C implementation for __arch_xprod_64()
152*0342e335SPeng Fan  *
153*0342e335SPeng Fan  * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
154*0342e335SPeng Fan  * Semantic:  retval = ((bias ? m : 0) + m * n) >> 64
155*0342e335SPeng Fan  *
156*0342e335SPeng Fan  * The product is a 128-bit value, scaled down to 64 bits.
157*0342e335SPeng Fan  * Assuming constant propagation to optimize away unused conditional code.
158*0342e335SPeng Fan  * Architectures may provide their own optimized assembly implementation.
159*0342e335SPeng Fan  */
__arch_xprod_64(const uint64_t m,uint64_t n,bool bias)160*0342e335SPeng Fan static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
161*0342e335SPeng Fan {
162*0342e335SPeng Fan 	uint32_t m_lo = m;
163*0342e335SPeng Fan 	uint32_t m_hi = m >> 32;
164*0342e335SPeng Fan 	uint32_t n_lo = n;
165*0342e335SPeng Fan 	uint32_t n_hi = n >> 32;
166*0342e335SPeng Fan 	uint64_t res, tmp;
167*0342e335SPeng Fan 
168*0342e335SPeng Fan 	if (!bias) {
169*0342e335SPeng Fan 		res = ((uint64_t)m_lo * n_lo) >> 32;
170*0342e335SPeng Fan 	} else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
171*0342e335SPeng Fan 		/* there can't be any overflow here */
172*0342e335SPeng Fan 		res = (m + (uint64_t)m_lo * n_lo) >> 32;
173*0342e335SPeng Fan 	} else {
174*0342e335SPeng Fan 		res = m + (uint64_t)m_lo * n_lo;
175*0342e335SPeng Fan 		tmp = (res < m) ? (1ULL << 32) : 0;
176*0342e335SPeng Fan 		res = (res >> 32) + tmp;
177*0342e335SPeng Fan 	}
178*0342e335SPeng Fan 
179*0342e335SPeng Fan 	if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
180*0342e335SPeng Fan 		/* there can't be any overflow here */
181*0342e335SPeng Fan 		res += (uint64_t)m_lo * n_hi;
182*0342e335SPeng Fan 		res += (uint64_t)m_hi * n_lo;
183*0342e335SPeng Fan 		res >>= 32;
184*0342e335SPeng Fan 	} else {
185*0342e335SPeng Fan 		tmp = res += (uint64_t)m_lo * n_hi;
186*0342e335SPeng Fan 		res += (uint64_t)m_hi * n_lo;
187*0342e335SPeng Fan 		tmp = (res < tmp) ? (1ULL << 32) : 0;
188*0342e335SPeng Fan 		res = (res >> 32) + tmp;
189*0342e335SPeng Fan 	}
190*0342e335SPeng Fan 
191*0342e335SPeng Fan 	res += (uint64_t)m_hi * n_hi;
192*0342e335SPeng Fan 
193*0342e335SPeng Fan 	return res;
194*0342e335SPeng Fan }
195*0342e335SPeng Fan #endif
196*0342e335SPeng Fan 
197*0342e335SPeng Fan #ifndef __div64_32
198f7c086e9SDirk Behme extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
199*0342e335SPeng Fan #endif
200f7c086e9SDirk Behme 
201f7c086e9SDirk Behme /* The unnecessary pointer compare is there
202f7c086e9SDirk Behme  * to check for type safety (n must be 64bit)
203f7c086e9SDirk Behme  */
204f7c086e9SDirk Behme # define do_div(n,base) ({				\
205f7c086e9SDirk Behme 	uint32_t __base = (base);			\
206f7c086e9SDirk Behme 	uint32_t __rem;					\
207f7c086e9SDirk Behme 	(void)(((typeof((n)) *)0) == ((uint64_t *)0));	\
208*0342e335SPeng Fan 	if (__builtin_constant_p(__base) &&		\
209*0342e335SPeng Fan 	    is_power_of_2(__base)) {			\
210*0342e335SPeng Fan 		__rem = (n) & (__base - 1);		\
211*0342e335SPeng Fan 		(n) >>= ilog2(__base);			\
212*0342e335SPeng Fan 	} else if (__div64_const32_is_OK &&		\
213*0342e335SPeng Fan 		   __builtin_constant_p(__base) &&	\
214*0342e335SPeng Fan 		   __base != 0) {			\
215*0342e335SPeng Fan 		uint32_t __res_lo, __n_lo = (n);	\
216*0342e335SPeng Fan 		(n) = __div64_const32(n, __base);	\
217*0342e335SPeng Fan 		/* the remainder can be computed with 32-bit regs */ \
218*0342e335SPeng Fan 		__res_lo = (n);				\
219*0342e335SPeng Fan 		__rem = __n_lo - __res_lo * __base;	\
220*0342e335SPeng Fan 	} else if (likely(((n) >> 32) == 0)) {		\
221f7c086e9SDirk Behme 		__rem = (uint32_t)(n) % __base;		\
222f7c086e9SDirk Behme 		(n) = (uint32_t)(n) / __base;		\
223f7c086e9SDirk Behme 	} else 						\
224f7c086e9SDirk Behme 		__rem = __div64_32(&(n), __base);	\
225f7c086e9SDirk Behme 	__rem;						\
226f7c086e9SDirk Behme  })
227f7c086e9SDirk Behme 
228*0342e335SPeng Fan #else /* BITS_PER_LONG == ?? */
229*0342e335SPeng Fan 
230*0342e335SPeng Fan # error do_div() does not yet support the C64
231*0342e335SPeng Fan 
232*0342e335SPeng Fan #endif /* BITS_PER_LONG */
233*0342e335SPeng Fan 
2343feb647fSSergei Poselenov /* Wrapper for do_div(). Doesn't modify dividend and returns
2353feb647fSSergei Poselenov  * the result, not reminder.
2363feb647fSSergei Poselenov  */
lldiv(uint64_t dividend,uint32_t divisor)2373feb647fSSergei Poselenov static inline uint64_t lldiv(uint64_t dividend, uint32_t divisor)
2383feb647fSSergei Poselenov {
2393feb647fSSergei Poselenov 	uint64_t __res = dividend;
2403feb647fSSergei Poselenov 	do_div(__res, divisor);
2413feb647fSSergei Poselenov 	return(__res);
2423feb647fSSergei Poselenov }
2433feb647fSSergei Poselenov 
244f7c086e9SDirk Behme #endif /* _ASM_GENERIC_DIV64_H */
245