From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 47C543858038; Wed, 30 Dec 2020 14:03:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 47C543858038 From: "dabler at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/98479] New: Missed optimization opportunity for unsigned __int128 modulo Date: Wed, 30 Dec 2020 14:03:10 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 9.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dabler at gmail dot com 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: Wed, 30 Dec 2020 14:03:10 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98479 Bug ID: 98479 Summary: Missed optimization opportunity for unsigned __int128 modulo Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: dabler at gmail dot com Target Milestone: --- I have found that manually calculating the % operator on __int128 is significantly faster than the built-in compiler operator. I will show you h= ow to calculate modulo 9, but the method can be used to calculate modulo any o= ther number. First, consider the built-in compiler operator: uint64_t mod9_v1(unsigned __int128 n) { return n % 9; } Now consider my manual implementation: uint64_t mod9_v2(unsigned __int128 n) { uint64_t r =3D 0; r +=3D (uint32_t)(n); r +=3D (uint32_t)(n >> 32) * (uint64_t)4; r +=3D (uint32_t)(n >> 64) * (uint64_t)7; r +=3D (uint32_t)(n >> 96); return r % 9; } Measuring over 100,000,000 random numbers gives the following results: mod9_v1 | 3.986052 secs mod9_v2 | 1.814339 secs GCC 9.3.0 with -march=3Dnative -O3 was used on AMD Ryzen Threadripper 2990W= X. Note that 2^32 =3D=3D 4 (mod 9), 2^64 =3D=3D 7 (mod 9), etc.=