math.abs on complex (squared)

Percentage Accurate: 100.0% → 100.0%
Time: 749.0ms
Alternatives: 1
Speedup: 1.1×

Specification

?
\[re \cdot re + im \cdot im \]
(FPCore modulus_sqr (re im)
  :precision binary64
  (+ (* re re) (* im im)))
double modulus_sqr(double re, double im) {
	return (re * re) + (im * im);
}
real(8) function modulus_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    modulus_sqr = (re * re) + (im * im)
end function
public static double modulus_sqr(double re, double im) {
	return (re * re) + (im * im);
}
def modulus_sqr(re, im):
	return (re * re) + (im * im)
function modulus_sqr(re, im)
	return Float64(Float64(re * re) + Float64(im * im))
end
function tmp = modulus_sqr(re, im)
	tmp = (re * re) + (im * im);
end
modulus$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]
re \cdot re + im \cdot im

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 1 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

\[re \cdot re + im \cdot im \]
(FPCore modulus_sqr (re im)
  :precision binary64
  (+ (* re re) (* im im)))
double modulus_sqr(double re, double im) {
	return (re * re) + (im * im);
}
real(8) function modulus_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    modulus_sqr = (re * re) + (im * im)
end function
public static double modulus_sqr(double re, double im) {
	return (re * re) + (im * im);
}
def modulus_sqr(re, im):
	return (re * re) + (im * im)
function modulus_sqr(re, im)
	return Float64(Float64(re * re) + Float64(im * im))
end
function tmp = modulus_sqr(re, im)
	tmp = (re * re) + (im * im);
end
modulus$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]
re \cdot re + im \cdot im

Alternative 1: 100.0% accurate, 1.1× speedup?

\[\mathsf{fma}\left(re, re, im \cdot im\right) \]
(FPCore modulus_sqr (re im)
  :precision binary64
  (fma re re (* im im)))
double modulus_sqr(double re, double im) {
	return fma(re, re, (im * im));
}
function modulus_sqr(re, im)
	return fma(re, re, Float64(im * im))
end
modulus$95$sqr[re_, im_] := N[(re * re + N[(im * im), $MachinePrecision]), $MachinePrecision]
\mathsf{fma}\left(re, re, im \cdot im\right)
Derivation
  1. Initial program 100.0%

    \[re \cdot re + im \cdot im \]
  2. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{re \cdot re + im \cdot im} \]
    2. lift-*.f64N/A

      \[\leadsto re \cdot re + \color{blue}{im \cdot im} \]
    3. sqr-neg-revN/A

      \[\leadsto re \cdot re + \color{blue}{\left(\mathsf{neg}\left(im\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)} \]
    4. fp-cancel-sign-sub-invN/A

      \[\leadsto \color{blue}{re \cdot re - \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)} \]
    5. fp-cancel-sub-sign-invN/A

      \[\leadsto \color{blue}{re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)} \]
    6. lift-*.f64N/A

      \[\leadsto \color{blue}{re \cdot re} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right) \]
    7. distribute-lft-neg-inN/A

      \[\leadsto re \cdot re + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)\right)\right)} \]
    8. distribute-lft-neg-inN/A

      \[\leadsto re \cdot re + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right) \cdot \left(\mathsf{neg}\left(im\right)\right)\right)\right)}\right)\right) \]
    9. sqr-neg-revN/A

      \[\leadsto re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{im \cdot im}\right)\right)\right)\right) \]
    10. lift-*.f64N/A

      \[\leadsto re \cdot re + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{im \cdot im}\right)\right)\right)\right) \]
    11. remove-double-negN/A

      \[\leadsto re \cdot re + \color{blue}{im \cdot im} \]
    12. lower-fma.f64100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)} \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2025313 -o setup:search
(FPCore modulus_sqr (re im)
  :name "math.abs on complex (squared)"
  :precision binary64
  (+ (* re re) (* im im)))