Given's Rotation SVD example

Percentage Accurate: 79.9% → 99.7%
Time: 12.4s
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.9% 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.3× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} t_0 := \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\\ \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 \frac{1 + {t_0}^{3}}{\mathsf{fma}\left(t_0, -1 + t_0, 1\right)}}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (let* ((t_0 (/ x (hypot x (* p 2.0)))))
   (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
     (/ (- p) x)
     (sqrt (* 0.5 (/ (+ 1.0 (pow t_0 3.0)) (fma t_0 (+ -1.0 t_0) 1.0)))))))
p = abs(p);
double code(double p, double x) {
	double t_0 = x / hypot(x, (p * 2.0));
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -p / x;
	} else {
		tmp = sqrt((0.5 * ((1.0 + pow(t_0, 3.0)) / fma(t_0, (-1.0 + t_0), 1.0))));
	}
	return tmp;
}
p = abs(p)
function code(p, x)
	t_0 = Float64(x / hypot(x, Float64(p * 2.0)))
	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(Float64(1.0 + (t_0 ^ 3.0)) / fma(t_0, Float64(-1.0 + t_0), 1.0))));
	end
	return tmp
end
NOTE: p should be positive before calling this function
code[p_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]}, 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[Power[t$95$0, 3.0], $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * N[(-1.0 + t$95$0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
t_0 := \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\\
\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 \frac{1 + {t_0}^{3}}{\mathsf{fma}\left(t_0, -1 + t_0, 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.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 55.7%

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    7. Simplified42.8%

      \[\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. flip3-+99.6%

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

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

    \[\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 \frac{1 + {\left(\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}^{3}}{\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}, -1 + \frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}, 1\right)}}\\ \end{array} \]

Alternative 2: 99.7% accurate, 0.4× 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 e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\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 (exp (log1p (/ x (hypot x (* p 2.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 * exp(log1p((x / hypot(x, (p * 2.0)))))));
	}
	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 * Math.exp(Math.log1p((x / Math.hypot(x, (p * 2.0)))))));
	}
	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 * math.exp(math.log1p((x / math.hypot(x, (p * 2.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 * exp(log1p(Float64(x / hypot(x, Float64(p * 2.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[Exp[N[Log[1 + N[(x / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $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 e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\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.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 55.7%

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    7. Simplified42.8%

      \[\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-exp-log99.6%

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

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

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

        \[\leadsto \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\sqrt{x \cdot x + \color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}}}}\right)}} \]
      5. hypot-def99.6%

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

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

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

        \[\leadsto \sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, \color{blue}{2} \cdot \sqrt{p \cdot p}\right)}\right)}} \]
      9. sqrt-unprod45.7%

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

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

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

    \[\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 e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, p \cdot 2\right)}\right)}}\\ \end{array} \]

Alternative 3: 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.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 55.7%

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    7. Simplified42.8%

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

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

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

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

    \[\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 4: 67.6% accurate, 2.0× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;p \leq 6.5 \cdot 10^{-172}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 3.8 \cdot 10^{-18}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x)
 :precision binary64
 (if (<= p 6.5e-172) 1.0 (if (<= p 3.8e-18) (/ (- p) x) (sqrt 0.5))))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (p <= 6.5e-172) {
		tmp = 1.0;
	} else if (p <= 3.8e-18) {
		tmp = -p / x;
	} 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) :: tmp
    if (p <= 6.5d-172) then
        tmp = 1.0d0
    else if (p <= 3.8d-18) then
        tmp = -p / x
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if (p <= 6.5e-172) {
		tmp = 1.0;
	} else if (p <= 3.8e-18) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if p <= 6.5e-172:
		tmp = 1.0
	elif p <= 3.8e-18:
		tmp = -p / x
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (p <= 6.5e-172)
		tmp = 1.0;
	elseif (p <= 3.8e-18)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= 6.5e-172)
		tmp = 1.0;
	elseif (p <= 3.8e-18)
		tmp = -p / x;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[p, 6.5e-172], 1.0, If[LessEqual[p, 3.8e-18], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 6.5 \cdot 10^{-172}:\\
\;\;\;\;1\\

\mathbf{elif}\;p \leq 3.8 \cdot 10^{-18}:\\
\;\;\;\;\frac{-p}{x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if p < 6.50000000000000012e-172

    1. Initial program 79.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. sqrt-prod78.7%

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

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

        \[\leadsto \sqrt{1 + \frac{x}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}}} \cdot \sqrt{0.5} \]
      4. add-sqr-sqrt78.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.50000000000000012e-172 < p < 3.7999999999999998e-18

    1. Initial program 56.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 30.8%

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    7. Simplified49.0%

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

    if 3.7999999999999998e-18 < p

    1. Initial program 98.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 95.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 6.5 \cdot 10^{-172}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 3.8 \cdot 10^{-18}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 5: 67.4% accurate, 2.1× speedup?

\[\begin{array}{l} p = |p|\\ \\ \begin{array}{l} \mathbf{if}\;p \leq 3.8 \cdot 10^{-18}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 (if (<= p 3.8e-18) (/ (- p) x) (sqrt 0.5)))
p = abs(p);
double code(double p, double x) {
	double tmp;
	if (p <= 3.8e-18) {
		tmp = -p / x;
	} 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) :: tmp
    if (p <= 3.8d-18) then
        tmp = -p / x
    else
        tmp = sqrt(0.5d0)
    end if
    code = tmp
end function
p = Math.abs(p);
public static double code(double p, double x) {
	double tmp;
	if (p <= 3.8e-18) {
		tmp = -p / x;
	} else {
		tmp = Math.sqrt(0.5);
	}
	return tmp;
}
p = abs(p)
def code(p, x):
	tmp = 0
	if p <= 3.8e-18:
		tmp = -p / x
	else:
		tmp = math.sqrt(0.5)
	return tmp
p = abs(p)
function code(p, x)
	tmp = 0.0
	if (p <= 3.8e-18)
		tmp = Float64(Float64(-p) / x);
	else
		tmp = sqrt(0.5);
	end
	return tmp
end
p = abs(p)
function tmp_2 = code(p, x)
	tmp = 0.0;
	if (p <= 3.8e-18)
		tmp = -p / x;
	else
		tmp = sqrt(0.5);
	end
	tmp_2 = tmp;
end
NOTE: p should be positive before calling this function
code[p_, x_] := If[LessEqual[p, 3.8e-18], N[((-p) / x), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]
\begin{array}{l}
p = |p|\\
\\
\begin{array}{l}
\mathbf{if}\;p \leq 3.8 \cdot 10^{-18}:\\
\;\;\;\;\frac{-p}{x}\\

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


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

    1. Initial program 75.9%

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

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

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{-p}}{x} \]
    7. Simplified13.8%

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

    if 3.7999999999999998e-18 < p

    1. Initial program 98.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 95.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;p \leq 3.8 \cdot 10^{-18}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]

Alternative 6: 28.0% accurate, 35.5× speedup?

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

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


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

    1. Initial program 65.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 25.3%

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

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

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

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

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

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

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

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

    if -3.999999999999988e-310 < 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. Taylor expanded in x around -inf 4.6%

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

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

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

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

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

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

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

Alternative 7: 6.3% accurate, 71.7× speedup?

\[\begin{array}{l} p = |p|\\ \\ \frac{p}{x} \end{array} \]
NOTE: p should be positive before calling this function
(FPCore (p x) :precision binary64 (/ p x))
p = abs(p);
double code(double p, double x) {
	return p / x;
}
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 = p / x
end function
p = Math.abs(p);
public static double code(double p, double x) {
	return p / x;
}
p = abs(p)
def code(p, x):
	return p / x
p = abs(p)
function code(p, x)
	return Float64(p / x)
end
p = abs(p)
function tmp = code(p, x)
	tmp = p / x;
end
NOTE: p should be positive before calling this function
code[p_, x_] := N[(p / x), $MachinePrecision]
\begin{array}{l}
p = |p|\\
\\
\frac{p}{x}
\end{array}
Derivation
  1. Initial program 81.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 15.4%

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{p}{x}} \]
  6. Final simplification16.0%

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

Developer target: 79.9% 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 2023279 
(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))))))))