| Alternative 1 | |
|---|---|
| Accuracy | 91.0% |
| Cost | 26884 |

(FPCore (p x) :precision binary64 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
(FPCore (p x) :precision binary64 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0) (/ (- (sqrt (* p p))) x) (sqrt (fma (/ 0.5 (hypot x (* p 2.0))) x 0.5))))
double code(double p, double x) {
return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
double code(double p, double x) {
double tmp;
if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
tmp = -sqrt((p * p)) / x;
} else {
tmp = sqrt(fma((0.5 / hypot(x, (p * 2.0))), x, 0.5));
}
return tmp;
}
function code(p, x) return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x))))))) end
function code(p, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -1.0) tmp = Float64(Float64(-sqrt(Float64(p * p))) / x); else tmp = sqrt(fma(Float64(0.5 / hypot(x, Float64(p * 2.0))), x, 0.5)); end return tmp end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[p_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-N[Sqrt[N[(p * p), $MachinePrecision]], $MachinePrecision]) / x), $MachinePrecision], N[Sqrt[N[(N[(0.5 / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * x + 0.5), $MachinePrecision]], $MachinePrecision]]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}\\
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
| Original | 79.5% |
|---|---|
| Target | 79.5% |
| Herbie | 91.0% |
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 15.8%
Taylor expanded in x around -inf 60.4%
Applied egg-rr64.9%
[Start]60.4% | \[ -1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}
\] |
|---|---|
associate-*r* [=>]60.1% | \[ -1 \cdot \frac{\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot p}}{x}
\] |
sqrt-unprod [=>]61.1% | \[ -1 \cdot \frac{\color{blue}{\sqrt{2 \cdot 0.5}} \cdot p}{x}
\] |
metadata-eval [=>]61.1% | \[ -1 \cdot \frac{\sqrt{\color{blue}{1}} \cdot p}{x}
\] |
metadata-eval [=>]61.1% | \[ -1 \cdot \frac{\color{blue}{1} \cdot p}{x}
\] |
*-un-lft-identity [<=]61.1% | \[ -1 \cdot \frac{\color{blue}{p}}{x}
\] |
add-sqr-sqrt [=>]54.3% | \[ -1 \cdot \frac{\color{blue}{\sqrt{p} \cdot \sqrt{p}}}{x}
\] |
sqrt-unprod [=>]64.9% | \[ -1 \cdot \frac{\color{blue}{\sqrt{p \cdot p}}}{x}
\] |
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 100.0%
Applied egg-rr100.0%
[Start]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\] |
|---|---|
clear-num [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)}
\] |
associate-/r/ [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot x}\right)}
\] |
+-commutative [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot x\right)}
\] |
add-sqr-sqrt [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\sqrt{x \cdot x + \color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}}}} \cdot x\right)}
\] |
hypot-def [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\color{blue}{\mathsf{hypot}\left(x, \sqrt{\left(4 \cdot p\right) \cdot p}\right)}} \cdot x\right)}
\] |
associate-*l* [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, \sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}\right)} \cdot x\right)}
\] |
sqrt-prod [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, \color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}\right)} \cdot x\right)}
\] |
metadata-eval [=>]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, \color{blue}{2} \cdot \sqrt{p \cdot p}\right)} \cdot x\right)}
\] |
sqrt-unprod [<=]50.5% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}\right)} \cdot x\right)}
\] |
add-sqr-sqrt [<=]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{p}\right)} \cdot x\right)}
\] |
Applied egg-rr98.9%
[Start]100.0% | \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}
\] |
|---|---|
expm1-log1p-u [=>]99.0% | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}\right)\right)}
\] |
expm1-udef [=>]98.9% | \[ \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}\right)} - 1}
\] |
distribute-lft-in [=>]98.9% | \[ e^{\mathsf{log1p}\left(\sqrt{\color{blue}{0.5 \cdot 1 + 0.5 \cdot \left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}}\right)} - 1
\] |
metadata-eval [=>]98.9% | \[ e^{\mathsf{log1p}\left(\sqrt{\color{blue}{0.5} + 0.5 \cdot \left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}\right)} - 1
\] |
associate-*l/ [=>]98.9% | \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \color{blue}{\frac{1 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}\right)} - 1
\] |
*-un-lft-identity [<=]98.9% | \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{\color{blue}{x}}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1
\] |
associate-*r/ [=>]98.9% | \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}\right)} - 1
\] |
Simplified100.0%
[Start]98.9% | \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1
\] |
|---|---|
expm1-def [=>]99.0% | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)\right)}
\] |
expm1-log1p [=>]100.0% | \[ \color{blue}{\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}
\] |
associate-/l* [=>]100.0% | \[ \sqrt{0.5 + \color{blue}{\frac{0.5}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}}}
\] |
*-commutative [=>]100.0% | \[ \sqrt{0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \color{blue}{p \cdot 2}\right)}{x}}}
\] |
Applied egg-rr100.0%
[Start]100.0% | \[ \sqrt{0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}}
\] |
|---|---|
+-commutative [=>]100.0% | \[ \sqrt{\color{blue}{\frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}} + 0.5}}
\] |
associate-/r/ [=>]100.0% | \[ \sqrt{\color{blue}{\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)} \cdot x} + 0.5}
\] |
fma-def [=>]100.0% | \[ \sqrt{\color{blue}{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}}
\] |
Final simplification91.2%
| Alternative 1 | |
|---|---|
| Accuracy | 91.0% |
| Cost | 26884 |
| Alternative 2 | |
|---|---|
| Accuracy | 91.0% |
| Cost | 20612 |
| Alternative 3 | |
|---|---|
| Accuracy | 80.3% |
| Cost | 13836 |
| Alternative 4 | |
|---|---|
| Accuracy | 68.3% |
| Cost | 7388 |
| Alternative 5 | |
|---|---|
| Accuracy | 64.3% |
| Cost | 7180 |
| Alternative 6 | |
|---|---|
| Accuracy | 44.8% |
| Cost | 456 |
| Alternative 7 | |
|---|---|
| Accuracy | 44.5% |
| Cost | 324 |
| Alternative 8 | |
|---|---|
| Accuracy | 36.2% |
| Cost | 64 |
herbie shell --seed 2023263
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:herbie-target
(sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x)))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))