PR middle-end/53267: Constant fold BUILT_IN_FMOD.

Message ID 027d01d75c99$efdc68b0$cf953a10$@nextmovesoftware.com
State New
Headers show
Series
  • PR middle-end/53267: Constant fold BUILT_IN_FMOD.
Related show

Commit Message

Roger Sayle June 8, 2021, 7:10 p.m.
Here's a three line patch to implement constant folding for fmod,
fmodf and fmodl, which resolves an enhancement request from 2012.

The following patch has been tested on x86_64-pc-linux-gnu with
a make bootstrap and make -k check with no new failures.

Ok for mainline?


2020-06-08  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	PR middle-end/53267
	* fold-const-call.c (fold_const_call_sss) [CASE_CFN_FMOD]:
	Support evaluation of fmod/fmodf/fmodl at compile-time.

gcc/testsuite/ChangeLog
	* gcc.dg/builtins-70.c: New test.


Roger
--
Roger Sayle
NextMove Software
Cambridge, UK

/* Copyright (C) 2021 Free Software Foundation.

   Check that constant folding of built-in fmod functions doesn't
   break anything and produces the expected results.

/* { dg-do link } */
/* { dg-options "-O2 -ffast-math" } */

extern void link_error(void);

extern double fmod(double,double);
extern float fmodf(float,float);
extern long double fmodl(long double,long double);

int main()
{
  if (fmod (6.5, 2.3) < 1.8999 || fmod (6.5, 2.3) > 1.9001)
    link_error ();
  if (fmod (-6.5, 2.3) < -1.9001 || fmod (-6.5, 2.3) > -1.8999)
    link_error ();
  if (fmod (6.5, -2.3) < 1.8999 || fmod (6.5, -2.3) > 1.9001)
    link_error ();
  if (fmod (-6.5, -2.3) < -1.9001 || fmod (-6.5, -2.3) > -1.8999)
    link_error ();

  if (fmodf (6.5f, 2.3f) < 1.8999f || fmodf (6.5f, 2.3f) > 1.9001f)
    link_error ();
  if (fmodf (-6.5f, 2.3f) < -1.9001f || fmodf (-6.5f, 2.3f) > -1.8999f)
    link_error ();
  if (fmodf (6.5f, -2.3f) < 1.8999f || fmodf (6.5f, -2.3f) > 1.9001f)
    link_error ();
  if (fmodf (-6.5f, -2.3f) < -1.9001f || fmodf (-6.5f, -2.3f) > -1.8999f)
    link_error ();

  if (fmodl (6.5l, 2.3l) < 1.8999l || fmod (6.5l, 2.3l) > 1.9001l)
    link_error ();
  if (fmodl (-6.5l, 2.3l) < -1.9001l || fmod (-6.5l, 2.3l) > -1.8999l)
    link_error ();
  if (fmodl (6.5l, -2.3l) < 1.8999l || fmod (6.5l, -2.3l) > 1.9001l)
    link_error ();
  if (fmodl (-6.5l, -2.3l) < -1.9001l || fmod (-6.5l, -2.3l) > -1.8999l)
    link_error ();

  return 0;
}

Comments

Jeff Law via Gcc-patches June 9, 2021, 10:51 a.m. | #1
On Tue, Jun 8, 2021 at 9:36 PM Roger Sayle <roger@nextmovesoftware.com> wrote:
>

>

> Here's a three line patch to implement constant folding for fmod,

> fmodf and fmodl, which resolves an enhancement request from 2012.

>

> The following patch has been tested on x86_64-pc-linux-gnu with

> a make bootstrap and make -k check with no new failures.

>

> Ok for mainline?


OK.  I double-checked and mpfr_fmod appeared in mpfr 2.4.0 and
we require at least 3.1.0.

Thanks,
Richard.

>

> 2020-06-08  Roger Sayle  <roger@nextmovesoftware.com>

>

> gcc/ChangeLog

>         PR middle-end/53267

>         * fold-const-call.c (fold_const_call_sss) [CASE_CFN_FMOD]:

>         Support evaluation of fmod/fmodf/fmodl at compile-time.

>

> gcc/testsuite/ChangeLog

>         * gcc.dg/builtins-70.c: New test.

>

>

> Roger

> --

> Roger Sayle

> NextMove Software

> Cambridge, UK

>
Jeff Law via Gcc-patches June 9, 2021, 3:26 p.m. | #2
On 6/9/2021 4:51 AM, Richard Biener via Gcc-patches wrote:
> On Tue, Jun 8, 2021 at 9:36 PM Roger Sayle <roger@nextmovesoftware.com> wrote:

>>

>> Here's a three line patch to implement constant folding for fmod,

>> fmodf and fmodl, which resolves an enhancement request from 2012.

>>

>> The following patch has been tested on x86_64-pc-linux-gnu with

>> a make bootstrap and make -k check with no new failures.

>>

>> Ok for mainline?

> OK.  I double-checked and mpfr_fmod appeared in mpfr 2.4.0 and

> we require at least 3.1.0.

I'm not sure if Roger has write access these days, so I went ahead and 
committed the patch on his behalf.

jeff
Roger Sayle June 9, 2021, 5:01 p.m. | #3
Hi Jeff (and Richard),
Many thanks to you both.  Fingers-crossed my write-access (after approval) still works
(I think it's just maintainer status that I've lost over time), but finding time to contribute
is getting much harder, so my response time is much slower than it was a decade ago.
Having someone help with committing patches is always very much appreciated.
Cheers,
Roger
--

-----Original Message-----
From: Jeff Law <jeffreyalaw@gmail.com> 

Sent: 09 June 2021 16:27
To: Richard Biener <richard.guenther@gmail.com>; Roger Sayle <roger@nextmovesoftware.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] PR middle-end/53267: Constant fold BUILT_IN_FMOD.



On 6/9/2021 4:51 AM, Richard Biener via Gcc-patches wrote:
> On Tue, Jun 8, 2021 at 9:36 PM Roger Sayle <roger@nextmovesoftware.com> wrote:

>>

>> Here's a three line patch to implement constant folding for fmod, 

>> fmodf and fmodl, which resolves an enhancement request from 2012.

>>

>> The following patch has been tested on x86_64-pc-linux-gnu with a 

>> make bootstrap and make -k check with no new failures.

>>

>> Ok for mainline?

> OK.  I double-checked and mpfr_fmod appeared in mpfr 2.4.0 and we 

> require at least 3.1.0.

I'm not sure if Roger has write access these days, so I went ahead and committed the patch on his behalf.

jeff

Patch

diff --git a/gcc/fold-const-call.c b/gcc/fold-const-call.c
index a1d70b6..d6cb9b1 100644
--- a/gcc/fold-const-call.c
+++ b/gcc/fold-const-call.c
@@ -1375,6 +1375,9 @@  fold_const_call_sss (real_value *result, combined_fn fn,
     CASE_CFN_FDIM:
       return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
 
+    CASE_CFN_FMOD:
+      return do_mpfr_arg2 (result, mpfr_fmod, arg0, arg1, format);
+
     CASE_CFN_HYPOT:
       return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);