From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C0B88388A42C; Fri, 11 Dec 2020 11:28:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0B88388A42C From: "amonakov at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/98226] Slow std::countr_one Date: Fri, 11 Dec 2020 11:28:39 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: amonakov at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Dec 2020 11:28:39 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98226 Alexander Monakov changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |amonakov at gcc dot gnu.org --- Comment #10 from Alexander Monakov --- (In reply to Oleg Zaikin from comment #6) > There > we have a function firstzero(unsigned x) that returns 2^i, with i the > position of the first 0 of x, and 0 iff there is no 0. Its implementation= is: > unsigned firstzero(const unsigned x) noexcept { > #if __cplusplus > 201703L > return x =3D=3D unsigned(-1) ? 0 : unsigned(1) << std::countr_one(x); > #else > const unsigned y =3D x+1; return (y ^ x) & y; > #endif > } But why you are trying to use a more complex branchy expression in C++17 mo= de when you already have a more efficient expression as a "fallback"? Note that a cheaper way is available: return (x+1) & ~x; (though gcc can optimize '(y ^ x) & y' you have to the same machine code)=