From owner-freebsd-numerics@freebsd.org Tue Jan 1 05:14:48 2019 Return-Path: Delivered-To: freebsd-numerics@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 60D03148984C for ; Tue, 1 Jan 2019 05:14:48 +0000 (UTC) (envelope-from pfg@FreeBSD.org) Received: from sonic308-10.consmr.mail.gq1.yahoo.com (sonic308-10.consmr.mail.gq1.yahoo.com [98.137.68.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B11B77043C for ; Tue, 1 Jan 2019 05:14:47 +0000 (UTC) (envelope-from pfg@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1546319684; bh=daKsXulAQjTqXHVBa3qqyNGANBtrlHOWANep3fQvi+0=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From:Subject; b=o8aMGazHK3sjPvWrxTBfKLVPIXed5iazwQAIpK2VkQ4f8YjC9wDIKk792fPWXpgzNlEtxkGQ/vnWpTIsEzn9tMmPfUYn1icZrNjRa1vw1TLewKzFDR30xtJlMuV4IPJ6o+Ax4mmCYS8rK04PfanpybNhmH/+KqztnS+FhajqXpYpol46YSf2Cts6EqwsnrCM7FHDlXAr7n9qczHP6JoK0281sh39Dg+BGFu78MmhEBRUbKlXIfz7zue4SHGBNX+pX85K32sTxIE7AjGpsv9vaakRCHQGhE5TWtjOJXhzTbzphmn4U67IEGlfbYqXhbgTb+MLBvdtoAWEsFTPWs19Fg== X-YMail-OSG: Jx8utVkVM1lF07FWQcAl7C60aekPo7r62b4fjnDQuiNgHDwp_uFSj9jc9dY8DVy MLcMVLQNZ7GgrXBtLU6n18cAYM3x_.nX7VG1L6fy_JAoY_E3yl7qTB6.nKIG_L4KMgQbqF1JR8Wv QShbo9Wr6KqwOOXp2hv2YaB8_C5ty8RVIv30v3yNsdfMXL08oU6pOiw8d4UdlT_AoDy0aR7JvMD0 JKnwsav66fSY.KmI42r7teFlBx7vhpHwzA3i.Xc.3FJxW2Xz21YI4CvAPbiHVPNkUaf2TYN.AyVt C3s4YFxVoSz_ZW7cAVuyrclMgu3V_QK1J9O6orjR2SqkJ9rbYn.c85cQ3pfDALoLIYLlBjuJXhwv aG.42ZgA605WPP_s_iMFuvjXoAYzXFPZO7sTSsicBOyplTkVfSqBmlohw6BIpolzgiXcGAYPhftU fzgeQaf4.D_ei0GW4duL8NMjGaNa5BUA9y2O5A8TiL88dbLOXWhH2VE9rRT6pFt8L9DIpTeEj8k_ BrOlF1EP9MGehMxvBY5taKIDV_Ob5Qmw7GtqC9eBKPlGwaqQcU2YmSAOMD9Wl_OqX_3ZoTnb3ApM hIisQEYV3K6qN9zt1FGt5UKR4YHVPzVhjyVVTB1maRU4xmFojEqgrEgu7qTxQ2waZ6pEllo4nIYO AXLxifEpMxE5_n53kAIk8Y.3I8Pv7uxFqDozZm6pz1VEh9ZzyoiYVmlbDxFB5dWhWRbV5TaIyHxj qtf07zsiGgLAaQ358hxUdlw6CmlQ5z1Pg1hyD7usykgRjQjEOYJC8kpR1KbEwaPGnh.2QQlElBjq qJeKeGCkw0dgKdMdXXcLrTaiihf2BByhlF_MK5ibP1sfISK9KebzTIb2DcjVA9E36Jn7PJJp88TM koXZkfG083qi0JRLjFmaJgnrlNoAw3cJfhLs61CJ4dXgPJjGrTGXNoKyCHM32ara7vYG7RRxls0v xM2kzATg1sC8VlwxDCgnyFrrr7JIguqNS6t9aDL4rMHQ2.i79OwkOFjXmRoQuZZDxDZinuGLK19P 9sj9_d7sOM8WLCDbVEAldBvkJOday750keVz.oAU- Received: from sonic.gate.mail.ne1.yahoo.com by sonic308.consmr.mail.gq1.yahoo.com with HTTP; Tue, 1 Jan 2019 05:14:44 +0000 Received: from 181.52.72.201 (EHLO [192.168.0.5]) ([181.52.72.201]) by smtp427.mail.gq1.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID ecd8ae500f6c69bcb014abf6efa764dd; Tue, 01 Jan 2019 05:14:39 +0000 (UTC) Subject: Re: Undefined Behavior in lib/msun/src/e_pow.c (was Re: New math library from ARM) To: sgk@troutmask.apl.washington.edu Cc: freebsd-numerics@freebsd.org References: <797a7755-db93-1b9c-f3b9-8850d948e098@FreeBSD.org> <20181231151904.GB823@troutmask.apl.washington.edu> <20181231152230.GC823@troutmask.apl.washington.edu> <06c8b6a2-ed26-f255-3947-c79b593a9dea@FreeBSD.org> <20190101045425.GA5767@troutmask.apl.washington.edu> From: Pedro Giffuni Organization: FreeBSD Project Message-ID: Date: Tue, 1 Jan 2019 00:14:38 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190101045425.GA5767@troutmask.apl.washington.edu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Rspamd-Queue-Id: B11B77043C X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.96)[-0.964,0]; ASN(0.00)[asn:36647, ipnet:98.137.64.0/21, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] 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: Tue, 01 Jan 2019 05:14:48 -0000 On 31/12/2018 23:54, Steve Kargl wrote: > On Mon, Dec 31, 2018 at 10:32:06PM -0500, Pedro Giffuni wrote: >> Hmm ... >> >> Looking at the changes in musl's libc I found this issue which seems >> real (although somewhat theoretical): >> >> https://git.musl-libc.org/cgit/musl/commit/src/math?id=688d3da0f1730daddbc954bbc2d27cc96ceee04c >> >> Is the attached patch acceptable? >> >> Also, their code is bit different here: >> >> https://git.musl-libc.org/cgit/musl/commit/src/math?id=282b1cd26649d69de038111f5876853df6ddc345 >> >> but we may also have to check fmaf(-0x1.26524ep-54, -0x1.cb7868p+11, >> 0x1.d10f5ep-29). >> >> Cheers, >> >> Pedro. >> >> Index: lib/msun/src/e_pow.c >> =================================================================== >> --- lib/msun/src/e_pow.c (revision 342665) >> +++ lib/msun/src/e_pow.c (working copy) >> @@ -130,6 +130,7 @@ >> if(hx<0) { >> if(iy>=0x43400000) yisint = 2; /* even integer y */ >> else if(iy>=0x3ff00000) { >> + uint32_t j; /* Avoid UB in bit operations below. */ >> k = (iy>>20)-0x3ff; /* exponent */ >> if(k>20) { >> j = ly>>(52-k); > > I'll defer to Bruce on this. My only comments are > 1) declarations belong at the top of the file where all declarations occur Modern standards let you declare variables within blocks. For style we do prefer to start the function with them but in this case we can't. > 2) j is already declared as int32_t And it has to be a signed integer: we later check for negative values. > 3) uint32_t should be written as u_int32_t. No, uint32_t is the standard type, u_int32_t is a BSDism. Also, the file consistently uses uint32_t. > Are you sure that UB occurs? Or, is this an attempt to > placate a static analysis tool that only see shifting of > a signed type? Do you need to make a similar change to > e_powf.c? The point of the Undefined Behavior is precisely that it doesn't occur: we are lucky and the compiler behaves as it always has but the standard will let a different compiler behave differently and then we will have surprises.