From patchwork Wed Jul 17 22:54:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1961817 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=jtBlzzNP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WPWTy4Xytz1yY1 for ; Thu, 18 Jul 2024 08:54:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CFE17385E833 for ; Wed, 17 Jul 2024 22:54:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [69.48.154.134]) by sourceware.org (Postfix) with ESMTPS id 31C8F385841D for ; Wed, 17 Jul 2024 22:54:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31C8F385841D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 31C8F385841D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=69.48.154.134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721256865; cv=none; b=wa++Nprdwu776vLKtMT9oVZnqlK6aHx3GQzx9YZ/UOP5+xwkNNSjE2IAcG32UB8E8nrf8yE6ac3BacHGHt7gItBELfs7gNnN13VFaV+FAHHJk3+UPhw9Nyv5r2CwkUUNKeVZECPVFagAMIB8+gZR8bubTk42h8S4ghQTUPP6Gz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721256865; c=relaxed/simple; bh=QctbL0afk+hjE1bm4+bxlXdqVjN5ps1hgDwsO8/3W54=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Jr8HroJZZpd2E/xPFRtDz9yML4Z7rqJxkRjpJBTMuc315xBN50saA4pNQg2w9l3kuO5T7hELLID/xlUEUtHRPU9/iu2sOg7Nv0EAdQDAb8sFuWqAj8J/yeYQUxkZI4XVdY7U0eLRquqdteo+cxkCVhnqaw9LbqxpAJyz1M3wyFE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=NmdgyNCS6nYuwEEzFKBVWCYJQjQaWtDhBa8dNAu77yk=; b=jtBlzzNP6aMR73FJ1nkKQyKaC1 0kuXuvomQUmdzkkWU6IhSNrgk1bm0sI4gOODgKDxPSFXIxemc6CmCLhw3RwiTS2OKTux+90MotNZ4 g0fDq2UW7vx8YCCO+nX8Jy9D2+CPFGKNKmqAY2N2z4o3V1WnKqB1M7OQdvaNdmsUhjaituiykOp3v ZMk/8FwPVTEpKDuDKmQWyNH07by5jIumk5IHW2kzkxP+2g+IT/VfAkwJwIJ6uhtiq7xuoVbP9eUNI 3HdfYCVI3JDjDPuBgPmUx8YszjYIJ5hl3ijnlYDGrVrfb2SjwphG+e0QBTQ5bbg631wVgM4YHIp4C OoHMwR2w==; Received: from [168.86.198.82] (port=52880 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1sUDXR-00000009AET-2JTQ for gcc-patches@gcc.gnu.org; Wed, 17 Jul 2024 18:54:21 -0400 From: "Roger Sayle" To: Subject: [PATCH] Implement a -ftrapping-math/-fsignaling-nans TODO in match.pd. Date: Wed, 17 Jul 2024 23:54:20 +0100 Message-ID: <003501dad89c$43dfd010$cb9f7030$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdrYnEHKNh4HusksSVOg/pT6FZgcvw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.4 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org I've been investigating some (float)i == CST optimizations for match.pd, and noticed there's already a TODO comment in match.pd that's relatively easy to implement. When CST is a NaN, we only need to worry about exceptions with flag_trapping_math, and equality/inequality tests for sNaN only behave differently to qNaN with -fsignaling-nans. These issues are related to PR 57371 and PR 106805 in bugzilla. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32} with no new failures. Ok for mainline? 2024-07-17 Roger Sayle gcc/ChangeLog * match.pd ((FTYPE) N CMP CST): Only worry about exceptions with flag_trapping_math, and about signaling NaNs with HONOR_SNANS. gcc/testsuite/ChangeLog * c-c++-common/pr57371-4.c: Update comment. * c-c++-common/pr57371-5.c: Add missing testcases from pr57371-4.c and update for -fno-singnaling-nans -fno-trapping-math. Thanks in advance, Roger diff --git a/gcc/match.pd b/gcc/match.pd index 7fff7b5..2ab3bb3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6823,13 +6823,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) tree itype = TREE_TYPE (@0); format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1)))); const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1); - /* Be careful to preserve any potential exceptions due to - NaNs. qNaNs are ok in == or != context. - TODO: relax under -fno-trapping-math or - -fno-signaling-nans. */ - bool exception_p - = real_isnan (cst) && (cst->signalling - || (cmp != EQ_EXPR && cmp != NE_EXPR)); + /* Be careful to preserve any potential exceptions due to NaNs. + qNaNs are ok in == or != context. */ + bool exception_p = real_isnan (cst) + && flag_trapping_math + && ((cmp != EQ_EXPR && cmp != NE_EXPR) + || (cst->signalling + && HONOR_SNANS (TREE_TYPE (@1)))); } /* TODO: allow non-fitting itype and SNaNs when -fno-trapping-math. */ diff --git a/gcc/testsuite/c-c++-common/pr57371-4.c b/gcc/testsuite/c-c++-common/pr57371-4.c index f43f7c2..b0e539d 100644 --- a/gcc/testsuite/c-c++-common/pr57371-4.c +++ b/gcc/testsuite/c-c++-common/pr57371-4.c @@ -2,9 +2,7 @@ /* { dg-options "-O -fsignaling-nans -fdump-tree-original" } */ /* We can not get rid of comparison in tests below because of - pending NaN exceptions. - - TODO: avoid under -fno-trapping-math. */ + pending NaN exceptions. */ #define QNAN __builtin_nanf ("0") #define SNAN __builtin_nansf ("0") diff --git a/gcc/testsuite/c-c++-common/pr57371-5.c b/gcc/testsuite/c-c++-common/pr57371-5.c index 8e18b0a..77decbe 100644 --- a/gcc/testsuite/c-c++-common/pr57371-5.c +++ b/gcc/testsuite/c-c++-common/pr57371-5.c @@ -2,11 +2,10 @@ /* { dg-options "-O -fno-signaling-nans -fno-trapping-math -fdump-tree-original" } */ /* We can not get rid of comparison in tests below because of - pending NaN exceptions. - - TODO: avoid under -fno-trapping-math. */ + pending NaN exceptions. */ #define QNAN __builtin_nanf ("0") +#define SNAN __builtin_nansf ("0") void nonfinite(unsigned short x) { { @@ -34,6 +33,43 @@ void nonfinite(unsigned short x) { } { + volatile int nonfinite_5; + nonfinite_5 = (float) x > SNAN; + /* { dg-final { scan-tree-dump "nonfinite_5 = 0" "original" } } */ + } + + { + volatile int nonfinite_6; + nonfinite_6 = (float) x >= SNAN; + /* { dg-final { scan-tree-dump "nonfinite_6 = 0" "original" } } */ + } + + { + volatile int nonfinite_7; + nonfinite_7 = (float) x < SNAN; + /* { dg-final { scan-tree-dump "nonfinite_7 = 0" "original" } } */ + } + + { + volatile int nonfinite_8; + nonfinite_8 = (float) x <= SNAN; + /* { dg-final { scan-tree-dump "nonfinite_8 = 0" "original" } } */ + } + + { + volatile int nonfinite_9; + nonfinite_9 = (float) x == SNAN; + /* { dg-final { scan-tree-dump "nonfinite_9 = 0" "original" } } */ + } + + { + volatile int nonfinite_10; + nonfinite_10 = (float) x != SNAN; + /* { dg-final { scan-tree-dump "nonfinite_10 = 1" "original" } } * + */ + } + + { volatile int nonfinite_11; nonfinite_11 = (float) x == QNAN; /* { dg-final { scan-tree-dump "nonfinite_11 = 0" "original" } } */