_multiplyComplex, imaginary part

Percentage Accurate: 99.4% → 99.6%
Time: 1.4s
Alternatives: 2
Speedup: 1.1×

Specification

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

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 2 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.4% accurate, 1.0× speedup?

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

Alternative 1: 99.6% accurate, 1.1× speedup?

\[\mathsf{fma}\left(y.re, x.im, y.im \cdot x.re\right) \]
(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]
\mathsf{fma}\left(y.re, x.im, y.im \cdot x.re\right)
Derivation
  1. Initial program 99.4%

    \[x.re \cdot y.im + x.im \cdot y.re \]
  2. 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) \]
  3. Applied rewrites99.6%

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

Alternative 2: 51.8% accurate, 2.4× speedup?

\[x.im \cdot y.re \]
(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)
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_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]
x.im \cdot y.re
Derivation
  1. Initial program 99.4%

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

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

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

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

Reproduce

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