From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3E91D3858D38; Sun, 24 Mar 2024 14:23:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E91D3858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1711290216; bh=PZhHCRjS7gUys1Qy/gGRDBVh5rtJNbpffotYue9Fms4=; h=From:To:Subject:Date:From; b=qnnCRMAaBhmmbyM6113Mrg7TOOkn/gAW7rGSZ4ssqtyarR6GK8nZZhsPX1A24VMT0 xQvzKjZLvXFqMctmCEODD7aKijBEzK8J17jwgFGuT0tjt+A15xbHlkRhXHhCqHxhPt /WGkaTge7X4LIscp+DLiflLfQ3MRFSbLcSfRmEAA= From: "pali at kernel dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/114448] New: Roundup not optimized Date: Sun, 24 Mar 2024 14:23:35 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 13.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pali at kernel dot 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114448 Bug ID: 114448 Summary: Roundup not optimized Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: pali at kernel dot org Target Milestone: --- https://godbolt.org/z/4fPKGzs1M Straightforward code which round up unsigned number to the next multiply of= 4 is: (num % 4 =3D=3D 0) ? num : num + (4 - num % 4); gcc -O2 generates: mov edx, edi mov eax, edi and edx, -4 add edx, 4 test dil, 3 cmovne eax, edx ret This is not optimal and branch/test can be avoided by using double modulo: num + (4 - num % 4) % 4; for which gcc -O2 generates: mov eax, edi neg eax and eax, 3 add eax, edi ret Optimal implementation for round up 4 is using bithacks: (num + 3) & ~3; for which gcc -O2 generates: lea eax, [rdi+3] and eax, -4 ret=