
(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 96.1%
sqr-neg96.1%
cancel-sign-sub96.1%
fma-define98.0%
Simplified98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 4e+292) (- (* re re) (* im im)) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 4e+292) {
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+292) 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+292) {
tmp = (re * re) - (im * im);
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 4e+292: tmp = (re * re) - (im * im) else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 4e+292) 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+292) 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+292], 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^{+292}:\\
\;\;\;\;re \cdot re - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 4.0000000000000001e292Initial program 100.0%
if 4.0000000000000001e292 < (*.f64 im im) Initial program 83.9%
Taylor expanded in re around 0 91.9%
neg-mul-191.9%
Simplified91.9%
unpow291.9%
distribute-lft-neg-in91.9%
Applied egg-rr91.9%
Final simplification98.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (* im im) 5e+45) (* re re) (* im (- im))))
double re_sqr(double re, double im) {
double tmp;
if ((im * im) <= 5e+45) {
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+45) 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+45) {
tmp = re * re;
} else {
tmp = im * -im;
}
return tmp;
}
def re_sqr(re, im): tmp = 0 if (im * im) <= 5e+45: tmp = re * re else: tmp = im * -im return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(im * im) <= 5e+45) 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+45) tmp = re * re; else tmp = im * -im; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(im * im), $MachinePrecision], 5e+45], N[(re * re), $MachinePrecision], N[(im * (-im)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 5 \cdot 10^{+45}:\\
\;\;\;\;re \cdot re\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(-im\right)\\
\end{array}
\end{array}
if (*.f64 im im) < 5e45Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt57.9%
sqrt-unprod91.2%
sqr-neg91.2%
sqrt-prod33.3%
add-sqr-sqrt83.3%
Applied egg-rr83.3%
Taylor expanded in re around inf 83.6%
Taylor expanded in re around inf 83.9%
if 5e45 < (*.f64 im im) Initial program 91.0%
Taylor expanded in re around 0 79.3%
neg-mul-179.3%
Simplified79.3%
unpow279.3%
distribute-lft-neg-in79.3%
Applied egg-rr79.3%
Final simplification81.9%
(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 96.1%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt55.4%
sqrt-unprod78.6%
sqr-neg78.6%
sqrt-prod23.9%
add-sqr-sqrt55.8%
Applied egg-rr55.8%
Taylor expanded in re around inf 58.4%
Taylor expanded in re around inf 56.6%
(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 96.1%
Taylor expanded in re around 0 53.5%
neg-mul-153.5%
Simplified53.5%
add-sqr-sqrt9.4%
sqrt-unprod16.1%
sqr-neg16.1%
sqrt-unprod12.9%
add-sqr-sqrt12.9%
unpow212.9%
Applied egg-rr12.9%
herbie shell --seed 2024186
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))