From patchwork Mon Dec 28 20:39:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 1421058 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=PV75VcKm; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4D4Ttd2vktz9sW4 for ; Tue, 29 Dec 2020 07:39:35 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 73EB63844026; Mon, 28 Dec 2020 20:39:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73EB63844026 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1609187967; bh=JwDO3EOeMpkmzt2kP5rwurJFal+T6bik8jZGRsHPExs=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=PV75VcKmW6xMPW1iv06vopj04ZXEheo+lhfuIB23pve/qL/8HEABT3GLFo4H7AeLL iVZ4hAJIWd6Qt+O6C361EUTgvubuqd7Ujn9HBKLleamBVoH7oY/q9yWB8P1Koi9WCo Q9yLqFi5CNMNhMocjlUr6XxsCCdFMgrh0rjfcfKA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by sourceware.org (Postfix) with ESMTPS id 114CC3870913 for ; Mon, 28 Dec 2020 20:39:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 114CC3870913 Received: by mail-qk1-x72f.google.com with SMTP id n142so9861829qkn.2 for ; Mon, 28 Dec 2020 12:39:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=JwDO3EOeMpkmzt2kP5rwurJFal+T6bik8jZGRsHPExs=; b=YZZUgp4SM0vPyIlBPdWSufCTtQ3Yt26cCiE61++sjwDtSqV1CRrOMIAOzxicomnvjd GZxHIEfo0CraqL3UktdUqrMJoacmJATabzx7/YGaxjT+UO2BxLFZ6J9YKsu8opAeQaXY Au54wBMHOv7ccoUzQaoP+C3nCgQDvpq55kC0EFxUtRcgKoS6uElr8PueJKEkkcZmsNAr drGkM6mcKwWsHeX0regp1ZgQEs10LNejQcUB6T9a+tZzRQSiQW6rx2KxQajTZ0V05rhd nh1eC+1ziVy2gKW3rDg2sVhv4qBRcGAsOOrRCEt0Hdpg0trl8fxRuMZ9s1B9BKoSAA7f VDmg== X-Gm-Message-State: AOAM531TpzquD9QQyN5j9Fl1w2zK5KsqUMxb9szdZPBQXg097YOFQBZV VT46ogzGlBWfzi7ppLJi7hG9jfVCKbcH4/RqNd+1WYSM76q/fg== X-Google-Smtp-Source: ABdhPJxAGZL8EJilXeXlRY+3xd1g7Grp41LN1hNYULZxsbpx7nyb8r40I45ATueNg6lgVLi8mtJNhAMIB/W5wbQ6tV4= X-Received: by 2002:a05:620a:69c:: with SMTP id f28mr46355243qkh.127.1609187964444; Mon, 28 Dec 2020 12:39:24 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 28 Dec 2020 21:39:13 +0100 Message-ID: Subject: [PATCH] i386: Fix __builtin_rint with FE_DOWNWARD rounding direction [PR96793] To: "gcc-patches@gcc.gnu.org" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Uros Bizjak via Gcc-patches From: Uros Bizjak Reply-To: Uros Bizjak Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" [The same cure is needed for __builtin_rint.] x86_expand_rint expander uses x86_sse_copysign_to_positive, which is unable to change the sign from - to +. When FE_DOWNWARD rounding direction is in effect, the expanded sequence that involves subtraction can trigger x - x = -0.0 special rule. x86_sse_copysign_to_positive fails to change the sign of the intermediate value, assumed to always be positive, back to positive. The patch adds one extra fabs that strips the sign from the intermediate value when flag_rounding_math is in effect. 2020-12-28 Uroš Bizjak gcc/ PR target/96793 * config/i386/i386-expand.c (ix86_expand_rint): Remove the sign of the intermediate value for flag_rounding_math. gcc/testsuite/ PR target/96793 * gcc.target/i386/pr96793-2.c: New test. Bootstrapped and regression tested on x86_64-linux-gnu {,-m32}. Pushed to mainline, will be backported to other release branches. Uros. diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c index 5fe38052056..5c48cded954 100644 --- a/gcc/config/i386/i386-expand.c +++ b/gcc/config/i386/i386-expand.c @@ -16310,7 +16310,7 @@ ix86_expand_rint (rtx operand0, rtx operand1) return copysign (xa, operand1); */ machine_mode mode = GET_MODE (operand0); - rtx res, xa, TWO52, two52, mask; + rtx res, xa, TWO52, mask; rtx_code_label *label; res = gen_reg_rtx (mode); @@ -16323,16 +16323,18 @@ ix86_expand_rint (rtx operand0, rtx operand1) TWO52 = ix86_gen_TWO52 (mode); label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false); - two52 = TWO52; if (flag_rounding_math) { - two52 = gen_reg_rtx (mode); - ix86_sse_copysign_to_positive (two52, TWO52, res, mask); + ix86_sse_copysign_to_positive (TWO52, TWO52, res, mask); xa = res; } - xa = expand_simple_binop (mode, PLUS, xa, two52, NULL_RTX, 0, OPTAB_DIRECT); - xa = expand_simple_binop (mode, MINUS, xa, two52, xa, 0, OPTAB_DIRECT); + xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT); + xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT); + + /* Remove the sign with FE_DOWNWARD, where x - x = -0.0. */ + if (HONOR_SIGNED_ZEROS (mode) && flag_rounding_math) + xa = ix86_expand_sse_fabs (xa, NULL); ix86_sse_copysign_to_positive (res, xa, res, mask); @@ -16577,7 +16579,7 @@ ix86_expand_truncdf_32 (rtx operand0, rtx operand1) tmp = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT); /* Remove the sign with FE_DOWNWARD, where x - x = -0.0. */ - if (flag_rounding_math) + if (HONOR_SIGNED_ZEROS (mode) && flag_rounding_math) tmp = ix86_expand_sse_fabs (tmp, NULL); /* res = copysign (xa2, operand1) */ diff --git a/gcc/testsuite/gcc.target/i386/pr96793-2.c b/gcc/testsuite/gcc.target/i386/pr96793-2.c new file mode 100644 index 00000000000..14efaab9faa --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr96793-2.c @@ -0,0 +1,28 @@ +/* PR target/96793 */ +/* { dg-do run { target sse2_runtime } } */ +/* { dg-require-effective-target fenv } */ +/* { dg-options "-O2 -frounding-math -msse2 -mno-sse4 -mfpmath=sse" } */ + +#include + +double +__attribute__((noinline)) +test (double value) +{ + return __builtin_rint (value); +} + +int +main () +{ + double result; + + fesetround (FE_DOWNWARD); + + result = test (0.25); + + if (__builtin_signbit (result) != 0) + __builtin_abort (); + + return 0; +}