
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{a \cdot a - b \cdot b}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{a \cdot a - b \cdot b}
\end{array}
(FPCore (a b) :precision binary64 (let* ((t_0 (/ b (/ a b)))) (if (<= a -1e-248) (fma 0.5 t_0 (- a)) (+ a (* t_0 -0.5)))))
double code(double a, double b) {
double t_0 = b / (a / b);
double tmp;
if (a <= -1e-248) {
tmp = fma(0.5, t_0, -a);
} else {
tmp = a + (t_0 * -0.5);
}
return tmp;
}
function code(a, b) t_0 = Float64(b / Float64(a / b)) tmp = 0.0 if (a <= -1e-248) tmp = fma(0.5, t_0, Float64(-a)); else tmp = Float64(a + Float64(t_0 * -0.5)); end return tmp end
code[a_, b_] := Block[{t$95$0 = N[(b / N[(a / b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[a, -1e-248], N[(0.5 * t$95$0 + (-a)), $MachinePrecision], N[(a + N[(t$95$0 * -0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{b}{\frac{a}{b}}\\
\mathbf{if}\;a \leq -1 \cdot 10^{-248}:\\
\;\;\;\;\mathsf{fma}\left(0.5, t_0, -a\right)\\
\mathbf{else}:\\
\;\;\;\;a + t_0 \cdot -0.5\\
\end{array}
\end{array}
if a < -9.9999999999999998e-249Initial program 57.3%
difference-of-squares57.4%
Simplified57.4%
add-cube-cbrt56.3%
associate-*l*56.3%
pow1/353.8%
pow1/253.8%
difference-of-squares53.7%
pow-pow53.7%
fma-neg53.8%
metadata-eval53.8%
cbrt-prod53.8%
add-sqr-sqrt53.8%
difference-of-squares53.6%
fma-neg53.7%
Applied egg-rr53.7%
Taylor expanded in a around -inf 97.7%
fma-def97.7%
unpow297.7%
associate-*l/100.0%
associate-/r/100.0%
neg-mul-1100.0%
Simplified100.0%
if -9.9999999999999998e-249 < a Initial program 46.6%
difference-of-squares47.3%
Simplified47.3%
add-cube-cbrt46.5%
associate-*l*46.5%
pow1/344.6%
pow1/244.6%
difference-of-squares43.8%
pow-pow43.8%
fma-neg44.6%
metadata-eval44.6%
cbrt-prod44.6%
add-sqr-sqrt44.6%
difference-of-squares43.8%
fma-neg43.8%
Applied egg-rr43.8%
Taylor expanded in a around inf 90.6%
unpow290.6%
associate-*l/99.5%
associate-/r/99.5%
Simplified99.5%
Final simplification99.7%
(FPCore (a b) :precision binary64 (if (<= a -1e-248) (- a) (+ a (* (/ b (/ a b)) -0.5))))
double code(double a, double b) {
double tmp;
if (a <= -1e-248) {
tmp = -a;
} else {
tmp = a + ((b / (a / b)) * -0.5);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-1d-248)) then
tmp = -a
else
tmp = a + ((b / (a / b)) * (-0.5d0))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -1e-248) {
tmp = -a;
} else {
tmp = a + ((b / (a / b)) * -0.5);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -1e-248: tmp = -a else: tmp = a + ((b / (a / b)) * -0.5) return tmp
function code(a, b) tmp = 0.0 if (a <= -1e-248) tmp = Float64(-a); else tmp = Float64(a + Float64(Float64(b / Float64(a / b)) * -0.5)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -1e-248) tmp = -a; else tmp = a + ((b / (a / b)) * -0.5); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -1e-248], (-a), N[(a + N[(N[(b / N[(a / b), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1 \cdot 10^{-248}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a + \frac{b}{\frac{a}{b}} \cdot -0.5\\
\end{array}
\end{array}
if a < -9.9999999999999998e-249Initial program 57.3%
difference-of-squares57.4%
Simplified57.4%
add-cube-cbrt56.3%
associate-*l*56.3%
pow1/353.8%
pow1/253.8%
difference-of-squares53.7%
pow-pow53.7%
fma-neg53.8%
metadata-eval53.8%
cbrt-prod53.8%
add-sqr-sqrt53.8%
difference-of-squares53.6%
fma-neg53.7%
Applied egg-rr53.7%
Taylor expanded in a around -inf 99.2%
neg-mul-199.2%
Simplified99.2%
if -9.9999999999999998e-249 < a Initial program 46.6%
difference-of-squares47.3%
Simplified47.3%
add-cube-cbrt46.5%
associate-*l*46.5%
pow1/344.6%
pow1/244.6%
difference-of-squares43.8%
pow-pow43.8%
fma-neg44.6%
metadata-eval44.6%
cbrt-prod44.6%
add-sqr-sqrt44.6%
difference-of-squares43.8%
fma-neg43.8%
Applied egg-rr43.8%
Taylor expanded in a around inf 90.6%
unpow290.6%
associate-*l/99.5%
associate-/r/99.5%
Simplified99.5%
Final simplification99.3%
(FPCore (a b) :precision binary64 (if (<= a -1e-248) (- a) a))
double code(double a, double b) {
double tmp;
if (a <= -1e-248) {
tmp = -a;
} else {
tmp = a;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-1d-248)) then
tmp = -a
else
tmp = a
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -1e-248) {
tmp = -a;
} else {
tmp = a;
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -1e-248: tmp = -a else: tmp = a return tmp
function code(a, b) tmp = 0.0 if (a <= -1e-248) tmp = Float64(-a); else tmp = a; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -1e-248) tmp = -a; else tmp = a; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -1e-248], (-a), a]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1 \cdot 10^{-248}:\\
\;\;\;\;-a\\
\mathbf{else}:\\
\;\;\;\;a\\
\end{array}
\end{array}
if a < -9.9999999999999998e-249Initial program 57.3%
difference-of-squares57.4%
Simplified57.4%
add-cube-cbrt56.3%
associate-*l*56.3%
pow1/353.8%
pow1/253.8%
difference-of-squares53.7%
pow-pow53.7%
fma-neg53.8%
metadata-eval53.8%
cbrt-prod53.8%
add-sqr-sqrt53.8%
difference-of-squares53.6%
fma-neg53.7%
Applied egg-rr53.7%
Taylor expanded in a around -inf 99.2%
neg-mul-199.2%
Simplified99.2%
if -9.9999999999999998e-249 < a Initial program 46.6%
difference-of-squares47.3%
Simplified47.3%
Taylor expanded in a around inf 98.5%
Final simplification98.9%
(FPCore (a b) :precision binary64 a)
double code(double a, double b) {
return a;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = a
end function
public static double code(double a, double b) {
return a;
}
def code(a, b): return a
function code(a, b) return a end
function tmp = code(a, b) tmp = a; end
code[a_, b_] := a
\begin{array}{l}
\\
a
\end{array}
Initial program 52.0%
difference-of-squares52.4%
Simplified52.4%
Taylor expanded in a around inf 49.4%
Final simplification49.4%
(FPCore (a b) :precision binary64 (* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b)))))
double code(double a, double b) {
return sqrt((fabs(a) + fabs(b))) * sqrt((fabs(a) - fabs(b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt((abs(a) + abs(b))) * sqrt((abs(a) - abs(b)))
end function
public static double code(double a, double b) {
return Math.sqrt((Math.abs(a) + Math.abs(b))) * Math.sqrt((Math.abs(a) - Math.abs(b)));
}
def code(a, b): return math.sqrt((math.fabs(a) + math.fabs(b))) * math.sqrt((math.fabs(a) - math.fabs(b)))
function code(a, b) return Float64(sqrt(Float64(abs(a) + abs(b))) * sqrt(Float64(abs(a) - abs(b)))) end
function tmp = code(a, b) tmp = sqrt((abs(a) + abs(b))) * sqrt((abs(a) - abs(b))); end
code[a_, b_] := N[(N[Sqrt[N[(N[Abs[a], $MachinePrecision] + N[Abs[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(N[Abs[a], $MachinePrecision] - N[Abs[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|a\right| + \left|b\right|} \cdot \sqrt{\left|a\right| - \left|b\right|}
\end{array}
herbie shell --seed 2023178
(FPCore (a b)
:name "bug366, discussion (missed optimization)"
:precision binary64
:herbie-target
(* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b))))
(sqrt (- (* a a) (* b b))))