From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 0D5C8385703A; Fri, 6 Nov 2020 08:23:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0D5C8385703A From: "tkoenig at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/97738] New: Optimizing division by value & - value for HAKMEM 175 Date: Fri, 06 Nov 2020 08:23:11 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: tkoenig 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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, 06 Nov 2020 08:23:12 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97738 Bug ID: 97738 Summary: Optimizing division by value & - value for HAKMEM 175 Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: tkoenig at gcc dot gnu.org Target Milestone: --- A straightforward implementation of HAKMEM 175 (returning the next number with the same number of bits) is unsigned int next_same_bit (unsigned int value) { unsigned int lowest_bit; unsigned int left_bits; unsigned int changed_bits; unsigned int right_bits; lowest_bit =3D value & - value; left_bits =3D value + lowest_bit; changed_bits =3D value ^ left_bits; right_bits =3D (changed_bits / lowest_bit) >> 2; return left_bits | right_bits; } In two's complement, this can be replaced by unsigned int next_s_bit (unsigned int value) { unsigned int lowest_bit; unsigned int ctz; unsigned int left_bits; unsigned int changed_bits; unsigned int right_bits; ctz =3D __builtin_ctz (value); lowest_bit =3D 1u << ctz; left_bits =3D value + lowest_bit; changed_bits =3D value ^ left_bits; right_bits =3D changed_bits >> (ctz + 2); return left_bits | right_bits; } to replace the expensive division by what is known to be a power of two by a shift. That transformation is counter-productive (and might be done the other way) if there is no division by lowest_bit.=