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

?

Percentage Accurate: 96.0% → 97.5%
Time: 18.3s
Precision: binary64
Cost: 8260

?

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

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, z \cdot t\right)\\


\end{array}

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.

Herbie found 19 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Derivation?

  1. Split input into 2 regimes
  2. if (+.f64 (+.f64 (+.f64 (*.f64 x y) (*.f64 z t)) (*.f64 a b)) (*.f64 c i)) < +inf.0

    1. Initial program 100.0%

      \[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, z \cdot t\right) + \left(a \cdot b + c \cdot i\right)} \]
      Step-by-step derivation

      [Start]100.0%

      \[ \left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]

      associate-+l+ [=>]100.0%

      \[ \color{blue}{\left(x \cdot y + z \cdot t\right) + \left(a \cdot b + c \cdot i\right)} \]

      fma-def [=>]100.0%

      \[ \color{blue}{\mathsf{fma}\left(x, y, z \cdot t\right)} + \left(a \cdot b + c \cdot i\right) \]

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

    1. Initial program 0.0%

      \[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]
    2. Simplified50.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, \mathsf{fma}\left(a, b, c \cdot i\right)\right)\right)} \]
      Step-by-step derivation

      [Start]0.0%

      \[ \left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]

      associate-+l+ [=>]0.0%

      \[ \color{blue}{\left(x \cdot y + z \cdot t\right) + \left(a \cdot b + c \cdot i\right)} \]

      associate-+l+ [=>]0.0%

      \[ \color{blue}{x \cdot y + \left(z \cdot t + \left(a \cdot b + c \cdot i\right)\right)} \]

      fma-def [=>]20.0%

      \[ \color{blue}{\mathsf{fma}\left(x, y, z \cdot t + \left(a \cdot b + c \cdot i\right)\right)} \]

      fma-def [=>]40.0%

      \[ \mathsf{fma}\left(x, y, \color{blue}{\mathsf{fma}\left(z, t, a \cdot b + c \cdot i\right)}\right) \]

      fma-def [=>]50.0%

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

      \[\leadsto \mathsf{fma}\left(x, y, \color{blue}{c \cdot i + t \cdot z}\right) \]
    4. Taylor expanded in c around 0 50.5%

      \[\leadsto \color{blue}{y \cdot x + t \cdot z} \]
    5. Simplified60.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, t \cdot z\right)} \]
      Step-by-step derivation

      [Start]50.5%

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

      fma-def [=>]60.5%

      \[ \color{blue}{\mathsf{fma}\left(y, x, t \cdot z\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.4%

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

Alternatives

Alternative 1
Accuracy97.5%
Cost8260
\[\begin{array}{l} \mathbf{if}\;c \cdot i + \left(a \cdot b + \left(z \cdot t + x \cdot y\right)\right) \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(x, y, z \cdot t\right) + \left(a \cdot b + c \cdot i\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z \cdot t\right)\\ \end{array} \]
Alternative 2
Accuracy98.2%
Cost19776
\[\mathsf{fma}\left(c, i, \mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right)\right) \]
Alternative 3
Accuracy97.6%
Cost7748
\[\begin{array}{l} t_1 := c \cdot i + \left(a \cdot b + \left(z \cdot t + x \cdot y\right)\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t, z, x \cdot y\right)\\ \end{array} \]
Alternative 4
Accuracy97.5%
Cost7748
\[\begin{array}{l} t_1 := c \cdot i + \left(a \cdot b + \left(z \cdot t + x \cdot y\right)\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z \cdot t\right)\\ \end{array} \]
Alternative 5
Accuracy96.9%
Cost7232
\[\mathsf{fma}\left(c, i, z \cdot t + \left(a \cdot b + x \cdot y\right)\right) \]
Alternative 6
Accuracy42.3%
Cost2532
\[\begin{array}{l} \mathbf{if}\;c \cdot i \leq -1.46 \cdot 10^{+113}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;c \cdot i \leq -7.8 \cdot 10^{-65}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;c \cdot i \leq -2.75 \cdot 10^{-174}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;c \cdot i \leq -7.9 \cdot 10^{-177}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;c \cdot i \leq -2.2 \cdot 10^{-264}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;c \cdot i \leq 4 \cdot 10^{-194}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;c \cdot i \leq 2 \cdot 10^{-144}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;c \cdot i \leq 6 \cdot 10^{-31}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;c \cdot i \leq 4.8 \cdot 10^{+184}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;c \cdot i\\ \end{array} \]
Alternative 7
Accuracy97.2%
Cost1988
\[\begin{array}{l} t_1 := z \cdot t + x \cdot y\\ t_2 := c \cdot i + \left(a \cdot b + t_1\right)\\ \mathbf{if}\;t_2 \leq \infty:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 8
Accuracy62.8%
Cost1748
\[\begin{array}{l} t_1 := a \cdot b + z \cdot t\\ t_2 := a \cdot b + x \cdot y\\ \mathbf{if}\;c \cdot i \leq -2.1 \cdot 10^{+147}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;c \cdot i \leq -3.05 \cdot 10^{-176}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \cdot i \leq 3.35 \cdot 10^{-195}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \cdot i \leq 4.5 \cdot 10^{-102}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \cdot i \leq 6 \cdot 10^{+179}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;c \cdot i\\ \end{array} \]
Alternative 9
Accuracy65.3%
Cost1748
\[\begin{array}{l} t_1 := a \cdot b + z \cdot t\\ t_2 := a \cdot b + x \cdot y\\ t_3 := a \cdot b + c \cdot i\\ \mathbf{if}\;c \cdot i \leq -1.75 \cdot 10^{+120}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;c \cdot i \leq -1.5 \cdot 10^{-176}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \cdot i \leq 2.7 \cdot 10^{-194}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \cdot i \leq 4.5 \cdot 10^{-102}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \cdot i \leq 1.7 \cdot 10^{+179}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_3\\ \end{array} \]
Alternative 10
Accuracy42.3%
Cost1492
\[\begin{array}{l} \mathbf{if}\;c \cdot i \leq -1.35 \cdot 10^{+109}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;c \cdot i \leq -2.1 \cdot 10^{-68}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;c \cdot i \leq 8.5 \cdot 10^{-195}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;c \cdot i \leq 8.6 \cdot 10^{-144}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;c \cdot i \leq 6.5 \cdot 10^{+132}:\\ \;\;\;\;a \cdot b\\ \mathbf{else}:\\ \;\;\;\;c \cdot i\\ \end{array} \]
Alternative 11
Accuracy87.7%
Cost1484
\[\begin{array}{l} t_1 := a \cdot b + \left(c \cdot i + x \cdot y\right)\\ \mathbf{if}\;c \cdot i \leq -4 \cdot 10^{+119}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \cdot i \leq 3.6 \cdot 10^{+41}:\\ \;\;\;\;a \cdot b + \left(z \cdot t + x \cdot y\right)\\ \mathbf{elif}\;c \cdot i \leq 4 \cdot 10^{+258}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;c \cdot i + z \cdot t\\ \end{array} \]
Alternative 12
Accuracy41.0%
Cost1234
\[\begin{array}{l} \mathbf{if}\;c \cdot i \leq -2.4 \cdot 10^{+140} \lor \neg \left(c \cdot i \leq -1.4 \cdot 10^{+81}\right) \land \left(c \cdot i \leq -1.75 \cdot 10^{-40} \lor \neg \left(c \cdot i \leq 7.5 \cdot 10^{+132}\right)\right):\\ \;\;\;\;c \cdot i\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 13
Accuracy76.2%
Cost1232
\[\begin{array}{l} t_1 := a \cdot b + \left(c \cdot i + x \cdot y\right)\\ t_2 := c \cdot i + z \cdot t\\ \mathbf{if}\;z \leq -2.9 \cdot 10^{+258}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq -3.6 \cdot 10^{+238}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -5.2 \cdot 10^{+147}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{elif}\;z \leq 2.2 \cdot 10^{-35}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 14
Accuracy87.6%
Cost1224
\[\begin{array}{l} t_1 := z \cdot t + x \cdot y\\ \mathbf{if}\;c \cdot i \leq -5.5 \cdot 10^{+119}:\\ \;\;\;\;a \cdot b + \left(c \cdot i + x \cdot y\right)\\ \mathbf{elif}\;c \cdot i \leq 7.4 \cdot 10^{+130}:\\ \;\;\;\;a \cdot b + t_1\\ \mathbf{else}:\\ \;\;\;\;c \cdot i + t_1\\ \end{array} \]
Alternative 15
Accuracy60.6%
Cost1108
\[\begin{array}{l} t_1 := c \cdot i + x \cdot y\\ \mathbf{if}\;x \leq -1.45 \cdot 10^{+111}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{elif}\;x \leq -7.7 \cdot 10^{+65}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -5.9 \cdot 10^{+62}:\\ \;\;\;\;a \cdot b + c \cdot i\\ \mathbf{elif}\;x \leq -6.8 \cdot 10^{-274}:\\ \;\;\;\;c \cdot i + z \cdot t\\ \mathbf{elif}\;x \leq 9.6 \cdot 10^{-109}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 16
Accuracy61.0%
Cost976
\[\begin{array}{l} t_1 := a \cdot b + c \cdot i\\ t_2 := z \cdot t + x \cdot y\\ \mathbf{if}\;y \leq -1.5 \cdot 10^{-77}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq 3.1 \cdot 10^{-294}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 6.5 \cdot 10^{-205}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{elif}\;y \leq 4 \cdot 10^{+79}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 17
Accuracy59.5%
Cost972
\[\begin{array}{l} \mathbf{if}\;c \cdot i \leq -2.15 \cdot 10^{+147}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;c \cdot i \leq 3 \cdot 10^{-29}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{elif}\;c \cdot i \leq 9 \cdot 10^{+178}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;c \cdot i\\ \end{array} \]
Alternative 18
Accuracy60.9%
Cost844
\[\begin{array}{l} t_1 := a \cdot b + x \cdot y\\ \mathbf{if}\;x \leq -1.5 \cdot 10^{+111}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -7.8 \cdot 10^{-274}:\\ \;\;\;\;c \cdot i + z \cdot t\\ \mathbf{elif}\;x \leq 10^{-64}:\\ \;\;\;\;a \cdot b + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 19
Accuracy27.7%
Cost192
\[a \cdot b \]

Reproduce?

herbie shell --seed 2023277 
(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)))