https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96811

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargl at gcc dot gnu.org

--- Comment #2 from kargl at gcc dot gnu.org ---
(In reply to Tobias Burnus from comment #0)
> Fabio Azevedo has some observations at
> https://gcc.gnu.org/pipermail/fortran/2020-August/054931.html
> 
> In particular:
> 
>   The only difference in behaviour I see is that when the exponent is
>   negative, libgcc2 calculates 1/(x**(-n)) while libgfortran calculates
>   (1/x)**(-n). The first version being more accurate.
> [...]
>   On the top of that x**n with x real for negative exponents appears to yield
>   results more compatible with 1/(x**(-n)) than (1/x)**(-n).
> 
> libgcc/libgcc2.c has:
> 
> NAME (TYPE x, int m)
> {
>   unsigned int n = m < 0 ? -m : m;
>   TYPE y = n % 2 ? x : 1;
>   while (n >>= 1)
>     {
>       x = x * x;
>       if (n % 2)
>         y = y * x;
>     }
>   return m < 0 ? 1/y : y;  // <<< division done at the end.
> }
> 
> 
> While libgfortran/m4/pow.m4 has (for noninteger x):
> 

For real x, gfortran does not generate a call to libgfortran function.

% cat a.f90
function foo(x,n)
  real foo, x
  integer n
  foo = x**n
end

% cat a.f90.004.original
foo (real(kind=4) & restrict x, integer(kind=4) & restrict n)
{
  real(kind=4) __result_foo;

  {
    real(kind=4) D.3806;

    D.3806 = *x;
    __result_foo = __builtin_powif (D.3806, *n);
  }
  return __result_foo;
}

For complex or integer x, gfortran does generate the call.
With the obvious change to a.f90,

% cat a.f90.004t.original
foo (complex(kind=4) & restrict x, integer(kind=4) & restrict n)
{
  complex(kind=4) __result_foo;

  {
    complex(kind=4) D.3806;

    D.3806 = *x;
    __result_foo = _gfortran_pow_c4_i4 (D.3806, *n);
  }
  return __result_foo;
}

Richard's observation about rounding error is probably sufficient
reason to prefer libgcc2.c.

Reply via email to