From owner-freebsd-bugs@freebsd.org Sun Feb 7 16:17:17 2021 Return-Path: Delivered-To: freebsd-bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 9355553094A for ; Sun, 7 Feb 2021 16:17:17 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mailman.nyi.freebsd.org (unknown [127.0.1.3]) by mx1.freebsd.org (Postfix) with ESMTP id 4DYZ713c93z4nSY for ; Sun, 7 Feb 2021 16:17:17 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: by mailman.nyi.freebsd.org (Postfix) id 7BCD0530949; Sun, 7 Feb 2021 16:17:17 +0000 (UTC) Delivered-To: bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 7B987530B7E for ; Sun, 7 Feb 2021 16:17:17 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DYZ7130tKz4nSX for ; Sun, 7 Feb 2021 16:17:17 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5A7642731C for ; Sun, 7 Feb 2021 16:17:17 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 117GHHVN081623 for ; Sun, 7 Feb 2021 16:17:17 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 117GHHZU081622 for bugs@FreeBSD.org; Sun, 7 Feb 2021 16:17:17 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 253313] lib/msun: hypotl(3) mishandles subnormal numbers Date: Sun, 07 Feb 2021 16:17:17 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: bin X-Bugzilla-Version: CURRENT X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Some People X-Bugzilla-Who: dim@FreeBSD.org X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org 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: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 07 Feb 2021 16:17:17 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D253313 --- Comment #2 from Dimitry Andric --- The most minimal fix I could come up with is: diff --git a/lib/msun/src/e_hypotl.c b/lib/msun/src/e_hypotl.c index 9189b1fab54d..c66d2246c8e2 100644 --- a/lib/msun/src/e_hypotl.c +++ b/lib/msun/src/e_hypotl.c @@ -82,8 +82,8 @@ hypotl(long double x, long double y) man_t manh, manl; GET_LDBL_MAN(manh,manl,b); if((manh|manl)=3D=3D0) return a; - t1=3D0; - SET_HIGH_WORD(t1,ESW(MAX_EXP-2)); /* t1=3D2^(MAX_EXP-= 2) */ + /* t1=3D2^(MAX_EXP-2) (or maybe just t1=3D0x1p+16382 ?) */ + INSERT_LDBL80_WORDS(t1,ESW(MAX_EXP-2),0x8000000000000000); b *=3D t1; a *=3D t1; k -=3D MAX_EXP-2; diff --git a/lib/msun/src/math_private.h b/lib/msun/src/math_private.h index b91b54cea689..200a734f1233 100644 --- a/lib/msun/src/math_private.h +++ b/lib/msun/src/math_private.h @@ -272,7 +272,7 @@ do {=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20 \ #define INSERT_LDBL80_WORDS(d,ix0,ix1) \ do { \ - union IEEEl2bits iw_u; \ + volatile union IEEEl2bits iw_u; \ iw_u.xbits.expsign =3D (ix0); \ iw_u.xbits.man =3D (ix1); \ (d) =3D iw_u.e; \ Giving as output: Via scaling and sqrtl x=3D2.853684e-4932 y=3D1.444012e-4922 a=3D1.444012e-4922 x=3D0x1.b2933cafa0bb7p-16383 y=3D0x1.fffffffffffffp-16351 a=3D0x1.000000006ca4c72cp-16350 Via hypotl x=3D2.853684e-4932 y=3D1.444012e-4922 z=3D1.444012e-4922 x=3D0x1.b2933cafa0bb7p-16383 y=3D0x1.fffffffffffffp-16351 z=3D0x1.fffffffffffffp-16351 This consists of two parts: the first being that the "t1=3D0; SET_HIGH_WORD(t1,ESW(MAX_EXP-2));" statements result in a long double that printf's as 0x1p+16382, but has the high part of its mantissa set to 0. Thi= s is different from the 'real' 0x1p+16382 constant, which has high part of the mantissa set to 0x80000000 instead. E.g. compare this with glibc's implementation (https://sourceware.org/git/?p=3Dglibc.git;a=3Dblob;f=3Dsysdeps/ieee754/ldb= l-96/e_hypotl.c): 85 if(__builtin_expect(eb < 0x20bf, 0)) { /* b < 2**-8000 */ 86 if(eb =3D=3D 0) { /* subnormal b or 0 */ 87 uint32_t exp __attribute__ ((unused)); 88 uint32_t high,low; 89 GET_LDOUBLE_WORDS(exp,high,low,b); 90 if((high|low)=3D=3D0) return a; 91 SET_LDOUBLE_WORDS(t1, 0x7ffd, 0x80000000, 0); /* t1=3D2^16382 */ 92 b *=3D t1; 93 a *=3D t1; 94 k -=3D 16382; The second part is the addition of volatile to the INSERT_LDBL80_WORDS macr= o. This is basically a hack to force the compile to not optimize away the undefined behavior or reading and writing from different union fields. It should really be fixed more properly, and for all these macros, but that is much more invasive. (Note that the second part isn't needed for gcc, as it appears to avoid optimizing around this type of union field access.) --=20 You are receiving this mail because: You are the assignee for the bug.=