| Alternative 1 | |
|---|---|
| Accuracy | 96.7% |
| Cost | 7492 |

(FPCore (x.re x.im) :precision binary64 (+ (* (- (* x.re x.re) (* x.im x.im)) x.im) (* (+ (* x.re x.im) (* x.im x.re)) x.re)))
NOTE: x.re should be positive before calling this function (FPCore (x.re x.im) :precision binary64 (if (<= x.re 7.6e+153) (* x.im (- (* 3.0 (* x.re x.re)) (* x.im x.im))) (fma (* x.im (* x.re 2.0)) x.re (* (* x.im (+ x.re x.im)) (- x.re x.im)))))
double code(double x_46_re, double x_46_im) {
return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
}
x.re = abs(x.re);
double code(double x_46_re, double x_46_im) {
double tmp;
if (x_46_re <= 7.6e+153) {
tmp = x_46_im * ((3.0 * (x_46_re * x_46_re)) - (x_46_im * x_46_im));
} else {
tmp = fma((x_46_im * (x_46_re * 2.0)), x_46_re, ((x_46_im * (x_46_re + x_46_im)) * (x_46_re - x_46_im)));
}
return tmp;
}
function code(x_46_re, x_46_im) return Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im)) * x_46_im) + Float64(Float64(Float64(x_46_re * x_46_im) + Float64(x_46_im * x_46_re)) * x_46_re)) end
x.re = abs(x.re) function code(x_46_re, x_46_im) tmp = 0.0 if (x_46_re <= 7.6e+153) tmp = Float64(x_46_im * Float64(Float64(3.0 * Float64(x_46_re * x_46_re)) - Float64(x_46_im * x_46_im))); else tmp = fma(Float64(x_46_im * Float64(x_46_re * 2.0)), x_46_re, Float64(Float64(x_46_im * Float64(x_46_re + x_46_im)) * Float64(x_46_re - x_46_im))); end return tmp end
code[x$46$re_, x$46$im_] := N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] * x$46$im), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]
NOTE: x.re should be positive before calling this function code[x$46$re_, x$46$im_] := If[LessEqual[x$46$re, 7.6e+153], N[(x$46$im * N[(N[(3.0 * N[(x$46$re * x$46$re), $MachinePrecision]), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x$46$im * N[(x$46$re * 2.0), $MachinePrecision]), $MachinePrecision] * x$46$re + N[(N[(x$46$im * N[(x$46$re + x$46$im), $MachinePrecision]), $MachinePrecision] * N[(x$46$re - x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re
\begin{array}{l}
x.re = |x.re|\\
\\
\begin{array}{l}
\mathbf{if}\;x.re \leq 7.6 \cdot 10^{+153}:\\
\;\;\;\;x.im \cdot \left(3 \cdot \left(x.re \cdot x.re\right) - x.im \cdot x.im\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x.im \cdot \left(x.re \cdot 2\right), x.re, \left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)\right)\\
\end{array}
\end{array}
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
| Original | 82.7% |
|---|---|
| Target | 91.6% |
| Herbie | 96.7% |
if x.re < 7.59999999999999933e153Initial program 87.7%
Taylor expanded in x.re around 0 87.6%
Simplified87.6%
[Start]87.6% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + 2 \cdot \left({x.re}^{2} \cdot x.im\right)
\] |
|---|---|
associate-*r* [=>]87.6% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \color{blue}{\left(2 \cdot {x.re}^{2}\right) \cdot x.im}
\] |
*-commutative [=>]87.6% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \color{blue}{x.im \cdot \left(2 \cdot {x.re}^{2}\right)}
\] |
unpow2 [=>]87.6% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \left(2 \cdot \color{blue}{\left(x.re \cdot x.re\right)}\right)
\] |
associate-*r* [=>]87.6% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \color{blue}{\left(\left(2 \cdot x.re\right) \cdot x.re\right)}
\] |
Applied egg-rr46.1%
[Start]87.6% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right)
\] |
|---|---|
expm1-log1p-u [=>]61.4% | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right)\right)\right)}
\] |
expm1-udef [=>]42.0% | \[ \color{blue}{e^{\mathsf{log1p}\left(\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right)\right)} - 1}
\] |
+-commutative [=>]42.0% | \[ e^{\mathsf{log1p}\left(\color{blue}{x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right) + \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im}\right)} - 1
\] |
*-commutative [=>]42.0% | \[ e^{\mathsf{log1p}\left(x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right) + \color{blue}{x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right)}\right)} - 1
\] |
distribute-lft-out [=>]46.1% | \[ e^{\mathsf{log1p}\left(\color{blue}{x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)}\right)} - 1
\] |
*-commutative [=>]46.1% | \[ e^{\mathsf{log1p}\left(x.im \cdot \left(\color{blue}{x.re \cdot \left(2 \cdot x.re\right)} + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)\right)} - 1
\] |
*-commutative [=>]46.1% | \[ e^{\mathsf{log1p}\left(x.im \cdot \left(x.re \cdot \color{blue}{\left(x.re \cdot 2\right)} + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)\right)} - 1
\] |
Simplified94.1%
[Start]46.1% | \[ e^{\mathsf{log1p}\left(x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right) + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)\right)} - 1
\] |
|---|---|
expm1-def [=>]65.6% | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right) + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)\right)\right)}
\] |
expm1-log1p [=>]94.0% | \[ \color{blue}{x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right) + \left(x.re \cdot x.re - x.im \cdot x.im\right)\right)}
\] |
unpow2 [<=]94.0% | \[ x.im \cdot \left(x.re \cdot \left(x.re \cdot 2\right) + \left(\color{blue}{{x.re}^{2}} - x.im \cdot x.im\right)\right)
\] |
associate-+r- [=>]94.1% | \[ x.im \cdot \color{blue}{\left(\left(x.re \cdot \left(x.re \cdot 2\right) + {x.re}^{2}\right) - x.im \cdot x.im\right)}
\] |
associate-*r* [=>]94.1% | \[ x.im \cdot \left(\left(\color{blue}{\left(x.re \cdot x.re\right) \cdot 2} + {x.re}^{2}\right) - x.im \cdot x.im\right)
\] |
unpow2 [<=]94.1% | \[ x.im \cdot \left(\left(\color{blue}{{x.re}^{2}} \cdot 2 + {x.re}^{2}\right) - x.im \cdot x.im\right)
\] |
*-commutative [<=]94.1% | \[ x.im \cdot \left(\left(\color{blue}{2 \cdot {x.re}^{2}} + {x.re}^{2}\right) - x.im \cdot x.im\right)
\] |
distribute-lft1-in [=>]94.1% | \[ x.im \cdot \left(\color{blue}{\left(2 + 1\right) \cdot {x.re}^{2}} - x.im \cdot x.im\right)
\] |
metadata-eval [=>]94.1% | \[ x.im \cdot \left(\color{blue}{3} \cdot {x.re}^{2} - x.im \cdot x.im\right)
\] |
unpow2 [=>]94.1% | \[ x.im \cdot \left(3 \cdot \color{blue}{\left(x.re \cdot x.re\right)} - x.im \cdot x.im\right)
\] |
if 7.59999999999999933e153 < x.re Initial program 48.0%
Taylor expanded in x.re around 0 48.0%
Simplified48.0%
[Start]48.0% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + 2 \cdot \left({x.re}^{2} \cdot x.im\right)
\] |
|---|---|
associate-*r* [=>]48.0% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \color{blue}{\left(2 \cdot {x.re}^{2}\right) \cdot x.im}
\] |
*-commutative [=>]48.0% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \color{blue}{x.im \cdot \left(2 \cdot {x.re}^{2}\right)}
\] |
unpow2 [=>]48.0% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \left(2 \cdot \color{blue}{\left(x.re \cdot x.re\right)}\right)
\] |
associate-*r* [=>]48.0% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \color{blue}{\left(\left(2 \cdot x.re\right) \cdot x.re\right)}
\] |
Applied egg-rr89.1%
[Start]48.0% | \[ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right)
\] |
|---|---|
+-commutative [=>]48.0% | \[ \color{blue}{x.im \cdot \left(\left(2 \cdot x.re\right) \cdot x.re\right) + \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im}
\] |
associate-*r* [=>]48.0% | \[ \color{blue}{\left(x.im \cdot \left(2 \cdot x.re\right)\right) \cdot x.re} + \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im
\] |
fma-def [=>]48.0% | \[ \color{blue}{\mathsf{fma}\left(x.im \cdot \left(2 \cdot x.re\right), x.re, \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im\right)}
\] |
*-commutative [=>]48.0% | \[ \mathsf{fma}\left(x.im \cdot \color{blue}{\left(x.re \cdot 2\right)}, x.re, \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im\right)
\] |
*-commutative [=>]48.0% | \[ \mathsf{fma}\left(x.im \cdot \left(x.re \cdot 2\right), x.re, \color{blue}{x.im \cdot \left(x.re \cdot x.re - x.im \cdot x.im\right)}\right)
\] |
difference-of-squares [=>]64.2% | \[ \mathsf{fma}\left(x.im \cdot \left(x.re \cdot 2\right), x.re, x.im \cdot \color{blue}{\left(\left(x.re + x.im\right) \cdot \left(x.re - x.im\right)\right)}\right)
\] |
associate-*r* [=>]89.1% | \[ \mathsf{fma}\left(x.im \cdot \left(x.re \cdot 2\right), x.re, \color{blue}{\left(x.im \cdot \left(x.re + x.im\right)\right) \cdot \left(x.re - x.im\right)}\right)
\] |
Final simplification93.3%
| Alternative 1 | |
|---|---|
| Accuracy | 96.7% |
| Cost | 7492 |
| Alternative 2 | |
|---|---|
| Accuracy | 96.7% |
| Cost | 836 |
| Alternative 3 | |
|---|---|
| Accuracy | 77.4% |
| Cost | 580 |
| Alternative 4 | |
|---|---|
| Accuracy | 83.0% |
| Cost | 580 |
| Alternative 5 | |
|---|---|
| Accuracy | 83.0% |
| Cost | 580 |
| Alternative 6 | |
|---|---|
| Accuracy | 59.9% |
| Cost | 384 |
| Alternative 7 | |
|---|---|
| Accuracy | 2.7% |
| Cost | 64 |
herbie shell --seed 2023171
(FPCore (x.re x.im)
:name "math.cube on complex, imaginary part"
:precision binary64
:herbie-target
(+ (* (* x.re x.im) (* 2.0 x.re)) (* (* x.im (- x.re x.im)) (+ x.re x.im)))
(+ (* (- (* x.re x.re) (* x.im x.im)) x.im) (* (+ (* x.re x.im) (* x.im x.re)) x.re)))