\[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\]
↓
\[\begin{array}{l}
\mathbf{if}\;a \cdot b + \left(z \cdot t + x \cdot y\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, \mathsf{fma}\left(a, b, c \cdot i\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;c \cdot i + \mathsf{fma}\left(z, t, x \cdot y\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 (<= (+ (* a b) (+ (* z t) (* x y))) INFINITY)
(fma x y (fma z t (fma a b (* c i))))
(+ (* c i) (fma z t (* x y)))))
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);
}
\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
↓
\begin{array}{l}
\mathbf{if}\;a \cdot b + \left(z \cdot t + x \cdot y\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, \mathsf{fma}\left(a, b, c \cdot i\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;c \cdot i + \mathsf{fma}\left(z, t, x \cdot y\right)\\
\end{array}
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 18 alternatives:
Alternative
Accuracy
Speedup
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.
if (+.f64 (+.f64 (*.f64 x y) (*.f64 z t)) (*.f64 a b)) < +inf.0
Initial program 97.5%
\[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\]
Simplified99.2%
\[\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]97.5%
\[ \left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\]
associate-+l+ [=>]97.5%
\[ \color{blue}{\left(x \cdot y + z \cdot t\right) + \left(a \cdot b + c \cdot i\right)}
\]
associate-+l+ [=>]97.5%
\[ \color{blue}{x \cdot y + \left(z \cdot t + \left(a \cdot b + c \cdot i\right)\right)}
\]
fma-def [=>]98.3%
\[ \color{blue}{\mathsf{fma}\left(x, y, z \cdot t + \left(a \cdot b + c \cdot i\right)\right)}
\]
fma-def [=>]98.8%
\[ \mathsf{fma}\left(x, y, \color{blue}{\mathsf{fma}\left(z, t, a \cdot b + c \cdot i\right)}\right)
\]
fma-def [=>]99.2%
\[ \mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, \color{blue}{\mathsf{fma}\left(a, b, c \cdot i\right)}\right)\right)
\]
if +inf.0 < (+.f64 (+.f64 (*.f64 x y) (*.f64 z t)) (*.f64 a b))
Initial program 0.0%
\[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\]
Taylor expanded in a around 0 40.0%
\[\leadsto \color{blue}{c \cdot i + \left(y \cdot x + t \cdot z\right)}
\]
Applied egg-rr80.0%
\[\leadsto c \cdot i + \color{blue}{\mathsf{fma}\left(z, t, x \cdot y\right)}
\]
Step-by-step derivation
[Start]40.0%
\[ c \cdot i + \left(y \cdot x + t \cdot z\right)
\]
+-commutative [=>]40.0%
\[ c \cdot i + \color{blue}{\left(t \cdot z + y \cdot x\right)}
\]
*-commutative [=>]40.0%
\[ c \cdot i + \left(\color{blue}{z \cdot t} + y \cdot x\right)
\]
*-commutative [<=]40.0%
\[ c \cdot i + \left(z \cdot t + \color{blue}{x \cdot y}\right)
\]
fma-udef [<=]80.0%
\[ c \cdot i + \color{blue}{\mathsf{fma}\left(z, t, x \cdot y\right)}
\]
Recombined 2 regimes into one program.
Final simplification98.4%
\[\leadsto \begin{array}{l}
\mathbf{if}\;a \cdot b + \left(z \cdot t + x \cdot y\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, \mathsf{fma}\left(a, b, c \cdot i\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;c \cdot i + \mathsf{fma}\left(z, t, x \cdot y\right)\\
\end{array}
\]
Alternatives
Alternative 1
Accuracy
97.8%
Cost
20548
\[\begin{array}{l}
\mathbf{if}\;a \cdot b + \left(z \cdot t + x \cdot y\right) \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, \mathsf{fma}\left(a, b, c \cdot i\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;c \cdot i + \mathsf{fma}\left(z, t, x \cdot y\right)\\
\end{array}
\]
Alternative 2
Accuracy
97.6%
Cost
8260
\[\begin{array}{l}
\mathbf{if}\;c \cdot i + \left(a \cdot b + \left(z \cdot t + x \cdot y\right)\right) \leq \infty:\\
\;\;\;\;c \cdot i + \left(a \cdot b + \mathsf{fma}\left(z, t, x \cdot y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, t, c \cdot i\right)\\
\end{array}
\]
Alternative 3
Accuracy
97.6%
Cost
8260
\[\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(z, t, a \cdot b + c \cdot i\right) + x \cdot y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, t, c \cdot i\right)\\
\end{array}
\]
Alternative 4
Accuracy
97.5%
Cost
7748
\[\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(c, i, z \cdot t\right)\\
\end{array}
\]
Alternative 5
Accuracy
97.6%
Cost
7748
\[\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(z, t, c \cdot i\right)\\
\end{array}
\]
\[\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}:\\
\;\;\;\;c \cdot i + z \cdot t\\
\end{array}
\]
Alternative 8
Accuracy
63.5%
Cost
1490
\[\begin{array}{l}
\mathbf{if}\;c \cdot i \leq -2.25 \cdot 10^{+118} \lor \neg \left(c \cdot i \leq 1.8 \cdot 10^{-30}\right) \land \left(c \cdot i \leq 7.4 \cdot 10^{+80} \lor \neg \left(c \cdot i \leq 1.25 \cdot 10^{+212}\right)\right):\\
\;\;\;\;c \cdot i + z \cdot t\\
\mathbf{else}:\\
\;\;\;\;a \cdot b + x \cdot y\\
\end{array}
\]
Alternative 9
Accuracy
64.1%
Cost
1488
\[\begin{array}{l}
t_1 := a \cdot b + c \cdot i\\
\mathbf{if}\;c \cdot i \leq -1.1 \cdot 10^{+116}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;c \cdot i \leq 6.2 \cdot 10^{-150}:\\
\;\;\;\;z \cdot t + x \cdot y\\
\mathbf{elif}\;c \cdot i \leq 1.3 \cdot 10^{-30}:\\
\;\;\;\;a \cdot b + x \cdot y\\
\mathbf{elif}\;c \cdot i \leq 5.6 \cdot 10^{+79}:\\
\;\;\;\;c \cdot i + z \cdot t\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
Alternative 10
Accuracy
84.3%
Cost
1225
\[\begin{array}{l}
\mathbf{if}\;c \cdot i \leq -3.2 \cdot 10^{+121} \lor \neg \left(c \cdot i \leq 3 \cdot 10^{+212}\right):\\
\;\;\;\;c \cdot i + z \cdot t\\
\mathbf{else}:\\
\;\;\;\;a \cdot b + \left(z \cdot t + x \cdot y\right)\\
\end{array}
\]
Alternative 11
Accuracy
87.1%
Cost
1225
\[\begin{array}{l}
\mathbf{if}\;c \cdot i \leq -2.8 \cdot 10^{+122} \lor \neg \left(c \cdot i \leq 1.1 \cdot 10^{+41}\right):\\
\;\;\;\;c \cdot i + \left(a \cdot b + z \cdot t\right)\\
\mathbf{else}:\\
\;\;\;\;a \cdot b + \left(z \cdot t + x \cdot y\right)\\
\end{array}
\]
Alternative 12
Accuracy
88.3%
Cost
1225
\[\begin{array}{l}
\mathbf{if}\;z \cdot t \leq -5 \cdot 10^{+127} \lor \neg \left(z \cdot t \leq 5 \cdot 10^{+56}\right):\\
\;\;\;\;c \cdot i + \left(z \cdot t + x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;c \cdot i + \left(a \cdot b + x \cdot y\right)\\
\end{array}
\]
Alternative 13
Accuracy
85.4%
Cost
1224
\[\begin{array}{l}
t_1 := z \cdot t + x \cdot y\\
\mathbf{if}\;a \cdot b \leq -6.8 \cdot 10^{+71}:\\
\;\;\;\;a \cdot b + c \cdot i\\
\mathbf{elif}\;a \cdot b \leq 6.4 \cdot 10^{+69}:\\
\;\;\;\;c \cdot i + t_1\\
\mathbf{else}:\\
\;\;\;\;a \cdot b + t_1\\
\end{array}
\]
herbie shell --seed 2023271
(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)))