Received: by 10.223.176.5 with SMTP id f5csp1367611wra; Sun, 4 Feb 2018 02:23:16 -0800 (PST) X-Google-Smtp-Source: AH8x2253GduoGlF33B54cBM9Nffq2u6DlzRuciAEHDkY3BSsdctEaZpQfIU5Wh2UDt6MXXIDNFnu X-Received: by 2002:a17:902:328:: with SMTP id 37-v6mr34420598pld.398.1517739796399; Sun, 04 Feb 2018 02:23:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517739796; cv=none; d=google.com; s=arc-20160816; b=MHCLpX9B4YDXExtJlU2r8Jb4BXp/Zuz5YZ463NUyU41tpZnnKWTidPDfZN4myOoxpz PVYB63DbI/4uniNe4PrXCrDlAwguumnwZBMmMnWmWaR/L6nj6T40JWte7UJrBrgi4jn+ H6kzb4u0Eel9+13Jhs2QuCmKBlvzicfLqOxZ+3fDo3oLyRvXjzpkPaXOIS1f5RIcRJf3 VETHKbdfJqDsyQTNN+CgsVq3f4CMkubtzrOj8vK1ELepXUcFw6KputZO0vFZkbcvDdNH qsbqtBD6flbkChSbTV97RCHmSrHeENb6qqt0Ic9eb51NXeMoeWW/l+n4hlxeAQHC0OCx byEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dmarc-filter:arc-authentication-results; bh=SSbl0OQnz2VU8PSlB/jnqe+MuCOvKp61Ab3Tedmiblw=; b=iEibfQICDS1WyzYf4csb5M8tEAZneznxC0LhWRACjy3l+9kAtTwlvtauaziZBlwDzC TSEJl2G7qtaQS65TNfsvs30xUU0PIRYeOPAoI5uSAcEQM2Dgcxq5ML95xxZSC/ZVNy2g /YvLWX31WETP0S91nsLoSZ+0mwoa789iWW7dtcGIH8sk5CF+wPi+/Z3ENuecdpjaJ7sw Ii5ck6tcCjnUiFEfY7zkht56aJNNQb8FWJqW6s9yBmO6vgQovpZ2MhqM0EIcpkwSip0H b2OU4JKw7OYRuZfTDwnHuXZC5P5e+SBP99EgPjwv/p30e1R6Z+/hxj7Ho+bkPVeOiJgC xyhA== ARC-Authentication-Results: i=1; mx.google.com; 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 v186si5020907pfb.284.2018.02.04.02.23.01; Sun, 04 Feb 2018 02:23:16 -0800 (PST) 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; 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 S1751818AbeBDKUy (ORCPT + 99 others); Sun, 4 Feb 2018 05:20:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:49468 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751015AbeBDKUt (ORCPT ); Sun, 4 Feb 2018 05:20:49 -0500 Received: from archlinux (cpc91196-cmbg18-2-0-cust659.5-4.cable.virginm.net [81.96.234.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 653AD217AF; Sun, 4 Feb 2018 10:20:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 653AD217AF Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=jic23@kernel.org Date: Sun, 4 Feb 2018 10:20:43 +0000 From: Jonathan Cameron To: Crt Mori Cc: Ingo Molnar , Andrew Morton , Kees Cook , Rusty Russell , Ian Abbott , Larry Finger , Niklas Soderlund , Thomas Gleixner , Krzysztof Kozlowski , Masahiro Yamada , linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Peter Zijlstra , Joe Perches , David Laight Subject: Re: [PATCH v13 1/3] lib: Add strongly typed 64bit int_sqrt Message-ID: <20180204102043.39516d75@archlinux> In-Reply-To: <20180111101957.27290-1-cmo@melexis.com> References: <20180111101957.27290-1-cmo@melexis.com> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 11 Jan 2018 11:19:57 +0100 Crt Mori wrote: > There is no option to perform 64bit integer sqrt on 32bit platform. > Added stronger typed int_sqrt64 enables the 64bit calculations to > be performed on 32bit platforms. Using same algorithm as int_sqrt() > with strong typing provides enough precision also on 32bit platforms, > but it sacrifices some performance. In case values are smaller than > ULONG_MAX the standard int_sqrt is used for calculation to maximize the > performance due to more native calculations. > > Signed-off-by: Crt Mori Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it. Thanks all for input on this and to Crt for persistence! Jonathan > --- > include/linux/kernel.h | 9 +++++++++ > lib/int_sqrt.c | 31 +++++++++++++++++++++++++++++++ > 2 files changed, 40 insertions(+) > > diff --git a/include/linux/kernel.h b/include/linux/kernel.h > index 0ad4c3044cf9..e4a3dc64e650 100644 > --- a/include/linux/kernel.h > +++ b/include/linux/kernel.h > @@ -460,6 +460,15 @@ extern int func_ptr_is_kernel_text(void *ptr); > > unsigned long int_sqrt(unsigned long); > > +#if BITS_PER_LONG < 64 > +u32 int_sqrt64(u64 x); > +#else > +static inline u32 int_sqrt64(u64 x) > +{ > + return (u32)int_sqrt(x); > +} > +#endif > + > extern void bust_spinlocks(int yes); > extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ > extern int panic_timeout; > diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c > index 1ef4cc344977..77386bc611d0 100644 > --- a/lib/int_sqrt.c > +++ b/lib/int_sqrt.c > @@ -7,6 +7,7 @@ > > #include > #include > +#include > > /** > * int_sqrt - rough approximation to sqrt > @@ -36,3 +37,33 @@ unsigned long int_sqrt(unsigned long x) > return y; > } > EXPORT_SYMBOL(int_sqrt); > + > +#if BITS_PER_LONG < 64 > +/** > + * int_sqrt64 - strongly typed int_sqrt function when minimum 64 bit input > + * is expected. > + * @x: 64bit integer of which to calculate the sqrt > + */ > +u32 int_sqrt64(u64 x) > +{ > + u64 b, m, y = 0; > + > + if (x <= ULONG_MAX) > + return int_sqrt((unsigned long) x); > + > + m = 1ULL << (fls64(x) & ~1ULL); > + while (m != 0) { > + b = y + m; > + y >>= 1; > + > + if (x >= b) { > + x -= b; > + y += m; > + } > + m >>= 2; > + } > + > + return y; > +} > +EXPORT_SYMBOL(int_sqrt64); > +#endif