From owner-freebsd-numerics@freebsd.org Sat Nov 16 21:14:24 2019 Return-Path: Delivered-To: freebsd-numerics@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 7CE631AE676 for ; Sat, 16 Nov 2019 21:14:24 +0000 (UTC) (envelope-from sgk@troutmask.apl.washington.edu) Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.95.76.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "troutmask", Issuer "troutmask" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 47Fnz22zKwz4fvN; Sat, 16 Nov 2019 21:14:21 +0000 (UTC) (envelope-from sgk@troutmask.apl.washington.edu) Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.15.2/8.15.2) with ESMTPS id xAGLEJ6X040078 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 16 Nov 2019 13:14:20 -0800 (PST) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.15.2/8.15.2/Submit) id xAGLEJjE040077; Sat, 16 Nov 2019 13:14:19 -0800 (PST) (envelope-from sgk) Date: Sat, 16 Nov 2019 13:14:19 -0800 From: Steve Kargl To: Dimitry Andric Cc: freebsd-numerics@freebsd.org Subject: Re: UB in various hypot() implementations (left-shifting a negative, number) Message-ID: <20191116211419.GA40056@troutmask.apl.washington.edu> Reply-To: sgk@troutmask.apl.washington.edu References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.2 (2019-09-21) X-Rspamd-Queue-Id: 47Fnz22zKwz4fvN X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of sgk@troutmask.apl.washington.edu has no SPF policy when checking 128.95.76.21) smtp.mailfrom=sgk@troutmask.apl.washington.edu X-Spamd-Result: default: False [-1.19 / 15.00]; ARC_NA(0.00)[]; HAS_REPLYTO(0.00)[sgk@troutmask.apl.washington.edu]; NEURAL_HAM_MEDIUM(-0.88)[-0.885,0]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-0.98)[-0.983,0]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[washington.edu]; REPLYTO_ADDR_EQ_FROM(0.00)[]; AUTH_NA(1.00)[]; IP_SCORE(-0.22)[ip: (0.06), ipnet: 128.95.0.0/16(-0.28), asn: 73(-0.83), country: US(-0.05)]; RCPT_COUNT_TWO(0.00)[2]; R_SPF_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:73, ipnet:128.95.0.0/16, country:US]; MID_RHS_MATCH_FROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-numerics@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Discussions of high quality implementation of libm functions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Nov 2019 21:14:24 -0000 On Sat, Nov 16, 2019 at 09:40:40PM +0100, Dimitry Andric wrote: > On 2019-11-16 18:02:29 UTC, Steve Kargl wrote: > > The patch for hypotl() is likely unneeded as there is no shifting > > of a negative integer in the code being changed. Does csqrt_test.c > > pass without Jeff's patch? > > Yes, it passes. > > > This is the original code > > > > u_int32_t high; > > t1 = 1.0; > > GET_HIGH_WORD(high,t1); > > SET_HIGH_WORD(t1,high+DESW(k)); > > > > high + DESW(k) = high + k > > = 16383 + k > > > > and this is the code after the patch > > > > t1 = 0.0; > > SET_HIGH_WORD(t1,ESW(k)); > > > > ESW(k) = MAX_EXP - 1 + k > > = LDBL_MAX_EXP - 1 + k > > = 16384 - 1 + k > > = 16383 + k > > > > So, in principle there is no functional change. > > What about t1 changing from 1.0 to 0? If I revert just that line, all > tests do pass, including the csqrt test. > Well, clearly, the patch to e_hypotl.c is wrong. It clear the significand when t1 = 0 whereas t1 = 1 leaves one bit set in the significand. Simply looking at the value of t1 under a poor man's debugger shows the difference. Adding "printf("%Le %La\n", t1, t1);" after the SET_HIGH_WORD gives 2.962347e-2493 0x1p-8280 <-- t1 = 1 0.000000e+00 0x1p-8280 <-- t1 = 0 for hypotl(ldexpl(1.1,-16000), ldexpl(2.1, -16000)). -- Steve 20170425 https://www.youtube.com/watch?v=VWUpyCsUKR4 20161221 https://www.youtube.com/watch?v=IbCHE-hONow