0001
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
0011
0012
0013
0014
0015 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
0026
0027
0028
0029
0030
0031
0032
0033
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
0053
0054
0055
0056
0057
0058
0059 #define roundup(x, y) ( \
0060 { \
0061 typeof(y) __y = y; \
0062 (((x) + (__y - 1)) / __y) * __y; \
0063 } \
0064 )
0065
0066
0067
0068
0069
0070
0071
0072
0073 #define rounddown(x, y) ( \
0074 { \
0075 typeof(x) __x = (x); \
0076 __x - (__x % (y)); \
0077 } \
0078 )
0079
0080
0081
0082
0083
0084
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
0099
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
0123
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
0137
0138
0139
0140
0141
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
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
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