1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __VDSO_MATH64_H 3 #define __VDSO_MATH64_H 4 5 #include <linux/compiler.h> 6 7 static __always_inline u32 __iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder) 8 { 9 u32 ret = 0; 10 11 while (dividend >= divisor) { 12 /* 13 * Prevent the compiler from optimising this loop into a 14 * modulo operation. 15 */ 16 OPTIMIZER_HIDE_VAR(dividend); 17 18 dividend -= divisor; 19 ret++; 20 } 21 22 *remainder = dividend; 23 24 return ret; 25 } 26 27 static __always_inline u32 __iter_div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) 28 { 29 u32 ret = 0; 30 31 while (dividend >= divisor) { 32 /* 33 * Prevent the compiler from optimising this loop into a 34 * modulo operation. 35 */ 36 OPTIMIZER_HIDE_VAR(dividend); 37 38 dividend -= divisor; 39 ret++; 40 } 41 42 *remainder = dividend; 43 44 return ret; 45 } 46 47 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__) 48 49 #ifndef mul_u64_u32_add_u64_shr 50 static __always_inline u64 mul_u64_u32_add_u64_shr(u64 a, u32 mul, u64 b, unsigned int shift) 51 { 52 return (u64)((((unsigned __int128)a * mul) + b) >> shift); 53 } 54 #endif /* mul_u64_u32_add_u64_shr */ 55 56 #else 57 58 #ifndef mul_u64_u32_add_u64_shr 59 #ifndef mul_u32_u32 60 static inline u64 mul_u32_u32(u32 a, u32 b) 61 { 62 return (u64)a * b; 63 } 64 #define mul_u32_u32 mul_u32_u32 65 #endif 66 static __always_inline u64 mul_u64_u32_add_u64_shr(u64 a, u32 mul, u64 b, unsigned int shift) 67 { 68 u32 ah = a >> 32, al = a; 69 bool ovf; 70 u64 ret; 71 72 ovf = __builtin_add_overflow(mul_u32_u32(al, mul), b, &ret); 73 ret >>= shift; 74 if (ovf && shift) 75 ret += 1ULL << (64 - shift); 76 if (ah) 77 ret += mul_u32_u32(ah, mul) << (32 - shift); 78 79 return ret; 80 } 81 #endif /* mul_u64_u32_add_u64_shr */ 82 83 #endif 84 85 #endif /* __VDSO_MATH64_H */ 86