_multiplyComplex, real part

Percentage Accurate: 99.1% → 99.5%
Time: 2.1s
Alternatives: 4
Speedup: 1.0×

Specification

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

\\
x.re \cdot y.re - x.im \cdot y.im
\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.1% accurate, 1.0× speedup?

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

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

Alternative 1: 99.5% accurate, 0.1× speedup?

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

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

    \[x.re \cdot y.re - x.im \cdot y.im \]
  2. Step-by-step derivation
    1. *-commutative98.4%

      \[\leadsto \color{blue}{y.re \cdot x.re} - x.im \cdot y.im \]
    2. fma-neg99.2%

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

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

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

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

Alternative 2: 73.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y.re \cdot x.re \leq -2.45 \cdot 10^{+157}:\\ \;\;\;\;y.re \cdot x.re\\ \mathbf{elif}\;y.re \cdot x.re \leq -0.008 \lor \neg \left(y.re \cdot x.re \leq -4.3 \cdot 10^{-103}\right) \land y.re \cdot x.re \leq 6.2 \cdot 10^{+35}:\\ \;\;\;\;-x.im \cdot y.im\\ \mathbf{else}:\\ \;\;\;\;y.re \cdot x.re\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (<= (* y.re x.re) -2.45e+157)
   (* y.re x.re)
   (if (or (<= (* y.re x.re) -0.008)
           (and (not (<= (* y.re x.re) -4.3e-103)) (<= (* y.re x.re) 6.2e+35)))
     (- (* x.im y.im))
     (* y.re x.re))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if ((y_46_re * x_46_re) <= -2.45e+157) {
		tmp = y_46_re * x_46_re;
	} else if (((y_46_re * x_46_re) <= -0.008) || (!((y_46_re * x_46_re) <= -4.3e-103) && ((y_46_re * x_46_re) <= 6.2e+35))) {
		tmp = -(x_46_im * y_46_im);
	} else {
		tmp = y_46_re * x_46_re;
	}
	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 ((y_46re * x_46re) <= (-2.45d+157)) then
        tmp = y_46re * x_46re
    else if (((y_46re * x_46re) <= (-0.008d0)) .or. (.not. ((y_46re * x_46re) <= (-4.3d-103))) .and. ((y_46re * x_46re) <= 6.2d+35)) then
        tmp = -(x_46im * y_46im)
    else
        tmp = y_46re * x_46re
    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 ((y_46_re * x_46_re) <= -2.45e+157) {
		tmp = y_46_re * x_46_re;
	} else if (((y_46_re * x_46_re) <= -0.008) || (!((y_46_re * x_46_re) <= -4.3e-103) && ((y_46_re * x_46_re) <= 6.2e+35))) {
		tmp = -(x_46_im * y_46_im);
	} else {
		tmp = y_46_re * x_46_re;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	tmp = 0
	if (y_46_re * x_46_re) <= -2.45e+157:
		tmp = y_46_re * x_46_re
	elif ((y_46_re * x_46_re) <= -0.008) or (not ((y_46_re * x_46_re) <= -4.3e-103) and ((y_46_re * x_46_re) <= 6.2e+35)):
		tmp = -(x_46_im * y_46_im)
	else:
		tmp = y_46_re * x_46_re
	return tmp
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if (Float64(y_46_re * x_46_re) <= -2.45e+157)
		tmp = Float64(y_46_re * x_46_re);
	elseif ((Float64(y_46_re * x_46_re) <= -0.008) || (!(Float64(y_46_re * x_46_re) <= -4.3e-103) && (Float64(y_46_re * x_46_re) <= 6.2e+35)))
		tmp = Float64(-Float64(x_46_im * y_46_im));
	else
		tmp = Float64(y_46_re * x_46_re);
	end
	return tmp
end
function tmp_2 = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0;
	if ((y_46_re * x_46_re) <= -2.45e+157)
		tmp = y_46_re * x_46_re;
	elseif (((y_46_re * x_46_re) <= -0.008) || (~(((y_46_re * x_46_re) <= -4.3e-103)) && ((y_46_re * x_46_re) <= 6.2e+35)))
		tmp = -(x_46_im * y_46_im);
	else
		tmp = y_46_re * x_46_re;
	end
	tmp_2 = tmp;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[LessEqual[N[(y$46$re * x$46$re), $MachinePrecision], -2.45e+157], N[(y$46$re * x$46$re), $MachinePrecision], If[Or[LessEqual[N[(y$46$re * x$46$re), $MachinePrecision], -0.008], And[N[Not[LessEqual[N[(y$46$re * x$46$re), $MachinePrecision], -4.3e-103]], $MachinePrecision], LessEqual[N[(y$46$re * x$46$re), $MachinePrecision], 6.2e+35]]], (-N[(x$46$im * y$46$im), $MachinePrecision]), N[(y$46$re * x$46$re), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y.re \cdot x.re \leq -2.45 \cdot 10^{+157}:\\
\;\;\;\;y.re \cdot x.re\\

\mathbf{elif}\;y.re \cdot x.re \leq -0.008 \lor \neg \left(y.re \cdot x.re \leq -4.3 \cdot 10^{-103}\right) \land y.re \cdot x.re \leq 6.2 \cdot 10^{+35}:\\
\;\;\;\;-x.im \cdot y.im\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x.re y.re) < -2.4500000000000001e157 or -0.0080000000000000002 < (*.f64 x.re y.re) < -4.30000000000000023e-103 or 6.19999999999999973e35 < (*.f64 x.re y.re)

    1. Initial program 96.7%

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

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

    if -2.4500000000000001e157 < (*.f64 x.re y.re) < -0.0080000000000000002 or -4.30000000000000023e-103 < (*.f64 x.re y.re) < 6.19999999999999973e35

    1. Initial program 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y.re \cdot x.re \leq -2.45 \cdot 10^{+157}:\\ \;\;\;\;y.re \cdot x.re\\ \mathbf{elif}\;y.re \cdot x.re \leq -0.008 \lor \neg \left(y.re \cdot x.re \leq -4.3 \cdot 10^{-103}\right) \land y.re \cdot x.re \leq 6.2 \cdot 10^{+35}:\\ \;\;\;\;-x.im \cdot y.im\\ \mathbf{else}:\\ \;\;\;\;y.re \cdot x.re\\ \end{array} \]

Alternative 3: 99.1% accurate, 1.0× speedup?

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

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

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

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

Alternative 4: 52.0% accurate, 2.3× speedup?

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

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

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

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

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

Reproduce

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