From: Haozhong Zhang Subject: [PATCH v4 05/10] x86: Add functions for 64-bit integer arithmetic Date: Mon, 18 Jan 2016 05:58:54 +0800 Message-ID: <1453067939-9121-6-git-send-email-haozhong.zhang@intel.com> References: <1453067939-9121-1-git-send-email-haozhong.zhang@intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1453067939-9121-1-git-send-email-haozhong.zhang@intel.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org, Jan Beulich , Boris Ostrovsky , Kevin Tian Cc: Haozhong Zhang , Keir Fraser , Suravee Suthikulpanit , Andrew Cooper , Aravind Gopalakrishnan , Jun Nakajima List-Id: xen-devel@lists.xenproject.org This patch adds several functions to take multiplication, division and shifting involving 64-bit integers. Signed-off-by: Haozhong Zhang Reviewed-by: Boris Ostrovsky --- Changes in v4: (addressing Jan Beulich's comments) * Rewrite mul_u64_u64_shr() in assembly. xen/include/asm-x86/math64.h | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 xen/include/asm-x86/math64.h diff --git a/xen/include/asm-x86/math64.h b/xen/include/asm-x86/math64.h new file mode 100644 index 0000000..2ddec62 --- /dev/null +++ b/xen/include/asm-x86/math64.h @@ -0,0 +1,47 @@ +#ifndef __X86_MATH64 +#define __X86_MATH64 + +/* + * (a * mul) / divisor + * + * This function is derived from Linux kernel + * (mul_u64_u32_div() in include/linux/math64.h) + */ +static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor) +{ + union { + u64 ll; + struct { + u32 low, high; + } l; + } u, rl, rh; + + u.ll = a; + rl.ll = (u64)u.l.low * mul; + rh.ll = (u64)u.l.high * mul + rl.l.high; + + /* Bits 32-63 of the result will be in rh.l.low. */ + rl.l.high = do_div(rh.ll, divisor); + + /* Bits 0-31 of the result will be in rl.l.low. */ + do_div(rl.ll, divisor); + + rl.l.high = rh.l.low; + return rl.ll; +} + +/* + * (a * mul) >> n + */ +static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int n) +{ + u64 hi, lo; + + asm volatile ( "mulq %2; shrdq %1,%0" + : "=a" (lo), "=d" (hi) + : "rm" (mul), "0" (a), "c" (n) ); + + return lo; +} + +#endif -- 2.7.0