xref: /linux-6.15/include/asm-generic/div64.h (revision 00a31dd3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_DIV64_H
3 #define _ASM_GENERIC_DIV64_H
4 /*
5  * Copyright (C) 2003 Bernardo Innocenti <[email protected]>
6  * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
7  *
8  * Optimization for constant divisors on 32-bit machines:
9  * Copyright (C) 2006-2015 Nicolas Pitre
10  *
11  * The semantics of do_div() is, in C++ notation, observing that the name
12  * is a function-like macro and the n parameter has the semantics of a C++
13  * reference:
14  *
15  * uint32_t do_div(uint64_t &n, uint32_t base)
16  * {
17  * 	uint32_t remainder = n % base;
18  * 	n = n / base;
19  * 	return remainder;
20  * }
21  *
22  * NOTE: macro parameter n is evaluated multiple times,
23  *       beware of side effects!
24  */
25 
26 #include <linux/types.h>
27 #include <linux/compiler.h>
28 
29 #if BITS_PER_LONG == 64
30 
31 /**
32  * do_div - returns 2 values: calculate remainder and update new dividend
33  * @n: uint64_t dividend (will be updated)
34  * @base: uint32_t divisor
35  *
36  * Summary:
37  * ``uint32_t remainder = n % base;``
38  * ``n = n / base;``
39  *
40  * Return: (uint32_t)remainder
41  *
42  * NOTE: macro parameter @n is evaluated multiple times,
43  * beware of side effects!
44  */
45 # define do_div(n,base) ({					\
46 	uint32_t __base = (base);				\
47 	uint32_t __rem;						\
48 	__rem = ((uint64_t)(n)) % __base;			\
49 	(n) = ((uint64_t)(n)) / __base;				\
50 	__rem;							\
51  })
52 
53 #elif BITS_PER_LONG == 32
54 
55 #include <linux/log2.h>
56 
57 /*
58  * If the divisor happens to be constant, we determine the appropriate
59  * inverse at compile time to turn the division into a few inline
60  * multiplications which ought to be much faster.
61  *
62  * (It is unfortunate that gcc doesn't perform all this internally.)
63  */
64 
65 #define __div64_const32(n, ___b)					\
66 ({									\
67 	/*								\
68 	 * Multiplication by reciprocal of b: n / b = n * (p / b) / p	\
69 	 *								\
70 	 * We rely on the fact that most of this code gets optimized	\
71 	 * away at compile time due to constant propagation and only	\
72 	 * a few multiplication instructions should remain.		\
73 	 * Hence this monstrous macro (static inline doesn't always	\
74 	 * do the trick here).						\
75 	 */								\
76 	uint64_t ___res, ___x, ___t, ___m, ___n = (n);			\
77 	uint32_t ___p;							\
78 	bool ___bias = false;						\
79 									\
80 	/* determine MSB of b */					\
81 	___p = 1 << ilog2(___b);					\
82 									\
83 	/* compute m = ((p << 64) + b - 1) / b */			\
84 	___m = (~0ULL / ___b) * ___p;					\
85 	___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b;	\
86 									\
87 	/* one less than the dividend with highest result */		\
88 	___x = ~0ULL / ___b * ___b - 1;					\
89 									\
90 	/* test our ___m with res = m * x / (p << 64) */		\
91 	___res = (___m & 0xffffffff) * (___x & 0xffffffff);		\
92 	___t = (___m & 0xffffffff) * (___x >> 32) + (___res >> 32);	\
93 	___res = (___m >> 32) * (___x >> 32) + (___t >> 32);		\
94 	___t = (___m >> 32) * (___x & 0xffffffff) + (___t & 0xffffffff);\
95 	___res = (___res + (___t >> 32)) / ___p;			\
96 									\
97 	/* Now validate what we've got. */				\
98 	if (___res != ___x / ___b) {					\
99 		/*							\
100 		 * We can't get away without a bias to compensate	\
101 		 * for bit truncation errors.  To avoid it we'd need an	\
102 		 * additional bit to represent m which would overflow	\
103 		 * a 64-bit variable.					\
104 		 *							\
105 		 * Instead we do m = p / b and n / b = (n * m + m) / p.	\
106 		 */							\
107 		___bias = true;						\
108 		/* Compute m = (p << 64) / b */				\
109 		___m = (~0ULL / ___b) * ___p;				\
110 		___m += ((~0ULL % ___b + 1) * ___p) / ___b;		\
111 	}								\
112 									\
113 	/* Reduce m / p to help avoid overflow handling later. */	\
114 	___p /= (___m & -___m);						\
115 	___m /= (___m & -___m);						\
116 									\
117 	/*								\
118 	 * Perform (m_bias + m * n) / (1 << 64).			\
119 	 * From now on there will be actual runtime code generated.	\
120 	 */								\
121 	___res = __arch_xprod_64(___m, ___n, ___bias);			\
122 									\
123 	___res /= ___p;							\
124 })
125 
126 #ifndef __arch_xprod_64
127 /*
128  * Default C implementation for __arch_xprod_64()
129  *
130  * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
131  * Semantic:  retval = ((bias ? m : 0) + m * n) >> 64
132  *
133  * The product is a 128-bit value, scaled down to 64 bits.
134  * Hoping for compile-time optimization of  conditional code.
135  * Architectures may provide their own optimized assembly implementation.
136  */
137 static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
138 {
139 	uint32_t m_lo = m;
140 	uint32_t m_hi = m >> 32;
141 	uint32_t n_lo = n;
142 	uint32_t n_hi = n >> 32;
143 	uint64_t x, y;
144 
145 	/* Determine if overflow handling can be dispensed with. */
146 	bool no_ovf = __builtin_constant_p(m) &&
147 		      ((m >> 32) + (m & 0xffffffff) < 0x100000000);
148 
149 	if (no_ovf) {
150 		x = (uint64_t)m_lo * n_lo + (bias ? m : 0);
151 		x >>= 32;
152 		x += (uint64_t)m_lo * n_hi;
153 		x += (uint64_t)m_hi * n_lo;
154 		x >>= 32;
155 		x += (uint64_t)m_hi * n_hi;
156 	} else {
157 		x = (uint64_t)m_lo * n_lo + (bias ? m_lo : 0);
158 		y = (uint64_t)m_lo * n_hi + (uint32_t)(x >> 32) + (bias ? m_hi : 0);
159 		x = (uint64_t)m_hi * n_hi + (uint32_t)(y >> 32);
160 		y = (uint64_t)m_hi * n_lo + (uint32_t)y;
161 		x += (uint32_t)(y >> 32);
162 	}
163 
164 	return x;
165 }
166 #endif
167 
168 #ifndef __div64_32
169 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
170 #endif
171 
172 /* The unnecessary pointer compare is there
173  * to check for type safety (n must be 64bit)
174  */
175 # define do_div(n,base) ({				\
176 	uint32_t __base = (base);			\
177 	uint32_t __rem;					\
178 	(void)(((typeof((n)) *)0) == ((uint64_t *)0));	\
179 	if (__builtin_constant_p(__base) &&		\
180 	    is_power_of_2(__base)) {			\
181 		__rem = (n) & (__base - 1);		\
182 		(n) >>= ilog2(__base);			\
183 	} else if (__builtin_constant_p(__base) &&	\
184 		   __base != 0) {			\
185 		uint32_t __res_lo, __n_lo = (n);	\
186 		(n) = __div64_const32(n, __base);	\
187 		/* the remainder can be computed with 32-bit regs */ \
188 		__res_lo = (n);				\
189 		__rem = __n_lo - __res_lo * __base;	\
190 	} else if (likely(((n) >> 32) == 0)) {		\
191 		__rem = (uint32_t)(n) % __base;		\
192 		(n) = (uint32_t)(n) / __base;		\
193 	} else {					\
194 		__rem = __div64_32(&(n), __base);	\
195 	}						\
196 	__rem;						\
197  })
198 
199 #else /* BITS_PER_LONG == ?? */
200 
201 # error do_div() does not yet support the C64
202 
203 #endif /* BITS_PER_LONG */
204 
205 #endif /* _ASM_GENERIC_DIV64_H */
206