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

?

Percentage Accurate: 96.0% → 97.9%
Time: 12.2s
Precision: binary64
Cost: 19776

?

\[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i \]
\[\mathsf{fma}\left(c, i, \mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right)\right) \]
(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
 (fma c i (fma x y (fma z t (* a b)))))
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) {
	return fma(c, i, fma(x, y, fma(z, t, (a * b))));
}
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)
	return fma(c, i, fma(x, y, fma(z, t, Float64(a * b))))
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_] := N[(c * i + N[(x * y + N[(z * t + N[(a * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\mathsf{fma}\left(c, i, \mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right)\right)

Local Percentage Accuracy?

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 15 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Derivation?

  1. Initial program 93.3%

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

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

    [Start]93.3

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

    +-commutative [=>]93.3

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

    fma-def [=>]96.1

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

    associate-+l+ [=>]96.1

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

    fma-def [=>]96.9

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

    fma-def [=>]98.4

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

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

Alternatives

Alternative 1
Accuracy97.2%
Cost7753
\[\begin{array}{l} \mathbf{if}\;c \cdot i \leq -\infty \lor \neg \left(c \cdot i \leq 5 \cdot 10^{+271}\right):\\ \;\;\;\;\mathsf{fma}\left(c, i, x \cdot y + z \cdot t\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, y, z \cdot t\right) + \left(a \cdot b + c \cdot i\right)\\ \end{array} \]
Alternative 2
Accuracy96.8%
Cost7752
\[\begin{array}{l} t_1 := x \cdot y + z \cdot t\\ \mathbf{if}\;t_1 \leq -2 \cdot 10^{+247}:\\ \;\;\;\;\mathsf{fma}\left(c, i, t_1\right)\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+286}:\\ \;\;\;\;c \cdot i + \left(a \cdot b + t_1\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z \cdot t\right)\\ \end{array} \]
Alternative 3
Accuracy64.9%
Cost2268
\[\begin{array}{l} t_1 := a \cdot b + c \cdot i\\ t_2 := x \cdot y + z \cdot t\\ t_3 := c \cdot i + x \cdot y\\ \mathbf{if}\;a \cdot b \leq -3.2 \cdot 10^{+172}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \cdot b \leq -7.2 \cdot 10^{+38}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a \cdot b \leq -3.7:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \cdot b \leq -1.9 \cdot 10^{-307}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a \cdot b \leq 6.3 \cdot 10^{-308}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;a \cdot b \leq 2.45 \cdot 10^{-197}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a \cdot b \leq 1.55 \cdot 10^{+36}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \end{array} \]
Alternative 4
Accuracy42.0%
Cost2012
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -3.7 \cdot 10^{+130}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;a \cdot b \leq -2.5 \cdot 10^{-307}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 1.4 \cdot 10^{-306}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;a \cdot b \leq 3.4 \cdot 10^{-191}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 3.3 \cdot 10^{-105}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;a \cdot b \leq 5.5 \cdot 10^{-10}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;a \cdot b \leq 2.35 \cdot 10^{+44}:\\ \;\;\;\;c \cdot i\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 5
Accuracy66.1%
Cost2008
\[\begin{array}{l} t_1 := c \cdot i + z \cdot t\\ t_2 := a \cdot b + x \cdot y\\ t_3 := c \cdot i + x \cdot y\\ \mathbf{if}\;a \cdot b \leq -1.1 \cdot 10^{+40}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;a \cdot b \leq -6 \cdot 10^{-307}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \cdot b \leq 0:\\ \;\;\;\;t_3\\ \mathbf{elif}\;a \cdot b \leq 1.1 \cdot 10^{-198}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \cdot b \leq 5.6 \cdot 10^{+50}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;a \cdot b \leq 1.12 \cdot 10^{+114}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 6
Accuracy97.3%
Cost1988
\[\begin{array}{l} t_1 := c \cdot i + \left(a \cdot b + \left(x \cdot y + z \cdot t\right)\right)\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;a \cdot b + x \cdot y\\ \end{array} \]
Alternative 7
Accuracy37.8%
Cost1776
\[\begin{array}{l} \mathbf{if}\;y \leq -1.95 \cdot 10^{+31}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq -5 \cdot 10^{-50}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;y \leq -1.05 \cdot 10^{-53}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq -7.2 \cdot 10^{-275}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;y \leq 6.6 \cdot 10^{-244}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;y \leq 10^{-157}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;y \leq 1.05 \cdot 10^{-78}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;y \leq 6 \cdot 10^{+54}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;y \leq 1.05 \cdot 10^{+90}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;y \leq 6.2 \cdot 10^{+118}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;y \leq 5.5 \cdot 10^{+152}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;y \leq 2.3 \cdot 10^{+155}:\\ \;\;\;\;z \cdot t\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 8
Accuracy87.9%
Cost1484
\[\begin{array}{l} t_1 := c \cdot i + \left(a \cdot b + x \cdot y\right)\\ \mathbf{if}\;a \cdot b \leq -5.6 \cdot 10^{+142}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \cdot b \leq 1.12 \cdot 10^{+126}:\\ \;\;\;\;c \cdot i + \left(x \cdot y + z \cdot t\right)\\ \mathbf{elif}\;a \cdot b \leq 1.2 \cdot 10^{+277}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 9
Accuracy54.3%
Cost1372
\[\begin{array}{l} t_1 := a \cdot b + c \cdot i\\ t_2 := a \cdot b + x \cdot y\\ \mathbf{if}\;z \leq -7 \cdot 10^{+168}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;z \leq -4.8 \cdot 10^{+124}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -5.5 \cdot 10^{+37}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;z \leq -9.6 \cdot 10^{-82}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -1.75 \cdot 10^{-125}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq -1.1 \cdot 10^{-302}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 2.2 \cdot 10^{+41}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;z \cdot t\\ \end{array} \]
Alternative 10
Accuracy49.5%
Cost1248
\[\begin{array}{l} t_1 := a \cdot b + x \cdot y\\ \mathbf{if}\;i \leq -1.6 \cdot 10^{-37}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;i \leq 4.1 \cdot 10^{-109}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;i \leq 1.22 \cdot 10^{+22}:\\ \;\;\;\;z \cdot t\\ \mathbf{elif}\;i \leq 10^{+192}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;i \leq 2.15 \cdot 10^{+206}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;i \leq 1.5 \cdot 10^{+226}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;i \leq 4.5 \cdot 10^{+256}:\\ \;\;\;\;c \cdot i\\ \mathbf{elif}\;i \leq 3 \cdot 10^{+261}:\\ \;\;\;\;a \cdot b\\ \mathbf{else}:\\ \;\;\;\;c \cdot i\\ \end{array} \]
Alternative 11
Accuracy77.1%
Cost1234
\[\begin{array}{l} \mathbf{if}\;z \leq -3.2 \cdot 10^{+167} \lor \neg \left(z \leq -3.2 \cdot 10^{+124}\right) \land \left(z \leq -2.4 \cdot 10^{+44} \lor \neg \left(z \leq 3 \cdot 10^{+39}\right)\right):\\ \;\;\;\;x \cdot y + z \cdot t\\ \mathbf{else}:\\ \;\;\;\;c \cdot i + \left(a \cdot b + x \cdot y\right)\\ \end{array} \]
Alternative 12
Accuracy65.7%
Cost969
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -1.08 \cdot 10^{+40} \lor \neg \left(a \cdot b \leq 1.05 \cdot 10^{+111}\right):\\ \;\;\;\;a \cdot b + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;c \cdot i + z \cdot t\\ \end{array} \]
Alternative 13
Accuracy42.6%
Cost712
\[\begin{array}{l} \mathbf{if}\;a \cdot b \leq -6.6 \cdot 10^{+39}:\\ \;\;\;\;a \cdot b\\ \mathbf{elif}\;a \cdot b \leq 4 \cdot 10^{+45}:\\ \;\;\;\;c \cdot i\\ \mathbf{else}:\\ \;\;\;\;a \cdot b\\ \end{array} \]
Alternative 14
Accuracy27.1%
Cost192
\[a \cdot b \]

Reproduce?

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