
(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 91.0%
sqr-neg91.0%
cancel-sign-sub91.0%
fma-def97.3%
Simplified97.3%
Final simplification97.3%
(FPCore (a b)
:precision binary64
(if (or (<= (* b b) 1e-216)
(and (not (<= (* b b) 1e-183)) (<= (* b b) 1e+50)))
(* (- a b) (- a b))
(* b (- b))))
double code(double a, double b) {
double tmp;
if (((b * b) <= 1e-216) || (!((b * b) <= 1e-183) && ((b * b) <= 1e+50))) {
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-216) .or. (.not. ((b * b) <= 1d-183)) .and. ((b * b) <= 1d+50)) 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-216) || (!((b * b) <= 1e-183) && ((b * b) <= 1e+50))) {
tmp = (a - b) * (a - b);
} else {
tmp = b * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if ((b * b) <= 1e-216) or (not ((b * b) <= 1e-183) and ((b * b) <= 1e+50)): tmp = (a - b) * (a - b) else: tmp = b * -b return tmp
function code(a, b) tmp = 0.0 if ((Float64(b * b) <= 1e-216) || (!(Float64(b * b) <= 1e-183) && (Float64(b * b) <= 1e+50))) 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-216) || (~(((b * b) <= 1e-183)) && ((b * b) <= 1e+50))) tmp = (a - b) * (a - b); else tmp = b * -b; end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[N[(b * b), $MachinePrecision], 1e-216], And[N[Not[LessEqual[N[(b * b), $MachinePrecision], 1e-183]], $MachinePrecision], LessEqual[N[(b * b), $MachinePrecision], 1e+50]]], 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^{-216} \lor \neg \left(b \cdot b \leq 10^{-183}\right) \land b \cdot b \leq 10^{+50}:\\
\;\;\;\;\left(a - b\right) \cdot \left(a - b\right)\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 1e-216 or 1.00000000000000001e-183 < (*.f64 b b) < 1.0000000000000001e50Initial program 100.0%
difference-of-squares100.0%
add-sqr-sqrt49.6%
sqrt-prod92.3%
sqr-neg92.3%
sqrt-unprod42.7%
add-sqr-sqrt85.5%
sub-neg85.5%
pow185.5%
pow185.5%
pow-prod-up85.5%
add-sqr-sqrt42.6%
add-sqr-sqrt22.5%
difference-of-squares22.5%
metadata-eval22.5%
unpow-prod-down22.4%
Applied egg-rr22.4%
unpow222.4%
unpow222.4%
unswap-sqr22.5%
difference-of-squares22.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr22.5%
metadata-eval22.5%
unpow122.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr22.5%
metadata-eval22.5%
unpow122.5%
difference-of-squares22.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr42.8%
metadata-eval42.8%
unpow142.8%
Simplified85.5%
if 1e-216 < (*.f64 b b) < 1.00000000000000001e-183 or 1.0000000000000001e50 < (*.f64 b b) Initial program 81.9%
Taylor expanded in a around 0 81.4%
mul-1-neg81.4%
Simplified81.4%
unpow281.4%
Applied egg-rr81.4%
Final simplification83.5%
(FPCore (a b) :precision binary64 (if (<= (* b b) 2e+292) (- (* a a) (* b b)) (* b (- b))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 2e+292) {
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) <= 2d+292) 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) <= 2e+292) {
tmp = (a * a) - (b * b);
} else {
tmp = b * -b;
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 2e+292: tmp = (a * a) - (b * b) else: tmp = b * -b return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 2e+292) 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) <= 2e+292) tmp = (a * a) - (b * b); else tmp = b * -b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 2e+292], 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 2 \cdot 10^{+292}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;b \cdot \left(-b\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 2e292Initial program 100.0%
if 2e292 < (*.f64 b b) Initial program 66.7%
Taylor expanded in a around 0 89.9%
mul-1-neg89.9%
Simplified89.9%
unpow289.9%
Applied egg-rr89.9%
Final simplification97.3%
(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 91.0%
Taylor expanded in a around 0 56.4%
mul-1-neg56.4%
Simplified56.4%
unpow256.4%
Applied egg-rr56.4%
Final simplification56.4%
(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 2024031
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:herbie-target
(* (+ a b) (- a b))
(- (* a a) (* b b)))