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