\[\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
\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}
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:
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 (*.f64 x y) (*.f64 z t)) (*.f64 a b)) (*.f64 c i)) < +inf.0
Initial program 100.0%
\[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\]
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))
Initial program 0.0%
\[\left(\left(x \cdot y + z \cdot t\right) + a \cdot b\right) + c \cdot i
\]
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)
\]
Taylor expanded in a around 0 40.0%
\[\leadsto \mathsf{fma}\left(x, y, \color{blue}{c \cdot i + t \cdot z}\right)
\]
Taylor expanded in c around 0 50.5%
\[\leadsto \color{blue}{y \cdot x + t \cdot z}
\]
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)}
\]
Recombined 2 regimes into one program.
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
Accuracy
97.5%
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(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
Accuracy
98.2%
Cost
19776
\[\mathsf{fma}\left(c, i, \mathsf{fma}\left(x, y, \mathsf{fma}\left(z, t, a \cdot b\right)\right)\right)
\]
Alternative 3
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(t, z, x \cdot y\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(y, x, z \cdot t\right)\\
\end{array}
\]
Alternative 5
Accuracy
96.9%
Cost
7232
\[\mathsf{fma}\left(c, i, z \cdot t + \left(a \cdot b + x \cdot y\right)\right)
\]
\[\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
Accuracy
62.8%
Cost
1748
\[\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
Accuracy
65.3%
Cost
1748
\[\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}
\]
\[\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}
\]
\[\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
Accuracy
87.6%
Cost
1224
\[\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
Accuracy
60.6%
Cost
1108
\[\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
Accuracy
61.0%
Cost
976
\[\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
Accuracy
59.5%
Cost
972
\[\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
Accuracy
60.9%
Cost
844
\[\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}
\]
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)))