
(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 94.1%
sqr-neg94.1%
cancel-sign-sub94.1%
fma-def98.0%
Simplified98.0%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 1e+44) (* (- re im) (- re im)) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 1e+44) {
tmp = (re - im) * (re - im);
} else {
tmp = im * -im;
}
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) <= 1d+44) then
tmp = (re - im) * (re - im)
else
tmp = im * -im
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 1e+44) {
tmp = (re - im) * (re - im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 1e+44: tmp = (re - im) * (re - im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 1e+44) tmp = Float64(Float64(re - im) * Float64(re - im)); else tmp = Float64(im * Float64(-im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((im * im) <= 1e+44) tmp = (re - im) * (re - im); else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 1e+44], N[(N[(re - im), $MachinePrecision] * N[(re - im), $MachinePrecision]), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 10^{+44}:\\
\;\;\;\;\left(re - im\right) \cdot \left(re - im\right)\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 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 im im) Initial program 87.9%
Taylor expanded in re around 0 83.5%
mul-1-neg83.5%
Simplified83.5%
unpow283.5%
Applied egg-rr83.5%
Final simplification83.3%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 5e+291) (- (* re re) (* im im)) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 5e+291) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -im;
}
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) <= 5d+291) then
tmp = (re * re) - (im * im)
else
tmp = im * -im
end if
re_sqr = tmp
end function
public static double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 5e+291) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 5e+291: tmp = (re * re) - (im * im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 5e+291) tmp = Float64(Float64(re * re) - Float64(im * im)); else tmp = Float64(im * Float64(-im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((im * im) <= 5e+291) tmp = (re * re) - (im * im); else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 5e+291], N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 5 \cdot 10^{+291}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 5.0000000000000001e291Initial program 100.0%
if 5.0000000000000001e291 < (*.f64 im im) Initial program 78.3%
Taylor expanded in re around 0 92.7%
mul-1-neg92.7%
Simplified92.7%
unpow292.8%
Applied egg-rr92.8%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (* im (- im)))
double re_sqr(double re, double im) {
return im * -im;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = im * -im
end function
public static double re_sqr(double re, double im) {
return im * -im;
}
def re_sqr(re, im): return im * -im
function re_sqr(re, im) return Float64(im * Float64(-im)) end
function tmp = re_sqr(re, im) tmp = im * -im; end
re$95$sqr[re_, im_] := N[(im * (-im)), $MachinePrecision]
\begin{array}{l}
\\
im \cdot \left(-im\right)
\end{array}
Initial program 94.1%
Taylor expanded in re around 0 56.3%
mul-1-neg56.3%
Simplified56.3%
unpow256.3%
Applied egg-rr56.3%
Final simplification56.3%
herbie shell --seed 2024020
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))