Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1072020ybl; Wed, 28 Aug 2019 09:13:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqzR5eh/aHfu5RhxUjS+eqJt4/8DED8qafWDR2Ms/Ka3SlaehIFx/inLSSAm0GC+2VQ+epmq X-Received: by 2002:a17:902:7b97:: with SMTP id w23mr5100990pll.283.1567008835179; Wed, 28 Aug 2019 09:13:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567008835; cv=none; d=google.com; s=arc-20160816; b=gmKUlc+1I2zQII9pECKDnuQZJFpVWvSjFRu0Sy4rRp8U1zITZ8qsdkJ9pSs185Bpkd B10IhKoRJTIg/aILZJhKtMxqIWFPLv70W3SV0eiDSDrolz49n7d+YzH076suTk64QGil DAxCxZH9yC7xuMqv9vUffFqYmbcAhsrHwiHa9jujQ/+aKRcWEUOyQL2XJDyq0wZWQiIb q7tltTEyKHGCQ4Rbrv+2+MPZ0wwKBILR+GPxGrgv64g3sxj7nMfqc9t07NyV7O2pK0nq Mh2AMwuaPV8MC/xelg1EcO87ZMO/hdNXxf0mDQjlEP39+vZB9IfE2cpMgQYLdI9r8u1B KW0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=jiaOVki4EodwE0pYAnmxrX9Htp9sDWrdGfGGhC7eBQw=; b=YN2/TZY02jeOLD/1XggDzPQcLG3Msq/e87tQqw+bJHdqzg1UjaUIPGu3r5OR8x3thw khW7x8oKipdSuwPuoN/bZhIqPlbVZPkf5k+tCWyjeQ2yIEu+917RCcqKo9LdcYl+gSop t745+5smi6BYY/73iTvsDIVeK9tyadeQZqy/FrHw3x4+ZygxBNKOicP/jMYuq3J4B5pW CE9lGyhCuv0zM7bQ3MESSCsOYIbnwBc06UyCuIHE90k5j0PAfPFzPrEky8yJDIs219SR 64ZPG+4sxU+XmodoEF2wpXVyTwgAR1y3nN3l4m4fvK3NE6MgpxkFbg8d1NqSB14Oj1oP G8aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=1p8mfq1h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x3si2209654pll.127.2019.08.28.09.13.38; Wed, 28 Aug 2019 09:13:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=1p8mfq1h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726520AbfH1QLb (ORCPT + 99 others); Wed, 28 Aug 2019 12:11:31 -0400 Received: from merlin.infradead.org ([205.233.59.134]:49258 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726315AbfH1QLa (ORCPT ); Wed, 28 Aug 2019 12:11:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=jiaOVki4EodwE0pYAnmxrX9Htp9sDWrdGfGGhC7eBQw=; b=1p8mfq1heH7aRTZG74Nc5HL2d fMsHfpOqwz9b5/qasGWHkaD3BbtTroe83h+nKjZ6LMOwkil6b4dZ+t242vxJ3RZlaol1WuAe38MJe +WjUjT5qKa8nw4qvGty8MNmpyEbixidllMivvTePfoV8QhxIl0CBf1J7+wxhLYbKvYnYF/NRUaK5H SvF4O2/po0SQjUVqCKxEhi0Xy/9YOe0RCERHvQAPork+fS9XNwwYfI9rDfTQDRDlDLDptIpLECM9w cIUsUL6TEGoZKiNwNSmC5DFdNwhJIUNT7WOqdSdkGABZox6XA0dATArsuy16FLcma9NUTAvMT7oK4 1QIiKHk4Q==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1i30Xd-0005iz-2x; Wed, 28 Aug 2019 16:11:25 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 483CB3074C6; Wed, 28 Aug 2019 18:10:49 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 3F6BD20230B2E; Wed, 28 Aug 2019 18:11:23 +0200 (CEST) Date: Wed, 28 Aug 2019 18:11:23 +0200 From: Peter Zijlstra To: kan.liang@linux.intel.com Cc: acme@kernel.org, mingo@redhat.com, linux-kernel@vger.kernel.org, tglx@linutronix.de, jolsa@kernel.org, eranian@google.com, alexander.shishkin@linux.intel.com, ak@linux.intel.com, x86@kernel.org Subject: [PATCH] x86/math64: Provide a sane mul_u64_u32_div() implementation for x86_64 Message-ID: <20190828161123.GQ2386@hirez.programming.kicks-ass.net> References: <20190826144740.10163-1-kan.liang@linux.intel.com> <20190826144740.10163-4-kan.liang@linux.intel.com> <20190828151921.GD17205@worktop.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190828151921.GD17205@worktop.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 28, 2019 at 05:19:21PM +0200, Peter Zijlstra wrote: > On Mon, Aug 26, 2019 at 07:47:35AM -0700, kan.liang@linux.intel.com wrote: > > + return mul_u64_u32_div(slots, val, 0xff); > > But also; x86_64 seems to lack a sane implementation of that function, > and it currently compiles into utter crap (it can be 2 instructions). --- Subject: x86/math64: Provide a sane mul_u64_u32_div() implementation for x86_64 From: Peter Zijlstra Date: Wed Aug 28 17:39:46 CEST 2019 On x86_64 we can do a u64 * u64 -> u128 widening multiply followed by a u128 / u64 -> u64 division to implement a sane version of mul_u64_u32_div(). Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/div64.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) --- a/arch/x86/include/asm/div64.h +++ b/arch/x86/include/asm/div64.h @@ -73,6 +73,19 @@ static inline u64 mul_u32_u32(u32 a, u32 #else # include + +static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 div) +{ + u64 q; + + asm ("mulq %2; divq %3" : "=a" (q) + : "a" (a), "rm" (mul), "rm" (div) + : "rdx"); + + return q; +} +#define mul_u64_u32_div mul_u64_u32_div + #endif /* CONFIG_X86_32 */ #endif /* _ASM_X86_DIV64_H */