From owner-svn-src-stable@freebsd.org Sat Sep 28 08:57:34 2019 Return-Path: Delivered-To: svn-src-stable@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 C1F6CFFB29; Sat, 28 Sep 2019 08:57:34 +0000 (UTC) (envelope-from dim@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46gMxV4Fg5z3yjZ; Sat, 28 Sep 2019 08:57:34 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4241D26F43; Sat, 28 Sep 2019 08:57:34 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x8S8vYPJ037186; Sat, 28 Sep 2019 08:57:34 GMT (envelope-from dim@FreeBSD.org) Received: (from dim@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x8S8vXDe037182; Sat, 28 Sep 2019 08:57:33 GMT (envelope-from dim@FreeBSD.org) Message-Id: <201909280857.x8S8vXDe037182@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: dim set sender to dim@FreeBSD.org using -f From: Dimitry Andric Date: Sat, 28 Sep 2019 08:57:33 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org Subject: svn commit: r352835 - in stable: 10/lib/msun/src 11/lib/msun/src 12/lib/msun/src 8/lib/msun/src 9/lib/msun/src X-SVN-Group: stable-8 X-SVN-Commit-Author: dim X-SVN-Commit-Paths: in stable: 10/lib/msun/src 11/lib/msun/src 12/lib/msun/src 8/lib/msun/src 9/lib/msun/src X-SVN-Commit-Revision: 352835 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Sep 2019 08:57:34 -0000 Author: dim Date: Sat Sep 28 08:57:29 2019 New Revision: 352835 URL: https://svnweb.freebsd.org/changeset/base/352835 Log: MFC r352710: Do not left-shift a negative number (inducing undefined behavior in C/C++) in exp(3), expf(3), expm1(3) and expm1f(3) during intermediate computations that compute the IEEE-754 bit pattern for |2**k| for integer |k|. The implementations of exp(3), expf(3), expm1(3) and expm1f(3) need to compute IEEE-754 bit patterns for 2**k in certain places. (k is an integer and 2**k is exactly representable in IEEE-754.) Currently they do things like 0x3FF0'0000+(k<<20), which is to say they take the bit pattern representing 1 and then add directly to the exponent field to get the desired power of two. This is fine when k is non-negative. But when k<0 (and certain classes of input trigger this), this left-shifts a negative number -- an operation with undefined behavior in C and C++. The desired semantics can be achieved by instead adding the possibly-negative k to the IEEE-754 exponent bias to get the desired exponent field, _then_ shifting that into its proper overall position. (Note that in case of s_expm1.c and s_expm1f.c, there are SET_HIGH_WORD and SET_FLOAT_WORD uses further down in each of these files that perform shift operations involving k, but by these points k's range has been restricted to 2 < k <= 56, and the shift operations under those circumstances can't do anything that would be UB.) Submitted by: Jeff Walden, https://github.com/jswalden Obtained from: https://github.com/freebsd/freebsd/pull/411 Obtained from: https://github.com/freebsd/freebsd/pull/412 Modified: stable/8/lib/msun/src/e_exp.c stable/8/lib/msun/src/e_expf.c stable/8/lib/msun/src/s_expm1.c stable/8/lib/msun/src/s_expm1f.c Directory Properties: stable/8/ (props changed) stable/8/lib/ (props changed) stable/8/lib/msun/ (props changed) Changes in other areas also in this revision: Modified: stable/10/lib/msun/src/e_exp.c stable/10/lib/msun/src/e_expf.c stable/10/lib/msun/src/s_expm1.c stable/10/lib/msun/src/s_expm1f.c stable/11/lib/msun/src/e_exp.c stable/11/lib/msun/src/e_expf.c stable/11/lib/msun/src/s_expm1.c stable/11/lib/msun/src/s_expm1f.c stable/12/lib/msun/src/e_exp.c stable/12/lib/msun/src/e_expf.c stable/12/lib/msun/src/s_expm1.c stable/12/lib/msun/src/s_expm1f.c stable/9/lib/msun/src/e_exp.c stable/9/lib/msun/src/e_expf.c stable/9/lib/msun/src/s_expm1.c stable/9/lib/msun/src/s_expm1f.c Directory Properties: stable/10/ (props changed) stable/11/ (props changed) stable/12/ (props changed) stable/9/ (props changed) stable/9/lib/ (props changed) stable/9/lib/msun/ (props changed) Modified: stable/8/lib/msun/src/e_exp.c ============================================================================== --- stable/8/lib/msun/src/e_exp.c Sat Sep 28 08:54:32 2019 (r352834) +++ stable/8/lib/msun/src/e_exp.c Sat Sep 28 08:57:29 2019 (r352835) @@ -143,9 +143,9 @@ __ieee754_exp(double x) /* default IEEE double exp */ /* x is now in primary range */ t = x*x; if(k >= -1021) - INSERT_WORDS(twopk,0x3ff00000+(k<<20), 0); + INSERT_WORDS(twopk,((u_int32_t)(0x3ff+k))<<20, 0); else - INSERT_WORDS(twopk,0x3ff00000+((k+1000)<<20), 0); + INSERT_WORDS(twopk,((u_int32_t)(0x3ff+(k+1000)))<<20, 0); c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); if(k==0) return one-((x*c)/(c-2.0)-x); else y = one-((lo-(x*c)/(2.0-c))-hi); Modified: stable/8/lib/msun/src/e_expf.c ============================================================================== --- stable/8/lib/msun/src/e_expf.c Sat Sep 28 08:54:32 2019 (r352834) +++ stable/8/lib/msun/src/e_expf.c Sat Sep 28 08:57:29 2019 (r352835) @@ -80,9 +80,9 @@ __ieee754_expf(float x) /* default IEEE double exp */ /* x is now in primary range */ t = x*x; if(k >= -125) - SET_FLOAT_WORD(twopk,0x3f800000+(k<<23)); + SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+k))<<23); else - SET_FLOAT_WORD(twopk,0x3f800000+((k+100)<<23)); + SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+(k+100)))<<23); c = x - t*(P1+t*P2); if(k==0) return one-((x*c)/(c-(float)2.0)-x); else y = one-((lo-(x*c)/((float)2.0-c))-hi); Modified: stable/8/lib/msun/src/s_expm1.c ============================================================================== --- stable/8/lib/msun/src/s_expm1.c Sat Sep 28 08:54:32 2019 (r352834) +++ stable/8/lib/msun/src/s_expm1.c Sat Sep 28 08:57:29 2019 (r352835) @@ -186,7 +186,7 @@ expm1(double x) e = hxs*((r1-t)/(6.0 - x*t)); if(k==0) return x - (x*e-hxs); /* c is 0 */ else { - INSERT_WORDS(twopk,0x3ff00000+(k<<20),0); /* 2^k */ + INSERT_WORDS(twopk,((u_int32_t)(0x3ff+k))<<20,0); /* 2^k */ e = (x*(e-c)-c); e -= hxs; if(k== -1) return 0.5*(x-e)-0.5; Modified: stable/8/lib/msun/src/s_expm1f.c ============================================================================== --- stable/8/lib/msun/src/s_expm1f.c Sat Sep 28 08:54:32 2019 (r352834) +++ stable/8/lib/msun/src/s_expm1f.c Sat Sep 28 08:57:29 2019 (r352835) @@ -92,7 +92,7 @@ expm1f(float x) e = hxs*((r1-t)/((float)6.0 - x*t)); if(k==0) return x - (x*e-hxs); /* c is 0 */ else { - SET_FLOAT_WORD(twopk,0x3f800000+(k<<23)); /* 2^k */ + SET_FLOAT_WORD(twopk,((u_int32_t)(0x7f+k))<<23); /* 2^k */ e = (x*(e-c)-c); e -= hxs; if(k== -1) return (float)0.5*(x-e)-(float)0.5;