From owner-freebsd-toolchain@freebsd.org Mon Mar 14 19:23:44 2016 Return-Path: Delivered-To: freebsd-toolchain@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4EC16AD1F8F for ; Mon, 14 Mar 2016 19:23:44 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from tensor.andric.com (tensor.andric.com [87.251.56.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "tensor.andric.com", Issuer "COMODO RSA Domain Validation Secure Server CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D4C537E7 for ; Mon, 14 Mar 2016 19:23:43 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from [IPv6:2001:7b8:3a7::a43f:7b3f:ecbf:43e5] (unknown [IPv6:2001:7b8:3a7:0:a43f:7b3f:ecbf:43e5]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id 4E0912FCE7; Mon, 14 Mar 2016 20:23:40 +0100 (CET) Subject: Re: clang gets numerical underflow wrong, please fix. Mime-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) Content-Type: multipart/signed; boundary="Apple-Mail=_31F93044-399F-4178-AEAC-61A60D68E98B"; protocol="application/pgp-signature"; micalg=pgp-sha1 X-Pgp-Agent: GPGMail 2.6b2 (ebbf3ef) From: Dimitry Andric In-Reply-To: <20160314015311.GA28237@troutmask.apl.washington.edu> Date: Mon, 14 Mar 2016 20:23:33 +0100 Cc: freebsd-toolchain@freebsd.org Message-Id: <65DA5D5E-6BEE-4522-9478-B659724F9CDC@FreeBSD.org> References: <20160313182521.GA25361@troutmask.apl.washington.edu> <74970883-FE44-47C0-BDA0-92DB0723398A@FreeBSD.org> <20160313201004.GA26343@troutmask.apl.washington.edu> <20160314015311.GA28237@troutmask.apl.washington.edu> To: Steve Kargl X-Mailer: Apple Mail (2.3112) X-BeenThere: freebsd-toolchain@freebsd.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Maintenance of FreeBSD's integrated toolchain List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Mar 2016 19:23:44 -0000 --Apple-Mail=_31F93044-399F-4178-AEAC-61A60D68E98B Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 14 Mar 2016, at 02:53, Steve Kargl = wrote: ... > #include > #include >=20 > int > main(void) > { > int i; > float x =3D 1.f; > i =3D 0; > feclearexcept(FE_ALL_EXCEPT); > do { > x *=3D 2; > i++; > printf("%d %e\n", i, x); > } while(!fetestexcept(FE_OVERFLOW)); > if (fetestexcept(FE_OVERFLOW)) printf("FE_UNDERFLOW: "); > printf("x =3D %e after %d iterations\n", x, i); >=20 > return 0; > } >=20 > You'll get a bunch of invalid output before the OVERFLOW. >=20 > % cc -O -o z b.c -lm && ./z | tail > 1016 7.022239e+305 <-- not a valid float > 1017 1.404448e+306 <-- not a valid float > 1018 2.808896e+306 <-- not a valid float > 1019 5.617791e+306 <-- not a valid float > 1020 1.123558e+307 <-- not a valid float > 1021 2.247116e+307 <-- not a valid float > 1022 4.494233e+307 <-- not a valid float > 1023 8.988466e+307 <-- not a valid float > 1024 inf > FE_UNDERFLOW: x =3D inf after 1024 iterations >=20 > Clang is broken with or without #pragma FENV_ACCESS "on". Well, it simply doesn't support that #pragma [1], just like gcc [2]. :-( Apparently compiler writers have trouble with this pragma, don't implement it, and assume that it's always off. Which then appears to make most (or all) fenv.h functions into undefined behavior. That said, making 'x' in your test case volatile helps, e.g. the main loop was: fadd %st(0), %st(0) fstl -20(%ebp) incl %esi movl %esi, 4(%esp) fstpl 8(%esp) movl $.L.str, (%esp) calll printf fnstsw -10(%ebp) and becomes: flds -16(%ebp) fadd %st(0), %st(0) fstps -16(%ebp) incl %esi flds -16(%ebp) fstpl 8(%esp) movl %esi, 4(%esp) movl $.L.str, (%esp) calll printf #APP fnstsw -10(%ebp) So the fstps causes an overflow when 128 iterations are reached: [...] 126 8.507059e+37 127 1.701412e+38 128 inf FE_UNDERFLOW: x =3D inf after 128 iterations Maybe this is a usable workaround for libm. -Dimitry [1] https://llvm.org/bugs/show_bug.cgi?id=3D8100 [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D34678 --Apple-Mail=_31F93044-399F-4178-AEAC-61A60D68E98B Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP using GPGMail -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.29 iEYEARECAAYFAlbnD7sACgkQsF6jCi4glqO1pgCfV451ofXJJz9udR4DzWyPpPrx gLkAoK1IOJCSYSGWEaTBBiQ8SUHtdW6u =F9zX -----END PGP SIGNATURE----- --Apple-Mail=_31F93044-399F-4178-AEAC-61A60D68E98B--