From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7059E385781F; Fri, 9 Oct 2020 14:59:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7059E385781F From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/94823] modulo arithmetic bug in random.tcc Date: Fri, 09 Oct 2020 14:59:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW 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: Message-ID: In-Reply-To: References: 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, 09 Oct 2020 14:59:33 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94823 --- Comment #7 from Jonathan Wakely --- Despite the code being correct, I think it would be better to hoist this br= anch out of the loop: if (__k =3D=3D 0) __r2 +=3D __s; else if (__k <=3D __s) __r2 +=3D __kn + _M_v[__k - 1]; else __r2 +=3D __kn; We can do the k=3D0 case first, where we know that (k+p)%n=3D=3Dp and (k+q)= %n=3D=3Dq, and we also know that for that first iteration begin[0]^begin[q]^begin[n-1]= is simply 0x8b8b8b8bu because every element has that value: // k =3D=3D 0, every element in [begin,end) equals 0x8b8b8b8bu { uint_least32_t __r1 =3D 1371501266u; uint_least32_t __r2 =3D __r1 + __s; __begin[__p] +=3D __r1; __begin[__q] +=3D __r2; __begin[0] =3D __r2; } The we can loop up to __s+1 for (size_t __k =3D 1; __k <=3D __s; ++__k) ... And then up to m for (size_t __k =3D __s + 1; __k < __m; ++__k) ... This unasks the question of whether begin[-1ul % n] is the right element or= has the right value.=