Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4858471imu; Sat, 19 Jan 2019 21:10:26 -0800 (PST) X-Google-Smtp-Source: ALg8bN7SEOoFJyBWeRQ5UyKmYYXRwYcM+h99R2XHj0omIIYPvSvsIOCon0lXQ/qTIUfw2+kvM8ZJ X-Received: by 2002:a62:ca05:: with SMTP id n5mr25530131pfg.154.1547961026036; Sat, 19 Jan 2019 21:10:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547961026; cv=none; d=google.com; s=arc-20160816; b=sCvsHyF/RWct7NgkufzKu15s2H+zngocs9oRiPzds7WxOFA09z271qOR8rzTtq9igQ GpVABXUMk136VdbP1iaGDOoD50NRG4l19g6SA9yaA8YGo6CNLXKIAgZ05nBDHnnAA176 401zJR/3r9lbUSdctpom5shrWqBaLdwUliVXCUn7ezYawNx88i5hvalxhh1ZT/pBqjVj sVa3JtecpYlscWSaSTROqTR3BnhDOApPro01mGgS66YMMWizHN/lvos4wVl25J7dQ3Dj WnTD18PKCegEJN1HfwFrFJ1Obi6g+ADSvgjssRBuFsgFq9FdsPPWzE+J9ddKr0+Y/LhJ 4DZQ== 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=Zc70GkTYoCB6FM0EHkmD0aG16ESc2gl8eV2Wkz2pga4=; b=DfL4RANBhjE/TYipCZIuwPNJ9MlbpPqIWFo+bwzKqqCDonE9pCrgovcXPHIfuWB0x5 kt1kZgeYT8vIuk//hhjC+OGY0cJhk1IkDYBocqN98u3K0+mYzPOCfWKZQwTBwfsNcEd3 VEzjJlJCGyjgntUc6K/6fGv/qX3UOKO6t5tFYxu4NSiOdNlDXBkdqw7jZEHgHFfz2pjC TKRY8xJwNAatexi9lWAv7sgO8UjhRu4mi8fL9+mDTf6tN6PpwT9IkJZd7OcQhfaExNOS n/0cqhq/cCBxuYa526BM3XwwRK5yxNSpzRDQ6qXgKk0VlcJjdvBU98bVkr2BrMh0cP5U vtbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@googlemail.com header.s=20161025 header.b=o7Rn7CgC; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a2si9600519pfb.166.2019.01.19.21.09.38; Sat, 19 Jan 2019 21:10:26 -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=@googlemail.com header.s=20161025 header.b=o7Rn7CgC; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726071AbfATFDz (ORCPT + 99 others); Sun, 20 Jan 2019 00:03:55 -0500 Received: from mail-pf1-f175.google.com ([209.85.210.175]:34778 "EHLO mail-pf1-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725923AbfATFDy (ORCPT ); Sun, 20 Jan 2019 00:03:54 -0500 Received: by mail-pf1-f175.google.com with SMTP id h3so8598363pfg.1 for ; Sat, 19 Jan 2019 21:03:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Zc70GkTYoCB6FM0EHkmD0aG16ESc2gl8eV2Wkz2pga4=; b=o7Rn7CgCT4bBol44KtIlEvH+gT3c/V7JWbj29lklvDxtJOvAycHwuGPVIGlzVxCcoK RxNQ/I0H2HO5nH6krrOsw6h+z1kbydjlnv24xW0QbpHqnHtp+I8BsehJdFa1dNDZo61d qDLhIudRLu9jDk+Zg5VYIRVDC9XRrsCra8WZzpQmb+O95dcCrrxBGv1zvQh5lbc4eIpS zx6FsnTu7R/X9vmqrsfs069/lzoltLxo2XKuUYu80nhbdFBJm4tGwuoAZMi9qwhUiDb4 6TXmDT3cLtvYHCbFF4A0DDeIgWMgvpctaDy5nrodO3V4RzurNkuZFD9hN8Rval/DNzcV 2akQ== 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=Zc70GkTYoCB6FM0EHkmD0aG16ESc2gl8eV2Wkz2pga4=; b=gkY6u3DsiH63djraGy8Ltswy328vt8klelusFkAUGqHnWccaDer1+q9EEmXxEv2mrb xF2Me1VkxC7kG3KHDlRsJdvxP51YDUHasJIgeBgUpRupTKG1bJ7F34U4VohnsUyfKYdk IBodaoqPUTXwIu7S2Sui3cwRmGD61rBiBkBCSJs8Os1qh9wlEKUZMhFoIn5v8s1sLIdy 1hd2Otk4uBeZh8uYJUhpLVwLxQxTJYkr1524nR2L1oWdqcU0Iq/xs6PSLcBeMKMzD6r/ Fev73HlGM3RLTmu0i3hmIgykXj/mPy6yh1UYEVBlVoooj88tlAQxLf+fdSJ7SS5H/7z/ ArRg== X-Gm-Message-State: AJcUukfdyb1+Or2XNl2zrlasW8qui3Rrm/3EnRz3Fpn6dePJD+S6WuOd buZ01+t9s0dfJCtBEIKx6xJfpMjQI6xbCAXGS1oroPzRfjI= X-Received: by 2002:a63:504d:: with SMTP id q13mr23893467pgl.319.1547960633805; Sat, 19 Jan 2019 21:03:53 -0800 (PST) MIME-Version: 1.0 References: <20190119151450.26879-1-Florian.LaRoche@googlemail.com> <20190120000138.GI26876@brain-police> In-Reply-To: From: Florian La Roche Date: Sun, 20 Jan 2019 06:03:17 +0100 Message-ID: Subject: Re: fix int_sqrt() for very large numbers To: Linus Torvalds Cc: Will Deacon , Linux List Kernel Mailing , Crt Mori , 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 Hello all, my comment said ffs(), but the code only uses fls() and that's what I meant. Am So., 20. Jan. 2019 um 04:49 Uhr schrieb Linus Torvalds : > 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. Right. No real bug, just not 100% correct code. > And as Will mentioned, the regular int_sqrt() looks perfectly fine, > and subtracting 1 from the __fls() return value would actually > _introduce_ a bug. I think no bug introduced as the code handling 0 and 1 is already done. For __fls() and fls64() I am actually using the folloing code: /* * fls - find last (most-significant) bit set * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. */ static __always_inline unsigned int flsl(unsigned long x) { return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0; } Please note the "_builtin_clzl()" instead of the "_builtin_clz()". The real bug is that we compute 1 to 64 for bit 0 to bit 63, whereas the algorithm expects 0 to 63 for the value of m. best regards, Florian La Roche