_multiplyComplex, real part

Percentage Accurate: 99.2% → 99.6%
Time: 2.4s
Alternatives: 5
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 5 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.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.6% accurate, 0.1× speedup?

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

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

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

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

      \[\leadsto x.re \cdot y.re + \color{blue}{x.im \cdot \left(-y.im\right)} \]
    3. +-commutative99.6%

      \[\leadsto \color{blue}{x.im \cdot \left(-y.im\right) + x.re \cdot y.re} \]
    4. distribute-rgt-neg-out99.6%

      \[\leadsto \color{blue}{\left(-x.im \cdot y.im\right)} + x.re \cdot y.re \]
    5. distribute-lft-neg-in99.6%

      \[\leadsto \color{blue}{\left(-x.im\right) \cdot y.im} + x.re \cdot y.re \]
    6. fma-def100.0%

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

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

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

Alternative 2: 99.6% accurate, 0.1× speedup?

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

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

    \[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 \left(-y.im\right)\right) \]

Alternative 3: 68.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x.im \leq -5.2 \cdot 10^{+79} \lor \neg \left(x.im \leq 2 \cdot 10^{-87}\right):\\ \;\;\;\;x.im \cdot \left(-y.im\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot y.re\\ \end{array} \end{array} \]
(FPCore (x.re x.im y.re y.im)
 :precision binary64
 (if (or (<= x.im -5.2e+79) (not (<= x.im 2e-87)))
   (* x.im (- y.im))
   (* x.re y.re)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if ((x_46_im <= -5.2e+79) || !(x_46_im <= 2e-87)) {
		tmp = x_46_im * -y_46_im;
	} else {
		tmp = x_46_re * y_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 ((x_46im <= (-5.2d+79)) .or. (.not. (x_46im <= 2d-87))) then
        tmp = x_46im * -y_46im
    else
        tmp = x_46re * y_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 ((x_46_im <= -5.2e+79) || !(x_46_im <= 2e-87)) {
		tmp = x_46_im * -y_46_im;
	} else {
		tmp = x_46_re * y_46_re;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	tmp = 0
	if (x_46_im <= -5.2e+79) or not (x_46_im <= 2e-87):
		tmp = x_46_im * -y_46_im
	else:
		tmp = x_46_re * y_46_re
	return tmp
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if ((x_46_im <= -5.2e+79) || !(x_46_im <= 2e-87))
		tmp = Float64(x_46_im * Float64(-y_46_im));
	else
		tmp = Float64(x_46_re * y_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 ((x_46_im <= -5.2e+79) || ~((x_46_im <= 2e-87)))
		tmp = x_46_im * -y_46_im;
	else
		tmp = x_46_re * y_46_re;
	end
	tmp_2 = tmp;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[Or[LessEqual[x$46$im, -5.2e+79], N[Not[LessEqual[x$46$im, 2e-87]], $MachinePrecision]], N[(x$46$im * (-y$46$im)), $MachinePrecision], N[(x$46$re * y$46$re), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x.im \leq -5.2 \cdot 10^{+79} \lor \neg \left(x.im \leq 2 \cdot 10^{-87}\right):\\
\;\;\;\;x.im \cdot \left(-y.im\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x.im < -5.20000000000000029e79 or 2.00000000000000004e-87 < x.im

    1. Initial program 99.2%

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

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

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

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

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

    if -5.20000000000000029e79 < x.im < 2.00000000000000004e-87

    1. Initial program 100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x.im \leq -5.2 \cdot 10^{+79} \lor \neg \left(x.im \leq 2 \cdot 10^{-87}\right):\\ \;\;\;\;x.im \cdot \left(-y.im\right)\\ \mathbf{else}:\\ \;\;\;\;x.re \cdot y.re\\ \end{array} \]

Alternative 4: 99.2% 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}
Derivation
  1. Initial program 99.6%

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

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

Alternative 5: 52.7% accurate, 2.3× speedup?

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

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

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

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

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

Reproduce

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