| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 7104 |
\[x \cdot x + \mathsf{fma}\left(y \cdot 2, y, y \cdot y\right)
\]

(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
(FPCore (x y) :precision binary64 (+ (* x x) (fma (* y 2.0) y (* y y))))
double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
double code(double x, double y) {
return (x * x) + fma((y * 2.0), y, (y * y));
}
function code(x, y) return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y)) end
function code(x, y) return Float64(Float64(x * x) + fma(Float64(y * 2.0), y, Float64(y * y))) end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * 2.0), $MachinePrecision] * y + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
x \cdot x + \mathsf{fma}\left(y \cdot 2, y, y \cdot y\right)
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
| Original | 99.9% |
|---|---|
| Target | 99.9% |
| Herbie | 99.9% |
Initial program 99.9%
Taylor expanded in x around 0 99.9%
Simplified99.8%
Applied egg-rr99.9%
[Start]99.8% | \[ x \cdot x + y \cdot \left(y \cdot 3\right)
\] |
|---|---|
associate-*r* [=>]99.9% | \[ x \cdot x + \color{blue}{\left(y \cdot y\right) \cdot 3}
\] |
metadata-eval [<=]99.9% | \[ x \cdot x + \left(y \cdot y\right) \cdot \color{blue}{\left(2 + 1\right)}
\] |
distribute-rgt-out [<=]99.9% | \[ x \cdot x + \color{blue}{\left(2 \cdot \left(y \cdot y\right) + 1 \cdot \left(y \cdot y\right)\right)}
\] |
*-un-lft-identity [<=]99.9% | \[ x \cdot x + \left(2 \cdot \left(y \cdot y\right) + \color{blue}{y \cdot y}\right)
\] |
associate-*r* [=>]99.9% | \[ x \cdot x + \left(\color{blue}{\left(2 \cdot y\right) \cdot y} + y \cdot y\right)
\] |
fma-def [=>]99.9% | \[ x \cdot x + \color{blue}{\mathsf{fma}\left(2 \cdot y, y, y \cdot y\right)}
\] |
*-commutative [=>]99.9% | \[ x \cdot x + \mathsf{fma}\left(\color{blue}{y \cdot 2}, y, y \cdot y\right)
\] |
Final simplification99.9%
| Alternative 1 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 7104 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 6848 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 6848 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 960 |
| Alternative 5 | |
|---|---|
| Accuracy | 79.2% |
| Cost | 585 |
| Alternative 6 | |
|---|---|
| Accuracy | 79.3% |
| Cost | 584 |
| Alternative 7 | |
|---|---|
| Accuracy | 99.9% |
| Cost | 576 |
| Alternative 8 | |
|---|---|
| Accuracy | 75.2% |
| Cost | 452 |
| Alternative 9 | |
|---|---|
| Accuracy | 57.2% |
| Cost | 192 |
herbie shell --seed 2023271
(FPCore (x y)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
:precision binary64
:herbie-target
(+ (* x x) (* y (+ y (+ y y))))
(+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))