
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
def re_sqr(re, im): return (re * re) - (im * im)
function re_sqr(re, im) return Float64(Float64(re * re) - Float64(im * im)) end
function tmp = re_sqr(re, im) tmp = (re * re) - (im * im); end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
re \cdot re - im \cdot im
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
return (re * re) - (im * im);
}
def re_sqr(re, im): return (re * re) - (im * im)
function re_sqr(re, im) return Float64(Float64(re * re) - Float64(im * im)) end
function tmp = re_sqr(re, im) tmp = (re * re) - (im * im); end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
re \cdot re - im \cdot im
\end{array}
(FPCore re_sqr (re im) :precision binary64 (fma re re (* im (- im))))
double re_sqr(double re, double im) {
return fma(re, re, (im * -im));
}
function re_sqr(re, im) return fma(re, re, Float64(im * Float64(-im))) end
re$95$sqr[re_, im_] := N[(re * re + N[(im * (-im)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(re, re, im \cdot \left(-im\right)\right)
\end{array}
Initial program 96.1%
sqr-neg96.1%
cancel-sign-sub96.1%
fma-def98.0%
Simplified98.0%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 4e+295) (- (* re re) (* im im)) (- (pow im 2.0))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 4e+295) {
tmp = (re * re) - (im * im);
} else {
tmp = -pow(im, 2.0);
}
return tmp;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if ((im * im) <= 4d+295) then
tmp = (re * re) - (im * im)
else
tmp = -(im ** 2.0d0)
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 4e+295) {
tmp = (re * re) - (im * im);
} else {
tmp = -Math.pow(im, 2.0);
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 4e+295: tmp = (re * re) - (im * im) else: tmp = -math.pow(im, 2.0) return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 4e+295) tmp = Float64(Float64(re * re) - Float64(im * im)); else tmp = Float64(-(im ^ 2.0)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((im * im) <= 4e+295) tmp = (re * re) - (im * im); else tmp = -(im ^ 2.0); end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 4e+295], N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], (-N[Power[im, 2.0], $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 4 \cdot 10^{+295}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;-{im}^{2}\\
\end{array}
\end{array}
if (*.f64 im im) < 3.9999999999999999e295Initial program 100.0%
if 3.9999999999999999e295 < (*.f64 im im) Initial program 82.8%
Taylor expanded in re around 0 91.4%
mul-1-neg91.4%
Simplified91.4%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* re re) 1e+295) (- (* re re) (* im im)) (* (- re im) (- re im))))
double re_sqr(double re, double im) {
double tmp;
if ((re * re) <= 1e+295) {
tmp = (re * re) - (im * im);
} else {
tmp = (re - im) * (re - im);
}
return tmp;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if ((re * re) <= 1d+295) then
tmp = (re * re) - (im * im)
else
tmp = (re - im) * (re - im)
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if ((re * re) <= 1e+295) {
tmp = (re * re) - (im * im);
} else {
tmp = (re - im) * (re - im);
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (re * re) <= 1e+295: tmp = (re * re) - (im * im) else: tmp = (re - im) * (re - im) return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(re * re) <= 1e+295) tmp = Float64(Float64(re * re) - Float64(im * im)); else tmp = Float64(Float64(re - im) * Float64(re - im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((re * re) <= 1e+295) tmp = (re * re) - (im * im); else tmp = (re - im) * (re - im); end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(re * re), $MachinePrecision], 1e+295], N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], N[(N[(re - im), $MachinePrecision] * N[(re - im), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \cdot re \leq 10^{+295}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;\left(re - im\right) \cdot \left(re - im\right)\\
\end{array}
\end{array}
if (*.f64 re re) < 9.9999999999999998e294Initial program 100.0%
if 9.9999999999999998e294 < (*.f64 re re) Initial program 85.1%
difference-of-squares100.0%
add-sqr-sqrt47.8%
sqrt-prod92.5%
sqr-neg92.5%
sqrt-unprod49.3%
add-sqr-sqrt92.5%
sub-neg92.5%
pow192.5%
pow192.5%
pow-prod-up92.5%
metadata-eval92.5%
add-sqr-sqrt38.8%
add-sqr-sqrt20.9%
difference-of-squares20.9%
unpow-prod-down20.9%
Applied egg-rr20.9%
unpow220.9%
unpow220.9%
unswap-sqr20.9%
difference-of-squares20.9%
unpow1/220.9%
unpow1/220.9%
pow-sqr20.9%
metadata-eval20.9%
unpow120.9%
unpow1/220.9%
unpow1/220.9%
pow-sqr20.9%
metadata-eval20.9%
unpow120.9%
difference-of-squares20.9%
unpow1/220.9%
unpow1/220.9%
pow-sqr43.3%
metadata-eval43.3%
unpow143.3%
Simplified92.5%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (* (- re im) (- re im)))
double re_sqr(double re, double im) {
return (re - im) * (re - im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (re - im) * (re - im)
end function
public static double re_sqr(double re, double im) {
return (re - im) * (re - im);
}
def re_sqr(re, im): return (re - im) * (re - im)
function re_sqr(re, im) return Float64(Float64(re - im) * Float64(re - im)) end
function tmp = re_sqr(re, im) tmp = (re - im) * (re - im); end
re$95$sqr[re_, im_] := N[(N[(re - im), $MachinePrecision] * N[(re - im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(re - im\right) \cdot \left(re - im\right)
\end{array}
Initial program 96.1%
difference-of-squares100.0%
add-sqr-sqrt45.2%
sqrt-prod76.3%
sqr-neg76.3%
sqrt-unprod32.2%
add-sqr-sqrt60.9%
sub-neg60.9%
pow160.9%
pow160.9%
pow-prod-up60.9%
metadata-eval60.9%
add-sqr-sqrt27.6%
add-sqr-sqrt13.2%
difference-of-squares13.2%
unpow-prod-down13.2%
Applied egg-rr13.2%
unpow213.2%
unpow213.2%
unswap-sqr13.2%
difference-of-squares13.2%
unpow1/213.2%
unpow1/213.2%
pow-sqr13.3%
metadata-eval13.3%
unpow113.3%
unpow1/213.3%
unpow1/213.3%
pow-sqr13.3%
metadata-eval13.3%
unpow113.3%
difference-of-squares13.3%
unpow1/213.3%
unpow1/213.3%
pow-sqr28.7%
metadata-eval28.7%
unpow128.7%
Simplified60.9%
Final simplification60.9%
herbie shell --seed 2024013
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))