Graphics.Rasterific.CubicBezier:cachedBezierAt from Rasterific-0.6.1

?

Percentage Accurate: 92.4% → 97.6%
Time: 11.2s
Precision: binary64
Cost: 20676

?

\[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b \]
\[\begin{array}{l} t_1 := \left(t \cdot a + \left(x + y \cdot z\right)\right) + \left(z \cdot a\right) \cdot b\\ \mathbf{if}\;t_1 \leq 10^{+307}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, z, \mathsf{fma}\left(a, \mathsf{fma}\left(z, b, t\right), x\right)\right)\\ \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 (+ (+ (* t a) (+ x (* y z))) (* (* z a) b))))
   (if (<= t_1 1e+307) t_1 (fma y z (fma a (fma z b t) x)))))
double code(double x, double y, double z, double t, double a, double b) {
	return ((x + (y * z)) + (t * a)) + ((a * z) * b);
}
double code(double x, double y, double z, double t, double a, double b) {
	double t_1 = ((t * a) + (x + (y * z))) + ((z * a) * b);
	double tmp;
	if (t_1 <= 1e+307) {
		tmp = t_1;
	} else {
		tmp = fma(y, z, fma(a, fma(z, b, t), x));
	}
	return tmp;
}
function code(x, y, z, t, a, b)
	return Float64(Float64(Float64(x + Float64(y * z)) + Float64(t * a)) + Float64(Float64(a * z) * b))
end
function code(x, y, z, t, a, b)
	t_1 = Float64(Float64(Float64(t * a) + Float64(x + Float64(y * z))) + Float64(Float64(z * a) * b))
	tmp = 0.0
	if (t_1 <= 1e+307)
		tmp = t_1;
	else
		tmp = fma(y, z, fma(a, fma(z, b, t), x));
	end
	return tmp
end
code[x_, y_, z_, t_, a_, b_] := N[(N[(N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision] + N[(t * a), $MachinePrecision]), $MachinePrecision] + N[(N[(a * z), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_, a_, b_] := Block[{t$95$1 = N[(N[(N[(t * a), $MachinePrecision] + N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(z * a), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 1e+307], t$95$1, N[(y * z + N[(a * N[(z * b + t), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]]]
\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b
\begin{array}{l}
t_1 := \left(t \cdot a + \left(x + y \cdot z\right)\right) + \left(z \cdot a\right) \cdot b\\
\mathbf{if}\;t_1 \leq 10^{+307}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, z, \mathsf{fma}\left(a, \mathsf{fma}\left(z, b, t\right), x\right)\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 13 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

Target

Original92.4%
Target97.6%
Herbie97.6%
\[\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?

  1. Split input into 2 regimes
  2. if (+.f64 (+.f64 (+.f64 x (*.f64 y z)) (*.f64 t a)) (*.f64 (*.f64 a z) b)) < 9.99999999999999986e306

    1. Initial program 99.5%

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

    if 9.99999999999999986e306 < (+.f64 (+.f64 (+.f64 x (*.f64 y z)) (*.f64 t a)) (*.f64 (*.f64 a z) b))

    1. Initial program 66.0%

      \[\left(\left(x + y \cdot z\right) + t \cdot a\right) + \left(a \cdot z\right) \cdot b \]
    2. Simplified97.6%

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

      [Start]66.0%

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

      associate-+l+ [=>]66.0%

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

      +-commutative [=>]66.0%

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

      associate-+l+ [=>]66.0%

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

      fma-def [=>]70.9%

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

      +-commutative [=>]70.9%

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

      *-commutative [=>]70.9%

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

      associate-*l* [=>]85.4%

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

      distribute-lft-out [=>]97.6%

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

      fma-def [=>]97.6%

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

      +-commutative [=>]97.6%

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

      fma-def [=>]97.6%

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

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

Alternatives

Alternative 1
Accuracy97.6%
Cost20676
\[\begin{array}{l} t_1 := \left(t \cdot a + \left(x + y \cdot z\right)\right) + \left(z \cdot a\right) \cdot b\\ \mathbf{if}\;t_1 \leq 10^{+307}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, z, \mathsf{fma}\left(a, \mathsf{fma}\left(z, b, t\right), x\right)\right)\\ \end{array} \]
Alternative 2
Accuracy96.7%
Cost1988
\[\begin{array}{l} t_1 := \left(t \cdot a + \left(x + y \cdot z\right)\right) + \left(z \cdot a\right) \cdot b\\ \mathbf{if}\;t_1 \leq \infty:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(y + a \cdot b\right)\\ \end{array} \]
Alternative 3
Accuracy35.6%
Cost1644
\[\begin{array}{l} t_1 := a \cdot \left(z \cdot b\right)\\ \mathbf{if}\;y \leq -8.8 \cdot 10^{+239}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq -1.5 \cdot 10^{+138}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq -2.2 \cdot 10^{+43}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -9 \cdot 10^{-19}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq -2.8 \cdot 10^{-200}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -7.4 \cdot 10^{-256}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq 1.15 \cdot 10^{-298}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 1.22 \cdot 10^{-135}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq 1.6 \cdot 10^{-83}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;y \leq 4.6 \cdot 10^{+38}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq 3.2 \cdot 10^{+145}:\\ \;\;\;\;t \cdot a\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]
Alternative 4
Accuracy37.4%
Cost1376
\[\begin{array}{l} t_1 := z \cdot \left(a \cdot b\right)\\ \mathbf{if}\;z \leq -9.5 \cdot 10^{+189}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq -9 \cdot 10^{+126}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq -1.95 \cdot 10^{+47}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -2.1 \cdot 10^{-8}:\\ \;\;\;\;a \cdot \left(z \cdot b\right)\\ \mathbf{elif}\;z \leq -1.78 \cdot 10^{-273}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;z \leq 3.6 \cdot 10^{-7}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 1.32 \cdot 10^{+76}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;z \leq 3.9 \cdot 10^{+89}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy92.8%
Cost1092
\[\begin{array}{l} \mathbf{if}\;z \leq -2.05 \cdot 10^{+170}:\\ \;\;\;\;z \cdot \left(y + a \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;\left(t \cdot a + a \cdot \left(z \cdot b\right)\right) + \left(x + y \cdot z\right)\\ \end{array} \]
Alternative 6
Accuracy58.8%
Cost980
\[\begin{array}{l} t_1 := x + t \cdot a\\ t_2 := z \cdot \left(a \cdot b\right)\\ \mathbf{if}\;z \leq -1.05 \cdot 10^{+190}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq -9.6 \cdot 10^{+126}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;z \leq 2.15 \cdot 10^{+89}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.65 \cdot 10^{+130}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 7.8 \cdot 10^{+147}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 7
Accuracy39.4%
Cost852
\[\begin{array}{l} \mathbf{if}\;a \leq -4.2 \cdot 10^{+31}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;a \leq -1.08 \cdot 10^{-260}:\\ \;\;\;\;x\\ \mathbf{elif}\;a \leq -1.85 \cdot 10^{-297}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;a \leq 9.5 \cdot 10^{-155}:\\ \;\;\;\;x\\ \mathbf{elif}\;a \leq 2.6 \cdot 10^{-17}:\\ \;\;\;\;y \cdot z\\ \mathbf{else}:\\ \;\;\;\;t \cdot a\\ \end{array} \]
Alternative 8
Accuracy80.8%
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -7200000000000 \lor \neg \left(a \leq 1.26 \cdot 10^{-31}\right):\\ \;\;\;\;x + a \cdot \left(t + z \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;x + y \cdot z\\ \end{array} \]
Alternative 9
Accuracy86.4%
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -2.6 \cdot 10^{+89} \lor \neg \left(a \leq 7.5 \cdot 10^{-23}\right):\\ \;\;\;\;x + a \cdot \left(t + z \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x + t \cdot a\right) + y \cdot z\\ \end{array} \]
Alternative 10
Accuracy61.0%
Cost716
\[\begin{array}{l} t_1 := x + t \cdot a\\ \mathbf{if}\;a \leq -1.9 \cdot 10^{+133}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;a \leq 3.75 \cdot 10^{-31}:\\ \;\;\;\;x + y \cdot z\\ \mathbf{elif}\;a \leq 3.8 \cdot 10^{+113}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;a \cdot \left(z \cdot b\right)\\ \end{array} \]
Alternative 11
Accuracy73.6%
Cost713
\[\begin{array}{l} \mathbf{if}\;a \leq -7.2 \cdot 10^{+15} \lor \neg \left(a \leq 1.52 \cdot 10^{-15}\right):\\ \;\;\;\;a \cdot \left(t + z \cdot b\right)\\ \mathbf{else}:\\ \;\;\;\;x + y \cdot z\\ \end{array} \]
Alternative 12
Accuracy39.0%
Cost456
\[\begin{array}{l} \mathbf{if}\;a \leq -7.5 \cdot 10^{+30}:\\ \;\;\;\;t \cdot a\\ \mathbf{elif}\;a \leq 1.9 \cdot 10^{-15}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;t \cdot a\\ \end{array} \]
Alternative 13
Accuracy26.4%
Cost64
\[x \]

Reproduce?

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