
(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 95.7%
sqr-neg95.7%
cancel-sign-sub95.7%
fma-define98.0%
Simplified98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 4e+293) (- (* re re) (* im im)) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 4e+293) {
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) <= 4d+293) 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) <= 4e+293) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 4e+293: tmp = (re * re) - (im * im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 4e+293) 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) <= 4e+293) 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], 4e+293], 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 4 \cdot 10^{+293}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 3.9999999999999997e293Initial program 100.0%
if 3.9999999999999997e293 < (*.f64 im im) Initial program 82.5%
Taylor expanded in re around 0 92.1%
neg-mul-192.1%
Simplified92.1%
unpow292.1%
distribute-lft-neg-in92.1%
Applied egg-rr92.1%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 5e-112) (* re re) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 5e-112) {
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-112) 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-112) {
tmp = re * re;
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 5e-112: tmp = re * re else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 5e-112) 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-112) tmp = re * re; else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 5e-112], N[(re * re), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 5 \cdot 10^{-112}:\\
\;\;\;\;re \cdot re\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 5.00000000000000044e-112Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt47.7%
sqrt-unprod95.3%
sqr-neg95.3%
sqrt-prod47.6%
add-sqr-sqrt92.5%
Applied egg-rr92.5%
Taylor expanded in re around inf 92.7%
Taylor expanded in re around inf 93.0%
if 5.00000000000000044e-112 < (*.f64 im im) Initial program 92.5%
Taylor expanded in re around 0 76.3%
neg-mul-176.3%
Simplified76.3%
unpow276.3%
distribute-lft-neg-in76.3%
Applied egg-rr76.3%
Final simplification83.4%
(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 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt46.4%
sqrt-unprod73.5%
sqr-neg73.5%
sqrt-prod27.8%
add-sqr-sqrt53.2%
Applied egg-rr53.2%
Taylor expanded in re around inf 57.2%
Taylor expanded in re around inf 53.9%
(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 95.7%
Taylor expanded in re around 0 56.2%
neg-mul-156.2%
Simplified56.2%
add-sqr-sqrt8.3%
sqrt-unprod15.6%
sqr-neg15.6%
sqrt-unprod12.3%
add-sqr-sqrt12.3%
unpow212.3%
Applied egg-rr12.3%
herbie shell --seed 2024117
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))