From patchwork Tue Feb 25 18:05:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergei Zimmerman X-Patchwork-Id: 107072 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 5AAF93858C51 for ; Tue, 25 Feb 2025 18:06:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AAF93858C51 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=syntacore.com header.i=@syntacore.com header.a=rsa-sha256 header.s=m header.b=bInfIRoK X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from m.syntacore.com (m.syntacore.com [178.249.69.228]) by sourceware.org (Postfix) with ESMTPS id 48DE33858D26 for ; Tue, 25 Feb 2025 18:06:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48DE33858D26 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=syntacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=syntacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 48DE33858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=178.249.69.228 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740506762; cv=none; b=dxidUNJVq01KBnfTQAjL4laRAJkP9PUFXIcxDJqiQiEtS9sMgTGnKVVqFx30SlwA9T7pWymQaFHVWZY6hnQvPjjgCQtQ0HcEgnPQ2xmsrk+Dzi62GQW6tUSfiBWpmF5DbgBODLRG06bOEioHK6KlXkCLySks3hImYvm2N4/edMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740506762; c=relaxed/simple; bh=AuCPy/XgFscFcKjdQVP98JRtoUwvt8YX/WvCl/uMpqU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=H0Qodj+2WrE4uPzRbxdkcv6PjvDybUwuzLBw1uKiwk1G6DszXlM88S/NteQjfvAjVJE8dKGU/WlCtKj1Z/NTKFFrjEgwBIIucCD5ZzduUMSRD1w+Dxlb+Nz8FT6f/pOvHIwSikwzzjQAzgJo9Mqq5Dg86yIjrcY7DBlTMDZak+s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48DE33858D26 Received: from pmg.syntacore.com (localhost.localdomain [127.0.0.1]) by m.syntacore.com (Proxmox) with ESMTP id 3D59FB41D43; Tue, 25 Feb 2025 21:06:01 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; h=cc:cc:content-transfer-encoding:content-type:content-type :date:from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=m; bh=kYz1KL613fAILgYNE+jmqGT KIgEqTGr6E4r56lut9hE=; b=bInfIRoKJtz5tDEgL0dp9Vvx0R4wf4KiXCwdYKy 6x6mGsQGAol8GYyCS8bdbLfIxEqvebimHc57Q/NFIecfesHbQx3uAKoIFDxit078 i3LRF/sRMpKFs/UaIkKq0TS7oxl1PDdHjul2CgxK5K41CH1mpPiFzY5+GmYHE8jy +OU2N/O6qWRHjILBIN9Nh4EhUd4vmhXxRQouHyqhZRC9kK0KRLhoxkzuf8Mj+t/m Soz25PMzTGqRrSn8iVZ6+OFSS3iLkfFQYdOd/Y/cB8/hfWnX2kGPuaRX4RkppvRg vRyEtm9eP3eg3+SweMmzCGsnAY8kWkjHJjkx//IH6Vx5VhQ== Received: from S-SC-EXCH-01.corp.syntacore.com (mail.syntacore.com [10.76.202.20]) by m.syntacore.com (Proxmox) with ESMTPS id 2949BB41C2E; Tue, 25 Feb 2025 21:06:01 +0300 (MSK) Received: from localhost (10.178.153.234) by S-SC-EXCH-01.corp.syntacore.com (10.76.202.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 25 Feb 2025 21:05:00 +0300 From: Sergei Zimmerman To: , CC: Sergei Zimmerman Subject: [PATCH v3] sysdeps/ieee754: Fix remainder sign of zero for FE_DOWNWARD (BZ #32711) Date: Tue, 25 Feb 2025 18:05:40 +0000 Message-ID: <20250225180543.1293984-1-sergei.zimmerman@syntacore.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250217094007.34794-2-sergei.zimmerman@syntacore.com> References: <20250217094007.34794-2-sergei.zimmerman@syntacore.com> MIME-Version: 1.0 X-Originating-IP: [10.178.153.234] X-ClientProxiedBy: S-SC-EXCH-01.corp.syntacore.com (10.76.202.20) To S-SC-EXCH-01.corp.syntacore.com (10.76.202.20) X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Single-precision remainderf() and quad-precision remainderl() implementation derived from Sun is affected by an issue when the result is +-0. IEEE754 requires that if remainder(x, y) = 0, its sign shall be that of x regardless of the rounding direction. The implementation seems to have assumed that x - x = +0 in all rounding modes, which is not the case. When rounding direction is roundTowardNegative the sign of an exact zero sum (or difference) is −0. Regression tests that triggered this erroneous behavior are added to math/libm-test-remainder.inc. Tested for cross riscv64 and powerpc. Original fix by: Bruce Evans in FreeBSD's a2ddfa5ea726c56dbf825763ad371c261b89b7c7. --- Changes since v2: - Use XFAIL_ROUNDING_IBM128_LIBGCC for the added test cases. Changes since v1: - Apply identical fix for incorrect sign to ieee754/ldbl-128/e_remainderl.c - Change the check for -0 in ieee754/flt-32/e_remainderf.c - Only enable new test cases for binary{32,64,128} formats math/libm-test-remainder.inc | 4 ++++ sysdeps/ieee754/flt-32/e_remainderf.c | 3 +++ sysdeps/ieee754/ldbl-128/e_remainderl.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/math/libm-test-remainder.inc b/math/libm-test-remainder.inc index 1a1d3e9937..bca21af06b 100644 --- a/math/libm-test-remainder.inc +++ b/math/libm-test-remainder.inc @@ -173,6 +173,10 @@ static const struct test_ff_f_data remainder_test_data[] = TEST_ff_f (remainder, 2, -1, plus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED), TEST_ff_f (remainder, -2, 1, minus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED), TEST_ff_f (remainder, -2, -1, minus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED), + + TEST_ff_f (remainder, 0x1.08001cp-2, 0x1p-24, plus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED|XFAIL_ROUNDING_IBM128_LIBGCC), + TEST_ff_f (remainder, -0x1.003ffep-126, -0x1.8p-148, minus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED|XFAIL_ROUNDING_IBM128_LIBGCC), + TEST_ff_f (remainder, 0x1.2c3224p+17, 0x1p-5, plus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED|XFAIL_ROUNDING_IBM128_LIBGCC), }; static void diff --git a/sysdeps/ieee754/flt-32/e_remainderf.c b/sysdeps/ieee754/flt-32/e_remainderf.c index 81781363af..bb066b7162 100644 --- a/sysdeps/ieee754/flt-32/e_remainderf.c +++ b/sysdeps/ieee754/flt-32/e_remainderf.c @@ -56,6 +56,9 @@ __ieee754_remainderf(float x, float p) } } GET_FLOAT_WORD(hx,x); + /* Make sure x is not -0. This can occur only when x = p + and rounding direction is towards negative infinity. */ + if (hx==0x80000000) hx = 0; SET_FLOAT_WORD(x,hx^sx); return x; } diff --git a/sysdeps/ieee754/ldbl-128/e_remainderl.c b/sysdeps/ieee754/ldbl-128/e_remainderl.c index 07a15c2459..1e8605f258 100644 --- a/sysdeps/ieee754/ldbl-128/e_remainderl.c +++ b/sysdeps/ieee754/ldbl-128/e_remainderl.c @@ -64,7 +64,10 @@ __ieee754_remainderl(_Float128 x, _Float128 p) if(x>=p_half) x -= p; } } - GET_LDOUBLE_MSW64(hx,x); + GET_LDOUBLE_WORDS64(hx,lx,x); + /* Make sure x is not -0. This can occur only when x = p + and rounding direction is towards negative infinity. */ + if ((hx==0x8000000000000000ULL)&&(lx==0)) hx = 0; SET_LDOUBLE_MSW64(x,hx^sx); return x; }