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

--- Comment #1 from kargl at gcc dot gnu.org ---
Fortran code used in harmonic analysis often contains lines
of form 

   p = cmplx(cos(x),sin(x))   ! compute exp(i*x) for real x

On platforms with a sincos(x, &s, &c) routine the above should
be transformed to 

   call sincos(x, &s, &c)
   p = cmplx(c, s)

The reason is that both sin() and cos() perform argument reduction
of x into the range [0,pi/4].  sincos() performs this reduce once,
and hence is typically faster.   The same optimization should apply 
to C/C++ code.

#include <math.h>

double
foo(double x)
{
   double c,s,res;
   c = cos(x);
   s = sin(x);
   res = c * s;
   return res;
}

% ~/work/x/bin/gcc -o - -S -O3 -o a.s a.c
        .file   "a.c"
        .text
        .p2align 4
        .globl  foo
        .type   foo, @function
foo:
.LFB3:
        .cfi_startproc
        subq    $24, %rsp
        .cfi_def_cfa_offset 32
        movsd   %xmm0, 8(%rsp)
        call    cos
        movsd   8(%rsp), %xmm1
        movsd   %xmm0, (%rsp)
        movapd  %xmm1, %xmm0
        call    sin
        mulsd   (%rsp), %xmm0
        addq    $24, %rsp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc
.LFE3:
        .size   foo, .-foo
        .ident  "GCC: (GNU) 9.0.1 20190125 (experimental)"
        .section        .note.GNU-stack,"",@progbits

Reply via email to