1

I have some code which multiplies complex numbers, and have noticed that mulxc3 (long double version of muldc3) is being called frequently: i.e. the complex number multiplications are not being inlined.

I am compiling with g++ version 7.5, with -O3 and --ffast-math.

It is similar to this question, except the problem persists when I compile with -ffast-math. Since I do not require checking for whether the arguments are Inf or NaN, I was considering making my own very simple complex class without such checks to allow the multiplication to be inlined, but given my lack of C++ proficiency, and having read this article makes me think that would be counterproductive.

So, is there a way to change either my code or compilation process so that I can keep using std::complex, but inline the multiplication?

user366202
  • 249
  • 2
  • 7
  • 4
    Can you [edit] your question to include a [mre] that demonstrates the problem? – 1201ProgramAlarm Jun 13 '20 at 22:55
  • @1201ProgramAlarm not easily. Every simple example I try has mulxc3 appearing when compiled without optimizations, but the this vanishes when the --ffast-math flag is switched on. It will probably take at least a few hours to pinpoint which part of my code is preventing inlining. I will try to do this and I will edit my question once I have found the section, but in the mean time any answer of the form "[some sort of operation] will prevent inlining for muldc3 occurring in [some context]" would also be very much appreciated – user366202 Jun 13 '20 at 23:09
  • Maybe its worth trying C types for it https://stackoverflow.com/questions/10540228/c-complex-numbers-in-c – Abdurrahim Jun 13 '20 at 23:11
  • @Abdurrahim I considered this but would much prefer to use std::complex if possible, because portability is important, and at some point I will have to work on a different OS and use a different compiler or version – user366202 Jun 13 '20 at 23:21
  • it seems to have been due to an error in my makefile which meant that --ffast-math was not actually being called after all. – user366202 Jun 14 '20 at 00:21
  • 1
    Unless you've conducted profiling and obtained *evidence* that complex multiplication is a performance bottleneck (as distinct from being "done often", which is not the same thing) I suggest not worrying too much about whether the operation is inlined. Modern compilers do a pretty good job - admittedly not perfect - optimising code as needed, and the emitted code is not always intuitive. If you just write code without a stronger case than "I think I can do it faster", it's a good bet that your "hand-rolled to inline" version will be less efficient than what the compiler already does for you. – Peter Jun 14 '20 at 01:43
  • in my case, yes, the multiplication was taking over half the CPU time. Ensuring that -ffast-math was actually turned on (so that the multiplication was inlined) meant that my overall runtime decreased by a factor of more than 3. The increase in performance compared to just -O3 was suprising. – user366202 Jun 14 '20 at 04:16

0 Answers0