From patchwork Sat Oct 29 05:47:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 688697 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3t5V6K4C21z9t1B for ; Sat, 29 Oct 2016 16:48:23 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=lMLtrG/C; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:references:in-reply-to :content-type:mime-version; q=dns; s=default; b=XPUCh9w/wKczfhcN jGNYgFvBUFSobz0oNJFQIyGC9JT7wrE8UqkWhL41uNxN1E8pbInmV7Mve08h6X/o eox1T6CCZF5Th9UkGh6Dj5p7YgfzGNGIIgco+9W+4jDffbtfsl5tiOT7KNfD5mNx NI3vSVGQeU86s3ottsIjYUiNsIo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:references:in-reply-to :content-type:mime-version; s=default; bh=/4vyLqTaCiwy8YxYJGUyIF o5JXU=; b=lMLtrG/CBHNdmEhuZ8sb3gQn6Mr1NLZGDgVX0bgw7DzjdHQtMpXSOi uTZQIPTKX+uMC4Pj6BTKErMnkevUfi4Uelg8z413wEY0la/RlvbDEDTvZM513wSy 9bh8OLi2mN2Yw3SaQhOVzdygPmQd5uJ643MgcLMbmWYbttXoyliew= Received: (qmail 115599 invoked by alias); 29 Oct 2016 05:48:14 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 115578 invoked by uid 89); 29 Oct 2016 05:48:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=16436, 1643, 6, H*c:HHH X-HELO: COL004-OMC3S2.hotmail.com Received: from col004-omc3s2.hotmail.com (HELO COL004-OMC3S2.hotmail.com) (65.55.34.140) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 29 Oct 2016 05:47:58 +0000 Received: from EUR01-HE1-obe.outbound.protection.outlook.com ([65.55.34.137]) by COL004-OMC3S2.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Fri, 28 Oct 2016 22:47:56 -0700 Received: from HE1EUR01FT036.eop-EUR01.prod.protection.outlook.com (10.152.0.56) by HE1EUR01HT184.eop-EUR01.prod.protection.outlook.com (10.152.1.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.679.5; Sat, 29 Oct 2016 05:47:54 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com (10.152.0.55) by HE1EUR01FT036.mail.protection.outlook.com (10.152.1.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.679.5 via Frontend Transport; Sat, 29 Oct 2016 05:47:54 +0000 Received: from AM4PR0701MB2162.eurprd07.prod.outlook.com ([10.167.132.147]) by AM4PR0701MB2162.eurprd07.prod.outlook.com ([10.167.132.147]) with mapi id 15.01.0679.018; Sat, 29 Oct 2016 05:47:54 +0000 From: Bernd Edlinger To: Joseph Myers CC: GCC Patches , Ian Lance Taylor , Bernd Schmidt Subject: Re: [PATCH, LIBGCC] Avoid count_leading_zeros with undefined result (PR 78067) Date: Sat, 29 Oct 2016 05:47:54 +0000 Message-ID: References: In-Reply-To: authentication-results: codesourcery.com; dkim=none (message not signed) header.d=none; codesourcery.com; dmarc=none action=none header.from=hotmail.de; x-ms-exchange-messagesentrepresentingtype: 1 x-eopattributedmessage: 0 x-microsoft-exchange-diagnostics: 1; HE1EUR01HT184; 6:YamBPSBAJOceanVbP8y1RmMEvfuCqBpHyprHJZeOuK5bdgoWWhPm4lsp+zQhJj9RRGwJ76jLWLJTk+53oy/Pgo/rfNIyKmwCTlMBKljLAmza0QKzUXzU/YlTr2n9GQMDUgy0rgFdvZKjm1szfzJecuUR2cHXlRKdtznaOj8PvgoBYyIxmNeYwIUjg6QyeOg90HE13IwfcfkS8mlg0i3ZMYV+i0cpc46xyLG3fF/16JOVyUAqpRQe98q0GJRBctT5dA+HyuRiKiIRxqJuchMYmi35CGuJ4o2uTPPfAf2Cr9YDLySQPzf+wMIJNbgpBetV; 5:OwfJ1QFgu/+E4lslbFvc/bB4ZuT0NMxs+zFbJ1HxkAZh5nBfBMAEtgWncgzSO1+q2ZX/YTttaw3mryV+3vYudBvxqrt2NVXN37EJ6twaRP+a9GXvTWyBWQAstpMr0+KNW+b+OR0FmkHA0nQyrAea2w==; 24:JoQcWSzn3l570LzvOtz+rXblfd59YGMrgcc4FM/1Ji+Yj0KtgvLo9t/dXUr3Pn185nCpu6cyliapgzPjpXh8BZo/sjZtiYCYyfRbeFBmWtU=; 7:US5mEBCUO7QY/mSqcaoYV4wMpNYnyhmXI0tmdSkFhyeCK9S8Ld/+oRTNDJLoSzvdJh1jCYNofx6q83Nv2RIdmowLzPRZuDvhlQ9FOQqmvH77ZINmCypFbSo5/MuLshi3piX+ZYbDFZo2o6c1OJwIbM0EZpSOZPJkjcFZ3AQxPtE2radLhbhQ7HGVwAS/tLUb3nIjKPff743Jc6uWhOf7W+F6sawRoWs/Y6VTIYDVRIVsjAGagPanjxD7H5vWIcsazDACshNM5ZRMzvSSs01leM0sEsjPnA2ht1R/3yza4mY+NxZizehZjr/4MF8JbWgSNuHXCshCFyrEDHMSKYzt9ec0oIstSBIvqbGqf8x9F+E= x-forefront-antispam-report: EFV:NLI; SFV:NSPM; SFS:(10019020)(98900003); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1EUR01HT184; H:AM4PR0701MB2162.eurprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; x-ms-office365-filtering-correlation-id: 81aef519-7355-4b90-5b7f-08d3ffbf1ff3 x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(1601124038)(1603103081)(1601125047); SRVR:HE1EUR01HT184; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(432015012)(102415321)(82015046); SRVR:HE1EUR01HT184; BCL:0; PCL:0; RULEID:; SRVR:HE1EUR01HT184; x-forefront-prvs: 01106E96F6 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Oct 2016 05:47:54.0039 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1EUR01HT184 On 10/28/16 16:05, Bernd Edlinger wrote: > On 10/27/16 22:23, Joseph Myers wrote: >> On Thu, 27 Oct 2016, Bernd Edlinger wrote: >> >>> Hi, >>> >>> by code reading I became aware that libgcc can call count_leading_zeros >>> in certain cases which can give undefined results. This happens on >>> signed int128 -> float or double conversions, when the int128 is in >>> the range >>> INT64_MAX+1 to UINT64_MAX. >> >> I'd expect testcases added to the testsuite that exercise this case at >> runtime, if not already present. >> > > Yes, thanks. I somehow expected there were already test cases, > somewhere, but now when you ask that, I begin to doubt as well... > > I will try to add an asm("int 3") and see if that gets hit at all. > The breakpoint got hit only once, in the libgo testsuite: runtime/pprof. I see there are some int to float conversion tests at gcc.dg/torture/fp-int-convert*.c, where it is easy to add a test case that hits the breakpoint too. However the test case does not fail before the patch, it is just slightly undefined behavior, that is not causing problems (at least for x86_64). Find attached a new patch with test case. Boot-strapped on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. 2016-10-27 Bernd Edlinger PR libgcc/78067 * libgcc2.c (__floatdisf, __floatdidf): Avoid undefined results from count_leading_zeros. testsuite: 2016-10-27 Bernd Edlinger PR libgcc/78067 * gcc.dg/torture/fp-int-convert.h: Add more conversion tests. Index: libgcc2.c =================================================================== --- libgcc2.c (revision 241400) +++ libgcc2.c (working copy) @@ -1643,6 +1643,11 @@ hi = -(UWtype) hi; UWtype count, shift; +#if !defined (COUNT_LEADING_ZEROS_0) || COUNT_LEADING_ZEROS_0 != W_TYPE_SIZE + if (hi == 0) + count = W_TYPE_SIZE; + else +#endif count_leading_zeros (count, hi); /* No leading bits means u == minimum. */ Index: gcc/testsuite/gcc.dg/torture/fp-int-convert.h =================================================================== --- gcc/testsuite/gcc.dg/torture/fp-int-convert.h (revision 241647) +++ gcc/testsuite/gcc.dg/torture/fp-int-convert.h (working copy) @@ -53,6 +53,8 @@ do { \ TEST_I_F_VAL (U, F, HVAL1U (P, U), P_OK (P, U)); \ TEST_I_F_VAL (U, F, HVAL1U (P, U) + 1, P_OK (P, U)); \ TEST_I_F_VAL (U, F, HVAL1U (P, U) - 1, P_OK (P, U)); \ + TEST_I_F_VAL (I, F, WVAL0S (I), 1); \ + TEST_I_F_VAL (I, F, -WVAL0S (I), 1); \ } while (0) #define P_OK(P, T) ((P) >= sizeof(T) * CHAR_BIT) @@ -74,6 +76,7 @@ do { \ ? (S)1 \ : (((S)1 << (sizeof(S) * CHAR_BIT - 2)) \ + ((S)3 << (sizeof(S) * CHAR_BIT - 2 - P)))) +#define WVAL0S(S) (S)((S)1 << (sizeof(S) * CHAR_BIT / 2 - 1)) #define TEST_I_F_VAL(IT, FT, VAL, PREC_OK) \ do { \