From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4C23A3858417; Mon, 30 Aug 2021 08:07:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C23A3858417 From: "changyp6 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/102124] New: GCC 11.2.1 -ftree-loop-vectorize Causing Data To Lose Sign Bit Date: Mon, 30 Aug 2021 08:07:16 +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.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: changyp6 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 attachments.created 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: Mon, 30 Aug 2021 08:07:16 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102124 Bug ID: 102124 Summary: GCC 11.2.1 -ftree-loop-vectorize Causing Data To Lose Sign Bit Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: changyp6 at gmail dot com Target Milestone: --- Created attachment 51374 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D51374&action=3Dedit Test program for gcc 11.2.1 on AARCH64 Description of problem: When I'm building libgcrypt 1.9.4 with GCC 11.2.1 on my AARCH64 box(armv8.2 cortex-a76), I'm using -O3 compile options. however, -O3 generated code fai= led to pass "basic" test of libgcrypt, it fails on "gcry_cipher_checktag" funct= ion. After investigation, I found that, the problem occurs in buf_eq_const() function in file cipher/bufhelp.h of libgcrypt-1.9.4 362 /* Constant-time compare of two buffers. Returns 1 if buffers are equ= al, 363 and 0 if buffers differ. */ 364 static inline int 365 buf_eq_const(const void *_a, const void *_b, size_t len) 366 { 367 const byte *a =3D _a; 368 const byte *b =3D _b; 369 int ab, ba; 370 size_t i; 371=20 372 /* Constant-time compare. */ 373 for (i =3D 0, ab =3D 0, ba =3D 0; i < len; i++) 374 { 375 /* If a[i] !=3D b[i], either ab or ba will be negative. */ 376 ab |=3D a[i] - b[i]; 377 ba |=3D b[i] - a[i]; 378 } 379=20 380 /* 'ab | ba' is negative when buffers are not equal. */ 382 return (ab | ba) >=3D 0; 383 } The calculation of 2 different array becomes >=3D 0 on the return value, ho= wever, it should be negative value. After I change -O3 to -O2, this function works again. Then I compile libgcrypt 1.9.4 with -O2 plus additional GCC options which a= re added by -O3 to locate the actual option that causing this issue, finally I found that, if "-ftree-loop-vectorize" is used to compile this code, the calculated result is a positive value, if removing "-ftree-loop-vectorize",= the calculated result is negative. Then I downgraded GCC to 10.x, -ftree-loop-vectorize won't cause such issue. So I'm sure this is a GCC bug. It seems that "-ftree-loop-vectorize" causing "|=3D" operation ignore the "= sign bit" of "a[i] - b[i]" or "b[i] - a[i]". I have summarized a test-case, which is attached I have also compiled a cross-toolchain, using gcc git version (98e482761b083dbc35ae59704ee1eeb0b8eeb5d1), which is also gcc 11.2.1, this = git version also has such issue. Version-Release number of selected component (if applicable): GCC 11.2.1 Additional Info: GCC 11 for x86 / x86_64 doesn't have such issue. GCC 10.x for aarch64 doesn't have such issue. I have also submitted this bug to Fedora bugzilla https://bugzilla.redhat.com/show_bug.cgi?id=3D1998964=