Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_GENERIC_DIV64_H
0003 #define _ASM_GENERIC_DIV64_H
0004 /*
0005  * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
0006  * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
0007  *
0008  * Optimization for constant divisors on 32-bit machines:
0009  * Copyright (C) 2006-2015 Nicolas Pitre
0010  *
0011  * The semantics of do_div() is, in C++ notation, observing that the name
0012  * is a function-like macro and the n parameter has the semantics of a C++
0013  * reference:
0014  *
0015  * uint32_t do_div(uint64_t &n, uint32_t base)
0016  * {
0017  *  uint32_t remainder = n % base;
0018  *  n = n / base;
0019  *  return remainder;
0020  * }
0021  *
0022  * NOTE: macro parameter n is evaluated multiple times,
0023  *       beware of side effects!
0024  */
0025 
0026 #include <linux/types.h>
0027 #include <linux/compiler.h>
0028 
0029 #if BITS_PER_LONG == 64
0030 
0031 /**
0032  * do_div - returns 2 values: calculate remainder and update new dividend
0033  * @n: uint64_t dividend (will be updated)
0034  * @base: uint32_t divisor
0035  *
0036  * Summary:
0037  * ``uint32_t remainder = n % base;``
0038  * ``n = n / base;``
0039  *
0040  * Return: (uint32_t)remainder
0041  *
0042  * NOTE: macro parameter @n is evaluated multiple times,
0043  * beware of side effects!
0044  */
0045 # define do_div(n,base) ({                  \
0046     uint32_t __base = (base);               \
0047     uint32_t __rem;                     \
0048     __rem = ((uint64_t)(n)) % __base;           \
0049     (n) = ((uint64_t)(n)) / __base;             \
0050     __rem;                          \
0051  })
0052 
0053 #elif BITS_PER_LONG == 32
0054 
0055 #include <linux/log2.h>
0056 
0057 /*
0058  * If the divisor happens to be constant, we determine the appropriate
0059  * inverse at compile time to turn the division into a few inline
0060  * multiplications which ought to be much faster.
0061  *
0062  * (It is unfortunate that gcc doesn't perform all this internally.)
0063  */
0064 
0065 #define __div64_const32(n, ___b)                    \
0066 ({                                  \
0067     /*                              \
0068      * Multiplication by reciprocal of b: n / b = n * (p / b) / p   \
0069      *                              \
0070      * We rely on the fact that most of this code gets optimized    \
0071      * away at compile time due to constant propagation and only    \
0072      * a few multiplication instructions should remain.     \
0073      * Hence this monstrous macro (static inline doesn't always \
0074      * do the trick here).                      \
0075      */                             \
0076     uint64_t ___res, ___x, ___t, ___m, ___n = (n);          \
0077     uint32_t ___p, ___bias;                     \
0078                                     \
0079     /* determine MSB of b */                    \
0080     ___p = 1 << ilog2(___b);                    \
0081                                     \
0082     /* compute m = ((p << 64) + b - 1) / b */           \
0083     ___m = (~0ULL / ___b) * ___p;                   \
0084     ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b;    \
0085                                     \
0086     /* one less than the dividend with highest result */        \
0087     ___x = ~0ULL / ___b * ___b - 1;                 \
0088                                     \
0089     /* test our ___m with res = m * x / (p << 64) */        \
0090     ___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32; \
0091     ___t = ___res += (___m & 0xffffffff) * (___x >> 32);        \
0092     ___res += (___x & 0xffffffff) * (___m >> 32);           \
0093     ___t = (___res < ___t) ? (1ULL << 32) : 0;          \
0094     ___res = (___res >> 32) + ___t;                 \
0095     ___res += (___m >> 32) * (___x >> 32);              \
0096     ___res /= ___p;                         \
0097                                     \
0098     /* Now sanitize and optimize what we've got. */         \
0099     if (~0ULL % (___b / (___b & -___b)) == 0) {         \
0100         /* special case, can be simplified to ... */        \
0101         ___n /= (___b & -___b);                 \
0102         ___m = ~0ULL / (___b / (___b & -___b));         \
0103         ___p = 1;                       \
0104         ___bias = 1;                        \
0105     } else if (___res != ___x / ___b) {             \
0106         /*                          \
0107          * We can't get away without a bias to compensate   \
0108          * for bit truncation errors.  To avoid it we'd need an \
0109          * additional bit to represent m which would overflow   \
0110          * a 64-bit variable.                   \
0111          *                          \
0112          * Instead we do m = p / b and n / b = (n * m + m) / p. \
0113          */                         \
0114         ___bias = 1;                        \
0115         /* Compute m = (p << 64) / b */             \
0116         ___m = (~0ULL / ___b) * ___p;               \
0117         ___m += ((~0ULL % ___b + 1) * ___p) / ___b;     \
0118     } else {                            \
0119         /*                          \
0120          * Reduce m / p, and try to clear bit 31 of m when  \
0121          * possible, otherwise that'll need extra overflow  \
0122          * handling later.                  \
0123          */                         \
0124         uint32_t ___bits = -(___m & -___m);         \
0125         ___bits |= ___m >> 32;                  \
0126         ___bits = (~___bits) << 1;              \
0127         /*                          \
0128          * If ___bits == 0 then setting bit 31 is  unavoidable. \
0129          * Simply apply the maximum possible reduction in that  \
0130          * case. Otherwise the MSB of ___bits indicates the \
0131          * best reduction we should apply.          \
0132          */                         \
0133         if (!___bits) {                     \
0134             ___p /= (___m & -___m);             \
0135             ___m /= (___m & -___m);             \
0136         } else {                        \
0137             ___p >>= ilog2(___bits);            \
0138             ___m >>= ilog2(___bits);            \
0139         }                           \
0140         /* No bias needed. */                   \
0141         ___bias = 0;                        \
0142     }                               \
0143                                     \
0144     /*                              \
0145      * Now we have a combination of 2 conditions:           \
0146      *                              \
0147      * 1) whether or not we need to apply a bias, and       \
0148      *                              \
0149      * 2) whether or not there might be an overflow in the cross    \
0150      *    product determined by (___m & ((1 << 63) | (1 << 31))).   \
0151      *                              \
0152      * Select the best way to do (m_bias + m * n) / (1 << 64).  \
0153      * From now on there will be actual runtime code generated. \
0154      */                             \
0155     ___res = __arch_xprod_64(___m, ___n, ___bias);          \
0156                                     \
0157     ___res /= ___p;                         \
0158 })
0159 
0160 #ifndef __arch_xprod_64
0161 /*
0162  * Default C implementation for __arch_xprod_64()
0163  *
0164  * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
0165  * Semantic:  retval = ((bias ? m : 0) + m * n) >> 64
0166  *
0167  * The product is a 128-bit value, scaled down to 64 bits.
0168  * Assuming constant propagation to optimize away unused conditional code.
0169  * Architectures may provide their own optimized assembly implementation.
0170  */
0171 static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
0172 {
0173     uint32_t m_lo = m;
0174     uint32_t m_hi = m >> 32;
0175     uint32_t n_lo = n;
0176     uint32_t n_hi = n >> 32;
0177     uint64_t res;
0178     uint32_t res_lo, res_hi, tmp;
0179 
0180     if (!bias) {
0181         res = ((uint64_t)m_lo * n_lo) >> 32;
0182     } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
0183         /* there can't be any overflow here */
0184         res = (m + (uint64_t)m_lo * n_lo) >> 32;
0185     } else {
0186         res = m + (uint64_t)m_lo * n_lo;
0187         res_lo = res >> 32;
0188         res_hi = (res_lo < m_hi);
0189         res = res_lo | ((uint64_t)res_hi << 32);
0190     }
0191 
0192     if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
0193         /* there can't be any overflow here */
0194         res += (uint64_t)m_lo * n_hi;
0195         res += (uint64_t)m_hi * n_lo;
0196         res >>= 32;
0197     } else {
0198         res += (uint64_t)m_lo * n_hi;
0199         tmp = res >> 32;
0200         res += (uint64_t)m_hi * n_lo;
0201         res_lo = res >> 32;
0202         res_hi = (res_lo < tmp);
0203         res = res_lo | ((uint64_t)res_hi << 32);
0204     }
0205 
0206     res += (uint64_t)m_hi * n_hi;
0207 
0208     return res;
0209 }
0210 #endif
0211 
0212 #ifndef __div64_32
0213 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
0214 #endif
0215 
0216 /* The unnecessary pointer compare is there
0217  * to check for type safety (n must be 64bit)
0218  */
0219 # define do_div(n,base) ({              \
0220     uint32_t __base = (base);           \
0221     uint32_t __rem;                 \
0222     (void)(((typeof((n)) *)0) == ((uint64_t *)0));  \
0223     if (__builtin_constant_p(__base) &&     \
0224         is_power_of_2(__base)) {            \
0225         __rem = (n) & (__base - 1);     \
0226         (n) >>= ilog2(__base);          \
0227     } else if (__builtin_constant_p(__base) &&  \
0228            __base != 0) {           \
0229         uint32_t __res_lo, __n_lo = (n);    \
0230         (n) = __div64_const32(n, __base);   \
0231         /* the remainder can be computed with 32-bit regs */ \
0232         __res_lo = (n);             \
0233         __rem = __n_lo - __res_lo * __base; \
0234     } else if (likely(((n) >> 32) == 0)) {      \
0235         __rem = (uint32_t)(n) % __base;     \
0236         (n) = (uint32_t)(n) / __base;       \
0237     } else {                    \
0238         __rem = __div64_32(&(n), __base);   \
0239     }                       \
0240     __rem;                      \
0241  })
0242 
0243 #else /* BITS_PER_LONG == ?? */
0244 
0245 # error do_div() does not yet support the C64
0246 
0247 #endif /* BITS_PER_LONG */
0248 
0249 #endif /* _ASM_GENERIC_DIV64_H */