
(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 3 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}
(FPCore (x y) :precision binary64 (fma x x (* y (- y))))
double code(double x, double y) {
return fma(x, x, (y * -y));
}
function code(x, y) return fma(x, x, Float64(y * Float64(-y))) end
code[x_, y_] := N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)
\end{array}
Initial program 92.2%
sqr-neg92.2%
cancel-sign-sub92.2%
fma-def95.3%
Simplified95.3%
Final simplification95.3%
(FPCore (x y) :precision binary64 (* x (+ x (* y -2.0))))
double code(double x, double y) {
return x * (x + (y * -2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * (x + (y * (-2.0d0)))
end function
public static double code(double x, double y) {
return x * (x + (y * -2.0));
}
def code(x, y): return x * (x + (y * -2.0))
function code(x, y) return Float64(x * Float64(x + Float64(y * -2.0))) end
function tmp = code(x, y) tmp = x * (x + (y * -2.0)); end
code[x_, y_] := N[(x * N[(x + N[(y * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(x + y \cdot -2\right)
\end{array}
Initial program 92.2%
difference-of-squares100.0%
add-sqr-sqrt48.7%
sqrt-prod74.6%
sqr-neg74.6%
sqrt-unprod27.7%
add-sqr-sqrt52.8%
sub-neg52.8%
pow152.8%
pow152.8%
pow-prod-up52.8%
add-sqr-sqrt30.5%
add-sqr-sqrt13.3%
difference-of-squares13.3%
metadata-eval13.3%
unpow-prod-down13.3%
Applied egg-rr13.3%
unpow213.3%
unpow213.3%
unswap-sqr13.3%
difference-of-squares13.3%
unpow1/213.3%
unpow1/213.3%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
difference-of-squares13.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr25.1%
metadata-eval25.1%
unpow125.1%
Simplified52.8%
Taylor expanded in x around inf 51.5%
*-commutative51.5%
associate-*l*51.5%
unpow251.5%
distribute-lft-out57.0%
Simplified57.0%
Final simplification57.0%
(FPCore (x y) :precision binary64 (* -2.0 (* x y)))
double code(double x, double y) {
return -2.0 * (x * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (-2.0d0) * (x * y)
end function
public static double code(double x, double y) {
return -2.0 * (x * y);
}
def code(x, y): return -2.0 * (x * y)
function code(x, y) return Float64(-2.0 * Float64(x * y)) end
function tmp = code(x, y) tmp = -2.0 * (x * y); end
code[x_, y_] := N[(-2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot \left(x \cdot y\right)
\end{array}
Initial program 92.2%
difference-of-squares100.0%
add-sqr-sqrt48.7%
sqrt-prod74.6%
sqr-neg74.6%
sqrt-unprod27.7%
add-sqr-sqrt52.8%
sub-neg52.8%
pow152.8%
pow152.8%
pow-prod-up52.8%
add-sqr-sqrt30.5%
add-sqr-sqrt13.3%
difference-of-squares13.3%
metadata-eval13.3%
unpow-prod-down13.3%
Applied egg-rr13.3%
unpow213.3%
unpow213.3%
unswap-sqr13.3%
difference-of-squares13.3%
unpow1/213.3%
unpow1/213.3%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
difference-of-squares13.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr25.1%
metadata-eval25.1%
unpow125.1%
Simplified52.8%
Taylor expanded in x around inf 51.5%
*-commutative51.5%
associate-*l*51.5%
unpow251.5%
distribute-lft-out57.0%
Simplified57.0%
Taylor expanded in x around 0 17.8%
Final simplification17.8%
herbie shell --seed 2024033
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))