From owner-freebsd-numerics@freebsd.org Sat Nov 16 15:03:37 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 445811C7263 for ; Sat, 16 Nov 2019 15:03:37 +0000 (UTC) (envelope-from lwhsu.freebsd@gmail.com) Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47FdlC3S84z4DlH for ; Sat, 16 Nov 2019 15:03:34 +0000 (UTC) (envelope-from lwhsu.freebsd@gmail.com) Received: by mail-yb1-f176.google.com with SMTP id i15so5296402ybq.0 for ; Sat, 16 Nov 2019 07:03:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=4GjbzrDvfH5reXrzeZ+IXM5mUKcm4dlTAQ2IcyDM1as=; b=pQn+TxWcobIVLzYMUmyVKUGva/DbspCtlBeBwArchEcj5xVJCKlRSONrCmVOiHVZ1C mTp1BydKcaA/gu1vvj+84hgtFYQrJZKz0Qiau2cWkZM4IppJ++Cy4hPj9d/CWqZBY+Mq sKYA6KbQdoY2AK8WMUacAsxNNJuy+SdnZ1kZfQPhpZCpPVAuknk4Emcv8hUGq2JSCloq 9E/9pg2f1Nw3lZ5QOQlrR5LR8yX98amMHAwz+Pz5RG+1emkE5XR/6tgU2Wb6+cFqLtWD eGL9Ho+eykwDuq4I8XtuwrGSHO61hie4N5XMZIXzn3HO+FXqUkkpdfFlVbdS+dnjhP2G eE/w== X-Gm-Message-State: APjAAAU7t+CfxGCtty1fmrJBTeTkRe7Y9hJWcZaO6mvOr9oUo35cNXbW v6JnzOKSsgzVma+DPjk9Aa5fYUezuXaL60PdJrk= X-Google-Smtp-Source: APXvYqwwCwfRLEsE32ILkSEfHhRfoYJCvLZ2gywSb51b0hWYvWtqyGpglfelzYKxcuIg1KQt/oZhmK4VM+s1vYrgd7k= X-Received: by 2002:a25:4b03:: with SMTP id y3mr16618595yba.497.1573916611845; Sat, 16 Nov 2019 07:03:31 -0800 (PST) MIME-Version: 1.0 References: <20191113232233.GA64847@troutmask.apl.washington.edu> In-Reply-To: <20191113232233.GA64847@troutmask.apl.washington.edu> From: Li-Wen Hsu Date: Sat, 16 Nov 2019 23:03:19 +0800 Message-ID: Subject: Re: UB in various hypot() implementations (left-shifting a negative number) To: sgk@troutmask.apl.washington.edu Cc: Jeff Walden , freebsd-numerics@freebsd.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 47FdlC3S84z4DlH X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of lwhsufreebsd@gmail.com designates 209.85.219.176 as permitted sender) smtp.mailfrom=lwhsufreebsd@gmail.com X-Spamd-Result: default: False [-4.78 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; RCVD_TLS_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17:c]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-numerics@freebsd.org]; DMARC_NA(0.00)[freebsd.org]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[176.219.85.209.list.dnswl.org : 127.0.5.0]; IP_SCORE(-2.78)[ip: (-8.69), ipnet: 209.85.128.0/17(-3.18), asn: 15169(-1.98), country: US(-0.05)]; FORGED_SENDER(0.30)[lwhsu@freebsd.org,lwhsufreebsd@gmail.com]; R_DKIM_NA(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US]; FROM_NEQ_ENVFROM(0.00)[lwhsu@freebsd.org,lwhsufreebsd@gmail.com]; TAGGED_FROM(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 15:03:37 -0000 I did a quick test, it seems one regression test fails: lwhsu@x1c:/usr/tests/lib/msun > kyua debug csqrt_test:main 1..18 ok 1 - csqrt ok 2 - csqrt ok 3 - csqrt ok 4 - csqrt ok 5 - csqrt ok 6 - csqrt ok 7 - csqrt ok 8 - csqrt ok 9 - csqrt ok 10 - csqrt ok 11 - csqrt ok 12 - csqrt ok 13 - csqrt ok 14 - csqrt ok 15 - csqrt ok 16 - csqrt Assertion failed: (creall(result) =3D=3D ldexpl(14 * 0x1p-4, exp / 2)), function test_overflow, file /usr/home/lwhsu/freebsd-src/lib/msun/tests/csqrt_test.c, line 236. Process with PID 18700 exited with signal 6 and dumped core; attempting to gather stack trace [New LWP 101179] Core was generated by `/usr/tests/lib/msun/csqrt_test'. Program terminated with signal SIGABRT, Aborted. #0 thr_kill () at thr_kill.S:3 3 RSYSCALL(thr_kill) #0 thr_kill () at thr_kill.S:3 #1 0x00000008004543a4 in __raise (s=3D6) at /usr/src/lib/libc/gen/raise.c:= 52 #2 0x00000008003c3029 in abort () at /usr/src/lib/libc/stdlib/abort.c:67 #3 0x0000000800440e71 in __assert (func=3D, file=3D, line=3D, failedexpr=3D) at /usr/src/lib/libc/gen/assert.c:51 #4 0x00000000002040bd in test_overflow (maxexp=3D16384) at /usr/home/lwhsu/freebsd-src/lib/msun/tests/csqrt_test.c:236 #5 0x0000000000202a91 in main () at /usr/home/lwhsu/freebsd-src/lib/msun/tests/csqrt_test.c:363 GDB exited successfully Files left in work directory after failure: csqrt_test.core csqrt_test:main -> broken: Received signal 6 I haven't checked the details, but I definitely need experts in this field to help. Thanks, Li-Wen On Thu, Nov 14, 2019 at 7:22 AM Steve Kargl wrote: > > Looks good to me. Just need to convince someone to commit it. > > -- > steve > > > On Wed, Nov 13, 2019 at 02:43:04PM -0800, Jeff Walden wrote: > > > > Just wanted to note here that I filed https://reviews.freebsd.org/D2235= 4 to fix a bit of undefined behavior in the hypot() function implementation= s. > > > > `hypot(x, y)` computes `sqrt(x*x + y*y)`, with IEEE-754-aware precision= . For very large or small `x` or `y`, the naive implementation would lose = precision -- so in such cases the calculation is performed after multiplyin= g the numbers by a very large (or very small) power of two, then the multip= lication is undone at end. > > > > Undoing the multiplication involves multiplying a quantity `w` by `2**k= `, where `k` (which may be positive or negative) was computed depending on = the particular `x` and `y` values provided. Current algorithms generally t= ake `t1=3D0.0`, extract the high word, add `k<<20` or `k<<23` to it to appr= opriately bump the exponent, then overwrite `t1`'s high word. But it seems= equally effective to take `t1=3D0.0`, then write `(1023+k)<<20` or `(127+k= )<<23` to it for identical effect -- and `k` is never so negative to comput= e a negative value. My changes do this. (I wish there were named constant= s I could use for all these numbers, but as there don't seem to be any, mag= ic numbers seems like the best I can do.) > > > > Errors in these changes would most likely produce a power of two off by= a factor of two, so *probably* testing any inputs that would happen to inv= oke these code paths should be adequate testing. I'm fixing this in order = to upstream a likely fix in the SpiderMonkey JavaScript engine for this, so= the (double, double) algorithm/change is the only one I have (purely manua= lly) tested. Eyeballs on the other functions' changes especially appreciat= ed! > > > > Jeff > > _______________________________________________ > > freebsd-numerics@freebsd.org mailing list > > https://lists.freebsd.org/mailman/listinfo/freebsd-numerics > > To unsubscribe, send any mail to "freebsd-numerics-unsubscribe@freebsd.= org" > > -- > Steve > 20170425 https://www.youtube.com/watch?v=3DVWUpyCsUKR4 > 20161221 https://www.youtube.com/watch?v=3DIbCHE-hONow > _______________________________________________ > freebsd-numerics@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-numerics > To unsubscribe, send any mail to "freebsd-numerics-unsubscribe@freebsd.or= g"