From patchwork Mon Aug 22 02:04:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Hudson-Doyle X-Patchwork-Id: 56905 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C156B385800A for ; Mon, 22 Aug 2022 02:04:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C156B385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661133899; bh=UR12CqSQRFReWx6WHHC/UAgbmDibwi4Ylag2+eBhEGo=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=kwtrsydqhwoY5mWd6OVXUO5afEu1KrKuxBgEnmdbxBJmnSrzrYwP35epZG17D+6tN 5yp7KnxeJjjmexCOZjKEcdRWdcIxPNGd/KUGzEEpSJGH8mO+iWyUhEg0zgcoaC65Qp lII1hDaMCfwc7z+hRd/QIycfwQ9WbF13PcBHMKx4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by sourceware.org (Postfix) with ESMTPS id 1005D3858401 for ; Mon, 22 Aug 2022 02:04:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1005D3858401 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 172545C00CB; Sun, 21 Aug 2022 22:04:34 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sun, 21 Aug 2022 22:04:34 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdeiiedgheehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvfevufffkffojghfggfgsedtke ertdertddtnecuhfhrohhmpefoihgthhgrvghlucfjuhgushhonhdqffhohihlvgcuoehm ihgthhgrvghlrdhhuhgushhonhestggrnhhonhhitggrlhdrtghomheqnecuggftrfgrth htvghrnhepgeejjeelveeltedtfeehiedtgeelgedthedukedujefhhfdthfdvfeejledu ieeinecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepmh ifhhhuughsohhnsehfrghsthhmrghilhdrfhhm X-ME-Proxy: Feedback-ID: i833146b3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 21 Aug 2022 22:04:32 -0400 (EDT) To: libc-alpha@sourceware.org Subject: [PATCH v2] Avoid undefined behaviour in ibm128 implementation of llroundl Date: Mon, 22 Aug 2022 14:04:20 +1200 Message-Id: <20220822020419.137775-1-michael.hudson@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220821104446.46521-1-michael.hudson@canonical.com> References: <20220821104446.46521-1-michael.hudson@canonical.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_LOW, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Michael Hudson-Doyle via Libc-alpha From: Michael Hudson-Doyle Reply-To: Michael Hudson-Doyle Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Detecting an overflow edge case depended on signed overflow of a long long. Replace the signed long long with unsigned and cast it back to unsigned before comparisons (which is implementation defined behaviour, but I guess glibc does not support any one's complement architectures...). BZ #29488 --- v2: added casts to some references to 'res' I missed in v1. This version passes all tests on ppc64el with gcc 12 with both -O2 and -O3. --- sysdeps/ieee754/ldbl-128ibm/s_llroundl.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c index d85154e73a..5f54f92767 100644 --- a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c +++ b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c @@ -28,7 +28,8 @@ long long __llroundl (long double x) { double xh, xl; - long long res, hi, lo; + unsigned long long res; + long long hi, lo; ldbl_unpack (x, &xh, &xl); @@ -69,7 +70,7 @@ __llroundl (long double x) res = hi + lo; /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */ - if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0))) + if (__glibc_unlikely (((~(hi ^ lo) & (((long long)res) ^ hi)) < 0))) goto overflow; xh -= lo; @@ -82,7 +83,7 @@ __llroundl (long double x) } else if (xh == 0.5) { - if (xl > 0.0 || (xl == 0.0 && res >= 0)) + if (xl > 0.0 || (xl == 0.0 && ((long long)res) >= 0)) res += 1; } else if (-xh > 0.5) @@ -91,11 +92,11 @@ __llroundl (long double x) } else if (-xh == 0.5) { - if (xl < 0.0 || (xl == 0.0 && res <= 0)) + if (xl < 0.0 || (xl == 0.0 && ((long long)res) <= 0)) res -= 1; } - if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0))) + if (__glibc_unlikely (((~(hi ^ (((long long)res) - hi)) & (((long long)res) ^ hi)) < 0))) goto overflow; return res;