
(FPCore (x) :precision binary64 (- (sqrt (+ 1.0 x)) (sqrt (- 1.0 x))))
double code(double x) {
return sqrt((1.0 + x)) - sqrt((1.0 - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((1.0d0 + x)) - sqrt((1.0d0 - x))
end function
public static double code(double x) {
return Math.sqrt((1.0 + x)) - Math.sqrt((1.0 - x));
}
def code(x): return math.sqrt((1.0 + x)) - math.sqrt((1.0 - x))
function code(x) return Float64(sqrt(Float64(1.0 + x)) - sqrt(Float64(1.0 - x))) end
function tmp = code(x) tmp = sqrt((1.0 + x)) - sqrt((1.0 - x)); end
code[x_] := N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{1 + x} - \sqrt{1 - x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (sqrt (+ 1.0 x)) (sqrt (- 1.0 x))))
double code(double x) {
return sqrt((1.0 + x)) - sqrt((1.0 - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((1.0d0 + x)) - sqrt((1.0d0 - x))
end function
public static double code(double x) {
return Math.sqrt((1.0 + x)) - Math.sqrt((1.0 - x));
}
def code(x): return math.sqrt((1.0 + x)) - math.sqrt((1.0 - x))
function code(x) return Float64(sqrt(Float64(1.0 + x)) - sqrt(Float64(1.0 - x))) end
function tmp = code(x) tmp = sqrt((1.0 + x)) - sqrt((1.0 - x)); end
code[x_] := N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{1 + x} - \sqrt{1 - x}
\end{array}
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
(FPCore (x_s x_m)
:precision binary64
(*
x_s
(if (<= x_m 6e-123)
0.0
(+ x_m (+ (* 0.0546875 (pow x_m 5.0)) (* 0.125 (pow x_m 3.0)))))))x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = x_m + ((0.0546875 * pow(x_m, 5.0)) + (0.125 * pow(x_m, 3.0)));
}
return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
real(8), intent (in) :: x_s
real(8), intent (in) :: x_m
real(8) :: tmp
if (x_m <= 6d-123) then
tmp = 0.0d0
else
tmp = x_m + ((0.0546875d0 * (x_m ** 5.0d0)) + (0.125d0 * (x_m ** 3.0d0)))
end if
code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = x_m + ((0.0546875 * Math.pow(x_m, 5.0)) + (0.125 * Math.pow(x_m, 3.0)));
}
return x_s * tmp;
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): tmp = 0 if x_m <= 6e-123: tmp = 0.0 else: tmp = x_m + ((0.0546875 * math.pow(x_m, 5.0)) + (0.125 * math.pow(x_m, 3.0))) return x_s * tmp
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 6e-123) tmp = 0.0; else tmp = Float64(x_m + Float64(Float64(0.0546875 * (x_m ^ 5.0)) + Float64(0.125 * (x_m ^ 3.0)))); end return Float64(x_s * tmp) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp_2 = code(x_s, x_m) tmp = 0.0; if (x_m <= 6e-123) tmp = 0.0; else tmp = x_m + ((0.0546875 * (x_m ^ 5.0)) + (0.125 * (x_m ^ 3.0))); end tmp_2 = x_s * tmp; end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 6e-123], 0.0, N[(x$95$m + N[(N[(0.0546875 * N[Power[x$95$m, 5.0], $MachinePrecision]), $MachinePrecision] + N[(0.125 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 6 \cdot 10^{-123}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;x\_m + \left(0.0546875 \cdot {x\_m}^{5} + 0.125 \cdot {x\_m}^{3}\right)\\
\end{array}
\end{array}
if x < 5.99999999999999968e-123Initial program 80.6%
Taylor expanded in x around inf 78.3%
if 5.99999999999999968e-123 < x Initial program 9.5%
Taylor expanded in x around 0 99.5%
Final simplification82.6%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m) :precision binary64 (* x_s (if (<= x_m 6e-123) 0.0 (* 2.0 (/ x_m (fma (pow x_m 2.0) -0.25 2.0))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = 2.0 * (x_m / fma(pow(x_m, 2.0), -0.25, 2.0));
}
return x_s * tmp;
}
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 6e-123) tmp = 0.0; else tmp = Float64(2.0 * Float64(x_m / fma((x_m ^ 2.0), -0.25, 2.0))); end return Float64(x_s * tmp) end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 6e-123], 0.0, N[(2.0 * N[(x$95$m / N[(N[Power[x$95$m, 2.0], $MachinePrecision] * -0.25 + 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 6 \cdot 10^{-123}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \frac{x\_m}{\mathsf{fma}\left({x\_m}^{2}, -0.25, 2\right)}\\
\end{array}
\end{array}
if x < 5.99999999999999968e-123Initial program 80.6%
Taylor expanded in x around inf 78.3%
if 5.99999999999999968e-123 < x Initial program 9.5%
flip--9.4%
div-inv9.4%
add-sqr-sqrt9.5%
add-sqr-sqrt9.6%
associate--r-22.9%
add-exp-log22.9%
log1p-udef22.9%
expm1-udef100.0%
expm1-log1p-u100.0%
Applied egg-rr100.0%
associate-*r/100.0%
*-rgt-identity100.0%
count-2100.0%
associate-/l*99.9%
remove-double-neg99.9%
sub-neg99.9%
sub-neg99.9%
remove-double-neg99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in x around 0 99.0%
*-commutative99.0%
Simplified99.0%
clear-num99.0%
associate-/r/99.0%
clear-num99.2%
+-commutative99.2%
fma-def99.2%
Applied egg-rr99.2%
Final simplification82.5%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m) :precision binary64 (* x_s (if (<= x_m 6e-123) 0.0 (+ x_m (* 0.125 (pow x_m 3.0))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = x_m + (0.125 * pow(x_m, 3.0));
}
return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
real(8), intent (in) :: x_s
real(8), intent (in) :: x_m
real(8) :: tmp
if (x_m <= 6d-123) then
tmp = 0.0d0
else
tmp = x_m + (0.125d0 * (x_m ** 3.0d0))
end if
code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = x_m + (0.125 * Math.pow(x_m, 3.0));
}
return x_s * tmp;
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): tmp = 0 if x_m <= 6e-123: tmp = 0.0 else: tmp = x_m + (0.125 * math.pow(x_m, 3.0)) return x_s * tmp
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 6e-123) tmp = 0.0; else tmp = Float64(x_m + Float64(0.125 * (x_m ^ 3.0))); end return Float64(x_s * tmp) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp_2 = code(x_s, x_m) tmp = 0.0; if (x_m <= 6e-123) tmp = 0.0; else tmp = x_m + (0.125 * (x_m ^ 3.0)); end tmp_2 = x_s * tmp; end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 6e-123], 0.0, N[(x$95$m + N[(0.125 * N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 6 \cdot 10^{-123}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;x\_m + 0.125 \cdot {x\_m}^{3}\\
\end{array}
\end{array}
if x < 5.99999999999999968e-123Initial program 80.6%
Taylor expanded in x around inf 78.3%
if 5.99999999999999968e-123 < x Initial program 9.5%
Taylor expanded in x around 0 99.2%
Final simplification82.5%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m) :precision binary64 (* x_s (if (<= x_m 6e-123) 0.0 (/ 2.0 (+ (* x_m -0.25) (* 2.0 (/ 1.0 x_m)))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = 2.0 / ((x_m * -0.25) + (2.0 * (1.0 / x_m)));
}
return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
real(8), intent (in) :: x_s
real(8), intent (in) :: x_m
real(8) :: tmp
if (x_m <= 6d-123) then
tmp = 0.0d0
else
tmp = 2.0d0 / ((x_m * (-0.25d0)) + (2.0d0 * (1.0d0 / x_m)))
end if
code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = 2.0 / ((x_m * -0.25) + (2.0 * (1.0 / x_m)));
}
return x_s * tmp;
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): tmp = 0 if x_m <= 6e-123: tmp = 0.0 else: tmp = 2.0 / ((x_m * -0.25) + (2.0 * (1.0 / x_m))) return x_s * tmp
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 6e-123) tmp = 0.0; else tmp = Float64(2.0 / Float64(Float64(x_m * -0.25) + Float64(2.0 * Float64(1.0 / x_m)))); end return Float64(x_s * tmp) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp_2 = code(x_s, x_m) tmp = 0.0; if (x_m <= 6e-123) tmp = 0.0; else tmp = 2.0 / ((x_m * -0.25) + (2.0 * (1.0 / x_m))); end tmp_2 = x_s * tmp; end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 6e-123], 0.0, N[(2.0 / N[(N[(x$95$m * -0.25), $MachinePrecision] + N[(2.0 * N[(1.0 / x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 6 \cdot 10^{-123}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{x\_m \cdot -0.25 + 2 \cdot \frac{1}{x\_m}}\\
\end{array}
\end{array}
if x < 5.99999999999999968e-123Initial program 80.6%
Taylor expanded in x around inf 78.3%
if 5.99999999999999968e-123 < x Initial program 9.5%
flip--9.4%
div-inv9.4%
add-sqr-sqrt9.5%
add-sqr-sqrt9.6%
associate--r-22.9%
add-exp-log22.9%
log1p-udef22.9%
expm1-udef100.0%
expm1-log1p-u100.0%
Applied egg-rr100.0%
associate-*r/100.0%
*-rgt-identity100.0%
count-2100.0%
associate-/l*99.9%
remove-double-neg99.9%
sub-neg99.9%
sub-neg99.9%
remove-double-neg99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in x around 0 99.0%
*-commutative99.0%
Simplified99.0%
Taylor expanded in x around 0 99.1%
Final simplification82.5%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m) :precision binary64 (* x_s (if (<= x_m 6e-123) 0.0 x_m)))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = x_m;
}
return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
real(8), intent (in) :: x_s
real(8), intent (in) :: x_m
real(8) :: tmp
if (x_m <= 6d-123) then
tmp = 0.0d0
else
tmp = x_m
end if
code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
double tmp;
if (x_m <= 6e-123) {
tmp = 0.0;
} else {
tmp = x_m;
}
return x_s * tmp;
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): tmp = 0 if x_m <= 6e-123: tmp = 0.0 else: tmp = x_m return x_s * tmp
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) tmp = 0.0 if (x_m <= 6e-123) tmp = 0.0; else tmp = x_m; end return Float64(x_s * tmp) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp_2 = code(x_s, x_m) tmp = 0.0; if (x_m <= 6e-123) tmp = 0.0; else tmp = x_m; end tmp_2 = x_s * tmp; end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * If[LessEqual[x$95$m, 6e-123], 0.0, x$95$m]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 6 \cdot 10^{-123}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;x\_m\\
\end{array}
\end{array}
if x < 5.99999999999999968e-123Initial program 80.6%
Taylor expanded in x around inf 78.3%
if 5.99999999999999968e-123 < x Initial program 9.5%
Taylor expanded in x around 0 98.7%
Final simplification82.4%
x_m = (fabs.f64 x) x_s = (copysign.f64 1 x) (FPCore (x_s x_m) :precision binary64 (* x_s 0.0))
x_m = fabs(x);
x_s = copysign(1.0, x);
double code(double x_s, double x_m) {
return x_s * 0.0;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
real(8) function code(x_s, x_m)
real(8), intent (in) :: x_s
real(8), intent (in) :: x_m
code = x_s * 0.0d0
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
public static double code(double x_s, double x_m) {
return x_s * 0.0;
}
x_m = math.fabs(x) x_s = math.copysign(1.0, x) def code(x_s, x_m): return x_s * 0.0
x_m = abs(x) x_s = copysign(1.0, x) function code(x_s, x_m) return Float64(x_s * 0.0) end
x_m = abs(x); x_s = sign(x) * abs(1.0); function tmp = code(x_s, x_m) tmp = x_s * 0.0; end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_] := N[(x$95$s * 0.0), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot 0
\end{array}
Initial program 66.1%
Taylor expanded in x around inf 63.1%
Final simplification63.1%
(FPCore (x) :precision binary64 (/ (* 2.0 x) (+ (sqrt (+ 1.0 x)) (sqrt (- 1.0 x)))))
double code(double x) {
return (2.0 * x) / (sqrt((1.0 + x)) + sqrt((1.0 - x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (2.0d0 * x) / (sqrt((1.0d0 + x)) + sqrt((1.0d0 - x)))
end function
public static double code(double x) {
return (2.0 * x) / (Math.sqrt((1.0 + x)) + Math.sqrt((1.0 - x)));
}
def code(x): return (2.0 * x) / (math.sqrt((1.0 + x)) + math.sqrt((1.0 - x)))
function code(x) return Float64(Float64(2.0 * x) / Float64(sqrt(Float64(1.0 + x)) + sqrt(Float64(1.0 - x)))) end
function tmp = code(x) tmp = (2.0 * x) / (sqrt((1.0 + x)) + sqrt((1.0 - x))); end
code[x_] := N[(N[(2.0 * x), $MachinePrecision] / N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] + N[Sqrt[N[(1.0 - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2 \cdot x}{\sqrt{1 + x} + \sqrt{1 - x}}
\end{array}
herbie shell --seed 2024031
(FPCore (x)
:name "bug333 (missed optimization)"
:precision binary64
:pre (and (<= -1.0 x) (<= x 1.0))
:herbie-target
(/ (* 2.0 x) (+ (sqrt (+ 1.0 x)) (sqrt (- 1.0 x))))
(- (sqrt (+ 1.0 x)) (sqrt (- 1.0 x))))