
(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 5 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 93.8%
sqr-neg93.8%
cancel-sign-sub93.8%
fma-define97.3%
Simplified97.3%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 1e+271) (- (* re re) (* im im)) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 1e+271) {
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) <= 1d+271) 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) <= 1e+271) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 1e+271: tmp = (re * re) - (im * im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 1e+271) 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) <= 1e+271) 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], 1e+271], 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 10^{+271}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 9.99999999999999953e270Initial program 100.0%
if 9.99999999999999953e270 < (*.f64 im im) Initial program 75.8%
Taylor expanded in re around 0 89.4%
neg-mul-189.4%
Simplified89.4%
unpow289.4%
distribute-lft-neg-in89.4%
Applied egg-rr89.4%
Final simplification97.3%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 5e+18) (* re re) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 5e+18) {
tmp = re * re;
} 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+18) then
tmp = re * re
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+18) {
tmp = re * re;
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 5e+18: tmp = re * re else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 5e+18) tmp = Float64(re * re); else tmp = Float64(im * Float64(-im)); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if ((im * im) <= 5e+18) tmp = re * re; else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 5e+18], N[(re * re), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 5 \cdot 10^{+18}:\\
\;\;\;\;re \cdot re\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 5e18Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt59.1%
sqrt-unprod97.9%
sqr-neg97.9%
sqrt-prod38.8%
add-sqr-sqrt87.7%
Applied egg-rr87.7%
Taylor expanded in re around inf 87.9%
Taylor expanded in re around inf 87.9%
if 5e18 < (*.f64 im im) Initial program 86.0%
Taylor expanded in re around 0 79.8%
neg-mul-179.8%
Simplified79.8%
unpow279.8%
distribute-lft-neg-in79.8%
Applied egg-rr79.8%
Final simplification84.3%
(FPCore re_sqr (re im) :precision binary64 (* re re))
double re_sqr(double re, double im) {
return re * re;
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = re * re
end function
public static double re_sqr(double re, double im) {
return re * re;
}
def re_sqr(re, im): return re * re
function re_sqr(re, im) return Float64(re * re) end
function tmp = re_sqr(re, im) tmp = re * re; end
re$95$sqr[re_, im_] := N[(re * re), $MachinePrecision]
\begin{array}{l}
\\
re \cdot re
\end{array}
Initial program 93.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.8%
sqrt-unprod76.6%
sqr-neg76.6%
sqrt-prod26.6%
add-sqr-sqrt57.7%
Applied egg-rr57.7%
Taylor expanded in re around inf 61.6%
Taylor expanded in re around inf 58.1%
(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 * 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 im
\end{array}
Initial program 93.8%
Taylor expanded in re around 0 49.8%
neg-mul-149.8%
Simplified49.8%
add-sqr-sqrt6.9%
sqrt-unprod12.4%
sqr-neg12.4%
sqrt-unprod10.9%
add-sqr-sqrt10.9%
unpow210.9%
Applied egg-rr10.9%
herbie shell --seed 2024137
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))