Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4981497imu; Sun, 20 Jan 2019 00:34:29 -0800 (PST) X-Google-Smtp-Source: ALg8bN6Ru4fncMzgs4HGr0K5LPrU73euCK5Th5t/R5FvNEIVUhtuVYHA39baO2bQV4SG3saSjUl4 X-Received: by 2002:a62:c42:: with SMTP id u63mr25254148pfi.73.1547973269300; Sun, 20 Jan 2019 00:34:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547973269; cv=none; d=google.com; s=arc-20160816; b=pzpQMPbXiBsimJXz1w7KbTydbEnUFhj35oMuENZk4hrURanS6QBC2Mcr6bEjYxHOGA 1LkHOTYWdpkfLtK4yarB9S1O8MB3TUcetc5CKYKN+UePr3P95APHDhMCsl8eh4hZl4wW A8WjjUwzrpQlyeg6YKEBb0yY5k9J+yK1V2aCbj9iMvyGB9M+g5O7unCF0sJ2MImb2DXV TLT+g4nWIa6ANv71J85ZAOEhW1ChYqMUkddqFwjiAE4/ybgN56vG7CwIWkifuXREddvX cP2gEY5oAGVIvMQBtOxlpffMdbzqhceILSB4AmPgIIH/0QWfaAW5kPDKB0T99cm25PFE pFXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=vuGdsjMedZbRP27gfuWy7CNl4LFB73NmQrOD2muDJh8=; b=vLzcV6XkB5hAuLrjjTW5A9vVe4EuTJDpB22HT+LGeiJX4W6edBh/rYca2NO+LKUHEf nfT3Gx7vQIqEaiI3mCpEcGImsujLII5hVMllVTAOJQB6riIE2OARNpBX4GQlhFm1rAH/ CF4py37QtSLqRV/XOiVptgmH33KKSI5nR1NbWRK8HM+jQyDgrKqGgg9o5QHuFbXjEJ3a 7VAmWexYIo5xtkIpctuyX2ocRW6U1Mc3kx7UZSpK66FRzGqUj0nHB5nTxyhZsp/GJUhb Ci0Fu6z9hU/iIynsF5hKYKTUeIQ4UPz8BUdPrC7U/02L0Q4pY87Un4i/HE0YnSlNVqcS Ufvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@melexis.com header.s=google header.b=DUlHyDdv; 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 s36si9481307pld.46.2019.01.20.00.34.01; Sun, 20 Jan 2019 00:34:29 -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; dkim=pass header.i=@melexis.com header.s=google header.b=DUlHyDdv; 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 S1730215AbfATIcS (ORCPT + 99 others); Sun, 20 Jan 2019 03:32:18 -0500 Received: from mail-vs1-f47.google.com ([209.85.217.47]:42110 "EHLO mail-vs1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727294AbfATIcR (ORCPT ); Sun, 20 Jan 2019 03:32:17 -0500 Received: by mail-vs1-f47.google.com with SMTP id b74so10915189vsd.9 for ; Sun, 20 Jan 2019 00:32:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=melexis.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vuGdsjMedZbRP27gfuWy7CNl4LFB73NmQrOD2muDJh8=; b=DUlHyDdvZdIPrufeMKyRsCp5G3yyAbT5hTj7iSXivI+bG4OXl47ttg9E3w8rcfGlZ5 /8q/XRfNkFrcaw2XXBFJOJd7piGI0dTgikIzy5J5eRzxDg3EPCGf69He+ACXUeR0LUkb Vhl0EjXvegpZbX+WAbac7pPliSFU2V+HF0DvvsFUA1nNbcxeqllAgtTQL1HkAvNv+0dS VL8AnFB/Su52r4NNz0+0zHY1pl6Bs0C+BJL/Dv4J/ACyjagbRjobRpMbfFVNYB5+cHDk //79MvTpwpPEAmqk23hk8W31t+PqVVMbP34v0MWZGWKfvzjQjFJru0jbmfNMYLZzCka0 CI7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vuGdsjMedZbRP27gfuWy7CNl4LFB73NmQrOD2muDJh8=; b=BfiwpGZfRyXqEvJoLpH+kLCuqtnWD/qNoeofXOjWeO0Lr7BTs2uss024YENgHGH3ie mmAum7xKR5vvZRKtl01QJ7O26y8u5ZK6Vx+5n8miavOm+M/aphytb0Rz0f+o0JE9s3qm gcnKYDIJvDkT4dXfkUrhdMnYamDcgq553cJzJGoGs7/QxvAgf7OhL7DNUJWvZ8Q1yDyf VWNuLLtjK88xGVPaMLIGRE9vZbCw5kA52MfTAo4MIX3lBmIDcoo5Xz5NtAhZz+JC4sNF WtnW6mkxfQlXe5qH0HM1vvvfTEGi2WqwQlQYCAZV9DbcVmRIp7NcG52IQ3oWmvtI53gt 5ntg== X-Gm-Message-State: AJcUukd247UyDsYC0mBBuuknPkk1FnI6it7l9iUSxz1agsEiUHS1onxD dxT+lmEJqXSLweeB7KqIq633Kvlbm8GfrcBqkvjw9w== X-Received: by 2002:a67:99c2:: with SMTP id b185mr10078491vse.84.1547973135935; Sun, 20 Jan 2019 00:32:15 -0800 (PST) MIME-Version: 1.0 References: <20190119151450.26879-1-Florian.LaRoche@googlemail.com> <20190120000138.GI26876@brain-police> In-Reply-To: From: Crt Mori Date: Sun, 20 Jan 2019 09:31:39 +0100 Message-ID: Subject: Re: fix int_sqrt() for very large numbers To: Linus Torvalds Cc: Will Deacon , Florian La Roche , Linux List Kernel Mailing , Joe Perches , Davidlohr Bueso , Peter Zijlstra Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 20 Jan 2019 at 04:49, Linus Torvalds wrote: > > On Sun, Jan 20, 2019 at 12:01 PM Will Deacon wrote: > > > > > @@ -52,7 +52,7 @@ u32 int_sqrt64(u64 x) > > > if (x <= ULONG_MAX) > > > return int_sqrt((unsigned long) x); > > > > > > - m = 1ULL << (fls64(x) & ~1ULL); > > > + m = 1ULL << ((fls64(x) - 1) & ~1ULL); > > > > This just looks like a copy-paste error because there isn't an __fls64(). > > But I think your suggestion here is ok, given the previous check against > > ULONG_MAX. > > Hmm. We probably *should* add a __fls64(). > > There looks to be only one user of int_sqrt64(), and that one is > confused. It does int_sqrt64() twice, but since the inner one will > reduce the range to 32 bits, the outer one is just silly. II have a usecase (mlx90632) where this calculation worked on arm64 (nexus), but not in normal 32-bit arm (beaglebone). I have tried going with full u64 to u64, but I was persuaded that it is not necessary and testing on black body (sensor range from 0 - 80 degrees) confirmed that for my calculations u32/u64 is enough. Because of the testing range (and keep in mind it is casted to signed after two sqrts) the high bit might never affect my end result, but I needed precision, not the range. Inside the function the b was 32bit on 32bit core, but I needed it to be 64bit. To keep it similar to existing int_sqrt, I have decided to just type all variables there to 64bit. We have implementation of this with doubles (see datasheet) and I ported it to integer on arm64. The end result was fairly similar calculation (for within object tempearture range from 0-80), between both. > That one user also had better not be overflowing into the high bit - > it uses "s64" as a type and does seem to use signed operatons, so high > bit set really means negative. sqrt() returning something odd for a > negative number wouldn't be all that odd in that context. > > But yes, our current int_sqrt64() does seem buggy as-is, because it's > *supposed* to work on u64's, even if I don't think we really have any > users that care. I introduced strong types for existing int_sqrt implementation to keep it aligned between 64bit and 32bit. Best regards, Crt > And as Will mentioned, the regular int_sqrt() looks perfectly fine, > and subtracting 1 from the __fls() return value would actually > _introduce_ a bug. > > Linus