Given's Rotation SVD example

Percentage Accurate: 79.3% → 91.5%
Time: 9.1s
Alternatives: 7
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 7 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.3% 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.5% accurate, 0.5× speedup?

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

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(1 + \left(1 + \left(\frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)} + -1\right)\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.5

    1. Initial program 13.1%

      \[\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-sqrt13.1%

        \[\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-def13.2%

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

        \[\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-prod13.2%

        \[\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-eval13.2%

        \[\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-unprod8.5%

        \[\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-sqrt13.2%

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

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

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

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

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

        \[\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. distribute-rgt-out39.7%

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

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

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\mathsf{fma}\left(2, \frac{p \cdot p}{x \cdot x}, \frac{{p}^{4}}{{x}^{4}} \cdot -6\right)}} \]
    7. Step-by-step derivation
      1. expm1-log1p-u39.7%

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

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 \cdot \mathsf{fma}\left(2, \frac{p \cdot p}{x \cdot x}, \frac{{p}^{4}}{{x}^{4}} \cdot -6\right)}\right)} - 1} \]
    8. Applied egg-rr16.4%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 \cdot \mathsf{fma}\left(2, {\left(\frac{p}{x}\right)}^{2}, {\left(\frac{p}{x}\right)}^{4} \cdot -6\right)}\right)} - 1} \]
    9. Step-by-step derivation
      1. expm1-def66.4%

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

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{\color{blue}{{p}^{2}}}{x \cdot x} + {\left(\frac{p}{x}\right)}^{4} \cdot -6\right)} \]
      7. unpow253.3%

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \frac{{p}^{2}}{\color{blue}{{x}^{2}}} + {\left(\frac{p}{x}\right)}^{4} \cdot -6\right)} \]
      8. distribute-lft-in53.3%

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

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

    if -0.5 < (/.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-unprod46.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)} \]
    4. Step-by-step derivation
      1. expm1-log1p-u100.0%

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(1 + \left(e^{\color{blue}{\log \left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right)}} - 1\right)\right)} \]
      4. add-exp-log100.0%

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

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\left(\left(1 + \frac{x}{\mathsf{hypot}\left(2 \cdot p, x\right)}\right) - 1\right)}\right)} \]
    6. Step-by-step derivation
      1. associate--l+100.0%

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

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

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

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

Alternative 2: 91.6% accurate, 0.5× speedup?

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

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

    1. Initial program 11.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 -inf 52.7%

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

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

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

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

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

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

    1. Initial program 99.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. +-commutative99.8%

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

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

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

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

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

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

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

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

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

        \[\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-unprod47.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-sqrt99.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.9999996:\\ \;\;\;\;\sqrt{0.5 \cdot \left(2 \cdot \left(\frac{p}{x} \cdot \frac{p}{x}\right)\right)}\\ \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 3: 91.6% 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.9999996:\\ \;\;\;\;\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.9999996)
   (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.9999996) {
		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.9999996) {
		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.9999996:
		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.9999996)
		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.9999996)
		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.9999996], 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.9999996:\\
\;\;\;\;\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.99999959999999999

    1. Initial program 11.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 -inf 52.7%

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

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

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

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

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

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

    1. Initial program 99.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-sqr-sqrt99.8%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -0.9999996:\\ \;\;\;\;\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 4: 66.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -5.8 \cdot 10^{-65}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -8 \cdot 10^{-221}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.5 \cdot 10^{-74}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= p -5.8e-65)
   (sqrt 0.5)
   (if (<= p -8e-221) (/ p x) (if (<= p 1.5e-74) (/ (- p) x) (sqrt 0.5)))))
double code(double p, double x) {
	double tmp;
	if (p <= -5.8e-65) {
		tmp = sqrt(0.5);
	} else if (p <= -8e-221) {
		tmp = p / x;
	} else if (p <= 1.5e-74) {
		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 <= (-5.8d-65)) then
        tmp = sqrt(0.5d0)
    else if (p <= (-8d-221)) then
        tmp = p / x
    else if (p <= 1.5d-74) 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 <= -5.8e-65) {
		tmp = Math.sqrt(0.5);
	} else if (p <= -8e-221) {
		tmp = p / x;
	} else if (p <= 1.5e-74) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -5.8e-65:
		tmp = math.sqrt(0.5)
	elif p <= -8e-221:
		tmp = p / x
	elif p <= 1.5e-74:
		tmp = -p / x
	else:
		tmp = math.sqrt(0.5)
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -5.8e-65)
		tmp = sqrt(0.5);
	elseif (p <= -8e-221)
		tmp = Float64(p / x);
	elseif (p <= 1.5e-74)
		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 <= -5.8e-65)
		tmp = sqrt(0.5);
	elseif (p <= -8e-221)
		tmp = p / x;
	elseif (p <= 1.5e-74)
		tmp = -p / x;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -5.8e-65], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, -8e-221], N[(p / x), $MachinePrecision], If[LessEqual[p, 1.5e-74], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;p \leq -8 \cdot 10^{-221}:\\
\;\;\;\;\frac{p}{x}\\

\mathbf{elif}\;p \leq 1.5 \cdot 10^{-74}:\\
\;\;\;\;\frac{-p}{x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < -5.7999999999999996e-65 or 1.50000000000000003e-74 < p

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

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

    if -5.7999999999999996e-65 < p < -8.00000000000000014e-221

    1. Initial program 59.1%

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified18.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 45.0%

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

    if -8.00000000000000014e-221 < p < 1.50000000000000003e-74

    1. Initial program 70.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. log1p-expm1-u70.3%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)\right)} \]
      2. log1p-udef70.3%

        \[\leadsto \color{blue}{\log \left(1 + \mathsf{expm1}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)\right)} \]
      3. +-commutative70.3%

        \[\leadsto \log \left(1 + \mathsf{expm1}\left(\sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}}\right)\right) \]
      4. distribute-rgt-in70.3%

        \[\leadsto \log \left(1 + \mathsf{expm1}\left(\sqrt{\color{blue}{\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot 0.5 + 1 \cdot 0.5}}\right)\right) \]
      5. metadata-eval70.3%

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

        \[\leadsto \log \left(1 + \mathsf{expm1}\left(\sqrt{\color{blue}{\mathsf{fma}\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}, 0.5, 0.5\right)}}\right)\right) \]
    3. Applied egg-rr70.3%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -5.8 \cdot 10^{-65}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -8 \cdot 10^{-221}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 1.5 \cdot 10^{-74}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 5: 68.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -1.25 \cdot 10^{-42}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 4300000000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (p x)
 :precision binary64
 (if (<= p -1.25e-42) (sqrt 0.5) (if (<= p 4300000000000.0) 1.0 (sqrt 0.5))))
double code(double p, double x) {
	double tmp;
	if (p <= -1.25e-42) {
		tmp = sqrt(0.5);
	} else if (p <= 4300000000000.0) {
		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 <= (-1.25d-42)) then
        tmp = sqrt(0.5d0)
    else if (p <= 4300000000000.0d0) 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 <= -1.25e-42) {
		tmp = Math.sqrt(0.5);
	} else if (p <= 4300000000000.0) {
		tmp = 1.0;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -1.25e-42:
		tmp = math.sqrt(0.5)
	elif p <= 4300000000000.0:
		tmp = 1.0
	else:
		tmp = math.sqrt(0.5)
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -1.25e-42)
		tmp = sqrt(0.5);
	elseif (p <= 4300000000000.0)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= -1.25e-42)
		tmp = sqrt(0.5);
	elseif (p <= 4300000000000.0)
		tmp = 1.0;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -1.25e-42], N[Sqrt[0.5], $MachinePrecision], If[LessEqual[p, 4300000000000.0], 1.0, N[Sqrt[0.5], $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;p \leq 4300000000000:\\
\;\;\;\;1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if p < -1.25000000000000001e-42 or 4.3e12 < p

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

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

    if -1.25000000000000001e-42 < p < 4.3e12

    1. Initial program 72.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 62.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq -1.25 \cdot 10^{-42}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq 4300000000000:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 6: 24.6% accurate, 35.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;p \leq -8 \cdot 10^{-221}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{-p}{x}\\ \end{array} \end{array} \]
(FPCore (p x) :precision binary64 (if (<= p -8e-221) (/ p x) (/ (- p) x)))
double code(double p, double x) {
	double tmp;
	if (p <= -8e-221) {
		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 <= (-8d-221)) 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 <= -8e-221) {
		tmp = p / x;
	} else {
		tmp = -p / x;
	}
	return tmp;
}
def code(p, x):
	tmp = 0
	if p <= -8e-221:
		tmp = p / x
	else:
		tmp = -p / x
	return tmp
function code(p, x)
	tmp = 0.0
	if (p <= -8e-221)
		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 <= -8e-221)
		tmp = p / x;
	else
		tmp = -p / x;
	end
	tmp_2 = tmp;
end
code[p_, x_] := If[LessEqual[p, -8e-221], N[(p / x), $MachinePrecision], N[((-p) / x), $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 85.3%

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(2 \cdot \color{blue}{\left(\frac{p}{x} \cdot \frac{p}{x}\right)}\right)} \]
    4. Simplified14.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 0 18.1%

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

    if -8.00000000000000014e-221 < p

    1. Initial program 84.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. log1p-expm1-u84.3%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)\right)} \]
      2. log1p-udef84.2%

        \[\leadsto \color{blue}{\log \left(1 + \mathsf{expm1}\left(\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}\right)\right)} \]
      3. +-commutative84.2%

        \[\leadsto \log \left(1 + \mathsf{expm1}\left(\sqrt{0.5 \cdot \color{blue}{\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} + 1\right)}}\right)\right) \]
      4. distribute-rgt-in84.2%

        \[\leadsto \log \left(1 + \mathsf{expm1}\left(\sqrt{\color{blue}{\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot 0.5 + 1 \cdot 0.5}}\right)\right) \]
      5. metadata-eval84.2%

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

        \[\leadsto \log \left(1 + \mathsf{expm1}\left(\sqrt{\color{blue}{\mathsf{fma}\left(\frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}, 0.5, 0.5\right)}}\right)\right) \]
    3. Applied egg-rr84.3%

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

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    6. Simplified21.3%

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

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

Alternative 7: 17.1% 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 84.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 13.3%

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{p}{x}} \]
  6. Final simplification11.1%

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

Developer target: 79.3% 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 2023193 
(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))))))))