Given's Rotation SVD example

Percentage Accurate: 79.8% → 99.7%
Time: 10.3s
Alternatives: 5
Speedup: 0.7×

Specification

?
\[10^{-150} < \left|x\right| \land \left|x\right| < 10^{+150}\]
\[\begin{array}{l} \\ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
	return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
	return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x):
	return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
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 tmp = code(p, x)
	tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
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]
\begin{array}{l}

\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 79.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
double code(double p, double x) {
	return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = sqrt((0.5d0 * (1.0d0 + (x / sqrt((((4.0d0 * p) * p) + (x * x)))))))
end function
public static double code(double p, double x) {
	return Math.sqrt((0.5 * (1.0 + (x / Math.sqrt((((4.0 * p) * p) + (x * x)))))));
}
def code(p, x):
	return math.sqrt((0.5 * (1.0 + (x / math.sqrt((((4.0 * p) * p) + (x * x)))))))
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 tmp = code(p, x)
	tmp = sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
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]
\begin{array}{l}

\\
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\end{array}

Alternative 1: 99.7% accurate, 0.5× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 1\right)}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
   (/ (- p) x)
   (sqrt (* 0.5 (fma (/ 1.0 (hypot x (* p 2.0))) x 1.0)))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = sqrt((0.5 * fma((1.0 / hypot(x, (p * 2.0))), x, 1.0)));
	}
	return tmp;
}
p = abs(p)
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(-p) / x);
	else
		tmp = sqrt(Float64(0.5 * fma(Float64(1.0 / hypot(x, Float64(p * 2.0))), x, 1.0)));
	end
	return tmp
end
NOTE: p should be positive before calling this function
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[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[(1.0 / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * x + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 1\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1

    1. Initial program 10.9%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-cbrt-cube10.9%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
      2. pow1/310.9%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
    3. Applied egg-rr10.9%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    4. Taylor expanded in x around -inf 26.0%

      \[\leadsto {\left({\color{blue}{\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \left(\frac{{p}^{2}}{{x}^{2}} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    5. Step-by-step derivation
      1. associate-+r+26.0%

        \[\leadsto {\left({\color{blue}{\left(\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
      2. fma-def26.0%

        \[\leadsto {\left({\left(\color{blue}{\mathsf{fma}\left(-0.5, \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right)} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      3. distribute-rgt-out26.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{\color{blue}{{p}^{4} \cdot \left(4 + 2\right)}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      4. metadata-eval26.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot \color{blue}{6}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      5. unpow226.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{{p}^{2}}{\color{blue}{x \cdot x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      6. associate-/r*26.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \color{blue}{\frac{\frac{{p}^{2}}{x}}{x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      7. unpow226.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{\color{blue}{p \cdot p}}{x}}{x}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
    6. Simplified26.0%

      \[\leadsto {\left({\color{blue}{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    7. Step-by-step derivation
      1. *-un-lft-identity26.0%

        \[\leadsto \color{blue}{1 \cdot {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333}} \]
      2. pow-pow38.5%

        \[\leadsto 1 \cdot \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{\left(1.5 \cdot 0.3333333333333333\right)}} \]
    8. Applied egg-rr38.4%

      \[\leadsto \color{blue}{1 \cdot {\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
    9. Step-by-step derivation
      1. *-lft-identity38.4%

        \[\leadsto \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
      2. unpow1/238.4%

        \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}}} \]
      3. associate-/r/38.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{\frac{{p}^{4}}{{x}^{4}} \cdot 6}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      4. *-commutative38.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{6 \cdot \frac{{p}^{4}}{{x}^{4}}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      5. times-frac40.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      6. associate-*r*40.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \color{blue}{\left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)}\right)}{{x}^{6}}} \]
    10. Simplified40.4%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}}} \]
    11. Taylor expanded in x around -inf 45.5%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    12. Step-by-step derivation
      1. associate-*r/45.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg45.5%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    13. Simplified45.5%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]

    if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x))))

    1. Initial program 99.6%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. +-commutative99.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}} \]
      2. clear-num99.5%

        \[\leadsto \sqrt{0.5 \cdot \left(\color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}} + 1\right)} \]
      3. associate-/r/99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(\color{blue}{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot x} + 1\right)} \]
      4. fma-def99.6%

        \[\leadsto \sqrt{0.5 \cdot \color{blue}{\mathsf{fma}\left(\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}, x, 1\right)}} \]
      5. +-commutative99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}}, x, 1\right)} \]
      6. add-sqr-sqrt99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\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}}}}, x, 1\right)} \]
      7. hypot-def99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\color{blue}{\mathsf{hypot}\left(x, \sqrt{\left(4 \cdot p\right) \cdot p}\right)}}, x, 1\right)} \]
      8. associate-*l*99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, \sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}\right)}, x, 1\right)} \]
      9. sqrt-prod99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, \color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}\right)}, x, 1\right)} \]
      10. metadata-eval99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, \color{blue}{2} \cdot \sqrt{p \cdot p}\right)}, x, 1\right)} \]
      11. sqrt-unprod44.9%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}\right)}, x, 1\right)} \]
      12. add-sqr-sqrt99.6%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{p}\right)}, x, 1\right)} \]
    3. Applied egg-rr99.6%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, x, 1\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification86.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left(\frac{1}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 1\right)}\\ \end{array} \]

Alternative 2: 99.7% accurate, 0.7× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
   (/ (- p) x)
   (sqrt (* 0.5 (+ 1.0 (/ x (hypot (* p 2.0) x)))))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
	}
	return tmp;
}
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if ((x / Math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt((0.5 * (1.0 + (x / Math.hypot((p * 2.0), x)))));
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if (x / math.sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0:
		tmp = -p / x
	else:
		tmp = math.sqrt((0.5 * (1.0 + (x / math.hypot((p * 2.0), x)))))
	return tmp
p = abs(p)
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(-p) / x);
	else
		tmp = sqrt(Float64(0.5 * Float64(1.0 + Float64(x / hypot(Float64(p * 2.0), x)))));
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0)
		tmp = -p / x;
	else
		tmp = sqrt((0.5 * (1.0 + (x / hypot((p * 2.0), x)))));
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
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[((-p) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(p * 2.0), $MachinePrecision] ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1

    1. Initial program 10.9%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-cbrt-cube10.9%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
      2. pow1/310.9%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
    3. Applied egg-rr10.9%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    4. Taylor expanded in x around -inf 26.0%

      \[\leadsto {\left({\color{blue}{\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \left(\frac{{p}^{2}}{{x}^{2}} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    5. Step-by-step derivation
      1. associate-+r+26.0%

        \[\leadsto {\left({\color{blue}{\left(\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
      2. fma-def26.0%

        \[\leadsto {\left({\left(\color{blue}{\mathsf{fma}\left(-0.5, \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right)} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      3. distribute-rgt-out26.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{\color{blue}{{p}^{4} \cdot \left(4 + 2\right)}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      4. metadata-eval26.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot \color{blue}{6}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      5. unpow226.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{{p}^{2}}{\color{blue}{x \cdot x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      6. associate-/r*26.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \color{blue}{\frac{\frac{{p}^{2}}{x}}{x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      7. unpow226.0%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{\color{blue}{p \cdot p}}{x}}{x}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
    6. Simplified26.0%

      \[\leadsto {\left({\color{blue}{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    7. Step-by-step derivation
      1. *-un-lft-identity26.0%

        \[\leadsto \color{blue}{1 \cdot {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333}} \]
      2. pow-pow38.5%

        \[\leadsto 1 \cdot \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{\left(1.5 \cdot 0.3333333333333333\right)}} \]
    8. Applied egg-rr38.4%

      \[\leadsto \color{blue}{1 \cdot {\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
    9. Step-by-step derivation
      1. *-lft-identity38.4%

        \[\leadsto \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
      2. unpow1/238.4%

        \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}}} \]
      3. associate-/r/38.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{\frac{{p}^{4}}{{x}^{4}} \cdot 6}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      4. *-commutative38.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{6 \cdot \frac{{p}^{4}}{{x}^{4}}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      5. times-frac40.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      6. associate-*r*40.4%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \color{blue}{\left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)}\right)}{{x}^{6}}} \]
    10. Simplified40.4%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}}} \]
    11. Taylor expanded in x around -inf 45.5%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    12. Step-by-step derivation
      1. associate-*r/45.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg45.5%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    13. Simplified45.5%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]

    if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x))))

    1. Initial program 99.6%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-sqr-sqrt99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}} + x \cdot x}}\right)} \]
      2. hypot-def99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(\sqrt{\left(4 \cdot p\right) \cdot p}, x\right)}}\right)} \]
      3. associate-*l*99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}, x\right)}\right)} \]
      4. sqrt-prod99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}, x\right)}\right)} \]
      5. metadata-eval99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(\color{blue}{2} \cdot \sqrt{p \cdot p}, x\right)}\right)} \]
      6. sqrt-unprod44.9%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}, x\right)}\right)} \]
      7. add-sqr-sqrt99.6%

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot \color{blue}{p}, x\right)}\right)} \]
    3. Applied egg-rr99.6%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \frac{x}{\color{blue}{\mathsf{hypot}\left(2 \cdot p, x\right)}}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification86.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\ \end{array} \]

Alternative 3: 68.5% accurate, 2.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{-p}{x}\\ \mathbf{if}\;p \leq 5 \cdot 10^{-213}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.55 \cdot 10^{-190}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;p \leq 8.2 \cdot 10^{-144}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.2 \cdot 10^{-57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (/ (- p) x)))
   (if (<= p 5e-213)
     1.0
     (if (<= p 1.55e-190)
       t_0
       (if (<= p 8.2e-144) 1.0 (if (<= p 2.2e-57) t_0 (sqrt 0.5)))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (p <= 5e-213) {
		tmp = 1.0;
	} else if (p <= 1.55e-190) {
		tmp = t_0;
	} else if (p <= 8.2e-144) {
		tmp = 1.0;
	} else if (p <= 2.2e-57) {
		tmp = t_0;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = -p / x
    if (p <= 5d-213) then
        tmp = 1.0d0
    else if (p <= 1.55d-190) then
        tmp = t_0
    else if (p <= 8.2d-144) then
        tmp = 1.0d0
    else if (p <= 2.2d-57) then
        tmp = t_0
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double t_0 = -p / x;
	double tmp;
	if (p <= 5e-213) {
		tmp = 1.0;
	} else if (p <= 1.55e-190) {
		tmp = t_0;
	} else if (p <= 8.2e-144) {
		tmp = 1.0;
	} else if (p <= 2.2e-57) {
		tmp = t_0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	t_0 = -p / x
	tmp = 0
	if p <= 5e-213:
		tmp = 1.0
	elif p <= 1.55e-190:
		tmp = t_0
	elif p <= 8.2e-144:
		tmp = 1.0
	elif p <= 2.2e-57:
		tmp = t_0
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	t_0 = Float64(Float64(-p) / x)
	tmp = 0.0
	if (p <= 5e-213)
		tmp = 1.0;
	elseif (p <= 1.55e-190)
		tmp = t_0;
	elseif (p <= 8.2e-144)
		tmp = 1.0;
	elseif (p <= 2.2e-57)
		tmp = t_0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	t_0 = -p / x;
	tmp = 0.0;
	if (p <= 5e-213)
		tmp = 1.0;
	elseif (p <= 1.55e-190)
		tmp = t_0;
	elseif (p <= 8.2e-144)
		tmp = 1.0;
	elseif (p <= 2.2e-57)
		tmp = t_0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[((-p) / x), $MachinePrecision]}, If[LessEqual[p, 5e-213], 1.0, If[LessEqual[p, 1.55e-190], t$95$0, If[LessEqual[p, 8.2e-144], 1.0, If[LessEqual[p, 2.2e-57], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{-p}{x}\\
\mathbf{if}\;p \leq 5 \cdot 10^{-213}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 1.55 \cdot 10^{-190}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;p \leq 8.2 \cdot 10^{-144}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 2.2 \cdot 10^{-57}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < 4.99999999999999977e-213 or 1.54999999999999997e-190 < p < 8.2e-144

    1. Initial program 77.3%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-cbrt-cube77.2%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
      2. pow1/377.3%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
    3. Applied egg-rr77.3%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    4. Taylor expanded in x around inf 37.5%

      \[\leadsto \color{blue}{1} \]

    if 4.99999999999999977e-213 < p < 1.54999999999999997e-190 or 8.2e-144 < p < 2.19999999999999999e-57

    1. Initial program 53.2%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-cbrt-cube53.1%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
      2. pow1/353.2%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
    3. Applied egg-rr53.2%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    4. Taylor expanded in x around -inf 18.6%

      \[\leadsto {\left({\color{blue}{\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \left(\frac{{p}^{2}}{{x}^{2}} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    5. Step-by-step derivation
      1. associate-+r+18.6%

        \[\leadsto {\left({\color{blue}{\left(\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
      2. fma-def18.6%

        \[\leadsto {\left({\left(\color{blue}{\mathsf{fma}\left(-0.5, \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right)} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      3. distribute-rgt-out18.6%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{\color{blue}{{p}^{4} \cdot \left(4 + 2\right)}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      4. metadata-eval18.6%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot \color{blue}{6}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      5. unpow218.6%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{{p}^{2}}{\color{blue}{x \cdot x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      6. associate-/r*18.6%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \color{blue}{\frac{\frac{{p}^{2}}{x}}{x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      7. unpow218.6%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{\color{blue}{p \cdot p}}{x}}{x}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
    6. Simplified18.6%

      \[\leadsto {\left({\color{blue}{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    7. Step-by-step derivation
      1. *-un-lft-identity18.6%

        \[\leadsto \color{blue}{1 \cdot {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333}} \]
      2. pow-pow19.9%

        \[\leadsto 1 \cdot \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{\left(1.5 \cdot 0.3333333333333333\right)}} \]
    8. Applied egg-rr19.9%

      \[\leadsto \color{blue}{1 \cdot {\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
    9. Step-by-step derivation
      1. *-lft-identity19.9%

        \[\leadsto \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
      2. unpow1/219.9%

        \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}}} \]
      3. associate-/r/19.9%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{\frac{{p}^{4}}{{x}^{4}} \cdot 6}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      4. *-commutative19.9%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{6 \cdot \frac{{p}^{4}}{{x}^{4}}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      5. times-frac19.9%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      6. associate-*r*19.9%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \color{blue}{\left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)}\right)}{{x}^{6}}} \]
    10. Simplified19.9%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}}} \]
    11. Taylor expanded in x around -inf 53.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    12. Step-by-step derivation
      1. associate-*r/53.4%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg53.4%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    13. Simplified53.4%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]

    if 2.19999999999999999e-57 < p

    1. Initial program 90.2%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Taylor expanded in x around 0 84.2%

      \[\leadsto \color{blue}{\sqrt{0.5}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification51.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 5 \cdot 10^{-213}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 1.55 \cdot 10^{-190}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 8.2 \cdot 10^{-144}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.2 \cdot 10^{-57}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 4: 55.5% accurate, 35.5× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq -5.4 \cdot 10^{-212}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 (if (<= x -5.4e-212) (/ (- p) x) 1.0))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (x <= -5.4e-212) {
		tmp = -p / x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= (-5.4d-212)) then
        tmp = -p / x
    else
        tmp = 1.0d0
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if (x <= -5.4e-212) {
		tmp = -p / x;
	} else {
		tmp = 1.0;
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if x <= -5.4e-212:
		tmp = -p / x
	else:
		tmp = 1.0
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (x <= -5.4e-212)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = 1.0;
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (x <= -5.4e-212)
		tmp = -p / x;
	else
		tmp = 1.0;
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[x, -5.4e-212], N[((-p) / x), $MachinePrecision], 1.0]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.4 \cdot 10^{-212}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{else}:\\
\;\;\;\;1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -5.39999999999999962e-212

    1. Initial program 58.2%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-cbrt-cube58.2%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
      2. pow1/358.2%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
    3. Applied egg-rr58.2%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    4. Taylor expanded in x around -inf 12.9%

      \[\leadsto {\left({\color{blue}{\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \left(\frac{{p}^{2}}{{x}^{2}} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    5. Step-by-step derivation
      1. associate-+r+12.9%

        \[\leadsto {\left({\color{blue}{\left(\left(-0.5 \cdot \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}} + \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
      2. fma-def12.9%

        \[\leadsto {\left({\left(\color{blue}{\mathsf{fma}\left(-0.5, \frac{4 \cdot {p}^{4} + 2 \cdot {p}^{4}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right)} + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      3. distribute-rgt-out12.9%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{\color{blue}{{p}^{4} \cdot \left(4 + 2\right)}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      4. metadata-eval12.9%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot \color{blue}{6}}{{x}^{4}}, \frac{{p}^{2}}{{x}^{2}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      5. unpow212.9%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{{p}^{2}}{\color{blue}{x \cdot x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      6. associate-/r*12.9%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \color{blue}{\frac{\frac{{p}^{2}}{x}}{x}}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
      7. unpow212.9%

        \[\leadsto {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{\color{blue}{p \cdot p}}{x}}{x}\right) + -0.5 \cdot \frac{-8 \cdot {p}^{6} + -2 \cdot \left(\left(4 \cdot {p}^{4} + 2 \cdot {p}^{4}\right) \cdot {p}^{2}\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333} \]
    6. Simplified12.9%

      \[\leadsto {\left({\color{blue}{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}}^{1.5}\right)}^{0.3333333333333333} \]
    7. Step-by-step derivation
      1. *-un-lft-identity12.9%

        \[\leadsto \color{blue}{1 \cdot {\left({\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{1.5}\right)}^{0.3333333333333333}} \]
      2. pow-pow18.6%

        \[\leadsto 1 \cdot \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4} \cdot 6}{{x}^{4}}, \frac{\frac{p \cdot p}{x}}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}\right)}^{\left(1.5 \cdot 0.3333333333333333\right)}} \]
    8. Applied egg-rr18.6%

      \[\leadsto \color{blue}{1 \cdot {\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
    9. Step-by-step derivation
      1. *-lft-identity18.6%

        \[\leadsto \color{blue}{{\left(\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}\right)}^{0.5}} \]
      2. unpow1/218.6%

        \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, \frac{{p}^{4}}{\frac{{x}^{4}}{6}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}}} \]
      3. associate-/r/18.6%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{\frac{{p}^{4}}{{x}^{4}} \cdot 6}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      4. *-commutative18.6%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, \color{blue}{6 \cdot \frac{{p}^{4}}{{x}^{4}}}, \frac{p \cdot p}{x \cdot x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      5. times-frac19.5%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \color{blue}{\frac{p}{x} \cdot \frac{p}{x}}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(p \cdot \left(p \cdot \left({p}^{4} \cdot 6\right)\right)\right)\right)}{{x}^{6}}} \]
      6. associate-*r*19.5%

        \[\leadsto \sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \color{blue}{\left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)}\right)}{{x}^{6}}} \]
    10. Simplified19.5%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(-0.5, 6 \cdot \frac{{p}^{4}}{{x}^{4}}, \frac{p}{x} \cdot \frac{p}{x}\right) + -0.5 \cdot \frac{\mathsf{fma}\left(-8, {p}^{6}, -2 \cdot \left(\left(p \cdot p\right) \cdot \left({p}^{4} \cdot 6\right)\right)\right)}{{x}^{6}}}} \]
    11. Taylor expanded in x around -inf 22.5%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    12. Step-by-step derivation
      1. associate-*r/22.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot p}{x}} \]
      2. mul-1-neg22.5%

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    13. Simplified22.5%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]

    if -5.39999999999999962e-212 < x

    1. Initial program 100.0%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Step-by-step derivation
      1. add-cbrt-cube100.0%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
      2. pow1/3100.0%

        \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
    3. Applied egg-rr100.0%

      \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    4. Taylor expanded in x around inf 56.6%

      \[\leadsto \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification39.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5.4 \cdot 10^{-212}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Alternative 5: 35.9% accurate, 215.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ 1 \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 1.0)
p = abs(p);
double code(double p, double x) {
	return 1.0;
}
NOTE: p should be positive before calling this function
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = 1.0d0
end function
p = Math.abs(p);
public static double code(double p, double x) {
	return 1.0;
}
p = abs(p)
def code(p, x):
	return 1.0
p = abs(p)
function code(p, x)
	return 1.0
end
p = abs(p)
function tmp = code(p, x)
	tmp = 1.0;
end
NOTE: p should be positive before calling this function
code[p_, x_] := 1.0
\begin{array}{l}
p = |p|\\
\\
1
\end{array}
Derivation
  1. Initial program 78.8%

    \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
  2. Step-by-step derivation
    1. add-cbrt-cube78.7%

      \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}}} \]
    2. pow1/378.8%

      \[\leadsto \color{blue}{{\left(\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right) \cdot \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)}^{0.3333333333333333}} \]
  3. Applied egg-rr78.8%

    \[\leadsto \color{blue}{{\left({\left(\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}, 0.5, 0.5\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \]
  4. Taylor expanded in x around inf 34.4%

    \[\leadsto \color{blue}{1} \]
  5. Final simplification34.4%

    \[\leadsto 1 \]

Developer target: 79.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}} \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x))))))
double code(double p, double x) {
	return sqrt((0.5 + (copysign(0.5, x) / hypot(1.0, ((2.0 * p) / x)))));
}
public static double code(double p, double x) {
	return Math.sqrt((0.5 + (Math.copySign(0.5, x) / Math.hypot(1.0, ((2.0 * p) / x)))));
}
def code(p, x):
	return math.sqrt((0.5 + (math.copysign(0.5, x) / math.hypot(1.0, ((2.0 * p) / x)))))
function code(p, x)
	return sqrt(Float64(0.5 + Float64(copysign(0.5, x) / hypot(1.0, Float64(Float64(2.0 * p) / x)))))
end
function tmp = code(p, x)
	tmp = sqrt((0.5 + ((sign(x) * abs(0.5)) / hypot(1.0, ((2.0 * p) / x)))));
end
code[p_, x_] := N[Sqrt[N[(0.5 + N[(N[With[{TMP1 = Abs[0.5], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] / N[Sqrt[1.0 ^ 2 + N[(N[(2.0 * p), $MachinePrecision] / x), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}}
\end{array}

Reproduce

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