From owner-freebsd-toolchain@freebsd.org Sun Mar 13 20:04:16 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 4DFA5ACFC21 for ; Sun, 13 Mar 2016 20:04:16 +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 15244107B for ; Sun, 13 Mar 2016 20:04:15 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from [IPv6:2001:7b8:3a7::7d53:d1bf:838d:a3e3] (unknown [IPv6:2001:7b8:3a7:0:7d53:d1bf:838d:a3e3]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id 7B8CF6B67; Sun, 13 Mar 2016 21:04:07 +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=_11C2F5B6-8463-491B-A91C-A51E76493731"; protocol="application/pgp-signature"; micalg=pgp-sha1 X-Pgp-Agent: GPGMail 2.6b2 (ebbf3ef) From: Dimitry Andric In-Reply-To: <20160313182521.GA25361@troutmask.apl.washington.edu> Date: Sun, 13 Mar 2016 21:03:57 +0100 Cc: freebsd-toolchain@freebsd.org Message-Id: <74970883-FE44-47C0-BDA0-92DB0723398A@FreeBSD.org> References: <20160313182521.GA25361@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: Sun, 13 Mar 2016 20:04:16 -0000 --Apple-Mail=_11C2F5B6-8463-491B-A91C-A51E76493731 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 13 Mar 2016, at 19:25, Steve Kargl = wrote: >=20 > Consider this small piece of code: >=20 > #include > #include >=20 > float > foo() > { > static const volatile float tiny =3D 1.e-30f; > return (tiny * tiny); > } >=20 > int > main(void) > { > float x; > feclearexcept(FE_ALL_EXCEPT); > x =3D foo(); > if (fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW: "); > printf("x =3D %e\n", x); > return 0; > } >=20 > clang seems to get the underflow condition wrong. >=20 > % cc -o z a.c -lm && ./z > FE_UNDERFLOW: x =3D 0.000000e+00 >=20 > % cc -O -o z a.c -lm && ./z > x =3D 1.000000e-60 <--- This is not a possible value! >=20 > % gcc -o z a.c -lm && ./z > FE_UNDERFLOW: x =3D 0.000000e+00 >=20 > % gcc -O -o z a.c -lm && ./z > FE_UNDERFLOW: x =3D 0.000000e+00 Hmm, this is an interesting one. On amd64, it works as expected with clang, but there it always uses SSE, obviously: $ ./underflow-amd64 FE_UNDERFLOW: x =3D 0.000000e+00 The problem seems to be caused by the intermediate result being stored using fstpl instead of fstps, e.g. simplifying the sample program (to get rid of all the SSE stuff the fexxx() macros insert): int main(void) { float x; __uint16_t status; __fnclex(); x =3D foo(); __fnstsw(&status); printf("status: %#x\n", (unsigned)status); printf("x =3D %e\n", x); return 0; } With gcc, the assembly becomes: foo: flds tiny.1853 flds tiny.1853 fmulp %st, %st(1) ret [...] main: [...] fnclex call foo fstps 12(%esp) fnstsw %ax In this case, fmulp does not generate an underflow, but the fstps will. With clang, the assembly becomes: foo: flds foo.tiny fmuls foo.tiny retl [...] main: subl $24, %esp fnclex calll foo fstpl 12(%esp) # 8-byte Folded Spill fnstsw 22(%esp) So it's storing the intermediate result in a double, for some reason. The fnstsw will then result in zero, since there was no underflow at that point. I will submit a bug for this upstream, thanks for the report. -Dimitry --Apple-Mail=_11C2F5B6-8463-491B-A91C-A51E76493731 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 iEYEARECAAYFAlblx7YACgkQsF6jCi4glqNZZwCg31aoDFrKkjMxWFME/QNTcQAB 45gAniBh/gkRojA0mnSTGFXO2XyRoZor =GVRB -----END PGP SIGNATURE----- --Apple-Mail=_11C2F5B6-8463-491B-A91C-A51E76493731--