_multiplyComplex, real part

Percentage Accurate: 99.0% → 99.6%
Time: 2.0s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[x.re \cdot y.re - x.im \cdot y.im \]
(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)
use fmin_fmax_functions
    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]
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.

Initial Program: 99.0% accurate, 1.0× speedup?

\[x.re \cdot y.re - x.im \cdot y.im \]
(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)
use fmin_fmax_functions
    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]
x.re \cdot y.re - x.im \cdot y.im

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\mathsf{fma}\left(y.re, x.re, \left(-y.im\right) \cdot x.im\right) \]
(FPCore (x.re x.im y.re y.im)
  :precision binary64
  (fma y.re x.re (* (- y.im) x.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, (-y_46_im * x_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(-y_46_im) * x_46_im))
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(y$46$re * x$46$re + N[((-y$46$im) * x$46$im), $MachinePrecision]), $MachinePrecision]
\mathsf{fma}\left(y.re, x.re, \left(-y.im\right) \cdot x.im\right)
Derivation
  1. Initial program 99.0%

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

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

      \[\leadsto \color{blue}{x.re \cdot y.re + \left(\mathsf{neg}\left(x.im \cdot y.im\right)\right)} \]
    3. add-flipN/A

      \[\leadsto \color{blue}{x.re \cdot y.re - \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(x.im \cdot y.im\right)\right)\right)\right)} \]
    4. sub-flipN/A

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

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

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

      \[\leadsto y.re \cdot x.re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{x.im \cdot y.im}\right)\right)\right)\right)\right)\right) \]
    8. distribute-lft-neg-outN/A

      \[\leadsto y.re \cdot x.re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x.im\right)\right) \cdot y.im}\right)\right)\right)\right) \]
    9. distribute-lft-neg-outN/A

      \[\leadsto y.re \cdot x.re + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(x.im\right)\right)\right)\right) \cdot y.im}\right)\right) \]
    10. remove-double-negN/A

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

      \[\leadsto y.re \cdot x.re + \left(\mathsf{neg}\left(\color{blue}{x.im \cdot y.im}\right)\right) \]
    12. lower-fma.f64N/A

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

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

      \[\leadsto \mathsf{fma}\left(y.re, x.re, \mathsf{neg}\left(\color{blue}{y.im \cdot x.im}\right)\right) \]
    15. distribute-lft-neg-inN/A

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

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

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

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

Alternative 2: 75.3% accurate, 0.5× speedup?

\[\begin{array}{l} t_0 := \left(-y.im\right) \cdot x.im\\ \mathbf{if}\;x.im \cdot y.im \leq -1 \cdot 10^{-78}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x.im \cdot y.im \leq 4 \cdot 10^{+55}:\\ \;\;\;\;x.re \cdot y.re\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
(FPCore (x.re x.im y.re y.im)
  :precision binary64
  (let* ((t_0 (* (- y.im) x.im)))
  (if (<= (* x.im y.im) -1e-78)
    t_0
    (if (<= (* x.im y.im) 4e+55) (* x.re y.re) t_0))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double t_0 = -y_46_im * x_46_im;
	double tmp;
	if ((x_46_im * y_46_im) <= -1e-78) {
		tmp = t_0;
	} else if ((x_46_im * y_46_im) <= 4e+55) {
		tmp = x_46_re * y_46_re;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x_46re, x_46im, y_46re, y_46im)
use fmin_fmax_functions
    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) :: t_0
    real(8) :: tmp
    t_0 = -y_46im * x_46im
    if ((x_46im * y_46im) <= (-1d-78)) then
        tmp = t_0
    else if ((x_46im * y_46im) <= 4d+55) then
        tmp = x_46re * y_46re
    else
        tmp = t_0
    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 t_0 = -y_46_im * x_46_im;
	double tmp;
	if ((x_46_im * y_46_im) <= -1e-78) {
		tmp = t_0;
	} else if ((x_46_im * y_46_im) <= 4e+55) {
		tmp = x_46_re * y_46_re;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	t_0 = -y_46_im * x_46_im
	tmp = 0
	if (x_46_im * y_46_im) <= -1e-78:
		tmp = t_0
	elif (x_46_im * y_46_im) <= 4e+55:
		tmp = x_46_re * y_46_re
	else:
		tmp = t_0
	return tmp
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	t_0 = Float64(Float64(-y_46_im) * x_46_im)
	tmp = 0.0
	if (Float64(x_46_im * y_46_im) <= -1e-78)
		tmp = t_0;
	elseif (Float64(x_46_im * y_46_im) <= 4e+55)
		tmp = Float64(x_46_re * y_46_re);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x_46_re, x_46_im, y_46_re, y_46_im)
	t_0 = -y_46_im * x_46_im;
	tmp = 0.0;
	if ((x_46_im * y_46_im) <= -1e-78)
		tmp = t_0;
	elseif ((x_46_im * y_46_im) <= 4e+55)
		tmp = x_46_re * y_46_re;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := Block[{t$95$0 = N[((-y$46$im) * x$46$im), $MachinePrecision]}, If[LessEqual[N[(x$46$im * y$46$im), $MachinePrecision], -1e-78], t$95$0, If[LessEqual[N[(x$46$im * y$46$im), $MachinePrecision], 4e+55], N[(x$46$re * y$46$re), $MachinePrecision], t$95$0]]]
\begin{array}{l}
t_0 := \left(-y.im\right) \cdot x.im\\
\mathbf{if}\;x.im \cdot y.im \leq -1 \cdot 10^{-78}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;x.im \cdot y.im \leq 4 \cdot 10^{+55}:\\
\;\;\;\;x.re \cdot y.re\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x.im y.im) < -1e-78 or 4e55 < (*.f64 x.im y.im)

    1. Initial program 99.0%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x.im \cdot y.im\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{\left(x.im \cdot y.im\right)} \]
      2. lower-*.f6452.7%

        \[\leadsto -1 \cdot \left(x.im \cdot \color{blue}{y.im}\right) \]
    4. Applied rewrites52.7%

      \[\leadsto \color{blue}{-1 \cdot \left(x.im \cdot y.im\right)} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{\left(x.im \cdot y.im\right)} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(x.im \cdot y.im\right) \]
      3. lift-*.f64N/A

        \[\leadsto \mathsf{neg}\left(x.im \cdot y.im\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{neg}\left(y.im \cdot x.im\right) \]
      5. distribute-lft-neg-outN/A

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

        \[\leadsto \left(-y.im\right) \cdot x.im \]
      7. lift-*.f6452.7%

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

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

    if -1e-78 < (*.f64 x.im y.im) < 4e55

    1. Initial program 99.0%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x.im \cdot y.im\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{\left(x.im \cdot y.im\right)} \]
      2. lower-*.f6452.7%

        \[\leadsto -1 \cdot \left(x.im \cdot \color{blue}{y.im}\right) \]
    4. Applied rewrites52.7%

      \[\leadsto \color{blue}{-1 \cdot \left(x.im \cdot y.im\right)} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{\left(x.im \cdot y.im\right)} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(x.im \cdot y.im\right) \]
      3. lift-*.f64N/A

        \[\leadsto \mathsf{neg}\left(x.im \cdot y.im\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{neg}\left(y.im \cdot x.im\right) \]
      5. distribute-lft-neg-outN/A

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

        \[\leadsto \left(-y.im\right) \cdot x.im \]
      7. lift-*.f6452.7%

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

      \[\leadsto \left(-y.im\right) \cdot \color{blue}{x.im} \]
    7. Taylor expanded in x.re around inf

      \[\leadsto \color{blue}{x.re \cdot y.re} \]
    8. Step-by-step derivation
      1. lower-*.f6451.3%

        \[\leadsto x.re \cdot \color{blue}{y.re} \]
    9. Applied rewrites51.3%

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

Alternative 3: 51.3% accurate, 2.4× speedup?

\[x.re \cdot y.re \]
(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)
use fmin_fmax_functions
    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]
x.re \cdot y.re
Derivation
  1. Initial program 99.0%

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

    \[\leadsto \color{blue}{-1 \cdot \left(x.im \cdot y.im\right)} \]
  3. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto -1 \cdot \color{blue}{\left(x.im \cdot y.im\right)} \]
    2. lower-*.f6452.7%

      \[\leadsto -1 \cdot \left(x.im \cdot \color{blue}{y.im}\right) \]
  4. Applied rewrites52.7%

    \[\leadsto \color{blue}{-1 \cdot \left(x.im \cdot y.im\right)} \]
  5. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto -1 \cdot \color{blue}{\left(x.im \cdot y.im\right)} \]
    2. mul-1-negN/A

      \[\leadsto \mathsf{neg}\left(x.im \cdot y.im\right) \]
    3. lift-*.f64N/A

      \[\leadsto \mathsf{neg}\left(x.im \cdot y.im\right) \]
    4. *-commutativeN/A

      \[\leadsto \mathsf{neg}\left(y.im \cdot x.im\right) \]
    5. distribute-lft-neg-outN/A

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

      \[\leadsto \left(-y.im\right) \cdot x.im \]
    7. lift-*.f6452.7%

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

    \[\leadsto \left(-y.im\right) \cdot \color{blue}{x.im} \]
  7. Taylor expanded in x.re around inf

    \[\leadsto \color{blue}{x.re \cdot y.re} \]
  8. Step-by-step derivation
    1. lower-*.f6451.3%

      \[\leadsto x.re \cdot \color{blue}{y.re} \]
  9. Applied rewrites51.3%

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

Reproduce

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