Linear.V2:$cdot from linear-1.19.1.3, A

Percentage Accurate: 99.1% → 99.6%
Time: 3.1s
Alternatives: 4
Speedup: 0.5×

Specification

?
\[\begin{array}{l} \\ x \cdot y + z \cdot t \end{array} \]
(FPCore (x y z t) :precision binary64 (+ (* x y) (* z t)))
double code(double x, double y, double z, double t) {
	return (x * y) + (z * t);
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (x * y) + (z * t)
end function
public static double code(double x, double y, double z, double t) {
	return (x * y) + (z * t);
}
def code(x, y, z, t):
	return (x * y) + (z * t)
function code(x, y, z, t)
	return Float64(Float64(x * y) + Float64(z * t))
end
function tmp = code(x, y, z, t)
	tmp = (x * y) + (z * t);
end
code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot y + z \cdot t
\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 \cdot y + z \cdot t \end{array} \]
(FPCore (x y z t) :precision binary64 (+ (* x y) (* z t)))
double code(double x, double y, double z, double t) {
	return (x * y) + (z * t);
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (x * y) + (z * t)
end function
public static double code(double x, double y, double z, double t) {
	return (x * y) + (z * t);
}
def code(x, y, z, t):
	return (x * y) + (z * t)
function code(x, y, z, t)
	return Float64(Float64(x * y) + Float64(z * t))
end
function tmp = code(x, y, z, t)
	tmp = (x * y) + (z * t);
end
code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot y + z \cdot t
\end{array}

Alternative 1: 99.6% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := z \cdot t + x \cdot y\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (+ (* z t) (* x y)))) (if (<= t_1 INFINITY) t_1 (* x y))))
double code(double x, double y, double z, double t) {
	double t_1 = (z * t) + (x * y);
	double tmp;
	if (t_1 <= ((double) INFINITY)) {
		tmp = t_1;
	} else {
		tmp = x * y;
	}
	return tmp;
}
public static double code(double x, double y, double z, double t) {
	double t_1 = (z * t) + (x * y);
	double tmp;
	if (t_1 <= Double.POSITIVE_INFINITY) {
		tmp = t_1;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z, t):
	t_1 = (z * t) + (x * y)
	tmp = 0
	if t_1 <= math.inf:
		tmp = t_1
	else:
		tmp = x * y
	return tmp
function code(x, y, z, t)
	t_1 = Float64(Float64(z * t) + Float64(x * y))
	tmp = 0.0
	if (t_1 <= Inf)
		tmp = t_1;
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z, t)
	t_1 = (z * t) + (x * y);
	tmp = 0.0;
	if (t_1 <= Inf)
		tmp = t_1;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(z * t), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, Infinity], t$95$1, N[(x * y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := z \cdot t + x \cdot y\\
\mathbf{if}\;t_1 \leq \infty:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 x y) (*.f64 z t)) < +inf.0

    1. Initial program 100.0%

      \[x \cdot y + z \cdot t \]

    if +inf.0 < (+.f64 (*.f64 x y) (*.f64 z t))

    1. Initial program 0.0%

      \[x \cdot y + z \cdot t \]
    2. Taylor expanded in x around inf 80.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot t + x \cdot y \leq \infty:\\ \;\;\;\;z \cdot t + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 2: 99.5% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, y, z \cdot t\right) \end{array} \]
(FPCore (x y z t) :precision binary64 (fma x y (* z t)))
double code(double x, double y, double z, double t) {
	return fma(x, y, (z * t));
}
function code(x, y, z, t)
	return fma(x, y, Float64(z * t))
end
code[x_, y_, z_, t_] := N[(x * y + N[(z * t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, y, z \cdot t\right)
\end{array}
Derivation
  1. Initial program 98.0%

    \[x \cdot y + z \cdot t \]
  2. Step-by-step derivation
    1. fma-def98.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, z \cdot t\right)} \]
  3. Simplified98.4%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, z \cdot t\right)} \]
  4. Final simplification98.4%

    \[\leadsto \mathsf{fma}\left(x, y, z \cdot t\right) \]

Alternative 3: 76.5% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot y \leq -1.7 \cdot 10^{-41}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \cdot y \leq 4.8 \cdot 10^{+19}:\\ \;\;\;\;z \cdot t\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (if (<= (* x y) -1.7e-41) (* x y) (if (<= (* x y) 4.8e+19) (* z t) (* x y))))
double code(double x, double y, double z, double t) {
	double tmp;
	if ((x * y) <= -1.7e-41) {
		tmp = x * y;
	} else if ((x * y) <= 4.8e+19) {
		tmp = z * t;
	} else {
		tmp = x * y;
	}
	return tmp;
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8) :: tmp
    if ((x * y) <= (-1.7d-41)) then
        tmp = x * y
    else if ((x * y) <= 4.8d+19) then
        tmp = z * t
    else
        tmp = x * y
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t) {
	double tmp;
	if ((x * y) <= -1.7e-41) {
		tmp = x * y;
	} else if ((x * y) <= 4.8e+19) {
		tmp = z * t;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z, t):
	tmp = 0
	if (x * y) <= -1.7e-41:
		tmp = x * y
	elif (x * y) <= 4.8e+19:
		tmp = z * t
	else:
		tmp = x * y
	return tmp
function code(x, y, z, t)
	tmp = 0.0
	if (Float64(x * y) <= -1.7e-41)
		tmp = Float64(x * y);
	elseif (Float64(x * y) <= 4.8e+19)
		tmp = Float64(z * t);
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z, t)
	tmp = 0.0;
	if ((x * y) <= -1.7e-41)
		tmp = x * y;
	elseif ((x * y) <= 4.8e+19)
		tmp = z * t;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_] := If[LessEqual[N[(x * y), $MachinePrecision], -1.7e-41], N[(x * y), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], 4.8e+19], N[(z * t), $MachinePrecision], N[(x * y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot y \leq -1.7 \cdot 10^{-41}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \cdot y \leq 4.8 \cdot 10^{+19}:\\
\;\;\;\;z \cdot t\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x y) < -1.6999999999999999e-41 or 4.8e19 < (*.f64 x y)

    1. Initial program 96.2%

      \[x \cdot y + z \cdot t \]
    2. Taylor expanded in x around inf 75.8%

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

    if -1.6999999999999999e-41 < (*.f64 x y) < 4.8e19

    1. Initial program 100.0%

      \[x \cdot y + z \cdot t \]
    2. Taylor expanded in x around 0 79.8%

      \[\leadsto \color{blue}{t \cdot z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification77.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y \leq -1.7 \cdot 10^{-41}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \cdot y \leq 4.8 \cdot 10^{+19}:\\ \;\;\;\;z \cdot t\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 4: 51.9% accurate, 2.3× speedup?

\[\begin{array}{l} \\ z \cdot t \end{array} \]
(FPCore (x y z t) :precision binary64 (* z t))
double code(double x, double y, double z, double t) {
	return z * t;
}
real(8) function code(x, y, z, t)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = z * t
end function
public static double code(double x, double y, double z, double t) {
	return z * t;
}
def code(x, y, z, t):
	return z * t
function code(x, y, z, t)
	return Float64(z * t)
end
function tmp = code(x, y, z, t)
	tmp = z * t;
end
code[x_, y_, z_, t_] := N[(z * t), $MachinePrecision]
\begin{array}{l}

\\
z \cdot t
\end{array}
Derivation
  1. Initial program 98.0%

    \[x \cdot y + z \cdot t \]
  2. Taylor expanded in x around 0 52.5%

    \[\leadsto \color{blue}{t \cdot z} \]
  3. Final simplification52.5%

    \[\leadsto z \cdot t \]

Reproduce

?
herbie shell --seed 2023274 
(FPCore (x y z t)
  :name "Linear.V2:$cdot from linear-1.19.1.3, A"
  :precision binary64
  (+ (* x y) (* z t)))