From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22617 invoked by alias); 22 Aug 2019 16:28:00 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 22582 invoked by uid 89); 22 Aug 2019 16:28:00 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=no version=3.3.1 spammy=HX-Received:d94d, HX-Received:aa7 X-HELO: mail-ed1-f68.google.com Received: from mail-ed1-f68.google.com (HELO mail-ed1-f68.google.com) (209.85.208.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 22 Aug 2019 16:27:58 +0000 Received: by mail-ed1-f68.google.com with SMTP id w5so8767862edl.8 for ; Thu, 22 Aug 2019 09:27:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=eN0Uxo8Wgs32zJVCll7YItg0ARBbQbLF3tbRhCogV0A=; b=nqzVgfcpO89oyTGnMMe8tKIpP2lZdKc84G96v+C5lL0yHth72qytxlKkkLAYd7iJpc RFiZfI6yADb6AB37iAVr4HMpi1VbKAQP81cJDes6XzseGGQwjnugFSey8D2YUhLkBVsS 6LpaKu9z1APDPqPfxR/hrKO4ejrTntqwhFNLlp4SZGHc9+HYv9nFhB1aMv5rql02ow47 jr5/JHkYfLABbYj/y2WrOwr/yn6JekanDa9mzQnx6zE+c8NtroPNOXikF/aYXJQOPf8Q 3lIR8Q4GMWmIUU6iczDK+62dl/RHYjOqOmVW3OvsgqOG3ZRfq3MkJ858IqPS0OSI8LhM 5J+g== MIME-Version: 1.0 References: In-Reply-To: From: Tejas Joshi Date: Thu, 22 Aug 2019 16:28:00 -0000 Message-ID: Subject: Re: [PATCH] Builtin function roundeven folding implementation To: gcc@gcc.gnu.org Cc: Martin Jambor , hubicka@ucw.cz, joseph@codesourcery.com Content-Type: text/plain; charset="UTF-8" X-IsSubscribed: yes X-SW-Source: 2019-08/txt/msg00185.txt.bz2 > I'm concerned that this would produce +0.0 for an argument of -0.5 (via > -0.5 - 0.5 - -1.0 producing +0.0) when it needs to produce -0.0. Would the following overhaul be acceptable as the condition is specialized for -0.5 and +0.5 only. This seems to solve the problem. I did test the roundeven tests and it passes the tests. void real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt, const REAL_VALUE_TYPE *x) { if (is_halfway_below (x)) { if (REAL_EXP (x) == 0) { *r = *x; clear_significand_below (r, SIGNIFICAND_BITS); } else { do_add (r, x, &dconsthalf, x->sign); if (!is_even (r)) do_add (r, r, &dconstm1, x->sign); } if (fmt) real_convert (r, fmt, r); } else real_round (r, fmt, x); } tests: /* { dg-do link } */ extern int link_error (int); #define TEST(FN, VALUE, RESULT) \ if (__builtin_##FN (VALUE) != RESULT) link_error (__LINE__); int main (void) { TEST(roundeven, 0, 0); TEST(roundeven, 0.5, 0); TEST(roundeven, -0.5, 0); TEST(roundeven, 6, 6); TEST(roundeven, -8, -8); TEST(roundeven, 2.5, 2); TEST(roundeven, 3.5, 4); TEST(roundeven, -1.5, -2); TEST(roundeven, 3.499, 3); TEST(roundeven, 3.501, 4); if (__builtin_copysign (1, __builtin_roundeven (-0.5)) != -1) link_error (__LINE__); return 0; } Thanks, Tejas On Thu, 22 Aug 2019 at 20:03, Joseph Myers wrote: > > On Thu, 22 Aug 2019, Martin Jambor wrote: > > > +/* Round X to nearest integer, rounding halfway cases towards even. */ > > + > > +void > > +real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt, > > + const REAL_VALUE_TYPE *x) > > +{ > > + if (is_halfway_below (x)) > > + { > > + do_add (r, x, &dconsthalf, x->sign); > > + if (!is_even (r)) > > + do_add (r, r, &dconstm1, x->sign); > > I'm concerned that this would produce +0.0 for an argument of -0.5 (via > -0.5 - 0.5 - -1.0 producing +0.0) when it needs to produce -0.0. > > Note that testcases for the sign of zero results need to check e.g. > !!__builtin_signbit on the result, or the result of calling > __builtin_copysign* to extract the sign of the result, since 0.0 == -0.0 > so checking with ==, while necessary, is not sufficient in that case. > > -- > Joseph S. Myers > joseph@codesourcery.com