
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
return (a * a) - (b * b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
return (a * a) - (b * b);
}
def code(a, b): return (a * a) - (b * b)
function code(a, b) return Float64(Float64(a * a) - Float64(b * b)) end
function tmp = code(a, b) tmp = (a * a) - (b * b); end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
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 (- (* a a) (* b b)))
double code(double a, double b) {
return (a * a) - (b * b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
return (a * a) - (b * b);
}
def code(a, b): return (a * a) - (b * b)
function code(a, b) return Float64(Float64(a * a) - Float64(b * b)) end
function tmp = code(a, b) tmp = (a * a) - (b * b); end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot a - b \cdot b
\end{array}
(FPCore (a b) :precision binary64 (fma a a (* b (- b))))
double code(double a, double b) {
return fma(a, a, (b * -b));
}
function code(a, b) return fma(a, a, Float64(b * Float64(-b))) end
code[a_, b_] := N[(a * a + N[(b * (-b)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right)
\end{array}
Initial program 94.1%
sqr-neg94.1%
cancel-sign-sub94.1%
fma-def98.0%
Simplified98.0%
Final simplification98.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 1e+44) (* (- a b) (- a b)) (* b (- b))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 1e+44) {
tmp = (a - b) * (a - b);
} else {
tmp = b * -b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b * b) <= 1d+44) then
tmp = (a - b) * (a - b)
else
tmp = b * -b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 1e+44) {
tmp = (a - b) * (a - b);
} else {
tmp = b * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 1e+44: tmp = (a - b) * (a - b) else: tmp = b * -b return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 1e+44) tmp = Float64(Float64(a - b) * Float64(a - b)); else tmp = Float64(b * Float64(-b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 1e+44) tmp = (a - b) * (a - b); else tmp = b * -b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 1e+44], N[(N[(a - b), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision], N[(b * (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 10^{+44}:\\
\;\;\;\;\left(a - b\right) \cdot \left(a - b\right)\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 1.0000000000000001e44Initial program 100.0%
difference-of-squares100.0%
add-sqr-sqrt44.6%
sqrt-prod91.1%
sqr-neg91.1%
sqrt-unprod46.4%
add-sqr-sqrt83.0%
sub-neg83.0%
pow183.0%
pow183.0%
pow-prod-up83.0%
add-sqr-sqrt46.1%
add-sqr-sqrt19.7%
difference-of-squares19.7%
metadata-eval19.7%
unpow-prod-down19.7%
Applied egg-rr19.7%
unpow219.7%
unpow219.7%
unswap-sqr19.7%
difference-of-squares19.7%
unpow1/219.7%
unpow1/219.7%
pow-sqr19.8%
metadata-eval19.8%
unpow119.8%
unpow1/219.8%
unpow1/219.8%
pow-sqr19.8%
metadata-eval19.8%
unpow119.8%
difference-of-squares19.8%
unpow1/219.8%
unpow1/219.8%
pow-sqr36.6%
metadata-eval36.6%
unpow136.6%
Simplified83.0%
if 1.0000000000000001e44 < (*.f64 b b) Initial program 87.9%
Taylor expanded in a around 0 83.5%
mul-1-neg83.5%
Simplified83.5%
unpow283.5%
Applied egg-rr83.5%
Final simplification83.3%
(FPCore (a b) :precision binary64 (if (<= (* b b) 5e+291) (- (* a a) (* b b)) (* b (- b))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+291) {
tmp = (a * a) - (b * b);
} else {
tmp = b * -b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b * b) <= 5d+291) then
tmp = (a * a) - (b * b)
else
tmp = b * -b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 5e+291) {
tmp = (a * a) - (b * b);
} else {
tmp = b * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 5e+291: tmp = (a * a) - (b * b) else: tmp = b * -b return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 5e+291) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64(b * Float64(-b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 5e+291) tmp = (a * a) - (b * b); else tmp = b * -b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 5e+291], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(b * (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 5 \cdot 10^{+291}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 5.0000000000000001e291Initial program 100.0%
if 5.0000000000000001e291 < (*.f64 b b) Initial program 78.3%
Taylor expanded in a around 0 92.7%
mul-1-neg92.7%
Simplified92.7%
unpow292.8%
Applied egg-rr92.8%
Final simplification98.0%
(FPCore (a b) :precision binary64 (* b (- b)))
double code(double a, double b) {
return b * -b;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = b * -b
end function
public static double code(double a, double b) {
return b * -b;
}
def code(a, b): return b * -b
function code(a, b) return Float64(b * Float64(-b)) end
function tmp = code(a, b) tmp = b * -b; end
code[a_, b_] := N[(b * (-b)), $MachinePrecision]
\begin{array}{l}
\\
b \cdot \left(-b\right)
\end{array}
Initial program 94.1%
Taylor expanded in a around 0 56.3%
mul-1-neg56.3%
Simplified56.3%
unpow256.3%
Applied egg-rr56.3%
Final simplification56.3%
(FPCore (a b) :precision binary64 (* (+ a b) (- a b)))
double code(double a, double b) {
return (a + b) * (a - b);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (a + b) * (a - b)
end function
public static double code(double a, double b) {
return (a + b) * (a - b);
}
def code(a, b): return (a + b) * (a - b)
function code(a, b) return Float64(Float64(a + b) * Float64(a - b)) end
function tmp = code(a, b) tmp = (a + b) * (a - b); end
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(a + b\right) \cdot \left(a - b\right)
\end{array}
herbie shell --seed 2024020
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:herbie-target
(* (+ a b) (- a b))
(- (* a a) (* b b)))