
(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 5 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
(let* ((t_0 (* p_m (* 4.0 p_m))))
(if (<= (/ x (sqrt (+ t_0 (* x x)))) -1.0)
(/ p_m (- x))
(sqrt (fma (/ x (sqrt (fma x x t_0))) 0.5 0.5)))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = p_m * (4.0 * p_m);
double tmp;
if ((x / sqrt((t_0 + (x * x)))) <= -1.0) {
tmp = p_m / -x;
} else {
tmp = sqrt(fma((x / sqrt(fma(x, x, t_0))), 0.5, 0.5));
}
return tmp;
}
p_m = abs(p) function code(p_m, x) t_0 = Float64(p_m * Float64(4.0 * p_m)) tmp = 0.0 if (Float64(x / sqrt(Float64(t_0 + Float64(x * x)))) <= -1.0) tmp = Float64(p_m / Float64(-x)); else tmp = sqrt(fma(Float64(x / sqrt(fma(x, x, t_0))), 0.5, 0.5)); end return tmp end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x / N[Sqrt[N[(t$95$0 + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[(p$95$m / (-x)), $MachinePrecision], N[Sqrt[N[(N[(x / N[Sqrt[N[(x * x + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 0.5 + 0.5), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := p\_m \cdot \left(4 \cdot p\_m\right)\\
\mathbf{if}\;\frac{x}{\sqrt{t\_0 + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{x}{\sqrt{\mathsf{fma}\left(x, x, t\_0\right)}}, 0.5, 0.5\right)}\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < -1Initial program 9.5%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f649.5
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f649.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f649.5
Applied rewrites9.5%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6451.9
Applied rewrites51.9%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Final simplification91.0%
p_m = (fabs.f64 p)
(FPCore (p_m x)
:precision binary64
(let* ((t_0 (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x))))))
(if (<= t_0 -1.0)
(/ p_m (- x))
(if (<= t_0 0.5) (sqrt (fma 0.25 (/ x p_m) 0.5)) 1.0))))p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = x / sqrt(((p_m * (4.0 * p_m)) + (x * x)));
double tmp;
if (t_0 <= -1.0) {
tmp = p_m / -x;
} else if (t_0 <= 0.5) {
tmp = sqrt(fma(0.25, (x / p_m), 0.5));
} else {
tmp = 1.0;
}
return tmp;
}
p_m = abs(p) function code(p_m, x) t_0 = Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) tmp = 0.0 if (t_0 <= -1.0) tmp = Float64(p_m / Float64(-x)); elseif (t_0 <= 0.5) tmp = sqrt(fma(0.25, Float64(x / p_m), 0.5)); else tmp = 1.0; end return tmp end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1.0], N[(p$95$m / (-x)), $MachinePrecision], If[LessEqual[t$95$0, 0.5], N[Sqrt[N[(0.25 * N[(x / p$95$m), $MachinePrecision] + 0.5), $MachinePrecision]], $MachinePrecision], 1.0]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{x}{\sqrt{p\_m \cdot \left(4 \cdot p\_m\right) + x \cdot x}}\\
\mathbf{if}\;t\_0 \leq -1:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{elif}\;t\_0 \leq 0.5:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(0.25, \frac{x}{p\_m}, 0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < -1Initial program 9.5%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f649.5
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f649.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f649.5
Applied rewrites9.5%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6451.9
Applied rewrites51.9%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < 0.5Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6498.3
Applied rewrites98.3%
if 0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in x around inf
Applied rewrites100.0%
Final simplification90.1%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (let* ((t_0 (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))))) (if (<= t_0 -1.0) (/ p_m (- x)) (if (<= t_0 0.5) (sqrt 0.5) 1.0))))
p_m = fabs(p);
double code(double p_m, double x) {
double t_0 = x / sqrt(((p_m * (4.0 * p_m)) + (x * x)));
double tmp;
if (t_0 <= -1.0) {
tmp = p_m / -x;
} else if (t_0 <= 0.5) {
tmp = sqrt(0.5);
} 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) :: t_0
real(8) :: tmp
t_0 = x / sqrt(((p_m * (4.0d0 * p_m)) + (x * x)))
if (t_0 <= (-1.0d0)) then
tmp = p_m / -x
else if (t_0 <= 0.5d0) then
tmp = sqrt(0.5d0)
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 t_0 = x / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)));
double tmp;
if (t_0 <= -1.0) {
tmp = p_m / -x;
} else if (t_0 <= 0.5) {
tmp = Math.sqrt(0.5);
} else {
tmp = 1.0;
}
return tmp;
}
p_m = math.fabs(p) def code(p_m, x): t_0 = x / math.sqrt(((p_m * (4.0 * p_m)) + (x * x))) tmp = 0 if t_0 <= -1.0: tmp = p_m / -x elif t_0 <= 0.5: tmp = math.sqrt(0.5) else: tmp = 1.0 return tmp
p_m = abs(p) function code(p_m, x) t_0 = Float64(x / sqrt(Float64(Float64(p_m * Float64(4.0 * p_m)) + Float64(x * x)))) tmp = 0.0 if (t_0 <= -1.0) tmp = Float64(p_m / Float64(-x)); elseif (t_0 <= 0.5) tmp = sqrt(0.5); else tmp = 1.0; end return tmp end
p_m = abs(p); function tmp_2 = code(p_m, x) t_0 = x / sqrt(((p_m * (4.0 * p_m)) + (x * x))); tmp = 0.0; if (t_0 <= -1.0) tmp = p_m / -x; elseif (t_0 <= 0.5) tmp = sqrt(0.5); else tmp = 1.0; end tmp_2 = tmp; end
p_m = N[Abs[p], $MachinePrecision]
code[p$95$m_, x_] := Block[{t$95$0 = N[(x / N[Sqrt[N[(N[(p$95$m * N[(4.0 * p$95$m), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1.0], N[(p$95$m / (-x)), $MachinePrecision], If[LessEqual[t$95$0, 0.5], N[Sqrt[0.5], $MachinePrecision], 1.0]]]
\begin{array}{l}
p_m = \left|p\right|
\\
\begin{array}{l}
t_0 := \frac{x}{\sqrt{p\_m \cdot \left(4 \cdot p\_m\right) + x \cdot x}}\\
\mathbf{if}\;t\_0 \leq -1:\\
\;\;\;\;\frac{p\_m}{-x}\\
\mathbf{elif}\;t\_0 \leq 0.5:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < -1Initial program 9.5%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f649.5
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f649.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f649.5
Applied rewrites9.5%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6451.9
Applied rewrites51.9%
if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < 0.5Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites97.6%
if 0.5 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in x around inf
Applied rewrites100.0%
Final simplification89.7%
p_m = (fabs.f64 p) (FPCore (p_m x) :precision binary64 (if (<= (/ x (sqrt (+ (* p_m (* 4.0 p_m)) (* x x)))) 0.46) (sqrt 0.5) 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)))) <= 0.46) {
tmp = sqrt(0.5);
} 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 / sqrt(((p_m * (4.0d0 * p_m)) + (x * x)))) <= 0.46d0) then
tmp = sqrt(0.5d0)
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 / Math.sqrt(((p_m * (4.0 * p_m)) + (x * x)))) <= 0.46) {
tmp = Math.sqrt(0.5);
} else {
tmp = 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)))) <= 0.46: tmp = math.sqrt(0.5) else: tmp = 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)))) <= 0.46) tmp = sqrt(0.5); else tmp = 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)))) <= 0.46) tmp = sqrt(0.5); else tmp = 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], 0.46], N[Sqrt[0.5], $MachinePrecision], 1.0]
\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.46:\\
\;\;\;\;\sqrt{0.5}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) < 0.46000000000000002Initial program 76.3%
Taylor expanded in x around 0
Applied rewrites73.3%
if 0.46000000000000002 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 #s(literal 4 binary64) p) p) (*.f64 x x)))) Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in x around inf
Applied rewrites100.0%
Final simplification80.9%
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 83.0%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
metadata-evalN/A
lower-fma.f6483.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6483.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f6483.0
Applied rewrites83.0%
Taylor expanded in x around inf
Applied rewrites40.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 2024233
(FPCore (p x)
:name "Given's Rotation SVD example"
:precision binary64
:pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))
:alt
(! :herbie-platform default (sqrt (+ 1/2 (/ (copysign 1/2 x) (hypot 1 (/ (* 2 p) x))))))
(sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))