_multiplyComplex, real part

Percentage Accurate: 99.2% → 99.5%
Time: 2.2s
Alternatives: 4
Speedup: 2.3×

Specification

?
\[x.re \cdot y.re - x.im \cdot y.im \]

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Alternative 1: 99.5% accurate, 0.1× speedup?

\[\mathsf{fma}\left(x.re, y.re, -x.im \cdot y.im\right) \]
Derivation
  1. Initial program 99.2%

    \[x.re \cdot y.re - x.im \cdot y.im \]
  2. Step-by-step derivation
    1. fma-neg99.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x.re, y.re, -x.im \cdot y.im\right)} \]
    2. distribute-rgt-neg-in99.6%

      \[\leadsto \mathsf{fma}\left(x.re, y.re, \color{blue}{x.im \cdot \left(-y.im\right)}\right) \]
  3. Simplified99.6%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x.re, y.re, x.im \cdot \left(-y.im\right)\right)} \]
  4. Final simplification99.6%

    \[\leadsto \mathsf{fma}\left(x.re, y.re, -x.im \cdot y.im\right) \]

Alternative 2: 67.8% accurate, 0.6× speedup?

\[\begin{array}{l} \mathbf{if}\;x.re \leq -1.7 \cdot 10^{+17} \lor \neg \left(x.re \leq 1.45 \cdot 10^{-178}\right) \land \left(x.re \leq 2 \cdot 10^{-135} \lor \neg \left(x.re \leq 3.8 \cdot 10^{-37}\right)\right):\\ \;\;\;\;x.re \cdot y.re\\ \mathbf{else}:\\ \;\;\;\;-x.im \cdot y.im\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if x.re < -1.7e17 or 1.4499999999999999e-178 < x.re < 2.0000000000000001e-135 or 3.8000000000000004e-37 < x.re

    1. Initial program 98.5%

      \[x.re \cdot y.re - x.im \cdot y.im \]
    2. Taylor expanded in x.re around inf 69.0%

      \[\leadsto \color{blue}{x.re \cdot y.re} \]

    if -1.7e17 < x.re < 1.4499999999999999e-178 or 2.0000000000000001e-135 < x.re < 3.8000000000000004e-37

    1. Initial program 100.0%

      \[x.re \cdot y.re - x.im \cdot y.im \]
    2. Taylor expanded in x.re around 0 67.8%

      \[\leadsto \color{blue}{-1 \cdot \left(y.im \cdot x.im\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg67.8%

        \[\leadsto \color{blue}{-y.im \cdot x.im} \]
      2. distribute-rgt-neg-in67.8%

        \[\leadsto \color{blue}{y.im \cdot \left(-x.im\right)} \]
    4. Simplified67.8%

      \[\leadsto \color{blue}{y.im \cdot \left(-x.im\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x.re \leq -1.7 \cdot 10^{+17} \lor \neg \left(x.re \leq 1.45 \cdot 10^{-178}\right) \land \left(x.re \leq 2 \cdot 10^{-135} \lor \neg \left(x.re \leq 3.8 \cdot 10^{-37}\right)\right):\\ \;\;\;\;x.re \cdot y.re\\ \mathbf{else}:\\ \;\;\;\;-x.im \cdot y.im\\ \end{array} \]

Alternative 3: 99.2% accurate, 1.0× speedup?

\[x.re \cdot y.re - x.im \cdot y.im \]
Derivation
  1. Initial program 99.2%

    \[x.re \cdot y.re - x.im \cdot y.im \]
  2. Final simplification99.2%

    \[\leadsto x.re \cdot y.re - x.im \cdot y.im \]

Alternative 4: 52.7% accurate, 2.3× speedup?

\[x.re \cdot y.re \]
Derivation
  1. Initial program 99.2%

    \[x.re \cdot y.re - x.im \cdot y.im \]
  2. Taylor expanded in x.re around inf 55.0%

    \[\leadsto \color{blue}{x.re \cdot y.re} \]
  3. Final simplification55.0%

    \[\leadsto x.re \cdot y.re \]

Reproduce

?
herbie shell --seed 2023167 
(FPCore (x.re x.im y.re y.im)
  :name "_multiplyComplex, real part"
  :precision binary64
  (- (* x.re y.re) (* x.im y.im)))