Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752838AbdLULmq (ORCPT ); Thu, 21 Dec 2017 06:42:46 -0500 Received: from smtp-out6.electric.net ([192.162.217.185]:50644 "EHLO smtp-out6.electric.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751867AbdLULmn (ORCPT ); Thu, 21 Dec 2017 06:42:43 -0500 From: David Laight To: "'Crt Mori'" CC: Peter Zijlstra , Jonathan Cameron , 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" , Joe Perches Subject: RE: [PATCH v10 1/3] lib: Add strongly typed 64bit int_sqrt Thread-Topic: [PATCH v10 1/3] lib: Add strongly typed 64bit int_sqrt Thread-Index: AQHTeZ24vj4uJGdJVEmg6EfyY0tMJ6NMSiFAgAAfCS+AAAbEMIAADUYAgAEwoOA= Date: Thu, 21 Dec 2017 11:43:00 +0000 Message-ID: <95b9b2b52554410a85a9f10c7f5e8b13@AcuMS.aculab.com> References: <20171220142001.18161-1-cmo@melexis.com> <1c1d0ffa8ee140bf9adbc78f1559b1e8@AcuMS.aculab.com> <20171220160001.manjff26gfbjccsw@hirez.programming.kicks-ass.net> In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.33] Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 X-Outbound-IP: 156.67.243.126 X-Env-From: David.Laight@ACULAB.COM X-Proto: esmtps X-Revdns: X-HELO: AcuMS.aculab.com X-TLS: TLSv1.2:ECDHE-RSA-AES256-SHA384:256 X-Authenticated_ID: X-PolicySMART: 3396946, 3397078 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by mail.home.local id vBLBgoVO020983 Content-Length: 1266 Lines: 59 From: Crt Mori > Sent: 20 December 2017 17:30 > I did a quick run through unit tests for the sensor and the results > are way off > ... Try this version instead: unsigned int sqrt64(unsigned long long x_in) { unsigned int x = x_in >> 32; unsigned int b = 0; unsigned int y = 0; unsigned int i; i = 31; if (!x) { x = x_in; i = 15; } if (!(x & 0xffff0000)) { x <<= 16; i -= 8; } if (!(x & 0xff000000)) { x <<= 8; i -= 4; } if (!(x & 0xf0000000)) { x <<= 4; i -= 2; } do { b <<= 2; b |= x >> 30; x <<= 2; if (i == 16) x = x_in; y <<= 1; if (b > y) { b -= ++y; y++; } } while (--i); /* 'b' becomes 33 bits if the input is greater than 2^62 */ b <<= 1; b |= x >> 31; if (b > y || (b == y && x & (1u << 30))) y |= 1; return y; } I've tested that one with more values. David