18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_GENERIC_DIV64_H
38c2ecf20Sopenharmony_ci#define _ASM_GENERIC_DIV64_H
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
68c2ecf20Sopenharmony_ci * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Optimization for constant divisors on 32-bit machines:
98c2ecf20Sopenharmony_ci * Copyright (C) 2006-2015 Nicolas Pitre
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The semantics of do_div() are:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * uint32_t do_div(uint64_t *n, uint32_t base)
148c2ecf20Sopenharmony_ci * {
158c2ecf20Sopenharmony_ci * 	uint32_t remainder = *n % base;
168c2ecf20Sopenharmony_ci * 	*n = *n / base;
178c2ecf20Sopenharmony_ci * 	return remainder;
188c2ecf20Sopenharmony_ci * }
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * NOTE: macro parameter n is evaluated multiple times,
218c2ecf20Sopenharmony_ci *       beware of side effects!
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/types.h>
258c2ecf20Sopenharmony_ci#include <linux/compiler.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/**
308c2ecf20Sopenharmony_ci * do_div - returns 2 values: calculate remainder and update new dividend
318c2ecf20Sopenharmony_ci * @n: uint64_t dividend (will be updated)
328c2ecf20Sopenharmony_ci * @base: uint32_t divisor
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * Summary:
358c2ecf20Sopenharmony_ci * ``uint32_t remainder = n % base;``
368c2ecf20Sopenharmony_ci * ``n = n / base;``
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Return: (uint32_t)remainder
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * NOTE: macro parameter @n is evaluated multiple times,
418c2ecf20Sopenharmony_ci * beware of side effects!
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci# define do_div(n,base) ({					\
448c2ecf20Sopenharmony_ci	uint32_t __base = (base);				\
458c2ecf20Sopenharmony_ci	uint32_t __rem;						\
468c2ecf20Sopenharmony_ci	__rem = ((uint64_t)(n)) % __base;			\
478c2ecf20Sopenharmony_ci	(n) = ((uint64_t)(n)) / __base;				\
488c2ecf20Sopenharmony_ci	__rem;							\
498c2ecf20Sopenharmony_ci })
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#elif BITS_PER_LONG == 32
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#include <linux/log2.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * If the divisor happens to be constant, we determine the appropriate
578c2ecf20Sopenharmony_ci * inverse at compile time to turn the division into a few inline
588c2ecf20Sopenharmony_ci * multiplications which ought to be much faster. And yet only if compiling
598c2ecf20Sopenharmony_ci * with a sufficiently recent gcc version to perform proper 64-bit constant
608c2ecf20Sopenharmony_ci * propagation.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * (It is unfortunate that gcc doesn't perform all this internally.)
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#ifndef __div64_const32_is_OK
668c2ecf20Sopenharmony_ci#define __div64_const32_is_OK (__GNUC__ >= 4)
678c2ecf20Sopenharmony_ci#endif
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define __div64_const32(n, ___b)					\
708c2ecf20Sopenharmony_ci({									\
718c2ecf20Sopenharmony_ci	/*								\
728c2ecf20Sopenharmony_ci	 * Multiplication by reciprocal of b: n / b = n * (p / b) / p	\
738c2ecf20Sopenharmony_ci	 *								\
748c2ecf20Sopenharmony_ci	 * We rely on the fact that most of this code gets optimized	\
758c2ecf20Sopenharmony_ci	 * away at compile time due to constant propagation and only	\
768c2ecf20Sopenharmony_ci	 * a few multiplication instructions should remain.		\
778c2ecf20Sopenharmony_ci	 * Hence this monstrous macro (static inline doesn't always	\
788c2ecf20Sopenharmony_ci	 * do the trick here).						\
798c2ecf20Sopenharmony_ci	 */								\
808c2ecf20Sopenharmony_ci	uint64_t ___res, ___x, ___t, ___m, ___n = (n);			\
818c2ecf20Sopenharmony_ci	uint32_t ___p, ___bias;						\
828c2ecf20Sopenharmony_ci									\
838c2ecf20Sopenharmony_ci	/* determine MSB of b */					\
848c2ecf20Sopenharmony_ci	___p = 1 << ilog2(___b);					\
858c2ecf20Sopenharmony_ci									\
868c2ecf20Sopenharmony_ci	/* compute m = ((p << 64) + b - 1) / b */			\
878c2ecf20Sopenharmony_ci	___m = (~0ULL / ___b) * ___p;					\
888c2ecf20Sopenharmony_ci	___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b;	\
898c2ecf20Sopenharmony_ci									\
908c2ecf20Sopenharmony_ci	/* one less than the dividend with highest result */		\
918c2ecf20Sopenharmony_ci	___x = ~0ULL / ___b * ___b - 1;					\
928c2ecf20Sopenharmony_ci									\
938c2ecf20Sopenharmony_ci	/* test our ___m with res = m * x / (p << 64) */		\
948c2ecf20Sopenharmony_ci	___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32;	\
958c2ecf20Sopenharmony_ci	___t = ___res += (___m & 0xffffffff) * (___x >> 32);		\
968c2ecf20Sopenharmony_ci	___res += (___x & 0xffffffff) * (___m >> 32);			\
978c2ecf20Sopenharmony_ci	___t = (___res < ___t) ? (1ULL << 32) : 0;			\
988c2ecf20Sopenharmony_ci	___res = (___res >> 32) + ___t;					\
998c2ecf20Sopenharmony_ci	___res += (___m >> 32) * (___x >> 32);				\
1008c2ecf20Sopenharmony_ci	___res /= ___p;							\
1018c2ecf20Sopenharmony_ci									\
1028c2ecf20Sopenharmony_ci	/* Now sanitize and optimize what we've got. */			\
1038c2ecf20Sopenharmony_ci	if (~0ULL % (___b / (___b & -___b)) == 0) {			\
1048c2ecf20Sopenharmony_ci		/* special case, can be simplified to ... */		\
1058c2ecf20Sopenharmony_ci		___n /= (___b & -___b);					\
1068c2ecf20Sopenharmony_ci		___m = ~0ULL / (___b / (___b & -___b));			\
1078c2ecf20Sopenharmony_ci		___p = 1;						\
1088c2ecf20Sopenharmony_ci		___bias = 1;						\
1098c2ecf20Sopenharmony_ci	} else if (___res != ___x / ___b) {				\
1108c2ecf20Sopenharmony_ci		/*							\
1118c2ecf20Sopenharmony_ci		 * We can't get away without a bias to compensate	\
1128c2ecf20Sopenharmony_ci		 * for bit truncation errors.  To avoid it we'd need an	\
1138c2ecf20Sopenharmony_ci		 * additional bit to represent m which would overflow	\
1148c2ecf20Sopenharmony_ci		 * a 64-bit variable.					\
1158c2ecf20Sopenharmony_ci		 *							\
1168c2ecf20Sopenharmony_ci		 * Instead we do m = p / b and n / b = (n * m + m) / p.	\
1178c2ecf20Sopenharmony_ci		 */							\
1188c2ecf20Sopenharmony_ci		___bias = 1;						\
1198c2ecf20Sopenharmony_ci		/* Compute m = (p << 64) / b */				\
1208c2ecf20Sopenharmony_ci		___m = (~0ULL / ___b) * ___p;				\
1218c2ecf20Sopenharmony_ci		___m += ((~0ULL % ___b + 1) * ___p) / ___b;		\
1228c2ecf20Sopenharmony_ci	} else {							\
1238c2ecf20Sopenharmony_ci		/*							\
1248c2ecf20Sopenharmony_ci		 * Reduce m / p, and try to clear bit 31 of m when	\
1258c2ecf20Sopenharmony_ci		 * possible, otherwise that'll need extra overflow	\
1268c2ecf20Sopenharmony_ci		 * handling later.					\
1278c2ecf20Sopenharmony_ci		 */							\
1288c2ecf20Sopenharmony_ci		uint32_t ___bits = -(___m & -___m);			\
1298c2ecf20Sopenharmony_ci		___bits |= ___m >> 32;					\
1308c2ecf20Sopenharmony_ci		___bits = (~___bits) << 1;				\
1318c2ecf20Sopenharmony_ci		/*							\
1328c2ecf20Sopenharmony_ci		 * If ___bits == 0 then setting bit 31 is  unavoidable.	\
1338c2ecf20Sopenharmony_ci		 * Simply apply the maximum possible reduction in that	\
1348c2ecf20Sopenharmony_ci		 * case. Otherwise the MSB of ___bits indicates the	\
1358c2ecf20Sopenharmony_ci		 * best reduction we should apply.			\
1368c2ecf20Sopenharmony_ci		 */							\
1378c2ecf20Sopenharmony_ci		if (!___bits) {						\
1388c2ecf20Sopenharmony_ci			___p /= (___m & -___m);				\
1398c2ecf20Sopenharmony_ci			___m /= (___m & -___m);				\
1408c2ecf20Sopenharmony_ci		} else {						\
1418c2ecf20Sopenharmony_ci			___p >>= ilog2(___bits);			\
1428c2ecf20Sopenharmony_ci			___m >>= ilog2(___bits);			\
1438c2ecf20Sopenharmony_ci		}							\
1448c2ecf20Sopenharmony_ci		/* No bias needed. */					\
1458c2ecf20Sopenharmony_ci		___bias = 0;						\
1468c2ecf20Sopenharmony_ci	}								\
1478c2ecf20Sopenharmony_ci									\
1488c2ecf20Sopenharmony_ci	/*								\
1498c2ecf20Sopenharmony_ci	 * Now we have a combination of 2 conditions:			\
1508c2ecf20Sopenharmony_ci	 *								\
1518c2ecf20Sopenharmony_ci	 * 1) whether or not we need to apply a bias, and		\
1528c2ecf20Sopenharmony_ci	 *								\
1538c2ecf20Sopenharmony_ci	 * 2) whether or not there might be an overflow in the cross	\
1548c2ecf20Sopenharmony_ci	 *    product determined by (___m & ((1 << 63) | (1 << 31))).	\
1558c2ecf20Sopenharmony_ci	 *								\
1568c2ecf20Sopenharmony_ci	 * Select the best way to do (m_bias + m * n) / (1 << 64).	\
1578c2ecf20Sopenharmony_ci	 * From now on there will be actual runtime code generated.	\
1588c2ecf20Sopenharmony_ci	 */								\
1598c2ecf20Sopenharmony_ci	___res = __arch_xprod_64(___m, ___n, ___bias);			\
1608c2ecf20Sopenharmony_ci									\
1618c2ecf20Sopenharmony_ci	___res /= ___p;							\
1628c2ecf20Sopenharmony_ci})
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#ifndef __arch_xprod_64
1658c2ecf20Sopenharmony_ci/*
1668c2ecf20Sopenharmony_ci * Default C implementation for __arch_xprod_64()
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
1698c2ecf20Sopenharmony_ci * Semantic:  retval = ((bias ? m : 0) + m * n) >> 64
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * The product is a 128-bit value, scaled down to 64 bits.
1728c2ecf20Sopenharmony_ci * Assuming constant propagation to optimize away unused conditional code.
1738c2ecf20Sopenharmony_ci * Architectures may provide their own optimized assembly implementation.
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_cistatic inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	uint32_t m_lo = m;
1788c2ecf20Sopenharmony_ci	uint32_t m_hi = m >> 32;
1798c2ecf20Sopenharmony_ci	uint32_t n_lo = n;
1808c2ecf20Sopenharmony_ci	uint32_t n_hi = n >> 32;
1818c2ecf20Sopenharmony_ci	uint64_t res;
1828c2ecf20Sopenharmony_ci	uint32_t res_lo, res_hi, tmp;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (!bias) {
1858c2ecf20Sopenharmony_ci		res = ((uint64_t)m_lo * n_lo) >> 32;
1868c2ecf20Sopenharmony_ci	} else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
1878c2ecf20Sopenharmony_ci		/* there can't be any overflow here */
1888c2ecf20Sopenharmony_ci		res = (m + (uint64_t)m_lo * n_lo) >> 32;
1898c2ecf20Sopenharmony_ci	} else {
1908c2ecf20Sopenharmony_ci		res = m + (uint64_t)m_lo * n_lo;
1918c2ecf20Sopenharmony_ci		res_lo = res >> 32;
1928c2ecf20Sopenharmony_ci		res_hi = (res_lo < m_hi);
1938c2ecf20Sopenharmony_ci		res = res_lo | ((uint64_t)res_hi << 32);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
1978c2ecf20Sopenharmony_ci		/* there can't be any overflow here */
1988c2ecf20Sopenharmony_ci		res += (uint64_t)m_lo * n_hi;
1998c2ecf20Sopenharmony_ci		res += (uint64_t)m_hi * n_lo;
2008c2ecf20Sopenharmony_ci		res >>= 32;
2018c2ecf20Sopenharmony_ci	} else {
2028c2ecf20Sopenharmony_ci		res += (uint64_t)m_lo * n_hi;
2038c2ecf20Sopenharmony_ci		tmp = res >> 32;
2048c2ecf20Sopenharmony_ci		res += (uint64_t)m_hi * n_lo;
2058c2ecf20Sopenharmony_ci		res_lo = res >> 32;
2068c2ecf20Sopenharmony_ci		res_hi = (res_lo < tmp);
2078c2ecf20Sopenharmony_ci		res = res_lo | ((uint64_t)res_hi << 32);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	res += (uint64_t)m_hi * n_hi;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return res;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci#endif
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#ifndef __div64_32
2178c2ecf20Sopenharmony_ciextern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
2188c2ecf20Sopenharmony_ci#endif
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/* The unnecessary pointer compare is there
2218c2ecf20Sopenharmony_ci * to check for type safety (n must be 64bit)
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_ci# define do_div(n,base) ({				\
2248c2ecf20Sopenharmony_ci	uint32_t __base = (base);			\
2258c2ecf20Sopenharmony_ci	uint32_t __rem;					\
2268c2ecf20Sopenharmony_ci	(void)(((typeof((n)) *)0) == ((uint64_t *)0));	\
2278c2ecf20Sopenharmony_ci	if (__builtin_constant_p(__base) &&		\
2288c2ecf20Sopenharmony_ci	    is_power_of_2(__base)) {			\
2298c2ecf20Sopenharmony_ci		__rem = (n) & (__base - 1);		\
2308c2ecf20Sopenharmony_ci		(n) >>= ilog2(__base);			\
2318c2ecf20Sopenharmony_ci	} else if (__div64_const32_is_OK &&		\
2328c2ecf20Sopenharmony_ci		   __builtin_constant_p(__base) &&	\
2338c2ecf20Sopenharmony_ci		   __base != 0) {			\
2348c2ecf20Sopenharmony_ci		uint32_t __res_lo, __n_lo = (n);	\
2358c2ecf20Sopenharmony_ci		(n) = __div64_const32(n, __base);	\
2368c2ecf20Sopenharmony_ci		/* the remainder can be computed with 32-bit regs */ \
2378c2ecf20Sopenharmony_ci		__res_lo = (n);				\
2388c2ecf20Sopenharmony_ci		__rem = __n_lo - __res_lo * __base;	\
2398c2ecf20Sopenharmony_ci	} else if (likely(((n) >> 32) == 0)) {		\
2408c2ecf20Sopenharmony_ci		__rem = (uint32_t)(n) % __base;		\
2418c2ecf20Sopenharmony_ci		(n) = (uint32_t)(n) / __base;		\
2428c2ecf20Sopenharmony_ci	} else 						\
2438c2ecf20Sopenharmony_ci		__rem = __div64_32(&(n), __base);	\
2448c2ecf20Sopenharmony_ci	__rem;						\
2458c2ecf20Sopenharmony_ci })
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#else /* BITS_PER_LONG == ?? */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci# error do_div() does not yet support the C64
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci#endif /* BITS_PER_LONG */
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci#endif /* _ASM_GENERIC_DIV64_H */
254