aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorRoger Sayle <roger@eyesopen.com>2003-05-06 03:14:10 +0000
committerRoger Sayle <sayle@gcc.gnu.org>2003-05-06 03:14:10 +0000
commite82a312b55f88fc3d035e0c24409c71e043e9633 (patch)
tree2a99b03c08d036a34af9b1b5c844c11e3d67a3af /gcc
parentd7b4a590279f012756741c12f2027d714ea776c4 (diff)
downloadgcc-e82a312b55f88fc3d035e0c24409c71e043e9633.zip
gcc-e82a312b55f88fc3d035e0c24409c71e043e9633.tar.gz
gcc-e82a312b55f88fc3d035e0c24409c71e043e9633.tar.bz2
real.c (real_powi): New function to calculate the value of a real raised to an integer power, i.e.
* real.c (real_powi): New function to calculate the value of a real raised to an integer power, i.e. pow(x,n) for int n. (real_sqrt): Convert to using the faster do_add, do_multiply and do_divide API for consistency with the rest of real.c. * real.h (real_powi): Prototype here. * builtins.c (fold_builtin): Avoid local variable mode when evaluating sqrt at compile time. Attempt to evaluate pow at compile-time, by checking for an integral exponent. * gcc.dg/builtins-14.c: New test case. From-SVN: r66515
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog11
-rw-r--r--gcc/builtins.c26
-rw-r--r--gcc/real.c80
-rw-r--r--gcc/real.h6
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/gcc.dg/builtins-14.c26
6 files changed, 138 insertions, 15 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index e4a15d3..ae2db06 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,14 @@
+2003-05-05 Roger Sayle <roger@eyesopen.com>
+
+ * real.c (real_powi): New function to calculate the value of
+ a real raised to an integer power, i.e. pow(x,n) for int n.
+ (real_sqrt): Convert to using the faster do_add, do_multiply
+ and do_divide API for consistency with the rest of real.c.
+ * real.h (real_powi): Prototype here.
+ * builtins.c (fold_builtin): Avoid local variable mode when
+ evaluating sqrt at compile time. Attempt to evaluate pow at
+ compile-time, by checking for an integral exponent.
+
2003-05-05 Richard Henderson <rth@redhat.com>
* doc/extend.texi (Variable Attributes): Re-sort table and tidy.
diff --git a/gcc/builtins.c b/gcc/builtins.c
index feee531..c309997 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -5011,12 +5011,10 @@ fold_builtin (exp)
if (TREE_CODE (arg) == REAL_CST
&& ! TREE_CONSTANT_OVERFLOW (arg))
{
- enum machine_mode mode;
REAL_VALUE_TYPE r, x;
x = TREE_REAL_CST (arg);
- mode = TYPE_MODE (type);
- if (real_sqrt (&r, mode, &x)
+ if (real_sqrt (&r, TYPE_MODE (type), &x)
|| (!flag_trapping_math && !flag_errno_math))
return build_real (type, r);
}
@@ -5229,6 +5227,28 @@ fold_builtin (exp)
return build_function_call_expr (sqrtfn, arglist);
}
}
+
+ /* Attempt to evaluate pow at compile-time. */
+ if (TREE_CODE (arg0) == REAL_CST
+ && ! TREE_CONSTANT_OVERFLOW (arg0))
+ {
+ REAL_VALUE_TYPE cint;
+ HOST_WIDE_INT n;
+
+ n = real_to_integer(&c);
+ real_from_integer (&cint, VOIDmode, n,
+ n < 0 ? -1 : 0, 0);
+ if (real_identical (&c, &cint))
+ {
+ REAL_VALUE_TYPE x;
+ bool inexact;
+
+ x = TREE_REAL_CST (arg0);
+ inexact = real_powi (&x, TYPE_MODE (type), &x, n);
+ if (flag_unsafe_math_optimizations || !inexact)
+ return build_real (type, x);
+ }
+ }
}
/* Optimize pow(exp(x),y) = exp(x*y). */
diff --git a/gcc/real.c b/gcc/real.c
index db658fd..16cd02a 100644
--- a/gcc/real.c
+++ b/gcc/real.c
@@ -4606,7 +4606,7 @@ real_sqrt (r, mode, x)
if (!init)
{
- real_arithmetic (&halfthree, PLUS_EXPR, &dconst1, &dconsthalf);
+ do_add (&halfthree, &dconst1, &dconsthalf, 0);
init = true;
}
@@ -4618,11 +4618,11 @@ real_sqrt (r, mode, x)
for (iter = 0; iter < 16; iter++)
{
/* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
- real_arithmetic (&t, MULT_EXPR, x, &i);
- real_arithmetic (&h, MULT_EXPR, &t, &i);
- real_arithmetic (&t, MULT_EXPR, &h, &dconsthalf);
- real_arithmetic (&h, MINUS_EXPR, &halfthree, &t);
- real_arithmetic (&t, MULT_EXPR, &i, &h);
+ do_multiply (&t, x, &i);
+ do_multiply (&h, &t, &i);
+ do_multiply (&t, &h, &dconsthalf);
+ do_add (&h, &halfthree, &t, 1);
+ do_multiply (&t, &i, &h);
/* Check for early convergence. */
if (iter >= 6 && real_identical (&i, &t))
@@ -4633,12 +4633,12 @@ real_sqrt (r, mode, x)
}
/* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
- real_arithmetic (&t, MULT_EXPR, x, &i);
- real_arithmetic (&h, MULT_EXPR, &t, &i);
- real_arithmetic (&i, MINUS_EXPR, &dconst1, &h);
- real_arithmetic (&h, MULT_EXPR, &t, &i);
- real_arithmetic (&i, MULT_EXPR, &dconsthalf, &h);
- real_arithmetic (&h, PLUS_EXPR, &t, &i);
+ do_multiply (&t, x, &i);
+ do_multiply (&h, &t, &i);
+ do_add (&i, &dconst1, &h, 1);
+ do_multiply (&h, &t, &i);
+ do_multiply (&i, &dconsthalf, &h);
+ do_add (&h, &t, &i, 0);
/* ??? We need a Tuckerman test to get the last bit. */
@@ -4646,3 +4646,59 @@ real_sqrt (r, mode, x)
return true;
}
+/* Calculate X raised to the integer exponent N in mode MODE and store
+ the result in R. Return true if the result may be inexact due to
+ loss of precision. The algorithm is the classic "left-to-right binary
+ method" described in section 4.6.3 of Donald Knuth's "Seminumerical
+ Algorithms", "The Art of Computer Programming", Volume 2. */
+
+bool
+real_powi (r, mode, x, n)
+ REAL_VALUE_TYPE *r;
+ enum machine_mode mode;
+ const REAL_VALUE_TYPE *x;
+ HOST_WIDE_INT n;
+{
+ unsigned HOST_WIDE_INT bit;
+ REAL_VALUE_TYPE t;
+ bool inexact = false;
+ bool init = false;
+ bool neg;
+ int i;
+
+ if (n == 0)
+ {
+ *r = dconst1;
+ return false;
+ }
+ else if (n < 0)
+ {
+ /* Don't worry about overflow, from now on n is unsigned. */
+ neg = true;
+ n = -n;
+ }
+ else
+ neg = false;
+
+ t = *x;
+ bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
+ for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
+ {
+ if (init)
+ {
+ inexact |= do_multiply (&t, &t, &t);
+ if (n & bit)
+ inexact |= do_multiply (&t, &t, x);
+ }
+ else if (n & bit)
+ init = true;
+ bit >>= 1;
+ }
+
+ if (neg)
+ inexact |= do_divide (&t, &dconst1, &t);
+
+ real_convert (r, mode, &t);
+ return inexact;
+}
+
diff --git a/gcc/real.h b/gcc/real.h
index 0a470a5..1611392 100644
--- a/gcc/real.h
+++ b/gcc/real.h
@@ -365,4 +365,10 @@ extern bool real_sqrt PARAMS ((REAL_VALUE_TYPE *,
enum machine_mode,
const REAL_VALUE_TYPE *));
+/* Calculate R as X raised to the integer exponent N in mode MODE. */
+extern bool real_powi PARAMS ((REAL_VALUE_TYPE *,
+ enum machine_mode,
+ const REAL_VALUE_TYPE *,
+ HOST_WIDE_INT));
+
#endif /* ! GCC_REAL_H */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index a2e5159..9c161a7 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2003-05-05 Roger Sayle <roger@eyesopen.com>
+
+ * gcc.dg/builtins-14.c: New test case.
+
2003-05-05 Janis Johnson <janis187@us.ibm.com>
* lib/compat.exp (compat-execute): New argument.
diff --git a/gcc/testsuite/gcc.dg/builtins-14.c b/gcc/testsuite/gcc.dg/builtins-14.c
new file mode 100644
index 0000000..e025391
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/builtins-14.c
@@ -0,0 +1,26 @@
+/* Copyright (C) 2003 Free Software Foundation.
+
+ Check that constant folding of built-in math functions doesn't
+ break anything and produces the expected results.
+
+ Written by Roger Sayle, 9th April 2003. */
+
+/* { dg-do link } */
+/* { dg-options "-O2" } */
+
+extern void link_error(void);
+
+extern double pow(double,double);
+
+
+int main()
+{
+ if (pow (2.0, 3.0) != 8.0)
+ link_error ();
+
+ if (pow (2.0, -3.0) != 0.125)
+ link_error ();
+
+ return 0;
+}
+