| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 16580 |

(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))
(FPCore (a b)
:precision binary64
(let* ((t_0
(+
(pow (+ (* a a) (* b b)) 2.0)
(* 4.0 (+ (* (* a a) (+ a 1.0)) (* (* b b) (- 1.0 (* a 3.0))))))))
(if (<= t_0 INFINITY)
(+ t_0 -1.0)
(* (* a a) (+ (fma 2.0 (* b b) 4.0) (* a (+ a 4.0)))))))double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0;
}
double code(double a, double b) {
double t_0 = pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (a + 1.0)) + ((b * b) * (1.0 - (a * 3.0)))));
double tmp;
if (t_0 <= ((double) INFINITY)) {
tmp = t_0 + -1.0;
} else {
tmp = (a * a) * (fma(2.0, (b * b), 4.0) + (a * (a + 4.0)));
}
return tmp;
}
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(1.0 + a)) + Float64(Float64(b * b) * Float64(1.0 - Float64(3.0 * a)))))) - 1.0) end
function code(a, b) t_0 = Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(a + 1.0)) + Float64(Float64(b * b) * Float64(1.0 - Float64(a * 3.0)))))) tmp = 0.0 if (t_0 <= Inf) tmp = Float64(t_0 + -1.0); else tmp = Float64(Float64(a * a) * Float64(fma(2.0, Float64(b * b), 4.0) + Float64(a * Float64(a + 4.0)))); end return tmp end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(1.0 + a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[a_, b_] := Block[{t$95$0 = N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(a + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(a * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], N[(t$95$0 + -1.0), $MachinePrecision], N[(N[(a * a), $MachinePrecision] * N[(N[(2.0 * N[(b * b), $MachinePrecision] + 4.0), $MachinePrecision] + N[(a * N[(a + 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\begin{array}{l}
t_0 := {\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(a + 1\right) + \left(b \cdot b\right) \cdot \left(1 - a \cdot 3\right)\right)\\
\mathbf{if}\;t_0 \leq \infty:\\
\;\;\;\;t_0 + -1\\
\mathbf{else}:\\
\;\;\;\;\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(a + 4\right)\right)\\
\end{array}
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
if (+.f64 (pow.f64 (+.f64 (*.f64 a a) (*.f64 b b)) 2) (*.f64 4 (+.f64 (*.f64 (*.f64 a a) (+.f64 1 a)) (*.f64 (*.f64 b b) (-.f64 1 (*.f64 3 a)))))) < +inf.0Initial program 99.8%
if +inf.0 < (+.f64 (pow.f64 (+.f64 (*.f64 a a) (*.f64 b b)) 2) (*.f64 4 (+.f64 (*.f64 (*.f64 a a) (+.f64 1 a)) (*.f64 (*.f64 b b) (-.f64 1 (*.f64 3 a)))))) Initial program 0.0%
Simplified4.4%
[Start]0.0% | \[ \left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\] |
|---|---|
associate--l+ [=>]0.0% | \[ \color{blue}{{\left(a \cdot a + b \cdot b\right)}^{2} + \left(4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right) - 1\right)}
\] |
fma-def [=>]0.0% | \[ {\color{blue}{\left(\mathsf{fma}\left(a, a, b \cdot b\right)\right)}}^{2} + \left(4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right) - 1\right)
\] |
Taylor expanded in a around 0 4.4%
Simplified4.4%
[Start]4.4% | \[ \left(2 \cdot \left({a}^{2} \cdot {b}^{2}\right) + \left({a}^{4} + {b}^{4}\right)\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
|---|---|
fma-def [=>]4.4% | \[ \color{blue}{\mathsf{fma}\left(2, {a}^{2} \cdot {b}^{2}, {a}^{4} + {b}^{4}\right)} + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
+-commutative [<=]4.4% | \[ \mathsf{fma}\left(2, {a}^{2} \cdot {b}^{2}, \color{blue}{{b}^{4} + {a}^{4}}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
*-commutative [=>]4.4% | \[ \mathsf{fma}\left(2, \color{blue}{{b}^{2} \cdot {a}^{2}}, {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
unpow2 [=>]4.4% | \[ \mathsf{fma}\left(2, \color{blue}{\left(b \cdot b\right)} \cdot {a}^{2}, {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
associate-*l* [=>]4.4% | \[ \mathsf{fma}\left(2, \color{blue}{b \cdot \left(b \cdot {a}^{2}\right)}, {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
unpow2 [=>]4.4% | \[ \mathsf{fma}\left(2, b \cdot \left(b \cdot \color{blue}{\left(a \cdot a\right)}\right), {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)
\] |
Taylor expanded in a around inf 33.8%
Simplified100.0%
[Start]33.8% | \[ 4 \cdot {a}^{3} + \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right)
\] |
|---|---|
+-commutative [=>]33.8% | \[ \color{blue}{\left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + 4 \cdot {a}^{3}}
\] |
*-commutative [=>]33.8% | \[ \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + \color{blue}{{a}^{3} \cdot 4}
\] |
cube-mult [=>]33.8% | \[ \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + \color{blue}{\left(a \cdot \left(a \cdot a\right)\right)} \cdot 4
\] |
associate-*r* [<=]33.8% | \[ \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + \color{blue}{a \cdot \left(\left(a \cdot a\right) \cdot 4\right)}
\] |
associate-+l+ [=>]33.8% | \[ \color{blue}{\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + \left({a}^{4} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)}
\] |
unpow2 [=>]33.8% | \[ \left(2 \cdot {b}^{2} + 4\right) \cdot \color{blue}{\left(a \cdot a\right)} + \left({a}^{4} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)
\] |
*-commutative [=>]33.8% | \[ \color{blue}{\left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right)} + \left({a}^{4} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)
\] |
metadata-eval [<=]33.8% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left({a}^{\color{blue}{\left(2 \cdot 2\right)}} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)
\] |
pow-sqr [<=]33.8% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\color{blue}{{a}^{2} \cdot {a}^{2}} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)
\] |
unpow2 [=>]33.8% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\color{blue}{\left(a \cdot a\right)} \cdot {a}^{2} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)
\] |
unpow2 [=>]33.8% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\left(a \cdot a\right) \cdot \color{blue}{\left(a \cdot a\right)} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)
\] |
*-commutative [<=]33.8% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\left(a \cdot a\right) \cdot \left(a \cdot a\right) + \color{blue}{\left(\left(a \cdot a\right) \cdot 4\right) \cdot a}\right)
\] |
associate-*l* [=>]33.8% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\left(a \cdot a\right) \cdot \left(a \cdot a\right) + \color{blue}{\left(a \cdot a\right) \cdot \left(4 \cdot a\right)}\right)
\] |
distribute-lft-out [=>]100.0% | \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \color{blue}{\left(a \cdot a\right) \cdot \left(a \cdot a + 4 \cdot a\right)}
\] |
distribute-lft-out [=>]100.0% | \[ \color{blue}{\left(a \cdot a\right) \cdot \left(\left(2 \cdot {b}^{2} + 4\right) + \left(a \cdot a + 4 \cdot a\right)\right)}
\] |
fma-def [=>]100.0% | \[ \left(a \cdot a\right) \cdot \left(\color{blue}{\mathsf{fma}\left(2, {b}^{2}, 4\right)} + \left(a \cdot a + 4 \cdot a\right)\right)
\] |
unpow2 [=>]100.0% | \[ \left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, \color{blue}{b \cdot b}, 4\right) + \left(a \cdot a + 4 \cdot a\right)\right)
\] |
Final simplification99.9%
| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 16580 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.1% |
| Cost | 20544 |
| Alternative 3 | |
|---|---|
| Accuracy | 97.7% |
| Cost | 7625 |
| Alternative 4 | |
|---|---|
| Accuracy | 93.5% |
| Cost | 7304 |
| Alternative 5 | |
|---|---|
| Accuracy | 94.0% |
| Cost | 6788 |
| Alternative 6 | |
|---|---|
| Accuracy | 79.8% |
| Cost | 1220 |
| Alternative 7 | |
|---|---|
| Accuracy | 79.2% |
| Cost | 964 |
| Alternative 8 | |
|---|---|
| Accuracy | 66.7% |
| Cost | 836 |
| Alternative 9 | |
|---|---|
| Accuracy | 78.9% |
| Cost | 836 |
| Alternative 10 | |
|---|---|
| Accuracy | 50.9% |
| Cost | 448 |
| Alternative 11 | |
|---|---|
| Accuracy | 25.1% |
| Cost | 64 |
herbie shell --seed 2023271
(FPCore (a b)
:name "Bouland and Aaronson, Equation (25)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))