_multiplyComplex, imaginary part

Percentage Accurate: 99.2% → 99.6%
Time: 3.1s
Alternatives: 4
Speedup: 1.0×

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

Initial Program: 99.2% 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.6% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x.re, y.im, x.im \cdot y.re\right) \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (fma 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 fma(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 fma(x_46_re, y_46_im, Float64(x_46_im * y_46_re))
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(x$46$re * y$46$im + N[(x$46$im * y$46$re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

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

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

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

Alternative 2: 75.3% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x.im \cdot y.re \leq -1.8 \cdot 10^{+110} \lor \neg \left(x.im \cdot y.re \leq 2.7 \cdot 10^{+89}\right):\\ \;\;\;\;x.im \cdot y.re\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot y.im\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (or (<= (* x.im y.re) -1.8e+110) (not (<= (* x.im y.re) 2.7e+89)))
   (* x.im y.re)
   (* x.re y.im)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if (((x_46_im * y_46_re) <= -1.8e+110) || !((x_46_im * y_46_re) <= 2.7e+89)) {
		tmp = x_46_im * y_46_re;
	} else {
		tmp = x_46_re * y_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_46im * y_46re) <= (-1.8d+110)) .or. (.not. ((x_46im * y_46re) <= 2.7d+89))) then
        tmp = x_46im * y_46re
    else
        tmp = x_46re * y_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_im * y_46_re) <= -1.8e+110) || !((x_46_im * y_46_re) <= 2.7e+89)) {
		tmp = x_46_im * y_46_re;
	} else {
		tmp = x_46_re * y_46_im;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	tmp = 0
	if ((x_46_im * y_46_re) <= -1.8e+110) or not ((x_46_im * y_46_re) <= 2.7e+89):
		tmp = x_46_im * y_46_re
	else:
		tmp = x_46_re * y_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_im * y_46_re) <= -1.8e+110) || !(Float64(x_46_im * y_46_re) <= 2.7e+89))
		tmp = Float64(x_46_im * y_46_re);
	else
		tmp = Float64(x_46_re * y_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_im * y_46_re) <= -1.8e+110) || ~(((x_46_im * y_46_re) <= 2.7e+89)))
		tmp = x_46_im * y_46_re;
	else
		tmp = x_46_re * y_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$im * y$46$re), $MachinePrecision], -1.8e+110], N[Not[LessEqual[N[(x$46$im * y$46$re), $MachinePrecision], 2.7e+89]], $MachinePrecision]], N[(x$46$im * y$46$re), $MachinePrecision], N[(x$46$re * y$46$im), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x.im \cdot y.re \leq -1.8 \cdot 10^{+110} \lor \neg \left(x.im \cdot y.re \leq 2.7 \cdot 10^{+89}\right):\\
\;\;\;\;x.im \cdot y.re\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x.im y.re) < -1.7999999999999998e110 or 2.7e89 < (*.f64 x.im y.re)

    1. Initial program 96.7%

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

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

    if -1.7999999999999998e110 < (*.f64 x.im y.re) < 2.7e89

    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 inf 77.1%

      \[\leadsto \color{blue}{x.re \cdot y.im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification78.7%

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

Alternative 3: 99.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x.im \cdot y.re + x.re \cdot y.im \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (+ (* x.im y.re) (* x.re y.im)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_im * y_46_re) + (x_46_re * y_46_im);
}
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_46im * y_46re) + (x_46re * y_46im)
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_im * y_46_re) + (x_46_re * y_46_im);
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return (x_46_im * y_46_re) + (x_46_re * y_46_im)
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(x_46_im * y_46_re) + Float64(x_46_re * y_46_im))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = (x_46_im * y_46_re) + (x_46_re * y_46_im);
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(x$46$im * y$46$re), $MachinePrecision] + N[(x$46$re * y$46$im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x.im \cdot y.re + x.re \cdot y.im
\end{array}
Derivation
  1. Initial program 98.8%

    \[x.re \cdot y.im + x.im \cdot y.re \]
  2. Add Preprocessing
  3. Final simplification98.8%

    \[\leadsto x.im \cdot y.re + x.re \cdot y.im \]
  4. Add Preprocessing

Alternative 4: 52.0% accurate, 2.3× speedup?

\[\begin{array}{l} \\ x.im \cdot y.re \end{array} \]
(FPCore (x.re x.im y.re y.im) :precision binary64 (* 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_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_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_im * y_46_re;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return x_46_im * y_46_re
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return 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_im * y_46_re;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(x$46$im * y$46$re), $MachinePrecision]
\begin{array}{l}

\\
x.im \cdot y.re
\end{array}
Derivation
  1. Initial program 98.8%

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

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

Reproduce

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