Given's Rotation SVD example

Percentage Accurate: 79.2% → 91.0%
Time: 7.9s
Alternatives: 6
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 6 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.2% 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: 91.0% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.98:\\
\;\;\;\;\sqrt{0.5 \cdot \mathsf{fma}\left(2, {\left(\frac{p}{x}\right)}^{2}, {\left(\frac{p}{x}\right)}^{4} \cdot -6\right)}\\

\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)))) < -0.97999999999999998

    1. Initial program 22.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. +-commutative22.0%

        \[\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-num22.0%

        \[\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/18.4%

        \[\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-def3.0%

        \[\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. +-commutative3.0%

        \[\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-sqrt3.0%

        \[\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-def3.0%

        \[\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*3.0%

        \[\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-prod3.0%

        \[\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-eval3.0%

        \[\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-unprod2.0%

        \[\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-sqrt3.0%

        \[\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-rr3.0%

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}} + \left(-4 \cdot \frac{{p}^{4}}{{x}^{4}} + -2 \cdot \frac{{p}^{4}}{{x}^{4}}\right)\right)}} \]
    5. Step-by-step derivation
      1. fma-def49.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(2, {\left(\frac{p}{x}\right)}^{2}, \left({\left(\frac{p}{x}\right)}^{2} \cdot \color{blue}{{\left(\frac{p}{x}\right)}^{2}}\right) \cdot \left(-4 + -2\right)\right)} \]
      20. pow-sqr68.7%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(2, {\left(\frac{p}{x}\right)}^{2}, \color{blue}{{\left(\frac{p}{x}\right)}^{\left(2 \cdot 2\right)}} \cdot \left(-4 + -2\right)\right)} \]
      21. metadata-eval68.7%

        \[\leadsto \sqrt{0.5 \cdot \mathsf{fma}\left(2, {\left(\frac{p}{x}\right)}^{2}, {\left(\frac{p}{x}\right)}^{\color{blue}{4}} \cdot \left(-4 + -2\right)\right)} \]
      22. metadata-eval68.7%

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

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

    if -0.97999999999999998 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x 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-sqr-sqrt100.0%

        \[\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-def100.0%

        \[\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*100.0%

        \[\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-prod100.0%

        \[\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-eval100.0%

        \[\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-unprod51.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-sqrt100.0%

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

      \[\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 simplification91.0%

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

Alternative 2: 90.9% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.98:\\
\;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}\\

\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)))) < -0.97999999999999998

    1. Initial program 22.0%

      \[\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 -inf 54.8%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Step-by-step derivation
      1. unpow254.8%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
      2. unpow254.8%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
      3. times-frac68.6%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified68.6%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]

    if -0.97999999999999998 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x 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-sqr-sqrt100.0%

        \[\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-def100.0%

        \[\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*100.0%

        \[\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-prod100.0%

        \[\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-eval100.0%

        \[\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-unprod51.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-sqrt100.0%

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

      \[\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 simplification91.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.98:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}\\ \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} \\ \begin{array}{l} \mathbf{if}\;p \leq -4.4 \cdot 10^{-11}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.1 \cdot 10^{-306}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9 \cdot 10^{-105}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 1.05 \cdot 10^{-19}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= p -4.4e-11)
   (sqrt 0.5)
   (if (<= p -1.1e-306)
     1.0
     (if (<= p 9e-105) (/ (- p) x) (if (<= p 1.05e-19) 1.0 (sqrt 0.5))))))
double code(double p, double x) {
	double tmp;
	if (p <= -4.4e-11) {
		tmp = sqrt(0.5);
	} else if (p <= -1.1e-306) {
		tmp = 1.0;
	} else if (p <= 9e-105) {
		tmp = -p / x;
	} else if (p <= 1.05e-19) {
		tmp = 1.0;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p <= (-4.4d-11)) then
        tmp = sqrt(0.5d0)
    else if (p <= (-1.1d-306)) then
        tmp = 1.0d0
    else if (p <= 9d-105) then
        tmp = -p / x
    else if (p <= 1.05d-19) then
        tmp = 1.0d0
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (p <= -4.4e-11) {
		tmp = Math.sqrt(0.5);
	} else if (p <= -1.1e-306) {
		tmp = 1.0;
	} else if (p <= 9e-105) {
		tmp = -p / x;
	} else if (p <= 1.05e-19) {
		tmp = 1.0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -4.4e-11:
		tmp = math.sqrt(0.5)
	elif p <= -1.1e-306:
		tmp = 1.0
	elif p <= 9e-105:
		tmp = -p / x
	elif p <= 1.05e-19:
		tmp = 1.0
	else:
		tmp = math.sqrt(0.5)
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -4.4e-11)
		tmp = sqrt(0.5);
	elseif (p <= -1.1e-306)
		tmp = 1.0;
	elseif (p <= 9e-105)
		tmp = Float64(Float64(-p) / x);
	elseif (p <= 1.05e-19)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -4.4e-11)
		tmp = sqrt(0.5);
	elseif (p <= -1.1e-306)
		tmp = 1.0;
	elseif (p <= 9e-105)
		tmp = -p / x;
	elseif (p <= 1.05e-19)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -4.4e-11], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -1.1e-306], 1.0, If[LessEqual[p, 9e-105], N[((-p) / x), $MachinePrecision], If[LessEqual[p, 1.05e-19], 1.0, N[Sqrt[0.5], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;p \leq -4.4 \cdot 10^{-11}:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq -1.1 \cdot 10^{-306}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 9 \cdot 10^{-105}:\\
\;\;\;\;\frac{-p}{x}\\

\mathbf{elif}\;p \leq 1.05 \cdot 10^{-19}:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < -4.4000000000000003e-11 or 1.0499999999999999e-19 < p

    1. Initial program 93.8%

      \[\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 87.6%

      \[\leadsto \color{blue}{\sqrt{0.5}} \]

    if -4.4000000000000003e-11 < p < -1.10000000000000008e-306 or 8.9999999999999995e-105 < p < 1.0499999999999999e-19

    1. Initial program 68.6%

      \[\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 inf 54.5%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{2}} \]

    if -1.10000000000000008e-306 < p < 8.9999999999999995e-105

    1. Initial program 57.0%

      \[\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 -inf 28.1%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Step-by-step derivation
      1. unpow228.1%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
      2. unpow228.1%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
      3. times-frac39.2%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified39.2%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
    5. Taylor expanded in p around -inf 62.6%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    6. Step-by-step derivation
      1. neg-mul-162.6%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-frac-neg62.6%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    7. Simplified62.6%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification72.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -4.4 \cdot 10^{-11}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.1 \cdot 10^{-306}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 9 \cdot 10^{-105}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 1.05 \cdot 10^{-19}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 4: 67.4% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -4.6 \cdot 10^{-71}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.15 \cdot 10^{-290}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 9 \cdot 10^{-101}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= p -4.6e-71)
   (sqrt 0.5)
   (if (<= p -1.15e-290) (/ p x) (if (<= p 9e-101) (/ (- p) x) (sqrt 0.5)))))
double code(double p, double x) {
	double tmp;
	if (p <= -4.6e-71) {
		tmp = sqrt(0.5);
	} else if (p <= -1.15e-290) {
		tmp = p / x;
	} else if (p <= 9e-101) {
		tmp = -p / x;
	} else {
		tmp = sqrt(0.5);
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p <= (-4.6d-71)) then
        tmp = sqrt(0.5d0)
    else if (p <= (-1.15d-290)) then
        tmp = p / x
    else if (p <= 9d-101) then
        tmp = -p / x
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (p <= -4.6e-71) {
		tmp = Math.sqrt(0.5);
	} else if (p <= -1.15e-290) {
		tmp = p / x;
	} else if (p <= 9e-101) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -4.6e-71:
		tmp = math.sqrt(0.5)
	elif p <= -1.15e-290:
		tmp = p / x
	elif p <= 9e-101:
		tmp = -p / x
	else:
		tmp = math.sqrt(0.5)
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -4.6e-71)
		tmp = sqrt(0.5);
	elseif (p <= -1.15e-290)
		tmp = Float64(p / x);
	elseif (p <= 9e-101)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -4.6e-71)
		tmp = sqrt(0.5);
	elseif (p <= -1.15e-290)
		tmp = p / x;
	elseif (p <= 9e-101)
		tmp = -p / x;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -4.6e-71], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -1.15e-290], N[(p / x), $MachinePrecision], If[LessEqual[p, 9e-101], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;p \leq -4.6 \cdot 10^{-71}:\\
\;\;\;\;\sqrt{0.5}\\

\mathbf{elif}\;p \leq -1.15 \cdot 10^{-290}:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{elif}\;p \leq 9 \cdot 10^{-101}:\\
\;\;\;\;\frac{-p}{x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < -4.5999999999999997e-71 or 8.9999999999999997e-101 < p

    1. Initial program 92.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 78.8%

      \[\leadsto \color{blue}{\sqrt{0.5}} \]

    if -4.5999999999999997e-71 < p < -1.15e-290

    1. Initial program 60.7%

      \[\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 -inf 27.3%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Step-by-step derivation
      1. unpow227.3%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
      2. unpow227.3%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
      3. times-frac34.5%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified34.5%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
    5. Taylor expanded in p around 0 49.4%

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

    if -1.15e-290 < p < 8.9999999999999997e-101

    1. Initial program 57.5%

      \[\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 -inf 26.9%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Step-by-step derivation
      1. unpow226.9%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
      2. unpow226.9%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
      3. times-frac37.6%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified37.6%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
    5. Taylor expanded in p around -inf 61.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    6. Step-by-step derivation
      1. neg-mul-161.4%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-frac-neg61.4%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    7. Simplified61.4%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification69.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -4.6 \cdot 10^{-71}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -1.15 \cdot 10^{-290}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 9 \cdot 10^{-101}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 5: 26.5% accurate, 35.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -5.7 \cdot 10^{-291}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \end{array} \]
(FPCore (p x) :precision binary64 (if (<= p -5.7e-291) (/ p x) (/ (- p) x)))
double code(double p, double x) {
	double tmp;
	if (p <= -5.7e-291) {
		tmp = p / x;
	} else {
		tmp = -p / x;
	}
	return tmp;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    real(8) :: tmp
    if (p <= (-5.7d-291)) then
        tmp = p / x
    else
        tmp = -p / x
    end if
    code = tmp
end function
public static double code(double p, double x) {
	double tmp;
	if (p <= -5.7e-291) {
		tmp = p / x;
	} else {
		tmp = -p / x;
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -5.7e-291:
		tmp = p / x
	else:
		tmp = -p / x
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -5.7e-291)
		tmp = Float64(p / x);
	else
		tmp = Float64(Float64(-p) / x);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -5.7e-291)
		tmp = p / x;
	else
		tmp = -p / x;
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -5.7e-291], N[(p / x), $MachinePrecision], N[((-p) / x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;p \leq -5.7 \cdot 10^{-291}:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{else}:\\
\;\;\;\;\frac{-p}{x}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if p < -5.70000000000000034e-291

    1. Initial program 79.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 -inf 19.3%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Step-by-step derivation
      1. unpow219.3%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
      2. unpow219.3%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
      3. times-frac22.3%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified22.3%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
    5. Taylor expanded in p around 0 26.9%

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

    if -5.70000000000000034e-291 < p

    1. Initial program 76.5%

      \[\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 -inf 19.0%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
    3. Step-by-step derivation
      1. unpow219.0%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
      2. unpow219.0%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
      3. times-frac24.1%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified24.1%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
    5. Taylor expanded in p around -inf 33.9%

      \[\leadsto \color{blue}{-1 \cdot \frac{p}{x}} \]
    6. Step-by-step derivation
      1. neg-mul-133.9%

        \[\leadsto \color{blue}{-\frac{p}{x}} \]
      2. distribute-frac-neg33.9%

        \[\leadsto \color{blue}{\frac{-p}{x}} \]
    7. Simplified33.9%

      \[\leadsto \color{blue}{\frac{-p}{x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification30.8%

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

Alternative 6: 16.8% accurate, 71.7× speedup?

\[\begin{array}{l} \\ \frac{p}{x} \end{array} \]
(FPCore (p x) :precision binary64 (/ p x))
double code(double p, double x) {
	return p / x;
}
real(8) function code(p, x)
    real(8), intent (in) :: p
    real(8), intent (in) :: x
    code = p / x
end function
public static double code(double p, double x) {
	return p / x;
}
def code(p, x):
	return p / x
function code(p, x)
	return Float64(p / x)
end
function tmp = code(p, x)
	tmp = p / x;
end
code[p_, x_] := N[(p / x), $MachinePrecision]
\begin{array}{l}

\\
\frac{p}{x}
\end{array}
Derivation
  1. Initial program 77.7%

    \[\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 -inf 19.2%

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \frac{{p}^{2}}{{x}^{2}}\right)}} \]
  3. Step-by-step derivation
    1. unpow219.2%

      \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{p \cdot p}}{{x}^{2}}\right)} \]
    2. unpow219.2%

      \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{p \cdot p}{\color{blue}{x \cdot x}}\right)} \]
    3. times-frac23.3%

      \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
  4. Simplified23.3%

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}} \]
  5. Taylor expanded in p around 0 17.8%

    \[\leadsto \color{blue}{\frac{p}{x}} \]
  6. Final simplification17.8%

    \[\leadsto \frac{p}{x} \]

Developer target: 79.2% 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 2023182 
(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))))))))