Average Error: 7.2 → 0.2
Time: 21.3s
Precision: 64
\[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
\[\mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.re, x.im \cdot \left(\left(x.re + x.re\right) \cdot \left(-x.im\right)\right)\right)\]
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im
\mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.re, x.im \cdot \left(\left(x.re + x.re\right) \cdot \left(-x.im\right)\right)\right)
double f(double x_re, double x_im) {
        double r9943621 = x_re;
        double r9943622 = r9943621 * r9943621;
        double r9943623 = x_im;
        double r9943624 = r9943623 * r9943623;
        double r9943625 = r9943622 - r9943624;
        double r9943626 = r9943625 * r9943621;
        double r9943627 = r9943621 * r9943623;
        double r9943628 = r9943623 * r9943621;
        double r9943629 = r9943627 + r9943628;
        double r9943630 = r9943629 * r9943623;
        double r9943631 = r9943626 - r9943630;
        return r9943631;
}

double f(double x_re, double x_im) {
        double r9943632 = x_im;
        double r9943633 = x_re;
        double r9943634 = r9943632 + r9943633;
        double r9943635 = r9943633 - r9943632;
        double r9943636 = r9943635 * r9943633;
        double r9943637 = r9943633 + r9943633;
        double r9943638 = -r9943632;
        double r9943639 = r9943637 * r9943638;
        double r9943640 = r9943632 * r9943639;
        double r9943641 = fma(r9943634, r9943636, r9943640);
        return r9943641;
}

Error

Bits error versus x.re

Bits error versus x.im

Target

Original7.2
Target0.3
Herbie0.2
\[\left(x.re \cdot x.re\right) \cdot \left(x.re - x.im\right) + \left(x.re \cdot x.im\right) \cdot \left(x.re - 3 \cdot x.im\right)\]

Derivation

  1. Initial program 7.2

    \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  2. Using strategy rm
  3. Applied difference-of-squares7.2

    \[\leadsto \color{blue}{\left(\left(x.re + x.im\right) \cdot \left(x.re - x.im\right)\right)} \cdot x.re - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  4. Applied associate-*l*0.3

    \[\leadsto \color{blue}{\left(x.re + x.im\right) \cdot \left(\left(x.re - x.im\right) \cdot x.re\right)} - \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\]
  5. Using strategy rm
  6. Applied fma-neg0.2

    \[\leadsto \color{blue}{\mathsf{fma}\left(x.re + x.im, \left(x.re - x.im\right) \cdot x.re, -\left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\right)}\]
  7. Simplified0.2

    \[\leadsto \mathsf{fma}\left(x.re + x.im, \left(x.re - x.im\right) \cdot x.re, \color{blue}{x.im \cdot \left(-x.im \cdot \left(x.re + x.re\right)\right)}\right)\]
  8. Final simplification0.2

    \[\leadsto \mathsf{fma}\left(x.im + x.re, \left(x.re - x.im\right) \cdot x.re, x.im \cdot \left(\left(x.re + x.re\right) \cdot \left(-x.im\right)\right)\right)\]

Reproduce

herbie shell --seed 2019169 +o rules:numerics
(FPCore (x.re x.im)
  :name "math.cube on complex, real part"

  :herbie-target
  (+ (* (* x.re x.re) (- x.re x.im)) (* (* x.re x.im) (- x.re (* 3.0 x.im))))

  (- (* (- (* x.re x.re) (* x.im x.im)) x.re) (* (+ (* x.re x.im) (* x.im x.re)) x.im)))