Linear.V4:$cdot from linear-1.19.1.3, C

Percentage Accurate: 95.5% → 97.8%
Time: 12.1s
Alternatives: 15
Speedup: 1.3×

Specification

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

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

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

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

Alternative 1: 97.8% accurate, 1.3× speedup?

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

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

    \[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i} \]
    2. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right)} + c \cdot i \]
    3. associate-+l+N/A

      \[\leadsto \color{blue}{\left(x \cdot y + z \cdot t\right) + \left(a \cdot b + c \cdot i\right)} \]
    4. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot y + z \cdot t\right)} + \left(a \cdot b + c \cdot i\right) \]
    5. +-commutativeN/A

      \[\leadsto \color{blue}{\left(z \cdot t + x \cdot y\right)} + \left(a \cdot b + c \cdot i\right) \]
    6. associate-+l+N/A

      \[\leadsto \color{blue}{z \cdot t + \left(x \cdot y + \left(a \cdot b + c \cdot i\right)\right)} \]
    7. lift-*.f64N/A

      \[\leadsto \color{blue}{z \cdot t} + \left(x \cdot y + \left(a \cdot b + c \cdot i\right)\right) \]
    8. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, t, x \cdot y + \left(a \cdot b + c \cdot i\right)\right)} \]
    9. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{x \cdot y} + \left(a \cdot b + c \cdot i\right)\right) \]
    10. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{\mathsf{fma}\left(x, y, a \cdot b + c \cdot i\right)}\right) \]
    11. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(z, t, \mathsf{fma}\left(x, y, \color{blue}{a \cdot b} + c \cdot i\right)\right) \]
    12. lower-fma.f6498.0

      \[\leadsto \mathsf{fma}\left(z, t, \mathsf{fma}\left(x, y, \color{blue}{\mathsf{fma}\left(a, b, c \cdot i\right)}\right)\right) \]
  4. Applied rewrites98.0%

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

Alternative 2: 66.4% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \mathsf{fma}\left(i, c, x \cdot y\right)\\ t_2 := \mathsf{fma}\left(z, t, a \cdot b\right)\\ \mathbf{if}\;a \cdot b \leq -5 \cdot 10^{+82}:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;a \cdot b \leq -1 \cdot 10^{-305}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;a \cdot b \leq 2 \cdot 10^{-141}:\\ \;\;\;\;\mathsf{fma}\left(i, c, z \cdot t\right)\\ \mathbf{elif}\;a \cdot b \leq 5 \cdot 10^{-35}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;t\_2\\ \end{array} \end{array} \]
(FPCore (x y z t a b c i)
 :precision binary64
 (let* ((t_1 (fma i c (* x y))) (t_2 (fma z t (* a b))))
   (if (<= (* a b) -5e+82)
     t_2
     (if (<= (* a b) -1e-305)
       t_1
       (if (<= (* a b) 2e-141)
         (fma i c (* z t))
         (if (<= (* a b) 5e-35) t_1 t_2))))))
double code(double x, double y, double z, double t, double a, double b, double c, double i) {
	double t_1 = fma(i, c, (x * y));
	double t_2 = fma(z, t, (a * b));
	double tmp;
	if ((a * b) <= -5e+82) {
		tmp = t_2;
	} else if ((a * b) <= -1e-305) {
		tmp = t_1;
	} else if ((a * b) <= 2e-141) {
		tmp = fma(i, c, (z * t));
	} else if ((a * b) <= 5e-35) {
		tmp = t_1;
	} else {
		tmp = t_2;
	}
	return tmp;
}
function code(x, y, z, t, a, b, c, i)
	t_1 = fma(i, c, Float64(x * y))
	t_2 = fma(z, t, Float64(a * b))
	tmp = 0.0
	if (Float64(a * b) <= -5e+82)
		tmp = t_2;
	elseif (Float64(a * b) <= -1e-305)
		tmp = t_1;
	elseif (Float64(a * b) <= 2e-141)
		tmp = fma(i, c, Float64(z * t));
	elseif (Float64(a * b) <= 5e-35)
		tmp = t_1;
	else
		tmp = t_2;
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_, c_, i_] := Block[{t$95$1 = N[(i * c + N[(x * y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(z * t + N[(a * b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(a * b), $MachinePrecision], -5e+82], t$95$2, If[LessEqual[N[(a * b), $MachinePrecision], -1e-305], t$95$1, If[LessEqual[N[(a * b), $MachinePrecision], 2e-141], N[(i * c + N[(z * t), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(a * b), $MachinePrecision], 5e-35], t$95$1, t$95$2]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \mathsf{fma}\left(i, c, x \cdot y\right)\\
t_2 := \mathsf{fma}\left(z, t, a \cdot b\right)\\
\mathbf{if}\;a \cdot b \leq -5 \cdot 10^{+82}:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;a \cdot b \leq -1 \cdot 10^{-305}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;a \cdot b \leq 2 \cdot 10^{-141}:\\
\;\;\;\;\mathsf{fma}\left(i, c, z \cdot t\right)\\

\mathbf{elif}\;a \cdot b \leq 5 \cdot 10^{-35}:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 a b) < -5.00000000000000015e82 or 4.99999999999999964e-35 < (*.f64 a b)

    1. Initial program 93.3%

      \[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i} \]
      2. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right)} + c \cdot i \]
      3. associate-+l+N/A

        \[\leadsto \color{blue}{\left(x \cdot y + z \cdot t\right) + \left(a \cdot b + c \cdot i\right)} \]
      4. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot y + z \cdot t\right)} + \left(a \cdot b + c \cdot i\right) \]
      5. +-commutativeN/A

        \[\leadsto \color{blue}{\left(z \cdot t + x \cdot y\right)} + \left(a \cdot b + c \cdot i\right) \]
      6. associate-+l+N/A

        \[\leadsto \color{blue}{z \cdot t + \left(x \cdot y + \left(a \cdot b + c \cdot i\right)\right)} \]
      7. lift-*.f64N/A

        \[\leadsto \color{blue}{z \cdot t} + \left(x \cdot y + \left(a \cdot b + c \cdot i\right)\right) \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, t, x \cdot y + \left(a \cdot b + c \cdot i\right)\right)} \]
      9. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{x \cdot y} + \left(a \cdot b + c \cdot i\right)\right) \]
      10. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{\mathsf{fma}\left(x, y, a \cdot b + c \cdot i\right)}\right) \]
      11. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(z, t, \mathsf{fma}\left(x, y, \color{blue}{a \cdot b} + c \cdot i\right)\right) \]
      12. lower-fma.f6496.6

        \[\leadsto \mathsf{fma}\left(z, t, \mathsf{fma}\left(x, y, \color{blue}{\mathsf{fma}\left(a, b, c \cdot i\right)}\right)\right) \]
    4. Applied rewrites96.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, t, \mathsf{fma}\left(x, y, \mathsf{fma}\left(a, b, c \cdot i\right)\right)\right)} \]
    5. Taylor expanded in a around inf

      \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{a \cdot b}\right) \]
    6. Step-by-step derivation
      1. lower-*.f6467.7

        \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{a \cdot b}\right) \]
    7. Applied rewrites67.7%

      \[\leadsto \mathsf{fma}\left(z, t, \color{blue}{a \cdot b}\right) \]

    if -5.00000000000000015e82 < (*.f64 a b) < -9.99999999999999996e-306 or 2.0000000000000001e-141 < (*.f64 a b) < 4.99999999999999964e-35

    1. Initial program 97.1%

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

      \[\leadsto \color{blue}{x \cdot y} + c \cdot i \]
    4. Step-by-step derivation
      1. lower-*.f6462.8

        \[\leadsto \color{blue}{x \cdot y} + c \cdot i \]
    5. Applied rewrites62.8%

      \[\leadsto \color{blue}{x \cdot y} + c \cdot i \]
    6. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{x \cdot y + c \cdot i} \]
      2. +-commutativeN/A

        \[\leadsto \color{blue}{c \cdot i + x \cdot y} \]
      3. lift-*.f64N/A

        \[\leadsto \color{blue}{c \cdot i} + x \cdot y \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{i \cdot c} + x \cdot y \]
      5. lower-fma.f6463.3

        \[\leadsto \color{blue}{\mathsf{fma}\left(i, c, x \cdot y\right)} \]
    7. Applied rewrites63.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(i, c, x \cdot y\right)} \]

    if -9.99999999999999996e-306 < (*.f64 a b) < 2.0000000000000001e-141

    1. Initial program 97.8%

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

      \[\leadsto \color{blue}{a \cdot b} + c \cdot i \]
    4. Step-by-step derivation
      1. lower-*.f6435.9

        \[\leadsto \color{blue}{a \cdot b} + c \cdot i \]
    5. Applied rewrites35.9%

      \[\leadsto \color{blue}{a \cdot b} + c \cdot i \]
    6. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{a \cdot b + c \cdot i} \]
      2. +-commutativeN/A

        \[\leadsto \color{blue}{c \cdot i + a \cdot b} \]
      3. lift-*.f64N/A

        \[\leadsto \color{blue}{c \cdot i} + a \cdot b \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{i \cdot c} + a \cdot b \]
      5. lower-fma.f6435.9

        \[\leadsto \color{blue}{\mathsf{fma}\left(i, c, a \cdot b\right)} \]
    7. Applied rewrites35.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(i, c, a \cdot b\right)} \]
    8. Taylor expanded in z around inf

      \[\leadsto \mathsf{fma}\left(i, c, \color{blue}{t \cdot z}\right) \]
    9. Step-by-step derivation
      1. lower-*.f6468.2

        \[\leadsto \mathsf{fma}\left(i, c, \color{blue}{t \cdot z}\right) \]
    10. Applied rewrites68.2%

      \[\leadsto \mathsf{fma}\left(i, c, \color{blue}{t \cdot z}\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification66.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot b \leq -5 \cdot 10^{+82}:\\ \;\;\;\;\mathsf{fma}\left(z, t, a \cdot b\right)\\ \mathbf{elif}\;a \cdot b \leq -1 \cdot 10^{-305}:\\ \;\;\;\;\mathsf{fma}\left(i, c, x \cdot y\right)\\ \mathbf{elif}\;a \cdot b \leq 2 \cdot 10^{-141}:\\ \;\;\;\;\mathsf{fma}\left(i, c, z \cdot t\right)\\ \mathbf{elif}\;a \cdot b \leq 5 \cdot 10^{-35}:\\ \;\;\;\;\mathsf{fma}\left(i, c, x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(z, t, a \cdot b\right)\\ \end{array} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024223 
(FPCore (x y z t a b c i)
  :name "Linear.V4:$cdot from linear-1.19.1.3, C"
  :precision binary64
  (+ (+ (+ (* x y) (* z t)) (* a b)) (* c i)))