Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_MATH_H
0003 #define _LINUX_MATH_H
0004 
0005 #include <linux/types.h>
0006 #include <asm/div64.h>
0007 #include <uapi/linux/kernel.h>
0008 
0009 /*
0010  * This looks more complex than it should be. But we need to
0011  * get the type for the ~ right in round_down (it needs to be
0012  * as wide as the result!), and we want to evaluate the macro
0013  * arguments just once each.
0014  */
0015 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
0016 
0017 /**
0018  * round_up - round up to next specified power of 2
0019  * @x: the value to round
0020  * @y: multiple to round up to (must be a power of 2)
0021  *
0022  * Rounds @x up to next multiple of @y (which must be a power of 2).
0023  * To perform arbitrary rounding up, use roundup() below.
0024  */
0025 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
0026 
0027 /**
0028  * round_down - round down to next specified power of 2
0029  * @x: the value to round
0030  * @y: multiple to round down to (must be a power of 2)
0031  *
0032  * Rounds @x down to next multiple of @y (which must be a power of 2).
0033  * To perform arbitrary rounding down, use rounddown() below.
0034  */
0035 #define round_down(x, y) ((x) & ~__round_mask(x, y))
0036 
0037 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
0038 
0039 #define DIV_ROUND_DOWN_ULL(ll, d) \
0040     ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
0041 
0042 #define DIV_ROUND_UP_ULL(ll, d) \
0043     DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
0044 
0045 #if BITS_PER_LONG == 32
0046 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
0047 #else
0048 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
0049 #endif
0050 
0051 /**
0052  * roundup - round up to the next specified multiple
0053  * @x: the value to up
0054  * @y: multiple to round up to
0055  *
0056  * Rounds @x up to next multiple of @y. If @y will always be a power
0057  * of 2, consider using the faster round_up().
0058  */
0059 #define roundup(x, y) (                 \
0060 {                           \
0061     typeof(y) __y = y;              \
0062     (((x) + (__y - 1)) / __y) * __y;        \
0063 }                           \
0064 )
0065 /**
0066  * rounddown - round down to next specified multiple
0067  * @x: the value to round
0068  * @y: multiple to round down to
0069  *
0070  * Rounds @x down to next multiple of @y. If @y will always be a power
0071  * of 2, consider using the faster round_down().
0072  */
0073 #define rounddown(x, y) (               \
0074 {                           \
0075     typeof(x) __x = (x);                \
0076     __x - (__x % (y));              \
0077 }                           \
0078 )
0079 
0080 /*
0081  * Divide positive or negative dividend by positive or negative divisor
0082  * and round to closest integer. Result is undefined for negative
0083  * divisors if the dividend variable type is unsigned and for negative
0084  * dividends if the divisor variable type is unsigned.
0085  */
0086 #define DIV_ROUND_CLOSEST(x, divisor)(          \
0087 {                           \
0088     typeof(x) __x = x;              \
0089     typeof(divisor) __d = divisor;          \
0090     (((typeof(x))-1) > 0 ||             \
0091      ((typeof(divisor))-1) > 0 ||           \
0092      (((__x) > 0) == ((__d) > 0))) ?        \
0093         (((__x) + ((__d) / 2)) / (__d)) :   \
0094         (((__x) - ((__d) / 2)) / (__d));    \
0095 }                           \
0096 )
0097 /*
0098  * Same as above but for u64 dividends. divisor must be a 32-bit
0099  * number.
0100  */
0101 #define DIV_ROUND_CLOSEST_ULL(x, divisor)(      \
0102 {                           \
0103     typeof(divisor) __d = divisor;          \
0104     unsigned long long _tmp = (x) + (__d) / 2;  \
0105     do_div(_tmp, __d);              \
0106     _tmp;                       \
0107 }                           \
0108 )
0109 
0110 #define __STRUCT_FRACT(type)                \
0111 struct type##_fract {                   \
0112     __##type numerator;             \
0113     __##type denominator;               \
0114 };
0115 __STRUCT_FRACT(s16)
0116 __STRUCT_FRACT(u16)
0117 __STRUCT_FRACT(s32)
0118 __STRUCT_FRACT(u32)
0119 #undef __STRUCT_FRACT
0120 
0121 /*
0122  * Multiplies an integer by a fraction, while avoiding unnecessary
0123  * overflow or loss of precision.
0124  */
0125 #define mult_frac(x, numer, denom)(         \
0126 {                           \
0127     typeof(x) quot = (x) / (denom);         \
0128     typeof(x) rem  = (x) % (denom);         \
0129     (quot * (numer)) + ((rem * (numer)) / (denom)); \
0130 }                           \
0131 )
0132 
0133 #define sector_div(a, b) do_div(a, b)
0134 
0135 /**
0136  * abs - return absolute value of an argument
0137  * @x: the value.  If it is unsigned type, it is converted to signed type first.
0138  *     char is treated as if it was signed (regardless of whether it really is)
0139  *     but the macro's return type is preserved as char.
0140  *
0141  * Return: an absolute value of x.
0142  */
0143 #define abs(x)  __abs_choose_expr(x, long long,             \
0144         __abs_choose_expr(x, long,              \
0145         __abs_choose_expr(x, int,               \
0146         __abs_choose_expr(x, short,             \
0147         __abs_choose_expr(x, char,              \
0148         __builtin_choose_expr(                  \
0149             __builtin_types_compatible_p(typeof(x), char),  \
0150             (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
0151             ((void)0)))))))
0152 
0153 #define __abs_choose_expr(x, type, other) __builtin_choose_expr(    \
0154     __builtin_types_compatible_p(typeof(x),   signed type) ||   \
0155     __builtin_types_compatible_p(typeof(x), unsigned type),     \
0156     ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
0157 
0158 /**
0159  * reciprocal_scale - "scale" a value into range [0, ep_ro)
0160  * @val: value
0161  * @ep_ro: right open interval endpoint
0162  *
0163  * Perform a "reciprocal multiplication" in order to "scale" a value into
0164  * range [0, @ep_ro), where the upper interval endpoint is right-open.
0165  * This is useful, e.g. for accessing a index of an array containing
0166  * @ep_ro elements, for example. Think of it as sort of modulus, only that
0167  * the result isn't that of modulo. ;) Note that if initial input is a
0168  * small value, then result will return 0.
0169  *
0170  * Return: a result based on @val in interval [0, @ep_ro).
0171  */
0172 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
0173 {
0174     return (u32)(((u64) val * ep_ro) >> 32);
0175 }
0176 
0177 u64 int_pow(u64 base, unsigned int exp);
0178 unsigned long int_sqrt(unsigned long);
0179 
0180 #if BITS_PER_LONG < 64
0181 u32 int_sqrt64(u64 x);
0182 #else
0183 static inline u32 int_sqrt64(u64 x)
0184 {
0185     return (u32)int_sqrt(x);
0186 }
0187 #endif
0188 
0189 #endif  /* _LINUX_MATH_H */