1b2441318SGreg 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 *
11f2875832SMaciej W. Rozycki * The semantics of do_div() is, in C++ notation, observing that the name
12f2875832SMaciej W. Rozycki * is a function-like macro and the n parameter has the semantics of a C++
13f2875832SMaciej W. Rozycki * reference:
141da177e4SLinus Torvalds *
15f2875832SMaciej W. Rozycki * uint32_t do_div(uint64_t &n, uint32_t base)
161da177e4SLinus Torvalds * {
17f2875832SMaciej W. Rozycki * uint32_t remainder = n % base;
18f2875832SMaciej W. Rozycki * n = n / base;
191da177e4SLinus Torvalds * return remainder;
201da177e4SLinus Torvalds * }
211da177e4SLinus Torvalds *
221da177e4SLinus Torvalds * NOTE: macro parameter n is evaluated multiple times,
231da177e4SLinus Torvalds * beware of side effects!
241da177e4SLinus Torvalds */
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds #include <linux/types.h>
271da177e4SLinus Torvalds #include <linux/compiler.h>
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #if BITS_PER_LONG == 64
301da177e4SLinus Torvalds
316ec72e61SRandy Dunlap /**
326ec72e61SRandy Dunlap * do_div - returns 2 values: calculate remainder and update new dividend
33e8e4eb0fSJonathan Neuschäfer * @n: uint64_t dividend (will be updated)
346ec72e61SRandy Dunlap * @base: uint32_t divisor
356ec72e61SRandy Dunlap *
366ec72e61SRandy Dunlap * Summary:
37e8e4eb0fSJonathan Neuschäfer * ``uint32_t remainder = n % base;``
38e8e4eb0fSJonathan Neuschäfer * ``n = n / base;``
396ec72e61SRandy Dunlap *
406ec72e61SRandy Dunlap * Return: (uint32_t)remainder
416ec72e61SRandy Dunlap *
426ec72e61SRandy Dunlap * NOTE: macro parameter @n is evaluated multiple times,
436ec72e61SRandy Dunlap * beware of side effects!
446ec72e61SRandy Dunlap */
451da177e4SLinus Torvalds # define do_div(n,base) ({ \
461da177e4SLinus Torvalds uint32_t __base = (base); \
471da177e4SLinus Torvalds uint32_t __rem; \
481da177e4SLinus Torvalds __rem = ((uint64_t)(n)) % __base; \
491da177e4SLinus Torvalds (n) = ((uint64_t)(n)) / __base; \
501da177e4SLinus Torvalds __rem; \
511da177e4SLinus Torvalds })
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds #elif BITS_PER_LONG == 32
541da177e4SLinus Torvalds
55911918aaSNicolas Pitre #include <linux/log2.h>
56911918aaSNicolas Pitre
57461a5e51SNicolas Pitre /*
58461a5e51SNicolas Pitre * If the divisor happens to be constant, we determine the appropriate
59461a5e51SNicolas Pitre * inverse at compile time to turn the division into a few inline
60*c747ce47SGeert Uytterhoeven * multiplications which ought to be much faster.
61461a5e51SNicolas Pitre *
62461a5e51SNicolas Pitre * (It is unfortunate that gcc doesn't perform all this internally.)
63461a5e51SNicolas Pitre */
64461a5e51SNicolas Pitre
65461a5e51SNicolas Pitre #define __div64_const32(n, ___b) \
66461a5e51SNicolas Pitre ({ \
67461a5e51SNicolas Pitre /* \
68461a5e51SNicolas Pitre * Multiplication by reciprocal of b: n / b = n * (p / b) / p \
69461a5e51SNicolas Pitre * \
70461a5e51SNicolas Pitre * We rely on the fact that most of this code gets optimized \
71461a5e51SNicolas Pitre * away at compile time due to constant propagation and only \
72461a5e51SNicolas Pitre * a few multiplication instructions should remain. \
73461a5e51SNicolas Pitre * Hence this monstrous macro (static inline doesn't always \
74461a5e51SNicolas Pitre * do the trick here). \
75461a5e51SNicolas Pitre */ \
76461a5e51SNicolas Pitre uint64_t ___res, ___x, ___t, ___m, ___n = (n); \
77f682b27cSNicolas Pitre uint32_t ___p, ___bias; \
78461a5e51SNicolas Pitre \
79461a5e51SNicolas Pitre /* determine MSB of b */ \
80461a5e51SNicolas Pitre ___p = 1 << ilog2(___b); \
81461a5e51SNicolas Pitre \
82461a5e51SNicolas Pitre /* compute m = ((p << 64) + b - 1) / b */ \
83461a5e51SNicolas Pitre ___m = (~0ULL / ___b) * ___p; \
84461a5e51SNicolas Pitre ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
85461a5e51SNicolas Pitre \
86461a5e51SNicolas Pitre /* one less than the dividend with highest result */ \
87461a5e51SNicolas Pitre ___x = ~0ULL / ___b * ___b - 1; \
88461a5e51SNicolas Pitre \
89461a5e51SNicolas Pitre /* test our ___m with res = m * x / (p << 64) */ \
90461a5e51SNicolas Pitre ___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32; \
91461a5e51SNicolas Pitre ___t = ___res += (___m & 0xffffffff) * (___x >> 32); \
92461a5e51SNicolas Pitre ___res += (___x & 0xffffffff) * (___m >> 32); \
93461a5e51SNicolas Pitre ___t = (___res < ___t) ? (1ULL << 32) : 0; \
94461a5e51SNicolas Pitre ___res = (___res >> 32) + ___t; \
95461a5e51SNicolas Pitre ___res += (___m >> 32) * (___x >> 32); \
96461a5e51SNicolas Pitre ___res /= ___p; \
97461a5e51SNicolas Pitre \
98461a5e51SNicolas Pitre /* Now sanitize and optimize what we've got. */ \
99461a5e51SNicolas Pitre if (~0ULL % (___b / (___b & -___b)) == 0) { \
100461a5e51SNicolas Pitre /* special case, can be simplified to ... */ \
101461a5e51SNicolas Pitre ___n /= (___b & -___b); \
102461a5e51SNicolas Pitre ___m = ~0ULL / (___b / (___b & -___b)); \
103461a5e51SNicolas Pitre ___p = 1; \
104461a5e51SNicolas Pitre ___bias = 1; \
105461a5e51SNicolas Pitre } else if (___res != ___x / ___b) { \
106461a5e51SNicolas Pitre /* \
107461a5e51SNicolas Pitre * We can't get away without a bias to compensate \
108461a5e51SNicolas Pitre * for bit truncation errors. To avoid it we'd need an \
109461a5e51SNicolas Pitre * additional bit to represent m which would overflow \
110461a5e51SNicolas Pitre * a 64-bit variable. \
111461a5e51SNicolas Pitre * \
112461a5e51SNicolas Pitre * Instead we do m = p / b and n / b = (n * m + m) / p. \
113461a5e51SNicolas Pitre */ \
114461a5e51SNicolas Pitre ___bias = 1; \
115461a5e51SNicolas Pitre /* Compute m = (p << 64) / b */ \
116461a5e51SNicolas Pitre ___m = (~0ULL / ___b) * ___p; \
117461a5e51SNicolas Pitre ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
118461a5e51SNicolas Pitre } else { \
119461a5e51SNicolas Pitre /* \
120461a5e51SNicolas Pitre * Reduce m / p, and try to clear bit 31 of m when \
121461a5e51SNicolas Pitre * possible, otherwise that'll need extra overflow \
122461a5e51SNicolas Pitre * handling later. \
123461a5e51SNicolas Pitre */ \
124461a5e51SNicolas Pitre uint32_t ___bits = -(___m & -___m); \
125461a5e51SNicolas Pitre ___bits |= ___m >> 32; \
126461a5e51SNicolas Pitre ___bits = (~___bits) << 1; \
127461a5e51SNicolas Pitre /* \
128461a5e51SNicolas Pitre * If ___bits == 0 then setting bit 31 is unavoidable. \
129461a5e51SNicolas Pitre * Simply apply the maximum possible reduction in that \
130461a5e51SNicolas Pitre * case. Otherwise the MSB of ___bits indicates the \
131461a5e51SNicolas Pitre * best reduction we should apply. \
132461a5e51SNicolas Pitre */ \
133461a5e51SNicolas Pitre if (!___bits) { \
134461a5e51SNicolas Pitre ___p /= (___m & -___m); \
135461a5e51SNicolas Pitre ___m /= (___m & -___m); \
136461a5e51SNicolas Pitre } else { \
137461a5e51SNicolas Pitre ___p >>= ilog2(___bits); \
138461a5e51SNicolas Pitre ___m >>= ilog2(___bits); \
139461a5e51SNicolas Pitre } \
140461a5e51SNicolas Pitre /* No bias needed. */ \
141461a5e51SNicolas Pitre ___bias = 0; \
142461a5e51SNicolas Pitre } \
143461a5e51SNicolas Pitre \
144461a5e51SNicolas Pitre /* \
145461a5e51SNicolas Pitre * Now we have a combination of 2 conditions: \
146461a5e51SNicolas Pitre * \
147461a5e51SNicolas Pitre * 1) whether or not we need to apply a bias, and \
148461a5e51SNicolas Pitre * \
149461a5e51SNicolas Pitre * 2) whether or not there might be an overflow in the cross \
150461a5e51SNicolas Pitre * product determined by (___m & ((1 << 63) | (1 << 31))). \
151461a5e51SNicolas Pitre * \
152f682b27cSNicolas Pitre * Select the best way to do (m_bias + m * n) / (1 << 64). \
153461a5e51SNicolas Pitre * From now on there will be actual runtime code generated. \
154461a5e51SNicolas Pitre */ \
155f682b27cSNicolas Pitre ___res = __arch_xprod_64(___m, ___n, ___bias); \
156461a5e51SNicolas Pitre \
157461a5e51SNicolas Pitre ___res /= ___p; \
158461a5e51SNicolas Pitre })
159461a5e51SNicolas Pitre
160f682b27cSNicolas Pitre #ifndef __arch_xprod_64
161f682b27cSNicolas Pitre /*
162f682b27cSNicolas Pitre * Default C implementation for __arch_xprod_64()
163f682b27cSNicolas Pitre *
164f682b27cSNicolas Pitre * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
165f682b27cSNicolas Pitre * Semantic: retval = ((bias ? m : 0) + m * n) >> 64
166f682b27cSNicolas Pitre *
167f682b27cSNicolas Pitre * The product is a 128-bit value, scaled down to 64 bits.
168f682b27cSNicolas Pitre * Assuming constant propagation to optimize away unused conditional code.
169f682b27cSNicolas Pitre * Architectures may provide their own optimized assembly implementation.
170f682b27cSNicolas Pitre */
__arch_xprod_64(const uint64_t m,uint64_t n,bool bias)171f682b27cSNicolas Pitre static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
172f682b27cSNicolas Pitre {
173f682b27cSNicolas Pitre uint32_t m_lo = m;
174f682b27cSNicolas Pitre uint32_t m_hi = m >> 32;
175f682b27cSNicolas Pitre uint32_t n_lo = n;
176f682b27cSNicolas Pitre uint32_t n_hi = n >> 32;
177602828c1SNicolas Pitre uint64_t res;
178602828c1SNicolas Pitre uint32_t res_lo, res_hi, tmp;
179f682b27cSNicolas Pitre
180f682b27cSNicolas Pitre if (!bias) {
181f682b27cSNicolas Pitre res = ((uint64_t)m_lo * n_lo) >> 32;
182f682b27cSNicolas Pitre } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
183f682b27cSNicolas Pitre /* there can't be any overflow here */
184f682b27cSNicolas Pitre res = (m + (uint64_t)m_lo * n_lo) >> 32;
185f682b27cSNicolas Pitre } else {
186f682b27cSNicolas Pitre res = m + (uint64_t)m_lo * n_lo;
187602828c1SNicolas Pitre res_lo = res >> 32;
188602828c1SNicolas Pitre res_hi = (res_lo < m_hi);
189602828c1SNicolas Pitre res = res_lo | ((uint64_t)res_hi << 32);
190f682b27cSNicolas Pitre }
191f682b27cSNicolas Pitre
192f682b27cSNicolas Pitre if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
193f682b27cSNicolas Pitre /* there can't be any overflow here */
194f682b27cSNicolas Pitre res += (uint64_t)m_lo * n_hi;
195f682b27cSNicolas Pitre res += (uint64_t)m_hi * n_lo;
196f682b27cSNicolas Pitre res >>= 32;
197f682b27cSNicolas Pitre } else {
198602828c1SNicolas Pitre res += (uint64_t)m_lo * n_hi;
199602828c1SNicolas Pitre tmp = res >> 32;
200f682b27cSNicolas Pitre res += (uint64_t)m_hi * n_lo;
201602828c1SNicolas Pitre res_lo = res >> 32;
202602828c1SNicolas Pitre res_hi = (res_lo < tmp);
203602828c1SNicolas Pitre res = res_lo | ((uint64_t)res_hi << 32);
204f682b27cSNicolas Pitre }
205f682b27cSNicolas Pitre
206f682b27cSNicolas Pitre res += (uint64_t)m_hi * n_hi;
207f682b27cSNicolas Pitre
208f682b27cSNicolas Pitre return res;
209f682b27cSNicolas Pitre }
210f682b27cSNicolas Pitre #endif
211f682b27cSNicolas Pitre
212dce1eb93SNicolas Pitre #ifndef __div64_32
2131da177e4SLinus Torvalds extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
214dce1eb93SNicolas Pitre #endif
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds /* The unnecessary pointer compare is there
2171da177e4SLinus Torvalds * to check for type safety (n must be 64bit)
2181da177e4SLinus Torvalds */
2191da177e4SLinus Torvalds # define do_div(n,base) ({ \
2201da177e4SLinus Torvalds uint32_t __base = (base); \
2211da177e4SLinus Torvalds uint32_t __rem; \
2221da177e4SLinus Torvalds (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
223911918aaSNicolas Pitre if (__builtin_constant_p(__base) && \
224911918aaSNicolas Pitre is_power_of_2(__base)) { \
225911918aaSNicolas Pitre __rem = (n) & (__base - 1); \
226911918aaSNicolas Pitre (n) >>= ilog2(__base); \
227*c747ce47SGeert Uytterhoeven } else if (__builtin_constant_p(__base) && \
228461a5e51SNicolas Pitre __base != 0) { \
229461a5e51SNicolas Pitre uint32_t __res_lo, __n_lo = (n); \
230461a5e51SNicolas Pitre (n) = __div64_const32(n, __base); \
231461a5e51SNicolas Pitre /* the remainder can be computed with 32-bit regs */ \
232461a5e51SNicolas Pitre __res_lo = (n); \
233461a5e51SNicolas Pitre __rem = __n_lo - __res_lo * __base; \
234911918aaSNicolas Pitre } else if (likely(((n) >> 32) == 0)) { \
2351da177e4SLinus Torvalds __rem = (uint32_t)(n) % __base; \
2361da177e4SLinus Torvalds (n) = (uint32_t)(n) / __base; \
237*c747ce47SGeert Uytterhoeven } else { \
2381da177e4SLinus Torvalds __rem = __div64_32(&(n), __base); \
239*c747ce47SGeert Uytterhoeven } \
2401da177e4SLinus Torvalds __rem; \
2411da177e4SLinus Torvalds })
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds #else /* BITS_PER_LONG == ?? */
2441da177e4SLinus Torvalds
2451da177e4SLinus Torvalds # error do_div() does not yet support the C64
2461da177e4SLinus Torvalds
2471da177e4SLinus Torvalds #endif /* BITS_PER_LONG */
2481da177e4SLinus Torvalds
2491da177e4SLinus Torvalds #endif /* _ASM_GENERIC_DIV64_H */
250