\[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b
\]
↓
\[\begin{array}{l}
t_1 := b \cdot \left(z \cdot a\right) + \left(t \cdot a + \left(x + y \cdot z\right)\right)\\
\mathbf{if}\;b \leq -9.5 \cdot 10^{+124}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(a, t + b \cdot z, \mathsf{fma}\left(y, z, x\right)\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
(FPCore (x y z t a b)
:precision binary64
(+ (+ (+ x (* y z)) (* t a)) (* (* a z) b)))
↓
(FPCore (x y z t a b)
:precision binary64
(let* ((t_1 (+ (* b (* z a)) (+ (* t a) (+ x (* y z))))))
(if (<= b -9.5e+124)
t_1
(if (<= b 5e-9) (fma a (+ t (* b z)) (fma y z x)) t_1))))
\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b
↓
\begin{array}{l}
t_1 := b \cdot \left(z \cdot a\right) + \left(t \cdot a + \left(x + y \cdot z\right)\right)\\
\mathbf{if}\;b \leq -9.5 \cdot 10^{+124}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(a, t + b \cdot z, \mathsf{fma}\left(y, z, x\right)\right)\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
Error
Target
Original
2.0
Target
0.3
Herbie
0.5
\[\begin{array}{l}
\mathbf{if}\;z < -11820553527347888000:\\
\;\;\;\;z \cdot \left(b \cdot a + y\right) + \left(x + t \cdot a\right)\\
\mathbf{elif}\;z < 4.7589743188364287 \cdot 10^{-122}:\\
\;\;\;\;\left(b \cdot z + t\right) \cdot a + \left(z \cdot y + x\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(b \cdot a + y\right) + \left(x + t \cdot a\right)\\
\end{array}
\]
Derivation
Split input into 2 regimes
if b < -9.50000000000000004e124 or 5.0000000000000001e-9 < b
Initial program 0.7
\[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b
\]
if -9.50000000000000004e124 < b < 5.0000000000000001e-9
Initial program 2.7
\[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b
\]
Simplified0.4
\[\leadsto \color{blue}{\mathsf{fma}\left(a, t + z \cdot b, \mathsf{fma}\left(y, z, x\right)\right)}
\]
Proof
(fma.f64 a (+.f64 t (*.f64 z b)) (fma.f64 y z x)): 0 points increase in error, 0 points decrease in error
(fma.f64 a (+.f64 t (*.f64 z b)) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 y z) x))): 0 points increase in error, 0 points decrease in error
(fma.f64 a (+.f64 t (*.f64 z b)) (Rewrite<= +-commutative_binary64 (+.f64 x (*.f64 y z)))): 0 points increase in error, 0 points decrease in error
(Rewrite<= fma-def_binary64 (+.f64 (*.f64 a (+.f64 t (*.f64 z b))) (+.f64 x (*.f64 y z)))): 2 points increase in error, 0 points decrease in error
(+.f64 (Rewrite<= distribute-lft-out_binary64 (+.f64 (*.f64 a t) (*.f64 a (*.f64 z b)))) (+.f64 x (*.f64 y z))): 1 points increase in error, 1 points decrease in error
(+.f64 (+.f64 (Rewrite<= *-commutative_binary64 (*.f64 t a)) (*.f64 a (*.f64 z b))) (+.f64 x (*.f64 y z))): 0 points increase in error, 0 points decrease in error
(+.f64 (+.f64 (*.f64 t a) (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 a z) b))) (+.f64 x (*.f64 y z))): 13 points increase in error, 17 points decrease in error
(Rewrite<= +-commutative_binary64 (+.f64 (+.f64 x (*.f64 y z)) (+.f64 (*.f64 t a) (*.f64 (*.f64 a z) b)))): 0 points increase in error, 0 points decrease in error
(Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (+.f64 x (*.f64 y z)) (*.f64 t a)) (*.f64 (*.f64 a z) b))): 0 points increase in error, 0 points decrease in error
Recombined 2 regimes into one program.
Final simplification0.5
\[\leadsto \begin{array}{l}
\mathbf{if}\;b \leq -9.5 \cdot 10^{+124}:\\
\;\;\;\;b \cdot \left(z \cdot a\right) + \left(t \cdot a + \left(x + y \cdot z\right)\right)\\
\mathbf{elif}\;b \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(a, t + b \cdot z, \mathsf{fma}\left(y, z, x\right)\right)\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(z \cdot a\right) + \left(t \cdot a + \left(x + y \cdot z\right)\right)\\
\end{array}
\]
Alternatives
Alternative 1
Error
9.8
Cost
1756
\[\begin{array}{l}
t_1 := b \cdot \left(z \cdot a\right) + \left(t \cdot a + y \cdot z\right)\\
t_2 := t \cdot a + \left(x + y \cdot z\right)\\
\mathbf{if}\;t \leq -3.8 \cdot 10^{+79}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;t \leq -3.6 \cdot 10^{-47}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;t \leq -2.5 \cdot 10^{-160}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;t \leq 9 \cdot 10^{-100}:\\
\;\;\;\;x + z \cdot \left(y + b \cdot a\right)\\
\mathbf{elif}\;t \leq 7.1 \cdot 10^{-84}:\\
\;\;\;\;t \cdot a + \left(x + a \cdot \left(b \cdot z\right)\right)\\
\mathbf{elif}\;t \leq 8.6 \cdot 10^{-16}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;t \leq 1.5 \cdot 10^{+21}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;t_2\\
\end{array}
\]
Alternative 2
Error
15.1
Cost
1368
\[\begin{array}{l}
t_1 := x + a \cdot \left(t + b \cdot z\right)\\
t_2 := z \cdot \left(y + b \cdot a\right)\\
t_3 := x + y \cdot z\\
\mathbf{if}\;z \leq -1.8 \cdot 10^{+124}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;z \leq -2.1 \cdot 10^{+43}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;z \leq -9.5 \cdot 10^{-20}:\\
\;\;\;\;t_3\\
\mathbf{elif}\;z \leq 3.7 \cdot 10^{-55}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;z \leq 1.42 \cdot 10^{+163}:\\
\;\;\;\;t_3\\
\mathbf{elif}\;z \leq 10^{+215}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;t_2\\
\end{array}
\]
herbie shell --seed 2022329
(FPCore (x y z t a b)
:name "Graphics.Rasterific.CubicBezier:cachedBezierAt from Rasterific-0.6.1"
:precision binary64
:herbie-target
(if (< z -11820553527347888000.0) (+ (* z (+ (* b a) y)) (+ x (* t a))) (if (< z 4.7589743188364287e-122) (+ (* (+ (* b z) t) a) (+ (* z y) x)) (+ (* z (+ (* b a) y)) (+ x (* t a)))))
(+ (+ (+ x (* y z)) (* t a)) (* (* a z) b)))