
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
x_m = (fabs.f64 x) y_m = (fabs.f64 y) (FPCore (x_m y_m) :precision binary64 (if (<= (* x_m x_m) 5e+252) (fma x_m x_m (* y_m (- y_m))) (* x_m (+ x_m (* y_m -2.0)))))
x_m = fabs(x);
y_m = fabs(y);
double code(double x_m, double y_m) {
double tmp;
if ((x_m * x_m) <= 5e+252) {
tmp = fma(x_m, x_m, (y_m * -y_m));
} else {
tmp = x_m * (x_m + (y_m * -2.0));
}
return tmp;
}
x_m = abs(x) y_m = abs(y) function code(x_m, y_m) tmp = 0.0 if (Float64(x_m * x_m) <= 5e+252) tmp = fma(x_m, x_m, Float64(y_m * Float64(-y_m))); else tmp = Float64(x_m * Float64(x_m + Float64(y_m * -2.0))); end return tmp end
x_m = N[Abs[x], $MachinePrecision] y_m = N[Abs[y], $MachinePrecision] code[x$95$m_, y$95$m_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 5e+252], N[(x$95$m * x$95$m + N[(y$95$m * (-y$95$m)), $MachinePrecision]), $MachinePrecision], N[(x$95$m * N[(x$95$m + N[(y$95$m * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 5 \cdot 10^{+252}:\\
\;\;\;\;\mathsf{fma}\left(x\_m, x\_m, y\_m \cdot \left(-y\_m\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot \left(x\_m + y\_m \cdot -2\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 4.9999999999999997e252Initial program 100.0%
sqr-neg100.0%
cancel-sign-sub100.0%
fma-def100.0%
Simplified100.0%
if 4.9999999999999997e252 < (*.f64 x x) Initial program 76.4%
difference-of-squares100.0%
add-sqr-sqrt54.2%
sqrt-prod79.2%
sqr-neg79.2%
sqrt-unprod34.7%
add-sqr-sqrt83.3%
sub-neg83.3%
pow183.3%
pow183.3%
pow-prod-up83.3%
add-sqr-sqrt40.2%
add-sqr-sqrt25.0%
difference-of-squares25.0%
metadata-eval25.0%
unpow-prod-down25.0%
Applied egg-rr25.0%
unpow225.0%
unpow225.0%
unswap-sqr25.0%
difference-of-squares25.0%
unpow1/225.0%
unpow1/225.0%
pow-sqr25.0%
metadata-eval25.0%
unpow125.0%
unpow1/225.0%
unpow1/225.0%
pow-sqr25.0%
metadata-eval25.0%
unpow125.0%
difference-of-squares25.0%
unpow1/225.0%
unpow1/225.0%
pow-sqr48.6%
metadata-eval48.6%
unpow148.6%
Simplified83.3%
Taylor expanded in x around inf 72.2%
*-commutative72.2%
associate-*l*72.2%
unpow272.2%
distribute-lft-out88.9%
Simplified88.9%
Final simplification96.9%
x_m = (fabs.f64 x) y_m = (fabs.f64 y) (FPCore (x_m y_m) :precision binary64 (if (<= (* x_m x_m) 5e+252) (- (* x_m x_m) (* y_m y_m)) (* x_m (+ x_m (* y_m -2.0)))))
x_m = fabs(x);
y_m = fabs(y);
double code(double x_m, double y_m) {
double tmp;
if ((x_m * x_m) <= 5e+252) {
tmp = (x_m * x_m) - (y_m * y_m);
} else {
tmp = x_m * (x_m + (y_m * -2.0));
}
return tmp;
}
x_m = abs(x)
y_m = abs(y)
real(8) function code(x_m, y_m)
real(8), intent (in) :: x_m
real(8), intent (in) :: y_m
real(8) :: tmp
if ((x_m * x_m) <= 5d+252) then
tmp = (x_m * x_m) - (y_m * y_m)
else
tmp = x_m * (x_m + (y_m * (-2.0d0)))
end if
code = tmp
end function
x_m = Math.abs(x);
y_m = Math.abs(y);
public static double code(double x_m, double y_m) {
double tmp;
if ((x_m * x_m) <= 5e+252) {
tmp = (x_m * x_m) - (y_m * y_m);
} else {
tmp = x_m * (x_m + (y_m * -2.0));
}
return tmp;
}
x_m = math.fabs(x) y_m = math.fabs(y) def code(x_m, y_m): tmp = 0 if (x_m * x_m) <= 5e+252: tmp = (x_m * x_m) - (y_m * y_m) else: tmp = x_m * (x_m + (y_m * -2.0)) return tmp
x_m = abs(x) y_m = abs(y) function code(x_m, y_m) tmp = 0.0 if (Float64(x_m * x_m) <= 5e+252) tmp = Float64(Float64(x_m * x_m) - Float64(y_m * y_m)); else tmp = Float64(x_m * Float64(x_m + Float64(y_m * -2.0))); end return tmp end
x_m = abs(x); y_m = abs(y); function tmp_2 = code(x_m, y_m) tmp = 0.0; if ((x_m * x_m) <= 5e+252) tmp = (x_m * x_m) - (y_m * y_m); else tmp = x_m * (x_m + (y_m * -2.0)); end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] y_m = N[Abs[y], $MachinePrecision] code[x$95$m_, y$95$m_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], 5e+252], N[(N[(x$95$m * x$95$m), $MachinePrecision] - N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision], N[(x$95$m * N[(x$95$m + N[(y$95$m * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 5 \cdot 10^{+252}:\\
\;\;\;\;x\_m \cdot x\_m - y\_m \cdot y\_m\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot \left(x\_m + y\_m \cdot -2\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 4.9999999999999997e252Initial program 100.0%
if 4.9999999999999997e252 < (*.f64 x x) Initial program 76.4%
difference-of-squares100.0%
add-sqr-sqrt54.2%
sqrt-prod79.2%
sqr-neg79.2%
sqrt-unprod34.7%
add-sqr-sqrt83.3%
sub-neg83.3%
pow183.3%
pow183.3%
pow-prod-up83.3%
add-sqr-sqrt40.2%
add-sqr-sqrt25.0%
difference-of-squares25.0%
metadata-eval25.0%
unpow-prod-down25.0%
Applied egg-rr25.0%
unpow225.0%
unpow225.0%
unswap-sqr25.0%
difference-of-squares25.0%
unpow1/225.0%
unpow1/225.0%
pow-sqr25.0%
metadata-eval25.0%
unpow125.0%
unpow1/225.0%
unpow1/225.0%
pow-sqr25.0%
metadata-eval25.0%
unpow125.0%
difference-of-squares25.0%
unpow1/225.0%
unpow1/225.0%
pow-sqr48.6%
metadata-eval48.6%
unpow148.6%
Simplified83.3%
Taylor expanded in x around inf 72.2%
*-commutative72.2%
associate-*l*72.2%
unpow272.2%
distribute-lft-out88.9%
Simplified88.9%
Final simplification96.9%
x_m = (fabs.f64 x) y_m = (fabs.f64 y) (FPCore (x_m y_m) :precision binary64 (* x_m (+ x_m (* y_m -2.0))))
x_m = fabs(x);
y_m = fabs(y);
double code(double x_m, double y_m) {
return x_m * (x_m + (y_m * -2.0));
}
x_m = abs(x)
y_m = abs(y)
real(8) function code(x_m, y_m)
real(8), intent (in) :: x_m
real(8), intent (in) :: y_m
code = x_m * (x_m + (y_m * (-2.0d0)))
end function
x_m = Math.abs(x);
y_m = Math.abs(y);
public static double code(double x_m, double y_m) {
return x_m * (x_m + (y_m * -2.0));
}
x_m = math.fabs(x) y_m = math.fabs(y) def code(x_m, y_m): return x_m * (x_m + (y_m * -2.0))
x_m = abs(x) y_m = abs(y) function code(x_m, y_m) return Float64(x_m * Float64(x_m + Float64(y_m * -2.0))) end
x_m = abs(x); y_m = abs(y); function tmp = code(x_m, y_m) tmp = x_m * (x_m + (y_m * -2.0)); end
x_m = N[Abs[x], $MachinePrecision] y_m = N[Abs[y], $MachinePrecision] code[x$95$m_, y$95$m_] := N[(x$95$m * N[(x$95$m + N[(y$95$m * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
x\_m \cdot \left(x\_m + y\_m \cdot -2\right)
\end{array}
Initial program 93.4%
difference-of-squares100.0%
add-sqr-sqrt51.1%
sqrt-prod73.1%
sqr-neg73.1%
sqrt-unprod24.7%
add-sqr-sqrt52.2%
sub-neg52.2%
pow152.2%
pow152.2%
pow-prod-up52.2%
add-sqr-sqrt27.7%
add-sqr-sqrt15.2%
difference-of-squares15.2%
metadata-eval15.2%
unpow-prod-down15.2%
Applied egg-rr15.2%
unpow215.2%
unpow215.2%
unswap-sqr15.2%
difference-of-squares15.2%
unpow1/215.2%
unpow1/215.2%
pow-sqr15.2%
metadata-eval15.2%
unpow115.2%
unpow1/215.2%
unpow1/215.2%
pow-sqr15.2%
metadata-eval15.2%
unpow115.2%
difference-of-squares15.2%
unpow1/215.2%
unpow1/215.2%
pow-sqr27.5%
metadata-eval27.5%
unpow127.5%
Simplified52.2%
Taylor expanded in x around inf 50.7%
*-commutative50.7%
associate-*l*50.7%
unpow250.7%
distribute-lft-out55.4%
Simplified55.4%
Final simplification55.4%
x_m = (fabs.f64 x) y_m = (fabs.f64 y) (FPCore (x_m y_m) :precision binary64 (* y_m (* x_m -2.0)))
x_m = fabs(x);
y_m = fabs(y);
double code(double x_m, double y_m) {
return y_m * (x_m * -2.0);
}
x_m = abs(x)
y_m = abs(y)
real(8) function code(x_m, y_m)
real(8), intent (in) :: x_m
real(8), intent (in) :: y_m
code = y_m * (x_m * (-2.0d0))
end function
x_m = Math.abs(x);
y_m = Math.abs(y);
public static double code(double x_m, double y_m) {
return y_m * (x_m * -2.0);
}
x_m = math.fabs(x) y_m = math.fabs(y) def code(x_m, y_m): return y_m * (x_m * -2.0)
x_m = abs(x) y_m = abs(y) function code(x_m, y_m) return Float64(y_m * Float64(x_m * -2.0)) end
x_m = abs(x); y_m = abs(y); function tmp = code(x_m, y_m) tmp = y_m * (x_m * -2.0); end
x_m = N[Abs[x], $MachinePrecision] y_m = N[Abs[y], $MachinePrecision] code[x$95$m_, y$95$m_] := N[(y$95$m * N[(x$95$m * -2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
y\_m \cdot \left(x\_m \cdot -2\right)
\end{array}
Initial program 93.4%
difference-of-squares100.0%
add-sqr-sqrt51.1%
sqrt-prod73.1%
sqr-neg73.1%
sqrt-unprod24.7%
add-sqr-sqrt52.2%
sub-neg52.2%
pow152.2%
pow152.2%
pow-prod-up52.2%
add-sqr-sqrt27.7%
add-sqr-sqrt15.2%
difference-of-squares15.2%
metadata-eval15.2%
unpow-prod-down15.2%
Applied egg-rr15.2%
unpow215.2%
unpow215.2%
unswap-sqr15.2%
difference-of-squares15.2%
unpow1/215.2%
unpow1/215.2%
pow-sqr15.2%
metadata-eval15.2%
unpow115.2%
unpow1/215.2%
unpow1/215.2%
pow-sqr15.2%
metadata-eval15.2%
unpow115.2%
difference-of-squares15.2%
unpow1/215.2%
unpow1/215.2%
pow-sqr27.5%
metadata-eval27.5%
unpow127.5%
Simplified52.2%
Taylor expanded in x around inf 50.7%
*-commutative50.7%
associate-*l*50.7%
unpow250.7%
distribute-lft-out55.4%
Simplified55.4%
Taylor expanded in x around 0 16.6%
associate-*r*16.6%
*-commutative16.6%
Simplified16.6%
Final simplification16.6%
herbie shell --seed 2024027
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))