
(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 5 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+240) (sqrt (fma x_m x_m y)) (fma (/ 0.5 x_m) y x_m)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if ((x_m * x_m) <= 2e+240) {
tmp = sqrt(fma(x_m, x_m, y));
} else {
tmp = fma((0.5 / x_m), y, x_m);
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (Float64(x_m * x_m) <= 2e+240) tmp = sqrt(fma(x_m, x_m, y)); else tmp = fma(Float64(0.5 / x_m), y, 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+240], N[Sqrt[N[(x$95$m * x$95$m + y), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 / x$95$m), $MachinePrecision] * y + 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^{+240}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x\_m, x\_m, y\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{0.5}{x\_m}, y, x\_m\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 2.00000000000000003e240Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
if 2.00000000000000003e240 < (*.f64 x x) Initial program 25.5%
Taylor expanded in y around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6456.2
Applied rewrites56.2%
x_m = (fabs.f64 x)
(FPCore (x_m y)
:precision binary64
(let* ((t_0 (sqrt (* x_m x_m))))
(if (<= y 2.4e-147)
t_0
(if (<= y 3.6e-119) (sqrt y) (if (<= y 2.8e-22) t_0 (sqrt y))))))x_m = fabs(x);
double code(double x_m, double y) {
double t_0 = sqrt((x_m * x_m));
double tmp;
if (y <= 2.4e-147) {
tmp = t_0;
} else if (y <= 3.6e-119) {
tmp = sqrt(y);
} else if (y <= 2.8e-22) {
tmp = t_0;
} 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) :: t_0
real(8) :: tmp
t_0 = sqrt((x_m * x_m))
if (y <= 2.4d-147) then
tmp = t_0
else if (y <= 3.6d-119) then
tmp = sqrt(y)
else if (y <= 2.8d-22) then
tmp = t_0
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 t_0 = Math.sqrt((x_m * x_m));
double tmp;
if (y <= 2.4e-147) {
tmp = t_0;
} else if (y <= 3.6e-119) {
tmp = Math.sqrt(y);
} else if (y <= 2.8e-22) {
tmp = t_0;
} else {
tmp = Math.sqrt(y);
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y): t_0 = math.sqrt((x_m * x_m)) tmp = 0 if y <= 2.4e-147: tmp = t_0 elif y <= 3.6e-119: tmp = math.sqrt(y) elif y <= 2.8e-22: tmp = t_0 else: tmp = math.sqrt(y) return tmp
x_m = abs(x) function code(x_m, y) t_0 = sqrt(Float64(x_m * x_m)) tmp = 0.0 if (y <= 2.4e-147) tmp = t_0; elseif (y <= 3.6e-119) tmp = sqrt(y); elseif (y <= 2.8e-22) tmp = t_0; else tmp = sqrt(y); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y) t_0 = sqrt((x_m * x_m)); tmp = 0.0; if (y <= 2.4e-147) tmp = t_0; elseif (y <= 3.6e-119) tmp = sqrt(y); elseif (y <= 2.8e-22) tmp = t_0; else tmp = sqrt(y); end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := Block[{t$95$0 = N[Sqrt[N[(x$95$m * x$95$m), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[y, 2.4e-147], t$95$0, If[LessEqual[y, 3.6e-119], N[Sqrt[y], $MachinePrecision], If[LessEqual[y, 2.8e-22], t$95$0, N[Sqrt[y], $MachinePrecision]]]]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := \sqrt{x\_m \cdot x\_m}\\
\mathbf{if}\;y \leq 2.4 \cdot 10^{-147}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 3.6 \cdot 10^{-119}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{elif}\;y \leq 2.8 \cdot 10^{-22}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{y}\\
\end{array}
\end{array}
if y < 2.39999999999999998e-147 or 3.6e-119 < y < 2.79999999999999995e-22Initial program 62.0%
Taylor expanded in y around 0
unpow2N/A
lower-*.f6453.4
Applied rewrites53.4%
if 2.39999999999999998e-147 < y < 3.6e-119 or 2.79999999999999995e-22 < y Initial program 82.0%
Taylor expanded in y around inf
lower-sqrt.f6475.2
Applied rewrites75.2%
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.9%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6470.9
Applied rewrites70.9%
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.9%
Taylor expanded in y around inf
lower-sqrt.f6438.7
Applied rewrites38.7%
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.9%
Taylor expanded in x around -inf
mul-1-negN/A
lower-neg.f6430.6
Applied rewrites30.6%
(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 2024257
(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)))