Linear.V3:$cdot from linear-1.19.1.3, B

Percentage Accurate: 97.6% → 99.2%
Time: 9.6s
Alternatives: 9
Speedup: 0.4×

Specification

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

\\
\left(x \cdot y + z \cdot t\right) + a \cdot b
\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 9 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: 97.6% accurate, 1.0× speedup?

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

\\
\left(x \cdot y + z \cdot t\right) + a \cdot b
\end{array}

Alternative 1: 99.2% accurate, 0.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a \cdot b + \left(x \cdot y + z \cdot t\right) \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(a, b, \mathsf{fma}\left(x, y, z \cdot t\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\ \end{array} \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (if (<= (+ (* a b) (+ (* x y) (* z t))) INFINITY)
   (fma a b (fma x y (* z t)))
   (* x (+ y (* a (/ b x))))))
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (((a * b) + ((x * y) + (z * t))) <= ((double) INFINITY)) {
		tmp = fma(a, b, fma(x, y, (z * t)));
	} else {
		tmp = x * (y + (a * (b / x)));
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (Float64(Float64(a * b) + Float64(Float64(x * y) + Float64(z * t))) <= Inf)
		tmp = fma(a, b, fma(x, y, Float64(z * t)));
	else
		tmp = Float64(x * Float64(y + Float64(a * Float64(b / x))));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(N[(a * b), $MachinePrecision] + N[(N[(x * y), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], Infinity], N[(a * b + N[(x * y + N[(z * t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * N[(y + N[(a * N[(b / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a \cdot b + \left(x \cdot y + z \cdot t\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(a, b, \mathsf{fma}\left(x, y, z \cdot t\right)\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\


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

    1. Initial program 100.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \color{blue}{a \cdot b + \left(x \cdot y + z \cdot t\right)} \]
      2. fma-define100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(a, b, x \cdot y + z \cdot t\right)} \]
      3. fma-define100.0%

        \[\leadsto \mathsf{fma}\left(a, b, \color{blue}{\mathsf{fma}\left(x, y, z \cdot t\right)}\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(a, b, \mathsf{fma}\left(x, y, z \cdot t\right)\right)} \]
    4. Add Preprocessing

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

    1. Initial program 0.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf 66.7%

      \[\leadsto \color{blue}{z \cdot \left(t + \left(\frac{a \cdot b}{z} + \frac{x \cdot y}{z}\right)\right)} \]
    4. Taylor expanded in t around 0 66.7%

      \[\leadsto \color{blue}{z \cdot \left(\frac{a \cdot b}{z} + \frac{x \cdot y}{z}\right)} \]
    5. Taylor expanded in x around inf 77.8%

      \[\leadsto \color{blue}{x \cdot \left(y + \frac{a \cdot b}{x}\right)} \]
    6. Step-by-step derivation
      1. associate-/l*88.9%

        \[\leadsto x \cdot \left(y + \color{blue}{a \cdot \frac{b}{x}}\right) \]
    7. Simplified88.9%

      \[\leadsto \color{blue}{x \cdot \left(y + a \cdot \frac{b}{x}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot b + \left(x \cdot y + z \cdot t\right) \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(a, b, \mathsf{fma}\left(x, y, z \cdot t\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 98.9% accurate, 0.1× speedup?

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

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

    \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
  2. Step-by-step derivation
    1. associate-+l+96.5%

      \[\leadsto \color{blue}{x \cdot y + \left(z \cdot t + a \cdot b\right)} \]
    2. fma-define96.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, z \cdot t + a \cdot b\right)} \]
    3. fma-define98.8%

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right)} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 3: 78.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot y \leq -5.5 \cdot 10^{+207}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \cdot y \leq 2.6 \cdot 10^{+47}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{elif}\;x \cdot y \leq 3.05 \cdot 10^{+75} \lor \neg \left(x \cdot y \leq 6.8 \cdot 10^{+112}\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (if (<= (* x y) -5.5e+207)
   (* x y)
   (if (<= (* x y) 2.6e+47)
     (+ (* a b) (* z t))
     (if (or (<= (* x y) 3.05e+75) (not (<= (* x y) 6.8e+112)))
       (* x y)
       (* a b)))))
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((x * y) <= -5.5e+207) {
		tmp = x * y;
	} else if ((x * y) <= 2.6e+47) {
		tmp = (a * b) + (z * t);
	} else if (((x * y) <= 3.05e+75) || !((x * y) <= 6.8e+112)) {
		tmp = x * y;
	} else {
		tmp = a * b;
	}
	return tmp;
}
real(8) function code(x, y, z, t, a, b)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if ((x * y) <= (-5.5d+207)) then
        tmp = x * y
    else if ((x * y) <= 2.6d+47) then
        tmp = (a * b) + (z * t)
    else if (((x * y) <= 3.05d+75) .or. (.not. ((x * y) <= 6.8d+112))) then
        tmp = x * y
    else
        tmp = a * b
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((x * y) <= -5.5e+207) {
		tmp = x * y;
	} else if ((x * y) <= 2.6e+47) {
		tmp = (a * b) + (z * t);
	} else if (((x * y) <= 3.05e+75) || !((x * y) <= 6.8e+112)) {
		tmp = x * y;
	} else {
		tmp = a * b;
	}
	return tmp;
}
def code(x, y, z, t, a, b):
	tmp = 0
	if (x * y) <= -5.5e+207:
		tmp = x * y
	elif (x * y) <= 2.6e+47:
		tmp = (a * b) + (z * t)
	elif ((x * y) <= 3.05e+75) or not ((x * y) <= 6.8e+112):
		tmp = x * y
	else:
		tmp = a * b
	return tmp
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (Float64(x * y) <= -5.5e+207)
		tmp = Float64(x * y);
	elseif (Float64(x * y) <= 2.6e+47)
		tmp = Float64(Float64(a * b) + Float64(z * t));
	elseif ((Float64(x * y) <= 3.05e+75) || !(Float64(x * y) <= 6.8e+112))
		tmp = Float64(x * y);
	else
		tmp = Float64(a * b);
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a, b)
	tmp = 0.0;
	if ((x * y) <= -5.5e+207)
		tmp = x * y;
	elseif ((x * y) <= 2.6e+47)
		tmp = (a * b) + (z * t);
	elseif (((x * y) <= 3.05e+75) || ~(((x * y) <= 6.8e+112)))
		tmp = x * y;
	else
		tmp = a * b;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(x * y), $MachinePrecision], -5.5e+207], N[(x * y), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], 2.6e+47], N[(N[(a * b), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[N[(x * y), $MachinePrecision], 3.05e+75], N[Not[LessEqual[N[(x * y), $MachinePrecision], 6.8e+112]], $MachinePrecision]], N[(x * y), $MachinePrecision], N[(a * b), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot y \leq -5.5 \cdot 10^{+207}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \cdot y \leq 2.6 \cdot 10^{+47}:\\
\;\;\;\;a \cdot b + z \cdot t\\

\mathbf{elif}\;x \cdot y \leq 3.05 \cdot 10^{+75} \lor \neg \left(x \cdot y \leq 6.8 \cdot 10^{+112}\right):\\
\;\;\;\;x \cdot y\\

\mathbf{else}:\\
\;\;\;\;a \cdot b\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 x y) < -5.50000000000000036e207 or 2.60000000000000003e47 < (*.f64 x y) < 3.05000000000000005e75 or 6.79999999999999987e112 < (*.f64 x y)

    1. Initial program 93.5%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 84.4%

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

    if -5.50000000000000036e207 < (*.f64 x y) < 2.60000000000000003e47

    1. Initial program 97.7%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 83.3%

      \[\leadsto \color{blue}{a \cdot b + t \cdot z} \]

    if 3.05000000000000005e75 < (*.f64 x y) < 6.79999999999999987e112

    1. Initial program 100.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in a around inf 86.6%

      \[\leadsto \color{blue}{a \cdot b} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification83.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y \leq -5.5 \cdot 10^{+207}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \cdot y \leq 2.6 \cdot 10^{+47}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{elif}\;x \cdot y \leq 3.05 \cdot 10^{+75} \lor \neg \left(x \cdot y \leq 6.8 \cdot 10^{+112}\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 54.7% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot y \leq -4 \cdot 10^{+73}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \cdot y \leq -0.00185:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;x \cdot y \leq -1.02 \cdot 10^{-271}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;x \cdot y \leq 1.65 \cdot 10^{+47}:\\ \;\;\;\;a \cdot b\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (if (<= (* x y) -4e+73)
   (* x y)
   (if (<= (* x y) -0.00185)
     (* a b)
     (if (<= (* x y) -1.02e-271)
       (* z t)
       (if (<= (* x y) 1.65e+47) (* a b) (* x y))))))
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((x * y) <= -4e+73) {
		tmp = x * y;
	} else if ((x * y) <= -0.00185) {
		tmp = a * b;
	} else if ((x * y) <= -1.02e-271) {
		tmp = z * t;
	} else if ((x * y) <= 1.65e+47) {
		tmp = a * b;
	} else {
		tmp = x * y;
	}
	return tmp;
}
real(8) function code(x, y, z, t, a, b)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if ((x * y) <= (-4d+73)) then
        tmp = x * y
    else if ((x * y) <= (-0.00185d0)) then
        tmp = a * b
    else if ((x * y) <= (-1.02d-271)) then
        tmp = z * t
    else if ((x * y) <= 1.65d+47) then
        tmp = a * b
    else
        tmp = x * y
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((x * y) <= -4e+73) {
		tmp = x * y;
	} else if ((x * y) <= -0.00185) {
		tmp = a * b;
	} else if ((x * y) <= -1.02e-271) {
		tmp = z * t;
	} else if ((x * y) <= 1.65e+47) {
		tmp = a * b;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z, t, a, b):
	tmp = 0
	if (x * y) <= -4e+73:
		tmp = x * y
	elif (x * y) <= -0.00185:
		tmp = a * b
	elif (x * y) <= -1.02e-271:
		tmp = z * t
	elif (x * y) <= 1.65e+47:
		tmp = a * b
	else:
		tmp = x * y
	return tmp
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (Float64(x * y) <= -4e+73)
		tmp = Float64(x * y);
	elseif (Float64(x * y) <= -0.00185)
		tmp = Float64(a * b);
	elseif (Float64(x * y) <= -1.02e-271)
		tmp = Float64(z * t);
	elseif (Float64(x * y) <= 1.65e+47)
		tmp = Float64(a * b);
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a, b)
	tmp = 0.0;
	if ((x * y) <= -4e+73)
		tmp = x * y;
	elseif ((x * y) <= -0.00185)
		tmp = a * b;
	elseif ((x * y) <= -1.02e-271)
		tmp = z * t;
	elseif ((x * y) <= 1.65e+47)
		tmp = a * b;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(x * y), $MachinePrecision], -4e+73], N[(x * y), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], -0.00185], N[(a * b), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], -1.02e-271], N[(z * t), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], 1.65e+47], N[(a * b), $MachinePrecision], N[(x * y), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot y \leq -4 \cdot 10^{+73}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \cdot y \leq -0.00185:\\
\;\;\;\;a \cdot b\\

\mathbf{elif}\;x \cdot y \leq -1.02 \cdot 10^{-271}:\\
\;\;\;\;z \cdot t\\

\mathbf{elif}\;x \cdot y \leq 1.65 \cdot 10^{+47}:\\
\;\;\;\;a \cdot b\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 x y) < -3.99999999999999993e73 or 1.65e47 < (*.f64 x y)

    1. Initial program 95.4%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 70.1%

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

    if -3.99999999999999993e73 < (*.f64 x y) < -0.0018500000000000001 or -1.02e-271 < (*.f64 x y) < 1.65e47

    1. Initial program 97.2%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in a around inf 63.2%

      \[\leadsto \color{blue}{a \cdot b} \]

    if -0.0018500000000000001 < (*.f64 x y) < -1.02e-271

    1. Initial program 97.5%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf 60.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y \leq -4 \cdot 10^{+73}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \cdot y \leq -0.00185:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;x \cdot y \leq -1.02 \cdot 10^{-271}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;x \cdot y \leq 1.65 \cdot 10^{+47}:\\ \;\;\;\;a \cdot b\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 99.2% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
t_1 := a \cdot b + \left(x \cdot y + z \cdot t\right)\\
\mathbf{if}\;t\_1 \leq \infty:\\
\;\;\;\;t\_1\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\


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

    1. Initial program 100.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing

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

    1. Initial program 0.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf 66.7%

      \[\leadsto \color{blue}{z \cdot \left(t + \left(\frac{a \cdot b}{z} + \frac{x \cdot y}{z}\right)\right)} \]
    4. Taylor expanded in t around 0 66.7%

      \[\leadsto \color{blue}{z \cdot \left(\frac{a \cdot b}{z} + \frac{x \cdot y}{z}\right)} \]
    5. Taylor expanded in x around inf 77.8%

      \[\leadsto \color{blue}{x \cdot \left(y + \frac{a \cdot b}{x}\right)} \]
    6. Step-by-step derivation
      1. associate-/l*88.9%

        \[\leadsto x \cdot \left(y + \color{blue}{a \cdot \frac{b}{x}}\right) \]
    7. Simplified88.9%

      \[\leadsto \color{blue}{x \cdot \left(y + a \cdot \frac{b}{x}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot b + \left(x \cdot y + z \cdot t\right) \leq \infty:\\ \;\;\;\;a \cdot b + \left(x \cdot y + z \cdot t\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 84.5% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot y \leq -1 \cdot 10^{+83}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{+18}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\ \end{array} \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (if (<= (* x y) -1e+83)
   (+ (* a b) (* x y))
   (if (<= (* x y) 2e+18) (+ (* a b) (* z t)) (* x (+ y (* a (/ b x)))))))
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((x * y) <= -1e+83) {
		tmp = (a * b) + (x * y);
	} else if ((x * y) <= 2e+18) {
		tmp = (a * b) + (z * t);
	} else {
		tmp = x * (y + (a * (b / x)));
	}
	return tmp;
}
real(8) function code(x, y, z, t, a, b)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if ((x * y) <= (-1d+83)) then
        tmp = (a * b) + (x * y)
    else if ((x * y) <= 2d+18) then
        tmp = (a * b) + (z * t)
    else
        tmp = x * (y + (a * (b / x)))
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if ((x * y) <= -1e+83) {
		tmp = (a * b) + (x * y);
	} else if ((x * y) <= 2e+18) {
		tmp = (a * b) + (z * t);
	} else {
		tmp = x * (y + (a * (b / x)));
	}
	return tmp;
}
def code(x, y, z, t, a, b):
	tmp = 0
	if (x * y) <= -1e+83:
		tmp = (a * b) + (x * y)
	elif (x * y) <= 2e+18:
		tmp = (a * b) + (z * t)
	else:
		tmp = x * (y + (a * (b / x)))
	return tmp
function code(x, y, z, t, a, b)
	tmp = 0.0
	if (Float64(x * y) <= -1e+83)
		tmp = Float64(Float64(a * b) + Float64(x * y));
	elseif (Float64(x * y) <= 2e+18)
		tmp = Float64(Float64(a * b) + Float64(z * t));
	else
		tmp = Float64(x * Float64(y + Float64(a * Float64(b / x))));
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a, b)
	tmp = 0.0;
	if ((x * y) <= -1e+83)
		tmp = (a * b) + (x * y);
	elseif ((x * y) <= 2e+18)
		tmp = (a * b) + (z * t);
	else
		tmp = x * (y + (a * (b / x)));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_, b_] := If[LessEqual[N[(x * y), $MachinePrecision], -1e+83], N[(N[(a * b), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * y), $MachinePrecision], 2e+18], N[(N[(a * b), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision], N[(x * N[(y + N[(a * N[(b / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot y \leq -1 \cdot 10^{+83}:\\
\;\;\;\;a \cdot b + x \cdot y\\

\mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{+18}:\\
\;\;\;\;a \cdot b + z \cdot t\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 x y) < -1.00000000000000003e83

    1. Initial program 95.9%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0 84.4%

      \[\leadsto \color{blue}{a \cdot b + x \cdot y} \]

    if -1.00000000000000003e83 < (*.f64 x y) < 2e18

    1. Initial program 97.2%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 87.6%

      \[\leadsto \color{blue}{a \cdot b + t \cdot z} \]

    if 2e18 < (*.f64 x y)

    1. Initial program 95.2%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf 78.3%

      \[\leadsto \color{blue}{z \cdot \left(t + \left(\frac{a \cdot b}{z} + \frac{x \cdot y}{z}\right)\right)} \]
    4. Taylor expanded in t around 0 75.2%

      \[\leadsto \color{blue}{z \cdot \left(\frac{a \cdot b}{z} + \frac{x \cdot y}{z}\right)} \]
    5. Taylor expanded in x around inf 93.9%

      \[\leadsto \color{blue}{x \cdot \left(y + \frac{a \cdot b}{x}\right)} \]
    6. Step-by-step derivation
      1. associate-/l*92.5%

        \[\leadsto x \cdot \left(y + \color{blue}{a \cdot \frac{b}{x}}\right) \]
    7. Simplified92.5%

      \[\leadsto \color{blue}{x \cdot \left(y + a \cdot \frac{b}{x}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification88.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y \leq -1 \cdot 10^{+83}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{elif}\;x \cdot y \leq 2 \cdot 10^{+18}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y + a \cdot \frac{b}{x}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 81.7% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot y \leq -2.85 \cdot 10^{+72} \lor \neg \left(x \cdot y \leq 1.5 \cdot 10^{-160}\right):\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \end{array} \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (if (or (<= (* x y) -2.85e+72) (not (<= (* x y) 1.5e-160)))
   (+ (* a b) (* x y))
   (+ (* a b) (* z t))))
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (((x * y) <= -2.85e+72) || !((x * y) <= 1.5e-160)) {
		tmp = (a * b) + (x * y);
	} else {
		tmp = (a * b) + (z * t);
	}
	return tmp;
}
real(8) function code(x, y, z, t, a, b)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if (((x * y) <= (-2.85d+72)) .or. (.not. ((x * y) <= 1.5d-160))) then
        tmp = (a * b) + (x * y)
    else
        tmp = (a * b) + (z * t)
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (((x * y) <= -2.85e+72) || !((x * y) <= 1.5e-160)) {
		tmp = (a * b) + (x * y);
	} else {
		tmp = (a * b) + (z * t);
	}
	return tmp;
}
def code(x, y, z, t, a, b):
	tmp = 0
	if ((x * y) <= -2.85e+72) or not ((x * y) <= 1.5e-160):
		tmp = (a * b) + (x * y)
	else:
		tmp = (a * b) + (z * t)
	return tmp
function code(x, y, z, t, a, b)
	tmp = 0.0
	if ((Float64(x * y) <= -2.85e+72) || !(Float64(x * y) <= 1.5e-160))
		tmp = Float64(Float64(a * b) + Float64(x * y));
	else
		tmp = Float64(Float64(a * b) + Float64(z * t));
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a, b)
	tmp = 0.0;
	if (((x * y) <= -2.85e+72) || ~(((x * y) <= 1.5e-160)))
		tmp = (a * b) + (x * y);
	else
		tmp = (a * b) + (z * t);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[N[(x * y), $MachinePrecision], -2.85e+72], N[Not[LessEqual[N[(x * y), $MachinePrecision], 1.5e-160]], $MachinePrecision]], N[(N[(a * b), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(N[(a * b), $MachinePrecision] + N[(z * t), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot y \leq -2.85 \cdot 10^{+72} \lor \neg \left(x \cdot y \leq 1.5 \cdot 10^{-160}\right):\\
\;\;\;\;a \cdot b + x \cdot y\\

\mathbf{else}:\\
\;\;\;\;a \cdot b + z \cdot t\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x y) < -2.8499999999999998e72 or 1.49999999999999998e-160 < (*.f64 x y)

    1. Initial program 95.9%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0 87.5%

      \[\leadsto \color{blue}{a \cdot b + x \cdot y} \]

    if -2.8499999999999998e72 < (*.f64 x y) < 1.49999999999999998e-160

    1. Initial program 97.2%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 90.5%

      \[\leadsto \color{blue}{a \cdot b + t \cdot z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot y \leq -2.85 \cdot 10^{+72} \lor \neg \left(x \cdot y \leq 1.5 \cdot 10^{-160}\right):\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 53.1% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a \cdot b \leq -4.2 \cdot 10^{+25} \lor \neg \left(a \cdot b \leq 5.5 \cdot 10^{-28}\right):\\ \;\;\;\;a \cdot b\\ \mathbf{else}:\\ \;\;\;\;z \cdot t\\ \end{array} \end{array} \]
(FPCore (x y z t a b)
 :precision binary64
 (if (or (<= (* a b) -4.2e+25) (not (<= (* a b) 5.5e-28))) (* a b) (* z t)))
double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (((a * b) <= -4.2e+25) || !((a * b) <= 5.5e-28)) {
		tmp = a * b;
	} else {
		tmp = z * t;
	}
	return tmp;
}
real(8) function code(x, y, z, t, a, b)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if (((a * b) <= (-4.2d+25)) .or. (.not. ((a * b) <= 5.5d-28))) then
        tmp = a * b
    else
        tmp = z * t
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t, double a, double b) {
	double tmp;
	if (((a * b) <= -4.2e+25) || !((a * b) <= 5.5e-28)) {
		tmp = a * b;
	} else {
		tmp = z * t;
	}
	return tmp;
}
def code(x, y, z, t, a, b):
	tmp = 0
	if ((a * b) <= -4.2e+25) or not ((a * b) <= 5.5e-28):
		tmp = a * b
	else:
		tmp = z * t
	return tmp
function code(x, y, z, t, a, b)
	tmp = 0.0
	if ((Float64(a * b) <= -4.2e+25) || !(Float64(a * b) <= 5.5e-28))
		tmp = Float64(a * b);
	else
		tmp = Float64(z * t);
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a, b)
	tmp = 0.0;
	if (((a * b) <= -4.2e+25) || ~(((a * b) <= 5.5e-28)))
		tmp = a * b;
	else
		tmp = z * t;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_, b_] := If[Or[LessEqual[N[(a * b), $MachinePrecision], -4.2e+25], N[Not[LessEqual[N[(a * b), $MachinePrecision], 5.5e-28]], $MachinePrecision]], N[(a * b), $MachinePrecision], N[(z * t), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a \cdot b \leq -4.2 \cdot 10^{+25} \lor \neg \left(a \cdot b \leq 5.5 \cdot 10^{-28}\right):\\
\;\;\;\;a \cdot b\\

\mathbf{else}:\\
\;\;\;\;z \cdot t\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 a b) < -4.1999999999999998e25 or 5.49999999999999967e-28 < (*.f64 a b)

    1. Initial program 93.6%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in a around inf 68.6%

      \[\leadsto \color{blue}{a \cdot b} \]

    if -4.1999999999999998e25 < (*.f64 a b) < 5.49999999999999967e-28

    1. Initial program 100.0%

      \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf 43.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot b \leq -4.2 \cdot 10^{+25} \lor \neg \left(a \cdot b \leq 5.5 \cdot 10^{-28}\right):\\ \;\;\;\;a \cdot b\\ \mathbf{else}:\\ \;\;\;\;z \cdot t\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 34.9% accurate, 3.7× speedup?

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

\\
a \cdot b
\end{array}
Derivation
  1. Initial program 96.5%

    \[\left(x \cdot y + z \cdot t\right) + a \cdot b \]
  2. Add Preprocessing
  3. Taylor expanded in a around inf 41.8%

    \[\leadsto \color{blue}{a \cdot b} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2024110 
(FPCore (x y z t a b)
  :name "Linear.V3:$cdot from linear-1.19.1.3, B"
  :precision binary64
  (+ (+ (* x y) (* z t)) (* a b)))