From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 909F7385703E for ; Wed, 24 Aug 2022 05:08:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 909F7385703E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x632.google.com with SMTP id u15so22645686ejt.6 for ; Tue, 23 Aug 2022 22:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc; bh=UCed9AcZ3TVQ4PsP2fX3MHB0oKzr1zep+/252jf53+g=; b=G2wOGYwBZR5WF3TZEtKqmxpAFgFs11FeBS6R2k58kb6TDOfvVlw9cEz52jHiiEJAEM BIYzrdgFZE2BosLkmPoHIXbi0MEWASUHngsFAUvOi8msZ84/pjO2GbRzx8PLOdEm0q0F S314V1+YmV1+SyIkq7VpBoAMom1+Z6u461XPFQhBfnSst/3ynCkGpbsO3Ug0C3khB184 mTWyuZ3gPdozZWJTGB5fKqlM7HejRxQFcjdFINfA89OgiEEo3Wo/+HF1AZKLvj1MfZgU lkEj9APo+BGPcU9DC4q+7jsG3NlIZKng/UQGDkSmXG0NY1hmgI8kdkwil5JaGxx2RAL7 UhiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc; bh=UCed9AcZ3TVQ4PsP2fX3MHB0oKzr1zep+/252jf53+g=; b=N6WuMZkYt7V2Ji4u0aoZg9JZpz1gVPNcwUhWz4K1CUMETjJv+SPXLmvd1kMiuW9k19 SraamwgVNGK7B2v32yL87xhEYP5UdChrvt2njeff/epp0D1irnyh8Cv5rITga0G3nTAO UOTJan7sIzzPK0EXTPGxBs+5Xf4QU+IQTXg973fs9nspnX+BDLhIwgHdA0yw2A8l0ULx ihvuvvTqCZ0l/qEE6mtggQyLzGyQ9Ig86Pt8lPPkI8kX4Yb3dA4By0/RG9JR8A6RypDU HaUja/J4m6hVH90kyfu/j9XbuhXgeisBefkw9X9RxcwNis2SW3Aoo2OY6C3HaUFZb2TN v4+A== X-Gm-Message-State: ACgBeo0W457sG8YDtR5ODcwFWqssE6NrT1pLKSyr6PYc/kIzS9M0bdOI 9aSCLTb8gKiof7cQUxKsNBlCEoXKCKA= X-Google-Smtp-Source: AA6agR6qUhHwKs6RCzv2jXCBI8JVdXzsUXGez+b26Z6iBm1RmzQfsOqZJNE82eSFv635oq6bU9C1qA== X-Received: by 2002:a17:906:d550:b0:733:8e1a:f7 with SMTP id cr16-20020a170906d55000b007338e1a00f7mr1818375ejc.580.1661317701240; Tue, 23 Aug 2022 22:08:21 -0700 (PDT) Received: from smtpclient.apple ([2a02:3032:400:18e9:51b3:af7f:b856:a373]) by smtp.gmail.com with ESMTPSA id d12-20020a170906040c00b0073d6b9cb31bsm695822eja.14.2022.08.23.22.08.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Aug 2022 22:08:20 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: tree.cc: Fix optimization of DFP default initialization Date: Wed, 24 Aug 2022 07:08:19 +0200 Message-Id: <4A1EE58E-F843-45DE-82BC-206A912C7B3B@gmail.com> References: Cc: gcc-patches@gcc.gnu.org In-Reply-To: To: Joseph Myers X-Mailer: iPhone Mail (19G82) X-Spam-Status: No, score=-10.4 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > Am 24.08.2022 um 01:54 schrieb Joseph Myers : >=20 > =EF=BB=BFWhen an object of decimal floating-point type is default-initiali= zed, > GCC is inconsistent about whether it is given the all-zero-bits > representation (zero with the least quantum exponent) or whether it > acts like a conversion of integer 0 to the DFP type (zero with quantum > exponent 0). In particular, the representation stored in memory can > have all zero bits, but optimization of access to the same object > based on its known constant value can then produce zero with quantum > exponent 0 instead. >=20 > C2x leaves the quantum exponent for default initialization > implementation-defined, but that doesn't allow such inconsistency in > the interpretation of a single object. All zero bits seems most > appropriate; change build_real to special-case dconst0 the same way > other constants are special-cased and ensure that the correct zero for > the type is generated. >=20 > Bootstrapped with no regressions for x86_64-pc-linux-gnu. OK to commit? Ok Thanks, Richard=20 > gcc/ > * tree.cc (build_real): Give DFP dconst0 the minimum quantum > exponent for the type. >=20 > gcc/testsuite/ > * gcc.dg/torture/dfp-default-init-1.c, > gcc.dg/torture/dfp-default-init-2.c, > gcc.dg/torture/dfp-default-init-3.c: New tests. >=20 > diff --git a/gcc/testsuite/gcc.dg/torture/dfp-default-init-1.c b/gcc/tests= uite/gcc.dg/torture/dfp-default-init-1.c > new file mode 100644 > index 00000000000..f893ddb52b9 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/torture/dfp-default-init-1.c > @@ -0,0 +1,113 @@ > +/* Test that default-initialized DFP values consistently have the least q= uantum > + exponent. */ > +/* { dg-do run } */ > +/* { dg-require-effective-target dfp } */ > + > +extern void exit (int); > +extern void abort (void); > +void *memset (void *, int, __SIZE_TYPE__); > +int memcmp (const void *, const void *, __SIZE_TYPE__); > + > +#ifndef TYPE > +#define TYPE _Decimal32 > +#endif > + > +#ifndef ZEROFP > +#define ZEROFP 0e-101DF > +#endif > + > +TYPE zero_int =3D 0; > +TYPE zero_fp =3D ZEROFP; > +TYPE default_init; > +TYPE zero_bytes; > +TYPE x; > + > +struct s { TYPE a, b; }; > +struct s s_default_init; > +struct s s_empty_init =3D {}; > +struct s s_first_int =3D { 0 }; > +struct s s_both_int =3D { 0, 0 }; > +struct s sx; > + > +const TYPE a_default_init[10]; > +const TYPE a_empty_init[10] =3D {}; > +const TYPE a_first_int[10] =3D { 0 }; > +const TYPE a_two_int[10] =3D { 0, 0 }; > + > +#define CHECK_ZERO_BYTES(expr) \ > + do \ > + { \ > + if (memcmp (expr, &zero_bytes, sizeof zero_bytes) !=3D 0) \ > + abort (); \ > + TYPE tmp =3D *expr; \ > + if (memcmp (&tmp, &zero_bytes, sizeof zero_bytes) !=3D 0) \ > + abort (); \ > + } \ > + while (0) > + > +#define CHECK_INT_BYTES(expr) \ > + do \ > + { \ > + if (memcmp (expr, &zero_int, sizeof zero_int) !=3D 0) \ > + abort (); \ > + TYPE tmp =3D *expr; \ > + if (memcmp (&tmp, &zero_int, sizeof zero_int) !=3D 0) \ > + abort (); \ > + } \ > + while (0) > + > +int > +main (void) > +{ > + memset (&zero_bytes, 0, sizeof zero_bytes); > + if (memcmp (&zero_bytes, &zero_int, sizeof zero_int) =3D=3D 0) > + abort (); > + CHECK_ZERO_BYTES (&zero_fp); > + CHECK_ZERO_BYTES (&default_init); > + CHECK_ZERO_BYTES (&s_default_init.a); > + CHECK_ZERO_BYTES (&s_default_init.b); > + CHECK_ZERO_BYTES (&s_empty_init.a); > + CHECK_ZERO_BYTES (&s_empty_init.b); > + CHECK_INT_BYTES (&s_first_int.a); > + CHECK_ZERO_BYTES (&s_first_int.b); > + CHECK_INT_BYTES (&s_both_int.a); > + CHECK_INT_BYTES (&s_both_int.b); > + CHECK_ZERO_BYTES (&a_default_init[0]); > + CHECK_ZERO_BYTES (&a_default_init[1]); > + CHECK_ZERO_BYTES (&a_default_init[2]); > + CHECK_ZERO_BYTES (&a_default_init[9]); > + CHECK_ZERO_BYTES (&a_empty_init[0]); > + CHECK_ZERO_BYTES (&a_empty_init[1]); > + CHECK_ZERO_BYTES (&a_empty_init[2]); > + CHECK_ZERO_BYTES (&a_empty_init[9]); > + CHECK_INT_BYTES (&a_first_int[0]); > + CHECK_ZERO_BYTES (&a_first_int[1]); > + CHECK_ZERO_BYTES (&a_first_int[2]); > + CHECK_ZERO_BYTES (&a_first_int[9]); > + CHECK_INT_BYTES (&a_two_int[0]); > + CHECK_INT_BYTES (&a_two_int[1]); > + CHECK_ZERO_BYTES (&a_two_int[2]); > + CHECK_ZERO_BYTES (&a_two_int[9]); > + struct s s2 =3D {}; > + CHECK_ZERO_BYTES (&s2.a); > + CHECK_ZERO_BYTES (&s2.b); > + struct s s3 =3D { 0 }; > + CHECK_INT_BYTES (&s3.a); > + CHECK_ZERO_BYTES (&s3.b); > + struct s s4 =3D { 0, 0 }; > + CHECK_INT_BYTES (&s4.a); > + CHECK_INT_BYTES (&s4.b); > + struct s s5 =3D { 0 }; > + sx =3D s5; > + CHECK_INT_BYTES (&sx.a); > + CHECK_ZERO_BYTES (&sx.b); > + x =3D default_init; > + CHECK_ZERO_BYTES (&x); > + x =3D zero_int; > + CHECK_INT_BYTES (&x); > + x =3D s_default_init.a; > + CHECK_ZERO_BYTES (&x); > + x =3D s_default_init.b; > + CHECK_ZERO_BYTES (&x); > + exit (0); > +} > diff --git a/gcc/testsuite/gcc.dg/torture/dfp-default-init-2.c b/gcc/tests= uite/gcc.dg/torture/dfp-default-init-2.c > new file mode 100644 > index 00000000000..30f850be2ee > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/torture/dfp-default-init-2.c > @@ -0,0 +1,8 @@ > +/* Test that default-initialized DFP values consistently have the least q= uantum > + exponent. */ > +/* { dg-do run } */ > +/* { dg-require-effective-target dfp } */ > + > +#define TYPE _Decimal64 > +#define ZEROFP 0e-398DD > +#include "dfp-default-init-1.c" > diff --git a/gcc/testsuite/gcc.dg/torture/dfp-default-init-3.c b/gcc/tests= uite/gcc.dg/torture/dfp-default-init-3.c > new file mode 100644 > index 00000000000..cdf73508c76 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/torture/dfp-default-init-3.c > @@ -0,0 +1,8 @@ > +/* Test that default-initialized DFP values consistently have the least q= uantum > + exponent. */ > +/* { dg-do run } */ > +/* { dg-require-effective-target dfp } */ > + > +#define TYPE _Decimal128 > +#define ZEROFP 0e-6176DL > +#include "dfp-default-init-1.c" > diff --git a/gcc/tree.cc b/gcc/tree.cc > index fed1434d141..007c9325b17 100644 > --- a/gcc/tree.cc > +++ b/gcc/tree.cc > @@ -2385,12 +2385,12 @@ build_real (tree type, REAL_VALUE_TYPE d) > tree v; > int overflow =3D 0; >=20 > - /* dconst{1,2,m1,half} are used in various places in > + /* dconst{0,1,2,m1,half} are used in various places in > the middle-end and optimizers, allow them here > even for decimal floating point types as an exception > by converting them to decimal. */ > if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)) > - && d.cl =3D=3D rvc_normal > + && (d.cl =3D=3D rvc_normal || d.cl =3D=3D rvc_zero) > && !d.decimal) > { > if (memcmp (&d, &dconst1, sizeof (d)) =3D=3D 0) > @@ -2401,6 +2401,15 @@ build_real (tree type, REAL_VALUE_TYPE d) > decimal_real_from_string (&d, "-1"); > else if (memcmp (&d, &dconsthalf, sizeof (d)) =3D=3D 0) > decimal_real_from_string (&d, "0.5"); > + else if (memcmp (&d, &dconst0, sizeof (d)) =3D=3D 0) > + { > + /* Make sure to give zero the minimum quantum exponent for > + the type (which corresponds to all bits zero). */ > + const struct real_format *fmt =3D REAL_MODE_FORMAT (TYPE_MODE (type= )); > + char buf[16]; > + sprintf (buf, "0e%d", fmt->emin - fmt->p); > + decimal_real_from_string (&d, buf); > + } > else > gcc_unreachable (); > } >=20 > --=20 > Joseph S. Myers > joseph@codesourcery.com