From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 231563858D32; Sun, 8 May 2022 04:08:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 231563858D32 From: "goswin-v-b at web dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/105521] New: missed optimization in modulo arithmetic Date: Sun, 08 May 2022 04:08:22 +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: 11.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: goswin-v-b at web dot de 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: Sun, 08 May 2022 04:08:23 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105521 Bug ID: 105521 Summary: missed optimization in modulo arithmetic Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: goswin-v-b at web dot de Target Milestone: --- I'm trying to compute (a*a)%n for uint64_t types on x86_64 using "gcc -O2 -W -Wall" like this: #include #include uint64_t sqrmod(uint64_t a, uint64_t n) { assert(a < n); unsigned __int128 x =3D a; x *=3D a; return x % n; } I expected to get the following code: sqrmod: cmpq %rsi, %rdi jnb .L13 // assert(a < n) failure movq %rdi, %rax mul %rdi div %rsi movq %rdx, %rax ret The compiler does get the "mul" right but instead of the "div" it throws in= a call to "__umodti3". The "__umodti3" function is horribly long code that wi= ll be worlds slower than a simple div. Note: The "asset(a < n);" should tell the compiler that the "div" instructi= on can not overflow and will not cause a #DivisionError. Without the assert the compiler could (conditionally) add "a %=3D n;" for the same effect. https://godbolt.org/z/cd57Wd4oo=