Commit e058a3ad authored by Zhang Yi's avatar Zhang Yi Committed by Long Li
Browse files

math64: add rem_u64() to just return the remainder

maillist inclusion
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I9VTE3
CVE: NA

Reference: https://lore.kernel.org/linux-fsdevel/20240529095206.2568162-1-yi.zhang@huaweicloud.com/



--------------------------------

Add a new helper rem_u64() to only get the remainder of unsigned 64bit
divide with 32bit divisor.

Fixes: fabcdd2d ("fs: xfs: Introduce FORCEALIGN inode flag")
Signed-off-by: default avatarZhang Yi <yi.zhang@huawei.com>
Signed-off-by: default avatarLong Li <leo.lilong@huawei.com>
parent b4141d36
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
#define _LINUX_MATH64_H

#include <linux/types.h>
#include <linux/log2.h>
#include <vdso/math64.h>
#include <asm/div64.h>

@@ -11,6 +12,20 @@
#define div64_long(x, y) div64_s64((x), (y))
#define div64_ul(x, y)   div64_u64((x), (y))

/**
 * rem_u64 - remainder of unsigned 64bit divide with 32bit divisor
 * @dividend: unsigned 64bit dividend
 * @divisor: unsigned 32bit divisor
 *
 * Return: dividend % divisor
 */
static inline u32 rem_u64(u64 dividend, u32 divisor)
{
	if (is_power_of_2(divisor))
		return dividend & (divisor - 1);
	return dividend % divisor;
}

/**
 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
 * @dividend: unsigned 64bit dividend
@@ -85,6 +100,15 @@ static inline s64 div64_s64(s64 dividend, s64 divisor)
#define div64_long(x, y) div_s64((x), (y))
#define div64_ul(x, y)   div_u64((x), (y))

#ifndef rem_u64
static inline u32 rem_u64(u64 dividend, u32 divisor)
{
	if (is_power_of_2(divisor))
		return dividend & (divisor - 1);
	return do_div(dividend, divisor);
}
#endif

#ifndef div_u64_rem
static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
{