_multiplyComplex, imaginary part

Percentage Accurate: 99.1% → 99.5%
Time: 4.8s
Alternatives: 3
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ x.re \cdot y.im + x.im \cdot y.re \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (+ (* x.re y.im) (* x.im y.re)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = (x_46re * y_46im) + (x_46im * y_46re)
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return (x_46_re * y_46_im) + (x_46_im * y_46_re)
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(x_46_re * y_46_im) + Float64(x_46_im * y_46_re))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = (x_46_re * y_46_im) + (x_46_im * y_46_re);
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(x$46$re * y$46$im), $MachinePrecision] + N[(x$46$im * y$46$re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x.re \cdot y.im + x.im \cdot y.re
\end{array}

Sampling outcomes in binary64 precision:

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 3 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.

Initial Program: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x.re \cdot y.im + x.im \cdot y.re \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (+ (* x.re y.im) (* x.im y.re)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = (x_46re * y_46im) + (x_46im * y_46re)
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return (x_46_re * y_46_im) + (x_46_im * y_46_re)
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(x_46_re * y_46_im) + Float64(x_46_im * y_46_re))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = (x_46_re * y_46_im) + (x_46_im * y_46_re);
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(x$46$re * y$46$im), $MachinePrecision] + N[(x$46$im * y$46$re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x.re \cdot y.im + x.im \cdot y.re
\end{array}

Alternative 1: 99.5% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y.re, x.im, y.im \cdot x.re\right) \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (fma y.re x.im (* y.im x.re)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return fma(y_46_re, x_46_im, (y_46_im * x_46_re));
}
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return fma(y_46_re, x_46_im, Float64(y_46_im * x_46_re))
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(y$46$re * x$46$im + N[(y$46$im * x$46$re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

    \[x.re \cdot y.im + x.im \cdot y.re \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{x.re \cdot y.im + x.im \cdot y.re} \]
    2. +-commutativeN/A

      \[\leadsto \color{blue}{x.im \cdot y.re + x.re \cdot y.im} \]
    3. lift-*.f64N/A

      \[\leadsto \color{blue}{x.im \cdot y.re} + x.re \cdot y.im \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{y.re \cdot x.im} + x.re \cdot y.im \]
    5. lower-fma.f6499.6

      \[\leadsto \color{blue}{\mathsf{fma}\left(y.re, x.im, x.re \cdot y.im\right)} \]
    6. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(y.re, x.im, \color{blue}{x.re \cdot y.im}\right) \]
    7. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(y.re, x.im, \color{blue}{y.im \cdot x.re}\right) \]
    8. lower-*.f6499.6

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(y.re, x.im, y.im \cdot x.re\right)} \]
  5. Add Preprocessing

Alternative 2: 76.7% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x.re \cdot y.im \leq -500 \lor \neg \left(x.re \cdot y.im \leq 5 \cdot 10^{+50}\right):\\ \;\;\;\;y.im \cdot x.re\\ \mathbf{else}:\\ \;\;\;\;y.re \cdot x.im\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (or (<= (* x.re y.im) -500.0) (not (<= (* x.re y.im) 5e+50)))
   (* y.im x.re)
   (* y.re x.im)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (((x_46_re * y_46_im) <= -500.0) || !((x_46_re * y_46_im) <= 5e+50)) {
		tmp = y_46_im * x_46_re;
	} else {
		tmp = y_46_re * x_46_im;
	}
	return tmp;
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    real(8) :: tmp
    if (((x_46re * y_46im) <= (-500.0d0)) .or. (.not. ((x_46re * y_46im) <= 5d+50))) then
        tmp = y_46im * x_46re
    else
        tmp = y_46re * x_46im
    end if
    code = tmp
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (((x_46_re * y_46_im) <= -500.0) || !((x_46_re * y_46_im) <= 5e+50)) {
		tmp = y_46_im * x_46_re;
	} else {
		tmp = y_46_re * x_46_im;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	tmp = 0
	if ((x_46_re * y_46_im) <= -500.0) or not ((x_46_re * y_46_im) <= 5e+50):
		tmp = y_46_im * x_46_re
	else:
		tmp = y_46_re * x_46_im
	return tmp
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if ((Float64(x_46_re * y_46_im) <= -500.0) || !(Float64(x_46_re * y_46_im) <= 5e+50))
		tmp = Float64(y_46_im * x_46_re);
	else
		tmp = Float64(y_46_re * x_46_im);
	end
	return tmp
end
function tmp_2 = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0;
	if (((x_46_re * y_46_im) <= -500.0) || ~(((x_46_re * y_46_im) <= 5e+50)))
		tmp = y_46_im * x_46_re;
	else
		tmp = y_46_re * x_46_im;
	end
	tmp_2 = tmp;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[Or[LessEqual[N[(x$46$re * y$46$im), $MachinePrecision], -500.0], N[Not[LessEqual[N[(x$46$re * y$46$im), $MachinePrecision], 5e+50]], $MachinePrecision]], N[(y$46$im * x$46$re), $MachinePrecision], N[(y$46$re * x$46$im), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x.re \cdot y.im \leq -500 \lor \neg \left(x.re \cdot y.im \leq 5 \cdot 10^{+50}\right):\\
\;\;\;\;y.im \cdot x.re\\

\mathbf{else}:\\
\;\;\;\;y.re \cdot x.im\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x.re y.im) < -500 or 5e50 < (*.f64 x.re y.im)

    1. Initial program 98.2%

      \[x.re \cdot y.im + x.im \cdot y.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.re around 0

      \[\leadsto \color{blue}{x.im \cdot y.re} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{y.re \cdot x.im} \]
      2. lower-*.f6428.6

        \[\leadsto \color{blue}{y.re \cdot x.im} \]
    5. Applied rewrites28.6%

      \[\leadsto \color{blue}{y.re \cdot x.im} \]
    6. Step-by-step derivation
      1. Applied rewrites13.9%

        \[\leadsto \left|y.re \cdot x.im\right| \]
      2. Taylor expanded in x.re around inf

        \[\leadsto \color{blue}{x.re \cdot y.im} \]
      3. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{y.im \cdot x.re} \]
        2. lower-*.f6475.4

          \[\leadsto \color{blue}{y.im \cdot x.re} \]
      4. Applied rewrites75.4%

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

      if -500 < (*.f64 x.re y.im) < 5e50

      1. Initial program 100.0%

        \[x.re \cdot y.im + x.im \cdot y.re \]
      2. Add Preprocessing
      3. Taylor expanded in x.re around 0

        \[\leadsto \color{blue}{x.im \cdot y.re} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{y.re \cdot x.im} \]
        2. lower-*.f6481.9

          \[\leadsto \color{blue}{y.re \cdot x.im} \]
      5. Applied rewrites81.9%

        \[\leadsto \color{blue}{y.re \cdot x.im} \]
    7. Recombined 2 regimes into one program.
    8. Final simplification79.0%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x.re \cdot y.im \leq -500 \lor \neg \left(x.re \cdot y.im \leq 5 \cdot 10^{+50}\right):\\ \;\;\;\;y.im \cdot x.re\\ \mathbf{else}:\\ \;\;\;\;y.re \cdot x.im\\ \end{array} \]
    9. Add Preprocessing

    Alternative 3: 50.7% accurate, 2.3× speedup?

    \[\begin{array}{l} \\ y.im \cdot x.re \end{array} \]
    (FPCore (x.re x.im y.re y.im) :precision binary64 (* y.im x.re))
    double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
    	return y_46_im * x_46_re;
    }
    
    real(8) function code(x_46re, x_46im, y_46re, y_46im)
        real(8), intent (in) :: x_46re
        real(8), intent (in) :: x_46im
        real(8), intent (in) :: y_46re
        real(8), intent (in) :: y_46im
        code = y_46im * x_46re
    end function
    
    public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
    	return y_46_im * x_46_re;
    }
    
    def code(x_46_re, x_46_im, y_46_re, y_46_im):
    	return y_46_im * x_46_re
    
    function code(x_46_re, x_46_im, y_46_re, y_46_im)
    	return Float64(y_46_im * x_46_re)
    end
    
    function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
    	tmp = y_46_im * x_46_re;
    end
    
    code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(y$46$im * x$46$re), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    y.im \cdot x.re
    \end{array}
    
    Derivation
    1. Initial program 99.2%

      \[x.re \cdot y.im + x.im \cdot y.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.re around 0

      \[\leadsto \color{blue}{x.im \cdot y.re} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{y.re \cdot x.im} \]
      2. lower-*.f6458.1

        \[\leadsto \color{blue}{y.re \cdot x.im} \]
    5. Applied rewrites58.1%

      \[\leadsto \color{blue}{y.re \cdot x.im} \]
    6. Step-by-step derivation
      1. Applied rewrites32.0%

        \[\leadsto \left|y.re \cdot x.im\right| \]
      2. Taylor expanded in x.re around inf

        \[\leadsto \color{blue}{x.re \cdot y.im} \]
      3. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{y.im \cdot x.re} \]
        2. lower-*.f6446.0

          \[\leadsto \color{blue}{y.im \cdot x.re} \]
      4. Applied rewrites46.0%

        \[\leadsto \color{blue}{y.im \cdot x.re} \]
      5. Add Preprocessing

      Reproduce

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