From patchwork Mon Feb 17 09:40:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergei Zimmerman X-Patchwork-Id: 106659 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 7876A3858C78 for ; Mon, 17 Feb 2025 09:42:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7876A3858C78 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=oJVX2Fc9 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 C8B623858CD9 for ; Mon, 17 Feb 2025 09:41:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8B623858CD9 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 C8B623858CD9 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=1739785306; cv=none; b=ChlXoAcWqRRoMlif9GNmZapXr8OnFHsL9OO40DtQJxmhg5vRUe05RVtqXsLRIRMLkP19N8BtKpmceKmWlOBHEnihCLoLK3+33F6l61liFWwtEOPVxkezfcVIxPPF/5mzOD4FkIEUcxflmCTxnBchpJIRc5qQIWyPq6gIn632TkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739785306; c=relaxed/simple; bh=OoVKuZj7WpBykQ1ULw/OIAClW3+OeNPnFn6FYooLnoY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XBySsc70jXE+pAmROVNgV1ab8lZKruXn3zNi0YZ2CKmIz4cF6Ssrtsrje5PA6K2FKVi850YvpQjNP8ZFHWoyYO9/1CIFImZTJDp0XT5IVzIV+uEXgrHMNn65FN1cDTYsUcNKtr4femm2D7soLq+r6mvb0h6CSeLXJdpr2Mdh6NI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C8B623858CD9 Received: from pmg.syntacore.com (localhost.localdomain [127.0.0.1]) by m.syntacore.com (Proxmox) with ESMTP id 9E7BDB41CC1 for ; Mon, 17 Feb 2025 12:41:43 +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:message-id:mime-version:reply-to:subject:subject :to:to; s=m; bh=MAJ2ZihD49ei0dXelKFmnZHdBiJYRxOAMgMgykrkPLc=; b= oJVX2Fc9ja6M4kx/AIQ9Ft0AlC5MLM0eexTRbDO4JiK73uyIsm3zK8KgXf7x8HSj HEBjtLFerHA87EB67UUpchrqVeXI7lUBzSz5JROq7hA/wQmJiycE9ZZT13eCfdzc 8hN6deJwN8NhGRPj3sZYnq+AChfD5uSNnuHzu7Cw3MM7TNEaW6R7PeXHSB/bpJ4g vnGKoe9CsRWPecOk+hutC3+EYpt1nBQWREGWcqp36Pb5p4Ei2hQI0AY0BSDGII7u BP2R95HxH7Da2D1jELNa6PBI5SC6CKUzTflwvcx2Bp448xuDUYl+iLBN2li/8xoY ms45Ap2w8ObE/bRy0sw5IQ== Received: from S-SC-EXCH-01.corp.syntacore.com (mail.syntacore.com [10.76.202.20]) by m.syntacore.com (Proxmox) with ESMTPS id 8A37CB41CBA for ; Mon, 17 Feb 2025 12:41:43 +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; Mon, 17 Feb 2025 12:40:33 +0300 From: Sergei Zimmerman To: CC: Sergei Zimmerman Subject: [PATCH] sysdeps/ieee754/flt-32: Fix remainderf() sign of zero for FE_DOWNWARD Date: Mon, 17 Feb 2025 09:40:08 +0000 Message-ID: <20250217094007.34794-2-sergei.zimmerman@syntacore.com> X-Mailer: git-send-email 2.47.2 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=-14.0 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() 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. Original fix by: Bruce Evans in FreeBSD's a2ddfa5ea726c56dbf825763ad371c261b89b7c7. --- math/libm-test-remainder.inc | 4 ++++ sysdeps/ieee754/flt-32/e_remainderf.c | 3 +++ 2 files changed, 7 insertions(+) diff --git a/math/libm-test-remainder.inc b/math/libm-test-remainder.inc index 1a1d3e9937..f2b0104087 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), + TEST_ff_f (remainder, -0x1.003ffep-126, -0x1.8p-148, minus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED), + TEST_ff_f (remainder, 0x1.2c3224p+17, 0x1p-5, plus_zero, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED), }; static void diff --git a/sysdeps/ieee754/flt-32/e_remainderf.c b/sysdeps/ieee754/flt-32/e_remainderf.c index 81781363af..7d773ab36d 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&0x7fffffff)==0) hx = 0; SET_FLOAT_WORD(x,hx^sx); return x; }