
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= (* x_m x_m) 2e+231) (sqrt (+ (* x_m x_m) y)) (fma y (/ 0.5 x_m) x_m)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if ((x_m * x_m) <= 2e+231) {
tmp = sqrt(((x_m * x_m) + y));
} else {
tmp = fma(y, (0.5 / x_m), x_m);
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (Float64(x_m * x_m) <= 2e+231) tmp = sqrt(Float64(Float64(x_m * x_m) + y)); else tmp = fma(y, Float64(0.5 / x_m), x_m); end return tmp end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 2e+231], N[Sqrt[N[(N[(x$95$m * x$95$m), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(y * N[(0.5 / x$95$m), $MachinePrecision] + x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 2 \cdot 10^{+231}:\\
\;\;\;\;\sqrt{x\_m \cdot x\_m + y}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, \frac{0.5}{x\_m}, x\_m\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 2.0000000000000001e231Initial program 100.0%
if 2.0000000000000001e231 < (*.f64 x x) Initial program 25.7%
Taylor expanded in x around inf
distribute-rgt-inN/A
*-lft-identityN/A
+-commutativeN/A
associate-*r/N/A
associate-*l/N/A
unpow2N/A
times-fracN/A
*-inversesN/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6454.5
Applied rewrites54.5%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= (* x_m x_m) 2e+231) (sqrt (fma x_m x_m y)) (fma y (/ 0.5 x_m) x_m)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if ((x_m * x_m) <= 2e+231) {
tmp = sqrt(fma(x_m, x_m, y));
} else {
tmp = fma(y, (0.5 / x_m), x_m);
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (Float64(x_m * x_m) <= 2e+231) tmp = sqrt(fma(x_m, x_m, y)); else tmp = fma(y, Float64(0.5 / x_m), x_m); end return tmp end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 2e+231], N[Sqrt[N[(x$95$m * x$95$m + y), $MachinePrecision]], $MachinePrecision], N[(y * N[(0.5 / x$95$m), $MachinePrecision] + x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 2 \cdot 10^{+231}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x\_m, x\_m, y\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, \frac{0.5}{x\_m}, x\_m\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 2.0000000000000001e231Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
if 2.0000000000000001e231 < (*.f64 x x) Initial program 25.7%
Taylor expanded in x around inf
distribute-rgt-inN/A
*-lft-identityN/A
+-commutativeN/A
associate-*r/N/A
associate-*l/N/A
unpow2N/A
times-fracN/A
*-inversesN/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6454.5
Applied rewrites54.5%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= y 2.5e-144) (sqrt (* x_m x_m)) (sqrt y)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (y <= 2.5e-144) {
tmp = sqrt((x_m * x_m));
} else {
tmp = sqrt(y);
}
return tmp;
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 2.5d-144) then
tmp = sqrt((x_m * x_m))
else
tmp = sqrt(y)
end if
code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
double tmp;
if (y <= 2.5e-144) {
tmp = Math.sqrt((x_m * x_m));
} else {
tmp = Math.sqrt(y);
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y): tmp = 0 if y <= 2.5e-144: tmp = math.sqrt((x_m * x_m)) else: tmp = math.sqrt(y) return tmp
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (y <= 2.5e-144) tmp = sqrt(Float64(x_m * x_m)); else tmp = sqrt(y); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y) tmp = 0.0; if (y <= 2.5e-144) tmp = sqrt((x_m * x_m)); else tmp = sqrt(y); end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[y, 2.5e-144], N[Sqrt[N[(x$95$m * x$95$m), $MachinePrecision]], $MachinePrecision], N[Sqrt[y], $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.5 \cdot 10^{-144}:\\
\;\;\;\;\sqrt{x\_m \cdot x\_m}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{y}\\
\end{array}
\end{array}
if y < 2.4999999999999999e-144Initial program 61.6%
Taylor expanded in x around inf
unpow2N/A
lower-*.f6453.8
Applied rewrites53.8%
if 2.4999999999999999e-144 < y Initial program 79.7%
Taylor expanded in x around 0
lower-sqrt.f6465.9
Applied rewrites65.9%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (sqrt (fma x_m x_m y)))
x_m = fabs(x);
double code(double x_m, double y) {
return sqrt(fma(x_m, x_m, y));
}
x_m = abs(x) function code(x_m, y) return sqrt(fma(x_m, x_m, y)) end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := N[Sqrt[N[(x$95$m * x$95$m + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
\sqrt{\mathsf{fma}\left(x\_m, x\_m, y\right)}
\end{array}
Initial program 70.1%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6470.1
Applied rewrites70.1%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (sqrt y))
x_m = fabs(x);
double code(double x_m, double y) {
return sqrt(y);
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = sqrt(y)
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return Math.sqrt(y);
}
x_m = math.fabs(x) def code(x_m, y): return math.sqrt(y)
x_m = abs(x) function code(x_m, y) return sqrt(y) end
x_m = abs(x); function tmp = code(x_m, y) tmp = sqrt(y); end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := N[Sqrt[y], $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
\sqrt{y}
\end{array}
Initial program 70.1%
Taylor expanded in x around 0
lower-sqrt.f6436.0
Applied rewrites36.0%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (- x_m))
x_m = fabs(x);
double code(double x_m, double y) {
return -x_m;
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = -x_m
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return -x_m;
}
x_m = math.fabs(x) def code(x_m, y): return -x_m
x_m = abs(x) function code(x_m, y) return Float64(-x_m) end
x_m = abs(x); function tmp = code(x_m, y) tmp = -x_m; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := (-x$95$m)
\begin{array}{l}
x_m = \left|x\right|
\\
-x\_m
\end{array}
Initial program 70.1%
Taylor expanded in x around -inf
mul-1-negN/A
lower-neg.f6433.8
Applied rewrites33.8%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (* 0.5 (/ y x)) x)))
(if (< x -1.5097698010472593e+153)
(- t_0)
(if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) t_0))))
double code(double x, double y) {
double t_0 = (0.5 * (y / x)) + x;
double tmp;
if (x < -1.5097698010472593e+153) {
tmp = -t_0;
} else if (x < 5.582399551122541e+57) {
tmp = sqrt(((x * x) + y));
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = (0.5d0 * (y / x)) + x
if (x < (-1.5097698010472593d+153)) then
tmp = -t_0
else if (x < 5.582399551122541d+57) then
tmp = sqrt(((x * x) + y))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (0.5 * (y / x)) + x;
double tmp;
if (x < -1.5097698010472593e+153) {
tmp = -t_0;
} else if (x < 5.582399551122541e+57) {
tmp = Math.sqrt(((x * x) + y));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (0.5 * (y / x)) + x tmp = 0 if x < -1.5097698010472593e+153: tmp = -t_0 elif x < 5.582399551122541e+57: tmp = math.sqrt(((x * x) + y)) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(0.5 * Float64(y / x)) + x) tmp = 0.0 if (x < -1.5097698010472593e+153) tmp = Float64(-t_0); elseif (x < 5.582399551122541e+57) tmp = sqrt(Float64(Float64(x * x) + y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (0.5 * (y / x)) + x; tmp = 0.0; if (x < -1.5097698010472593e+153) tmp = -t_0; elseif (x < 5.582399551122541e+57) tmp = sqrt(((x * x) + y)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]}, If[Less[x, -1.5097698010472593e+153], (-t$95$0), If[Less[x, 5.582399551122541e+57], N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \frac{y}{x} + x\\
\mathbf{if}\;x < -1.5097698010472593 \cdot 10^{+153}:\\
\;\;\;\;-t\_0\\
\mathbf{elif}\;x < 5.582399551122541 \cdot 10^{+57}:\\
\;\;\;\;\sqrt{x \cdot x + y}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
herbie shell --seed 2024233
(FPCore (x y)
:name "Linear.Quaternion:$clog from linear-1.19.1.3"
:precision binary64
:alt
(! :herbie-platform default (if (< x -1509769801047259300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- (+ (* 1/2 (/ y x)) x)) (if (< x 5582399551122541000000000000000000000000000000000000000000) (sqrt (+ (* x x) y)) (+ (* 1/2 (/ y x)) x))))
(sqrt (+ (* x x) y)))