
(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 4 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)))) -0.6) (/ p_m (- x)) (sqrt (+ 0.5 (* 0.5 (/ 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)))) <= -0.6) {
tmp = p_m / -x;
} else {
tmp = sqrt((0.5 + (0.5 * (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)))) <= -0.6) {
tmp = p_m / -x;
} else {
tmp = Math.sqrt((0.5 + (0.5 * (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)))) <= -0.6: tmp = p_m / -x else: tmp = math.sqrt((0.5 + (0.5 * (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)))) <= -0.6) tmp = Float64(p_m / Float64(-x)); else tmp = sqrt(Float64(0.5 + Float64(0.5 * Float64(x / hypot(x, Float64(p_m * 2.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)))) <= -0.6) tmp = p_m / -x; else tmp = sqrt((0.5 + (0.5 * (x / hypot(x, (p_m * 2.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], -0.6], N[(p$95$m / (-x)), $MachinePrecision], N[Sqrt[N[(0.5 + N[(0.5 * N[(x / N[Sqrt[x ^ 2 + N[(p$95$m * 2.0), $MachinePrecision] ^ 2], $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 -0.6:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 + 0.5 \cdot \frac{x}{\mathsf{hypot}\left(x, p\_m \cdot 2\right)}}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < -0.599999999999999978Initial program 19.5%
+-commutative19.5%
sqr-neg19.5%
associate-*l*19.5%
sqr-neg19.5%
fma-define19.5%
sqr-neg19.5%
fma-define19.5%
associate-*l*19.5%
+-commutative19.5%
Simplified19.5%
Taylor expanded in x around -inf 57.0%
mul-1-neg57.0%
distribute-neg-frac257.0%
Simplified57.0%
if -0.599999999999999978 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 99.9%
+-commutative99.9%
sqr-neg99.9%
associate-*l*99.9%
sqr-neg99.9%
fma-define99.9%
sqr-neg99.9%
fma-define99.9%
associate-*l*99.9%
+-commutative99.9%
Simplified99.9%
div-inv99.9%
fma-undefine99.9%
associate-*r*99.9%
add-sqr-sqrt99.9%
hypot-define99.9%
associate-*r*99.9%
*-commutative99.9%
sqrt-prod99.9%
sqrt-prod53.6%
add-sqr-sqrt100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*r/100.0%
*-rgt-identity100.0%
*-commutative100.0%
Simplified100.0%
Final simplification91.8%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(if (<= p_m 2.05e-272)
1.0
(if (<= p_m 1.9e-249)
(/ p_m (- x))
(if (<= p_m 3.8e-96)
(+ 1.0 (* (* (/ p_m x) (/ p_m x)) -0.5))
(sqrt 0.5)))))p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (p_m <= 2.05e-272) {
tmp = 1.0;
} else if (p_m <= 1.9e-249) {
tmp = p_m / -x;
} else if (p_m <= 3.8e-96) {
tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
} 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) :: tmp
if (p_m <= 2.05d-272) then
tmp = 1.0d0
else if (p_m <= 1.9d-249) then
tmp = p_m / -x
else if (p_m <= 3.8d-96) then
tmp = 1.0d0 + (((p_m / x) * (p_m / x)) * (-0.5d0))
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 tmp;
if (p_m <= 2.05e-272) {
tmp = 1.0;
} else if (p_m <= 1.9e-249) {
tmp = p_m / -x;
} else if (p_m <= 3.8e-96) {
tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5);
} else {
tmp = Math.sqrt(0.5);
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if p_m <= 2.05e-272: tmp = 1.0 elif p_m <= 1.9e-249: tmp = p_m / -x elif p_m <= 3.8e-96: tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5) else: tmp = math.sqrt(0.5) return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (p_m <= 2.05e-272) tmp = 1.0; elseif (p_m <= 1.9e-249) tmp = Float64(p_m / Float64(-x)); elseif (p_m <= 3.8e-96) tmp = Float64(1.0 + Float64(Float64(Float64(p_m / x) * Float64(p_m / x)) * -0.5)); else tmp = sqrt(0.5); end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (p_m <= 2.05e-272) tmp = 1.0; elseif (p_m <= 1.9e-249) tmp = p_m / -x; elseif (p_m <= 3.8e-96) tmp = 1.0 + (((p_m / x) * (p_m / x)) * -0.5); else tmp = sqrt(0.5); end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[p$95$m, 2.05e-272], 1.0, If[LessEqual[p$95$m, 1.9e-249], N[(p$95$m / (-x)), $MachinePrecision], If[LessEqual[p$95$m, 3.8e-96], N[(1.0 + N[(N[(N[(p$95$m / x), $MachinePrecision] * N[(p$95$m / x), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision], N[Sqrt[0.5], $MachinePrecision]]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;p\_m \leq 2.05 \cdot 10^{-272}:\\
\;\;\;\;1\\
\mathbf{elif}\;p\_m \leq 1.9 \cdot 10^{-249}:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{elif}\;p\_m \leq 3.8 \cdot 10^{-96}:\\
\;\;\;\;1 + \left(\frac{p\_m}{x} \cdot \frac{p\_m}{x}\right) \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5}\\
\end{array}
\end{array}
if p < 2.0499999999999999e-272Initial program 83.5%
+-commutative83.5%
sqr-neg83.5%
associate-*l*83.5%
sqr-neg83.5%
fma-define83.5%
sqr-neg83.5%
fma-define83.5%
associate-*l*83.5%
+-commutative83.5%
Simplified83.5%
div-inv83.4%
fma-undefine83.4%
associate-*r*83.4%
add-sqr-sqrt83.4%
hypot-define83.4%
associate-*r*83.4%
*-commutative83.4%
sqrt-prod83.4%
sqrt-prod6.9%
add-sqr-sqrt83.6%
metadata-eval83.6%
Applied egg-rr83.6%
associate-*r/83.7%
*-rgt-identity83.7%
*-commutative83.7%
Simplified83.7%
Taylor expanded in x around inf 32.8%
mul-1-neg32.8%
unsub-neg32.8%
unpow232.8%
unpow232.8%
times-frac32.8%
unpow232.8%
Simplified32.8%
pow232.8%
Applied egg-rr32.8%
Taylor expanded in p around 0 41.8%
if 2.0499999999999999e-272 < p < 1.9e-249Initial program 68.1%
+-commutative68.1%
sqr-neg68.1%
associate-*l*68.1%
sqr-neg68.1%
fma-define68.1%
sqr-neg68.1%
fma-define68.1%
associate-*l*68.1%
+-commutative68.1%
Simplified68.1%
Taylor expanded in x around -inf 51.3%
mul-1-neg51.3%
distribute-neg-frac251.3%
Simplified51.3%
if 1.9e-249 < p < 3.8000000000000001e-96Initial program 75.6%
+-commutative75.6%
sqr-neg75.6%
associate-*l*75.6%
sqr-neg75.6%
fma-define75.6%
sqr-neg75.6%
fma-define75.6%
associate-*l*75.6%
+-commutative75.6%
Simplified75.6%
Taylor expanded in x around inf 71.6%
*-commutative71.6%
Simplified71.6%
unpow271.6%
unpow271.6%
times-frac71.6%
Applied egg-rr71.6%
if 3.8000000000000001e-96 < p Initial program 90.2%
Taylor expanded in x around 0 83.2%
Final simplification59.7%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= x -7.4e-109) (/ p_m (- x)) 1.0))
p_m = fabs(p);
double code(double p_m, double x) {
double tmp;
if (x <= -7.4e-109) {
tmp = p_m / -x;
} else {
tmp = 1.0;
}
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 <= (-7.4d-109)) then
tmp = p_m / -x
else
tmp = 1.0d0
end if
code = tmp
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
double tmp;
if (x <= -7.4e-109) {
tmp = p_m / -x;
} else {
tmp = 1.0;
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): tmp = 0 if x <= -7.4e-109: tmp = p_m / -x else: tmp = 1.0 return tmp
p_m = abs(p) function code(p_m, x) tmp = 0.0 if (x <= -7.4e-109) tmp = Float64(p_m / Float64(-x)); else tmp = 1.0; end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) tmp = 0.0; if (x <= -7.4e-109) tmp = p_m / -x; else tmp = 1.0; end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := If[LessEqual[x, -7.4e-109], N[(p$95$m / (-x)), $MachinePrecision], 1.0]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.4 \cdot 10^{-109}:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -7.39999999999999961e-109Initial program 63.4%
+-commutative63.4%
sqr-neg63.4%
associate-*l*63.4%
sqr-neg63.4%
fma-define63.4%
sqr-neg63.4%
fma-define63.4%
associate-*l*63.4%
+-commutative63.4%
Simplified63.4%
Taylor expanded in x around -inf 27.2%
mul-1-neg27.2%
distribute-neg-frac227.2%
Simplified27.2%
if -7.39999999999999961e-109 < x Initial program 98.9%
+-commutative98.9%
sqr-neg98.9%
associate-*l*98.9%
sqr-neg98.9%
fma-define98.9%
sqr-neg98.9%
fma-define98.9%
associate-*l*98.9%
+-commutative98.9%
Simplified98.9%
div-inv98.9%
fma-undefine98.9%
associate-*r*98.9%
add-sqr-sqrt98.9%
hypot-define98.9%
associate-*r*98.9%
*-commutative98.9%
sqrt-prod98.9%
sqrt-prod52.2%
add-sqr-sqrt98.9%
metadata-eval98.9%
Applied egg-rr98.9%
associate-*r/98.9%
*-rgt-identity98.9%
*-commutative98.9%
Simplified98.9%
Taylor expanded in x around inf 46.8%
mul-1-neg46.8%
unsub-neg46.8%
unpow246.8%
unpow246.8%
times-frac46.8%
unpow246.8%
Simplified46.8%
pow246.8%
Applied egg-rr46.8%
Taylor expanded in p around 0 56.7%
Final simplification44.7%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 1.0)
p_m = fabs(p);
double code(double p_m, double x) {
return 1.0;
}
p_m = abs(p)
real(8) function code(p_m, x)
real(8), intent (in) :: p_m
real(8), intent (in) :: x
code = 1.0d0
end function
p_m = Math.abs(p);
public static double code(double p_m, double x) {
return 1.0;
}
p_m = math.fabs(p) def code(p_m, x): return 1.0
p_m = abs(p) function code(p_m, x) return 1.0 end
p_m = abs(p); function tmp = code(p_m, x) tmp = 1.0; end
p_m = N[Abs[p], $MachinePrecision] code[p$95$m_, x_] := 1.0
\begin{array}{l}
p_m = \left|p\right|
\\
1
\end{array}
Initial program 84.5%
+-commutative84.5%
sqr-neg84.5%
associate-*l*84.5%
sqr-neg84.5%
fma-define84.5%
sqr-neg84.5%
fma-define84.5%
associate-*l*84.5%
+-commutative84.5%
Simplified84.5%
div-inv84.1%
fma-undefine84.1%
associate-*r*84.1%
add-sqr-sqrt84.1%
hypot-define84.1%
associate-*r*84.1%
*-commutative84.1%
sqrt-prod84.1%
sqrt-prod44.6%
add-sqr-sqrt84.2%
metadata-eval84.2%
Applied egg-rr84.2%
associate-*r/84.6%
*-rgt-identity84.6%
*-commutative84.6%
Simplified84.6%
Taylor expanded in x around inf 28.8%
mul-1-neg28.8%
unsub-neg28.8%
unpow228.8%
unpow228.8%
times-frac28.8%
unpow228.8%
Simplified28.8%
pow228.8%
Applied egg-rr28.8%
Taylor expanded in p around 0 39.0%
Final simplification39.0%
(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 2024077
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:alt
(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))))))))