
(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:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -1.0) (/ (- p_m) x) (sqrt (log (exp (fma (/ x (hypot x (* p_m 2.0))) 0.5 0.5))))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0) {
tmp = -p_m / x;
} else {
tmp = sqrt(log(exp(fma((x / hypot(x, (p_m * 2.0))), 0.5, 0.5))));
}
return tmp;
}
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -1.0) tmp = Float64(Float64(-p_m) / x); else tmp = sqrt(log(exp(fma(Float64(x / hypot(x, Float64(p_m * 2.0))), 0.5, 0.5)))); end return tmp end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-p$95$m) / x), $MachinePrecision], N[Sqrt[N[Log[N[Exp[N[(N[(x / N[Sqrt[x ^ 2 + N[(p$95$m * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * 0.5 + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p_m \cdot \left(4 \cdot p_m\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\log \left(e^{\mathsf{fma}\left(\frac{x}{\mathsf{hypot}\left(x, p_m \cdot 2\right)}, 0.5, 0.5\right)}\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 7.8%
+-commutative7.8%
sqr-neg7.8%
associate-*l*7.8%
sqr-neg7.8%
fma-def7.8%
sqr-neg7.8%
fma-def7.8%
associate-*l*7.8%
+-commutative7.8%
Simplified7.8%
Taylor expanded in x around -inf 52.2%
associate-*r/52.2%
mul-1-neg52.2%
Simplified52.2%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.8%
+-commutative99.8%
sqr-neg99.8%
associate-*l*99.8%
sqr-neg99.8%
fma-def99.8%
sqr-neg99.8%
fma-def99.8%
associate-*l*99.8%
+-commutative99.8%
Simplified99.8%
add-log-exp99.8%
+-commutative99.8%
*-commutative99.8%
fma-udef99.8%
associate-*r*99.8%
+-commutative99.8%
fma-def99.8%
Applied egg-rr99.8%
Final simplification88.5%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -1.0) (/ (- p_m) x) (sqrt (* 0.5 (exp (log1p (/ x (hypot x (* p_m 2.0)))))))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0) {
tmp = -p_m / x;
} else {
tmp = sqrt((0.5 * exp(log1p((x / hypot(x, (p_m * 2.0)))))));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if ((x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0) {
tmp = -p_m / x;
} else {
tmp = Math.sqrt((0.5 * Math.exp(Math.log1p((x / Math.hypot(x, (p_m * 2.0)))))));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if (x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0: tmp = -p_m / x else: tmp = math.sqrt((0.5 * math.exp(math.log1p((x / math.hypot(x, (p_m * 2.0))))))) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -1.0) tmp = Float64(Float64(-p_m) / x); else tmp = sqrt(Float64(0.5 * exp(log1p(Float64(x / hypot(x, Float64(p_m * 2.0))))))); end return tmp end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-p$95$m) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[Exp[N[Log[1 + N[(x / N[Sqrt[x ^ 2 + N[(p$95$m * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p_m \cdot \left(4 \cdot p_m\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot e^{\mathsf{log1p}\left(\frac{x}{\mathsf{hypot}\left(x, p_m \cdot 2\right)}\right)}}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 7.8%
+-commutative7.8%
sqr-neg7.8%
associate-*l*7.8%
sqr-neg7.8%
fma-def7.8%
sqr-neg7.8%
fma-def7.8%
associate-*l*7.8%
+-commutative7.8%
Simplified7.8%
Taylor expanded in x around -inf 52.2%
associate-*r/52.2%
mul-1-neg52.2%
Simplified52.2%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.8%
flip3-+99.8%
add-exp-log99.8%
flip3-+99.8%
log1p-udef99.8%
+-commutative99.8%
associate-*r*99.8%
fma-udef99.8%
Applied egg-rr99.8%
Final simplification88.5%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) -1.0) (/ (- p_m) x) (sqrt (* 0.5 (+ (/ x (hypot x (* p_m 2.0))) 1.0)))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0) {
tmp = -p_m / x;
} else {
tmp = sqrt((0.5 * ((x / hypot(x, (p_m * 2.0))) + 1.0)));
}
return tmp;
}
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if ((x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0) {
tmp = -p_m / x;
} else {
tmp = Math.sqrt((0.5 * ((x / Math.hypot(x, (p_m * 2.0))) + 1.0)));
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if (x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0: tmp = -p_m / x else: tmp = math.sqrt((0.5 * ((x / math.hypot(x, (p_m * 2.0))) + 1.0))) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) <= -1.0) tmp = Float64(Float64(-p_m) / x); else tmp = sqrt(Float64(0.5 * Float64(Float64(x / hypot(x, Float64(p_m * 2.0))) + 1.0))); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if ((x / sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= -1.0) tmp = -p_m / x; else tmp = sqrt((0.5 * ((x / hypot(x, (p_m * 2.0))) + 1.0))); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-p$95$m) / x), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[(x / N[Sqrt[x ^ 2 + N[(p$95$m * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p_m \cdot \left(4 \cdot p_m\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\frac{x}{\mathsf{hypot}\left(x, p_m \cdot 2\right)} + 1\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1Initial program 7.8%
+-commutative7.8%
sqr-neg7.8%
associate-*l*7.8%
sqr-neg7.8%
fma-def7.8%
sqr-neg7.8%
fma-def7.8%
associate-*l*7.8%
+-commutative7.8%
Simplified7.8%
Taylor expanded in x around -inf 52.2%
associate-*r/52.2%
mul-1-neg52.2%
Simplified52.2%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) Initial program 99.8%
+-commutative99.8%
sqr-neg99.8%
associate-*l*99.8%
sqr-neg99.8%
fma-def99.8%
sqr-neg99.8%
fma-def99.8%
associate-*l*99.8%
+-commutative99.8%
Simplified99.8%
*-commutative99.8%
fma-udef99.8%
associate-*r*99.8%
+-commutative99.8%
distribute-rgt1-in99.8%
+-commutative99.8%
Applied egg-rr99.8%
Final simplification88.5%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ (- p_m) x)) (t_1 (+ 1.0 (* (* (/ p_m x) (/ p_m x)) -0.5))))
(if (<= p_m 3e-215)
t_1
(if (<= p_m 1.4e-170)
t_0
(if (<= p_m 2.4e-108) t_1 (if (<= p_m 3.05e-65) t_0 (sqrt 0.5)))))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = -p_m / x;
double t_1 = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
double tmp;
if (p_m <= 3e-215) {
tmp = t_1;
} else if (p_m <= 1.4e-170) {
tmp = t_0;
} else if (p_m <= 2.4e-108) {
tmp = t_1;
} else if (p_m <= 3.05e-65) {
tmp = t_0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = -p_m / x
t_1 = 1.0d0 + (((p_m / x) * (p_m / x)) * (-0.5d0))
if (p_m <= 3d-215) then
tmp = t_1
else if (p_m <= 1.4d-170) then
tmp = t_0
else if (p_m <= 2.4d-108) then
tmp = t_1
else if (p_m <= 3.05d-65) then
tmp = t_0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double t_0 = -p_m / x;
double t_1 = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
double tmp;
if (p_m <= 3e-215) {
tmp = t_1;
} else if (p_m <= 1.4e-170) {
tmp = t_0;
} else if (p_m <= 2.4e-108) {
tmp = t_1;
} else if (p_m <= 3.05e-65) {
tmp = t_0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): t_0 = -p_m / x t_1 = 1.0 + (((p_m / x) * (p_m / x)) * -0.5) tmp = 0 if p_m <= 3e-215: tmp = t_1 elif p_m <= 1.4e-170: tmp = t_0 elif p_m <= 2.4e-108: tmp = t_1 elif p_m <= 3.05e-65: tmp = t_0 else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) t_0 = Float64(Float64(-p_m) / x) t_1 = Float64(1.0 + Float64(Float64(Float64(p_m / x) * Float64(p_m / x)) * -0.5)) tmp = 0.0 if (p_m <= 3e-215) tmp = t_1; elseif (p_m <= 1.4e-170) tmp = t_0; elseif (p_m <= 2.4e-108) tmp = t_1; elseif (p_m <= 3.05e-65) tmp = t_0; else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) t_0 = -p_m / x; t_1 = 1.0 + (((p_m / x) * (p_m / x)) * -0.5); tmp = 0.0; if (p_m <= 3e-215) tmp = t_1; elseif (p_m <= 1.4e-170) tmp = t_0; elseif (p_m <= 2.4e-108) tmp = t_1; elseif (p_m <= 3.05e-65) tmp = t_0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[((-p$95$m) / x), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(N[(N[(p$95$m / x), $MachinePrecision] * N[(p$95$m / x), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[p$95$m, 3e-215], t$95$1, If[LessEqual[p$95$m, 1.4e-170], t$95$0, If[LessEqual[p$95$m, 2.4e-108], t$95$1, If[LessEqual[p$95$m, 3.05e-65], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{-p_m}{x}\\
t_1 := 1 + \left(\frac{p_m}{x} \cdot \frac{p_m}{x}\right) \cdot -0.5\\
\mathbf{if}\;p_m \leq 3 \cdot 10^{-215}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;p_m \leq 1.4 \cdot 10^{-170}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p_m \leq 2.4 \cdot 10^{-108}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;p_m \leq 3.05 \cdot 10^{-65}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 3.00000000000000025e-215 or 1.39999999999999998e-170 < p < 2.40000000000000017e-108Initial program 77.6%
+-commutative77.6%
sqr-neg77.6%
associate-*l*77.6%
sqr-neg77.6%
fma-def77.6%
sqr-neg77.6%
fma-def77.6%
associate-*l*77.6%
+-commutative77.6%
Simplified77.6%
Taylor expanded in x around inf 33.7%
*-commutative33.7%
Simplified33.7%
unpow233.7%
unpow233.7%
times-frac33.7%
Applied egg-rr33.7%
if 3.00000000000000025e-215 < p < 1.39999999999999998e-170 or 2.40000000000000017e-108 < p < 3.05000000000000007e-65Initial program 40.5%
+-commutative40.5%
sqr-neg40.5%
associate-*l*40.5%
sqr-neg40.5%
fma-def40.5%
sqr-neg40.5%
fma-def40.5%
associate-*l*40.5%
+-commutative40.5%
Simplified40.5%
Taylor expanded in x around -inf 63.9%
associate-*r/63.9%
mul-1-neg63.9%
Simplified63.9%
if 3.05000000000000007e-65 < p Initial program 91.4%
Taylor expanded in x around 0 87.7%
Final simplification51.1%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ (- p_m) x)))
(if (<= p_m 4.8e-215)
(+ 1.0 (* (* (/ p_m x) (/ p_m x)) -0.5))
(if (<= p_m 1.35e-170)
t_0
(if (<= p_m 2.8e-106) 1.0 (if (<= p_m 4.1e-66) t_0 (sqrt 0.5)))))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = -p_m / x;
double tmp;
if (p_m <= 4.8e-215) {
tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
} else if (p_m <= 1.35e-170) {
tmp = t_0;
} else if (p_m <= 2.8e-106) {
tmp = 1.0;
} else if (p_m <= 4.1e-66) {
tmp = t_0;
} else {
tmp = sqrt(0.5);
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = -p_m / x
if (p_m <= 4.8d-215) then
tmp = 1.0d0 + (((p_m / x) * (p_m / x)) * (-0.5d0))
else if (p_m <= 1.35d-170) then
tmp = t_0
else if (p_m <= 2.8d-106) then
tmp = 1.0d0
else if (p_m <= 4.1d-66) then
tmp = t_0
else
tmp = sqrt(0.5d0)
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double t_0 = -p_m / x;
double tmp;
if (p_m <= 4.8e-215) {
tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
} else if (p_m <= 1.35e-170) {
tmp = t_0;
} else if (p_m <= 2.8e-106) {
tmp = 1.0;
} else if (p_m <= 4.1e-66) {
tmp = t_0;
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): t_0 = -p_m / x tmp = 0 if p_m <= 4.8e-215: tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5) elif p_m <= 1.35e-170: tmp = t_0 elif p_m <= 2.8e-106: tmp = 1.0 elif p_m <= 4.1e-66: tmp = t_0 else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) t_0 = Float64(Float64(-p_m) / x) tmp = 0.0 if (p_m <= 4.8e-215) tmp = Float64(1.0 + Float64(Float64(Float64(p_m / x) * Float64(p_m / x)) * -0.5)); elseif (p_m <= 1.35e-170) tmp = t_0; elseif (p_m <= 2.8e-106) tmp = 1.0; elseif (p_m <= 4.1e-66) tmp = t_0; else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) t_0 = -p_m / x; tmp = 0.0; if (p_m <= 4.8e-215) tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5); elseif (p_m <= 1.35e-170) tmp = t_0; elseif (p_m <= 2.8e-106) tmp = 1.0; elseif (p_m <= 4.1e-66) tmp = t_0; else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[((-p$95$m) / x), $MachinePrecision]}, If[LessEqual[p$95$m, 4.8e-215], N[(1.0 + N[(N[(N[(p$95$m / x), $MachinePrecision] * N[(p$95$m / x), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[p$95$m, 1.35e-170], t$95$0, If[LessEqual[p$95$m, 2.8e-106], 1.0, If[LessEqual[p$95$m, 4.1e-66], t$95$0, N[Sqrt[0.5], $MachinePrecision]]]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{-p_m}{x}\\
\mathbf{if}\;p_m \leq 4.8 \cdot 10^{-215}:\\
\;\;\;\;1 + \left(\frac{p_m}{x} \cdot \frac{p_m}{x}\right) \cdot -0.5\\
\mathbf{elif}\;p_m \leq 1.35 \cdot 10^{-170}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;p_m \leq 2.8 \cdot 10^{-106}:\\
\;\;\;\;1\\
\mathbf{elif}\;p_m \leq 4.1 \cdot 10^{-66}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 4.8000000000000002e-215Initial program 78.0%
+-commutative78.0%
sqr-neg78.0%
associate-*l*78.0%
sqr-neg78.0%
fma-def78.0%
sqr-neg78.0%
fma-def78.0%
associate-*l*78.0%
+-commutative78.0%
Simplified78.0%
Taylor expanded in x around inf 31.2%
*-commutative31.2%
Simplified31.2%
unpow231.2%
unpow231.2%
times-frac31.2%
Applied egg-rr31.2%
if 4.8000000000000002e-215 < p < 1.3499999999999999e-170 or 2.79999999999999988e-106 < p < 4.09999999999999998e-66Initial program 37.9%
+-commutative37.9%
sqr-neg37.9%
associate-*l*37.9%
sqr-neg37.9%
fma-def37.9%
sqr-neg37.9%
fma-def37.9%
associate-*l*37.9%
+-commutative37.9%
Simplified37.9%
Taylor expanded in x around -inf 66.3%
associate-*r/66.3%
mul-1-neg66.3%
Simplified66.3%
if 1.3499999999999999e-170 < p < 2.79999999999999988e-106Initial program 74.2%
Taylor expanded in x around inf 66.8%
if 4.09999999999999998e-66 < p Initial program 91.4%
Taylor expanded in x around 0 87.7%
Final simplification51.1%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x 3e-150) (/ (- p_m) x) (+ 1.0 (* (* (/ p_m x) (/ p_m x)) -0.5))))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= 3e-150) {
tmp = -p_m / x;
} else {
tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
}
return tmp;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3d-150) then
tmp = -p_m / x
else
tmp = 1.0d0 + (((p_m / x) * (p_m / x)) * (-0.5d0))
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= 3e-150) {
tmp = -p_m / x;
} else {
tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= 3e-150: tmp = -p_m / x else: tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= 3e-150) tmp = Float64(Float64(-p_m) / x); else tmp = Float64(1.0 + Float64(Float64(Float64(p_m / x) * Float64(p_m / x)) * -0.5)); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= 3e-150) tmp = -p_m / x; else tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, 3e-150], N[((-p$95$m) / x), $MachinePrecision], N[(1.0 + N[(N[(N[(p$95$m / x), $MachinePrecision] * N[(p$95$m / x), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3 \cdot 10^{-150}:\\
\;\;\;\;\frac{-p_m}{x}\\
\mathbf{else}:\\
\;\;\;\;1 + \left(\frac{p_m}{x} \cdot \frac{p_m}{x}\right) \cdot -0.5\\
\end{array}
\end{array}
if x < 3.0000000000000002e-150Initial program 61.5%
+-commutative61.5%
sqr-neg61.5%
associate-*l*61.5%
sqr-neg61.5%
fma-def61.5%
sqr-neg61.5%
fma-def61.5%
associate-*l*61.5%
+-commutative61.5%
Simplified61.5%
Taylor expanded in x around -inf 23.6%
associate-*r/23.6%
mul-1-neg23.6%
Simplified23.6%
if 3.0000000000000002e-150 < x Initial program 100.0%
+-commutative100.0%
sqr-neg100.0%
associate-*l*100.0%
sqr-neg100.0%
fma-def100.0%
sqr-neg100.0%
fma-def100.0%
associate-*l*100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around inf 56.2%
*-commutative56.2%
Simplified56.2%
unpow256.2%
unpow256.2%
times-frac56.2%
Applied egg-rr56.2%
Final simplification37.5%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (/ (- p_m) x))
p_m = fabs(p);
double code(double p_m, double x) {
return -p_m / x;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
code = -p_m / x
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
return -p_m / x;
}
p_m = math.fabs(p) def code(p_m, x): return -p_m / x
p_m = abs(p) function code(p_m, x) return Float64(Float64(-p_m) / x) end
p_m = abs(p); function tmp = code(p_m, x) tmp = -p_m / x; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := N[((-p$95$m) / x), $MachinePrecision]
\begin{array}{l}
p_m = \left|p\right|
\\
\frac{-p_m}{x}
\end{array}
Initial program 77.9%
+-commutative77.9%
sqr-neg77.9%
associate-*l*77.9%
sqr-neg77.9%
fma-def77.9%
sqr-neg77.9%
fma-def77.9%
associate-*l*77.9%
+-commutative77.9%
Simplified77.9%
Taylor expanded in x around -inf 15.1%
associate-*r/15.1%
mul-1-neg15.1%
Simplified15.1%
Final simplification15.1%
(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}
herbie shell --seed 2024019
(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))))))))