Average Error: 7.1 → 0.2
Time: 16.1s
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(\left(x.im + x.re\right), \left(\left(x.re - x.im\right) \cdot x.re\right), \left(x.im \cdot \left(x.re \cdot \left(-x.im\right) + x.re \cdot \left(-x.im\right)\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(\left(x.im + x.re\right), \left(\left(x.re - x.im\right) \cdot x.re\right), \left(x.im \cdot \left(x.re \cdot \left(-x.im\right) + x.re \cdot \left(-x.im\right)\right)\right)\right)
double f(double x_re, double x_im) {
        double r4555214 = x_re;
        double r4555215 = r4555214 * r4555214;
        double r4555216 = x_im;
        double r4555217 = r4555216 * r4555216;
        double r4555218 = r4555215 - r4555217;
        double r4555219 = r4555218 * r4555214;
        double r4555220 = r4555214 * r4555216;
        double r4555221 = r4555216 * r4555214;
        double r4555222 = r4555220 + r4555221;
        double r4555223 = r4555222 * r4555216;
        double r4555224 = r4555219 - r4555223;
        return r4555224;
}

double f(double x_re, double x_im) {
        double r4555225 = x_im;
        double r4555226 = x_re;
        double r4555227 = r4555225 + r4555226;
        double r4555228 = r4555226 - r4555225;
        double r4555229 = r4555228 * r4555226;
        double r4555230 = -r4555225;
        double r4555231 = r4555226 * r4555230;
        double r4555232 = r4555231 + r4555231;
        double r4555233 = r4555225 * r4555232;
        double r4555234 = fma(r4555227, r4555229, r4555233);
        return r4555234;
}

Error

Bits error versus x.re

Bits error versus x.im

Target

Original7.1
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.1

    \[\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.1

    \[\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.2

    \[\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(\left(x.re + x.im\right), \left(\left(x.re - x.im\right) \cdot x.re\right), \left(-\left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.im\right)\right)}\]
  7. Simplified0.2

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

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

Reproduce

herbie shell --seed 2019129 +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 x.im))))

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