On Mon, Jun 30, 2025 at 7:05 PM Alexander Monakov <amona...@ispras.ru> wrote:
>
>
> On Mon, 30 Jun 2025, Richard Biener wrote:
>
> > > * gcc.target/i386/pr116979.c
> > >
> > > Here we check that FMA is used for complex multiplication. This is a case
> > > where use of FMA is detrimental for accuracy, but I guess we can choose
> > > to preserve the status quo. I can look into teaching
> > > expand_complex_multiplication to emit FMAs.
> >
> > Yeah, I guess that's within -ffp-contract=on constraints.  When you say it's
> > detrimental for accuracy, is that a general statement for complex multiply
> > lowering?  If so we possibly do not want FMAs?
>
> We may want FMAs when completely disregarding accuracy for speed 
> (-ffast-math),
> but otherwise, yes, I meant that in general our strategy is not good. We are
> lowering complex multiplication by components
>
>   (x + yi)*(z + wi) = (x*z - y*w) + (x*w + y*z)i
>
> and when FMA is available, we end up with something like
>
>   fma(x, z, -y*w)
>
> for the real part and likewise fma(x, w, y*z) for the imaginary part, which is
> asymmetrical with respect to operands, and, for instance, yields wrong results
> for conjugate vectors: take z=x and w=-y, then imaginary part fma(x, w, y*z) 
> is
> fma(x, -y, y*x), which is negated round-off tail of y*x, but should be zero.
>
> FMA is useful for improving accuracy of sum/difference of two products, where
> you'd compute x*w + y*z as
>
>   xw = x*w
>   xw_tail = fma(x, w, -xw)
>   tmp = fma(y, z, xw)
>   return tmp + xw_tail
>
> but apart from this, I'd say we don't want to emit FMAs in
> expand_complex_multiplication. LLVM seems to produce FMAs for complex product
> only under -ffp-contract=fast.
>
> What are the next steps here, then?

For this case I would then suggest to not change complex lowering to emit
FMAs and adjust the testcases to pass -ffp-contract=fast instead, possibly
even add a testcase variant with -ffp-contract=on checking FMAs are not
used with a rationale present in a comment.

Richard.

>
> Thank you.
> Alexander

Reply via email to