
(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 3 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 92.2%
sqr-neg92.2%
cancel-sign-sub92.2%
fma-def95.3%
Simplified95.3%
Final simplification95.3%
(FPCore re_sqr (re im) :precision binary64 (* re (+ re (* im -2.0))))
double re_sqr(double re, double im) {
return re * (re + (im * -2.0));
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = re * (re + (im * (-2.0d0)))
end function
public static double re_sqr(double re, double im) {
return re * (re + (im * -2.0));
}
def re_sqr(re, im): return re * (re + (im * -2.0))
function re_sqr(re, im) return Float64(re * Float64(re + Float64(im * -2.0))) end
function tmp = re_sqr(re, im) tmp = re * (re + (im * -2.0)); end
re$95$sqr[re_, im_] := N[(re * N[(re + N[(im * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
re \cdot \left(re + im \cdot -2\right)
\end{array}
Initial program 92.2%
difference-of-squares100.0%
add-sqr-sqrt48.7%
sqrt-prod74.6%
sqr-neg74.6%
sqrt-unprod27.7%
add-sqr-sqrt52.8%
sub-neg52.8%
pow152.8%
pow152.8%
pow-prod-up52.8%
add-sqr-sqrt30.5%
add-sqr-sqrt13.3%
difference-of-squares13.3%
metadata-eval13.3%
unpow-prod-down13.3%
Applied egg-rr13.3%
unpow213.3%
unpow213.3%
unswap-sqr13.3%
difference-of-squares13.3%
unpow1/213.3%
unpow1/213.3%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
difference-of-squares13.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr25.1%
metadata-eval25.1%
unpow125.1%
Simplified52.8%
Taylor expanded in re around inf 51.5%
associate-*r*51.5%
unpow251.5%
distribute-rgt-out57.0%
*-commutative57.0%
Simplified57.0%
Final simplification57.0%
(FPCore re_sqr (re im) :precision binary64 (* -2.0 (* re im)))
double re_sqr(double re, double im) {
return -2.0 * (re * im);
}
real(8) function re_sqr(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
re_sqr = (-2.0d0) * (re * im)
end function
public static double re_sqr(double re, double im) {
return -2.0 * (re * im);
}
def re_sqr(re, im): return -2.0 * (re * im)
function re_sqr(re, im) return Float64(-2.0 * Float64(re * im)) end
function tmp = re_sqr(re, im) tmp = -2.0 * (re * im); end
re$95$sqr[re_, im_] := N[(-2.0 * N[(re * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot \left(re \cdot im\right)
\end{array}
Initial program 92.2%
difference-of-squares100.0%
add-sqr-sqrt48.7%
sqrt-prod74.6%
sqr-neg74.6%
sqrt-unprod27.7%
add-sqr-sqrt52.8%
sub-neg52.8%
pow152.8%
pow152.8%
pow-prod-up52.8%
add-sqr-sqrt30.5%
add-sqr-sqrt13.3%
difference-of-squares13.3%
metadata-eval13.3%
unpow-prod-down13.3%
Applied egg-rr13.3%
unpow213.3%
unpow213.3%
unswap-sqr13.3%
difference-of-squares13.3%
unpow1/213.3%
unpow1/213.3%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr13.4%
metadata-eval13.4%
unpow113.4%
difference-of-squares13.4%
unpow1/213.4%
unpow1/213.4%
pow-sqr25.1%
metadata-eval25.1%
unpow125.1%
Simplified52.8%
Taylor expanded in re around inf 51.5%
associate-*r*51.5%
unpow251.5%
distribute-rgt-out57.0%
*-commutative57.0%
Simplified57.0%
Taylor expanded in re around 0 17.8%
Final simplification17.8%
herbie shell --seed 2024033
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))