
(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 (if (<= (* b b) 2e+300) (fma a a (* b (- b))) (* a (* a (- 1.0 (* (/ b a) (/ b a)))))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 2e+300) {
tmp = fma(a, a, (b * -b));
} else {
tmp = a * (a * (1.0 - ((b / a) * (b / a))));
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 2e+300) tmp = fma(a, a, Float64(b * Float64(-b))); else tmp = Float64(a * Float64(a * Float64(1.0 - Float64(Float64(b / a) * Float64(b / a))))); end return tmp end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 2e+300], N[(a * a + N[(b * (-b)), $MachinePrecision]), $MachinePrecision], N[(a * N[(a * N[(1.0 - N[(N[(b / a), $MachinePrecision] * N[(b / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 2 \cdot 10^{+300}:\\
\;\;\;\;\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right)\\
\mathbf{else}:\\
\;\;\;\;a \cdot \left(a \cdot \left(1 - \frac{b}{a} \cdot \frac{b}{a}\right)\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 2.0000000000000001e300Initial program 100.0%
sqr-neg100.0%
cancel-sign-sub100.0%
fma-define100.0%
Simplified100.0%
if 2.0000000000000001e300 < (*.f64 b b) Initial program 76.0%
Taylor expanded in a around inf 53.5%
mul-1-neg53.5%
unsub-neg53.5%
Simplified53.5%
add-sqr-sqrt1.4%
pow21.4%
sqrt-prod1.4%
sqrt-pow11.4%
metadata-eval1.4%
pow11.4%
add-sqr-sqrt1.4%
pow21.4%
sqrt-div1.4%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
Applied egg-rr11.3%
unpow211.3%
*-commutative11.3%
*-commutative11.3%
swap-sqr11.2%
pow211.2%
pow211.2%
add-sqr-sqrt77.4%
associate-*r*100.0%
pow2100.0%
Applied egg-rr100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 2e+300) (- (* a a) (* b b)) (* a (* a (- 1.0 (* (/ b a) (/ b a)))))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 2e+300) {
tmp = (a * a) - (b * b);
} else {
tmp = a * (a * (1.0 - ((b / a) * (b / a))));
}
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+300) then
tmp = (a * a) - (b * b)
else
tmp = a * (a * (1.0d0 - ((b / a) * (b / a))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 2e+300) {
tmp = (a * a) - (b * b);
} else {
tmp = a * (a * (1.0 - ((b / a) * (b / a))));
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 2e+300: tmp = (a * a) - (b * b) else: tmp = a * (a * (1.0 - ((b / a) * (b / a)))) return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 2e+300) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64(a * Float64(a * Float64(1.0 - Float64(Float64(b / a) * Float64(b / a))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 2e+300) tmp = (a * a) - (b * b); else tmp = a * (a * (1.0 - ((b / a) * (b / a)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 2e+300], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(a * N[(a * N[(1.0 - N[(N[(b / a), $MachinePrecision] * N[(b / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 2 \cdot 10^{+300}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;a \cdot \left(a \cdot \left(1 - \frac{b}{a} \cdot \frac{b}{a}\right)\right)\\
\end{array}
\end{array}
if (*.f64 b b) < 2.0000000000000001e300Initial program 100.0%
if 2.0000000000000001e300 < (*.f64 b b) Initial program 76.0%
Taylor expanded in a around inf 53.5%
mul-1-neg53.5%
unsub-neg53.5%
Simplified53.5%
add-sqr-sqrt1.4%
pow21.4%
sqrt-prod1.4%
sqrt-pow11.4%
metadata-eval1.4%
pow11.4%
add-sqr-sqrt1.4%
pow21.4%
sqrt-div1.4%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
Applied egg-rr11.3%
unpow211.3%
*-commutative11.3%
*-commutative11.3%
swap-sqr11.2%
pow211.2%
pow211.2%
add-sqr-sqrt77.4%
associate-*r*100.0%
pow2100.0%
Applied egg-rr100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (* a a) 5e+302) (- (* a a) (* b b)) (* a a)))
double code(double a, double b) {
double tmp;
if ((a * a) <= 5e+302) {
tmp = (a * a) - (b * b);
} else {
tmp = a * a;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((a * a) <= 5d+302) then
tmp = (a * a) - (b * b)
else
tmp = a * a
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((a * a) <= 5e+302) {
tmp = (a * a) - (b * b);
} else {
tmp = a * a;
}
return tmp;
}
def code(a, b): tmp = 0 if (a * a) <= 5e+302: tmp = (a * a) - (b * b) else: tmp = a * a return tmp
function code(a, b) tmp = 0.0 if (Float64(a * a) <= 5e+302) tmp = Float64(Float64(a * a) - Float64(b * b)); else tmp = Float64(a * a); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((a * a) <= 5e+302) tmp = (a * a) - (b * b); else tmp = a * a; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(a * a), $MachinePrecision], 5e+302], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(a * a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \cdot a \leq 5 \cdot 10^{+302}:\\
\;\;\;\;a \cdot a - b \cdot b\\
\mathbf{else}:\\
\;\;\;\;a \cdot a\\
\end{array}
\end{array}
if (*.f64 a a) < 5e302Initial program 100.0%
if 5e302 < (*.f64 a a) Initial program 73.8%
Taylor expanded in a around inf 73.8%
mul-1-neg73.8%
unsub-neg73.8%
Simplified73.8%
add-sqr-sqrt73.8%
pow273.8%
sqrt-prod73.8%
sqrt-pow173.8%
metadata-eval73.8%
pow173.8%
add-sqr-sqrt73.8%
pow273.8%
sqrt-div73.8%
sqrt-pow184.6%
metadata-eval84.6%
pow184.6%
sqrt-pow184.6%
metadata-eval84.6%
pow184.6%
Applied egg-rr84.6%
unpow284.6%
*-commutative84.6%
*-commutative84.6%
swap-sqr84.6%
pow284.6%
pow284.6%
add-sqr-sqrt100.0%
associate-*r*100.0%
pow2100.0%
Applied egg-rr100.0%
Taylor expanded in b around 0 84.6%
Final simplification96.1%
(FPCore (a b) :precision binary64 (* a a))
double code(double a, double b) {
return a * a;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = a * a
end function
public static double code(double a, double b) {
return a * a;
}
def code(a, b): return a * a
function code(a, b) return Float64(a * a) end
function tmp = code(a, b) tmp = a * a; end
code[a_, b_] := N[(a * a), $MachinePrecision]
\begin{array}{l}
\\
a \cdot a
\end{array}
Initial program 93.3%
Taylor expanded in a around inf 66.9%
mul-1-neg66.9%
unsub-neg66.9%
Simplified66.9%
add-sqr-sqrt40.6%
pow240.6%
sqrt-prod40.6%
sqrt-pow140.6%
metadata-eval40.6%
pow140.6%
add-sqr-sqrt40.6%
pow240.6%
sqrt-div40.6%
sqrt-pow143.4%
metadata-eval43.4%
pow143.4%
sqrt-pow145.3%
metadata-eval45.3%
pow145.3%
Applied egg-rr45.3%
unpow245.3%
*-commutative45.3%
*-commutative45.3%
swap-sqr45.3%
pow245.3%
pow245.3%
add-sqr-sqrt78.0%
associate-*r*87.9%
pow287.9%
Applied egg-rr87.9%
Taylor expanded in b around 0 48.0%
Final simplification48.0%
(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 2024075
(FPCore (a b)
:name "Difference of squares"
:precision binary64
:alt
(* (+ a b) (- a b))
(- (* a a) (* b b)))