math.cube on complex, imaginary part

Percentage Accurate: 82.0% → 97.5%
Time: 8.2s
Alternatives: 6
Speedup: 1.3×

Specification

?
\[\begin{array}{l} \\ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \end{array} \]
(FPCore (x.re x.im)
 :precision binary64
 (+
  (* (- (* x.re x.re) (* x.im x.im)) x.im)
  (* (+ (* x.re x.im) (* x.im x.re)) x.re)))
double code(double x_46_re, double x_46_im) {
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_46re, x_46im)
use fmin_fmax_functions
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    code = (((x_46re * x_46re) - (x_46im * x_46im)) * x_46im) + (((x_46re * x_46im) + (x_46im * x_46re)) * x_46re)
end function
public static double code(double x_46_re, double x_46_im) {
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
}
def code(x_46_re, x_46_im):
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re)
function code(x_46_re, x_46_im)
	return Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im)) * x_46_im) + Float64(Float64(Float64(x_46_re * x_46_im) + Float64(x_46_im * x_46_re)) * x_46_re))
end
function tmp = code(x_46_re, x_46_im)
	tmp = (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
end
code[x$46$re_, x$46$im_] := N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] * x$46$im), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re
\end{array}

Sampling outcomes in binary64 precision:

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 6 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: 82.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \end{array} \]
(FPCore (x.re x.im)
 :precision binary64
 (+
  (* (- (* x.re x.re) (* x.im x.im)) x.im)
  (* (+ (* x.re x.im) (* x.im x.re)) x.re)))
double code(double x_46_re, double x_46_im) {
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_46re, x_46im)
use fmin_fmax_functions
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    code = (((x_46re * x_46re) - (x_46im * x_46im)) * x_46im) + (((x_46re * x_46im) + (x_46im * x_46re)) * x_46re)
end function
public static double code(double x_46_re, double x_46_im) {
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
}
def code(x_46_re, x_46_im):
	return (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re)
function code(x_46_re, x_46_im)
	return Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im * x_46_im)) * x_46_im) + Float64(Float64(Float64(x_46_re * x_46_im) + Float64(x_46_im * x_46_re)) * x_46_re))
end
function tmp = code(x_46_re, x_46_im)
	tmp = (((x_46_re * x_46_re) - (x_46_im * x_46_im)) * x_46_im) + (((x_46_re * x_46_im) + (x_46_im * x_46_re)) * x_46_re);
end
code[x$46$re_, x$46$im_] := N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im * x$46$im), $MachinePrecision]), $MachinePrecision] * x$46$im), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] + N[(x$46$im * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re
\end{array}

Alternative 1: 97.5% accurate, 1.0× speedup?

\[\begin{array}{l} x.im\_m = \left|x.im\right| \\ x.im\_s = \mathsf{copysign}\left(1, x.im\right) \\ x.im\_s \cdot \begin{array}{l} \mathbf{if}\;x.im\_m \leq 2.8 \cdot 10^{+216}:\\ \;\;\;\;\mathsf{fma}\left(\left(2 \cdot x.im\_m\right) \cdot x.re, x.re, \left(x.im\_m + x.re\right) \cdot \left(\left(x.re - x.im\_m\right) \cdot x.im\_m\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\ \end{array} \end{array} \]
x.im\_m = (fabs.f64 x.im)
x.im\_s = (copysign.f64 #s(literal 1 binary64) x.im)
(FPCore (x.im_s x.re x.im_m)
 :precision binary64
 (*
  x.im_s
  (if (<= x.im_m 2.8e+216)
    (fma
     (* (* 2.0 x.im_m) x.re)
     x.re
     (* (+ x.im_m x.re) (* (- x.re x.im_m) x.im_m)))
    (* (* (- x.im_m) x.im_m) x.im_m))))
x.im\_m = fabs(x_46_im);
x.im\_s = copysign(1.0, x_46_im);
double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double tmp;
	if (x_46_im_m <= 2.8e+216) {
		tmp = fma(((2.0 * x_46_im_m) * x_46_re), x_46_re, ((x_46_im_m + x_46_re) * ((x_46_re - x_46_im_m) * x_46_im_m)));
	} else {
		tmp = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	}
	return x_46_im_s * tmp;
}
x.im\_m = abs(x_46_im)
x.im\_s = copysign(1.0, x_46_im)
function code(x_46_im_s, x_46_re, x_46_im_m)
	tmp = 0.0
	if (x_46_im_m <= 2.8e+216)
		tmp = fma(Float64(Float64(2.0 * x_46_im_m) * x_46_re), x_46_re, Float64(Float64(x_46_im_m + x_46_re) * Float64(Float64(x_46_re - x_46_im_m) * x_46_im_m)));
	else
		tmp = Float64(Float64(Float64(-x_46_im_m) * x_46_im_m) * x_46_im_m);
	end
	return Float64(x_46_im_s * tmp)
end
x.im\_m = N[Abs[x$46$im], $MachinePrecision]
x.im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x$46$im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$46$im$95$s_, x$46$re_, x$46$im$95$m_] := N[(x$46$im$95$s * If[LessEqual[x$46$im$95$m, 2.8e+216], N[(N[(N[(2.0 * x$46$im$95$m), $MachinePrecision] * x$46$re), $MachinePrecision] * x$46$re + N[(N[(x$46$im$95$m + x$46$re), $MachinePrecision] * N[(N[(x$46$re - x$46$im$95$m), $MachinePrecision] * x$46$im$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[((-x$46$im$95$m) * x$46$im$95$m), $MachinePrecision] * x$46$im$95$m), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x.im\_m = \left|x.im\right|
\\
x.im\_s = \mathsf{copysign}\left(1, x.im\right)

\\
x.im\_s \cdot \begin{array}{l}
\mathbf{if}\;x.im\_m \leq 2.8 \cdot 10^{+216}:\\
\;\;\;\;\mathsf{fma}\left(\left(2 \cdot x.im\_m\right) \cdot x.re, x.re, \left(x.im\_m + x.re\right) \cdot \left(\left(x.re - x.im\_m\right) \cdot x.im\_m\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x.im < 2.79999999999999982e216

    1. Initial program 83.2%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Applied rewrites86.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\left(x.re + x.im\right) \cdot \left(x.re - x.im\right), x.im, \left(\left(2 \cdot x.im\right) \cdot x.re\right) \cdot x.re\right)} \]
    4. Applied rewrites96.5%

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

    if 2.79999999999999982e216 < x.im

    1. Initial program 66.7%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around 0

      \[\leadsto \color{blue}{x.im \cdot \left(-1 \cdot {x.im}^{2} + \left(2 \cdot {x.re}^{2} + {x.re}^{2}\right)\right)} \]
    4. Applied rewrites86.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(3, x.re \cdot x.re, -x.im \cdot x.im\right) \cdot x.im} \]
    5. Taylor expanded in x.re around 0

      \[\leadsto \left(-1 \cdot {x.im}^{2}\right) \cdot x.im \]
    6. Applied rewrites100.0%

      \[\leadsto \left(\left(-x.im\right) \cdot x.im\right) \cdot x.im \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 96.0% accurate, 0.4× speedup?

\[\begin{array}{l} x.im\_m = \left|x.im\right| \\ x.im\_s = \mathsf{copysign}\left(1, x.im\right) \\ \begin{array}{l} t_0 := \left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\ t_1 := \left(x.re \cdot x.re - x.im\_m \cdot x.im\_m\right) \cdot x.im\_m + \left(x.re \cdot x.im\_m + x.im\_m \cdot x.re\right) \cdot x.re\\ x.im\_s \cdot \begin{array}{l} \mathbf{if}\;t\_1 \leq -1 \cdot 10^{-306}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;t\_1 \leq \infty:\\ \;\;\;\;\left(\left(3 \cdot x.im\_m\right) \cdot x.re\right) \cdot x.re\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \end{array} \]
x.im\_m = (fabs.f64 x.im)
x.im\_s = (copysign.f64 #s(literal 1 binary64) x.im)
(FPCore (x.im_s x.re x.im_m)
 :precision binary64
 (let* ((t_0 (* (* (- x.im_m) x.im_m) x.im_m))
        (t_1
         (+
          (* (- (* x.re x.re) (* x.im_m x.im_m)) x.im_m)
          (* (+ (* x.re x.im_m) (* x.im_m x.re)) x.re))))
   (*
    x.im_s
    (if (<= t_1 -1e-306)
      t_0
      (if (<= t_1 INFINITY) (* (* (* 3.0 x.im_m) x.re) x.re) t_0)))))
x.im\_m = fabs(x_46_im);
x.im\_s = copysign(1.0, x_46_im);
double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	double t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	double tmp;
	if (t_1 <= -1e-306) {
		tmp = t_0;
	} else if (t_1 <= ((double) INFINITY)) {
		tmp = ((3.0 * x_46_im_m) * x_46_re) * x_46_re;
	} else {
		tmp = t_0;
	}
	return x_46_im_s * tmp;
}
x.im\_m = Math.abs(x_46_im);
x.im\_s = Math.copySign(1.0, x_46_im);
public static double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	double t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	double tmp;
	if (t_1 <= -1e-306) {
		tmp = t_0;
	} else if (t_1 <= Double.POSITIVE_INFINITY) {
		tmp = ((3.0 * x_46_im_m) * x_46_re) * x_46_re;
	} else {
		tmp = t_0;
	}
	return x_46_im_s * tmp;
}
x.im\_m = math.fabs(x_46_im)
x.im\_s = math.copysign(1.0, x_46_im)
def code(x_46_im_s, x_46_re, x_46_im_m):
	t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m
	t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re)
	tmp = 0
	if t_1 <= -1e-306:
		tmp = t_0
	elif t_1 <= math.inf:
		tmp = ((3.0 * x_46_im_m) * x_46_re) * x_46_re
	else:
		tmp = t_0
	return x_46_im_s * tmp
x.im\_m = abs(x_46_im)
x.im\_s = copysign(1.0, x_46_im)
function code(x_46_im_s, x_46_re, x_46_im_m)
	t_0 = Float64(Float64(Float64(-x_46_im_m) * x_46_im_m) * x_46_im_m)
	t_1 = Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im_m * x_46_im_m)) * x_46_im_m) + Float64(Float64(Float64(x_46_re * x_46_im_m) + Float64(x_46_im_m * x_46_re)) * x_46_re))
	tmp = 0.0
	if (t_1 <= -1e-306)
		tmp = t_0;
	elseif (t_1 <= Inf)
		tmp = Float64(Float64(Float64(3.0 * x_46_im_m) * x_46_re) * x_46_re);
	else
		tmp = t_0;
	end
	return Float64(x_46_im_s * tmp)
end
x.im\_m = abs(x_46_im);
x.im\_s = sign(x_46_im) * abs(1.0);
function tmp_2 = code(x_46_im_s, x_46_re, x_46_im_m)
	t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	tmp = 0.0;
	if (t_1 <= -1e-306)
		tmp = t_0;
	elseif (t_1 <= Inf)
		tmp = ((3.0 * x_46_im_m) * x_46_re) * x_46_re;
	else
		tmp = t_0;
	end
	tmp_2 = x_46_im_s * tmp;
end
x.im\_m = N[Abs[x$46$im], $MachinePrecision]
x.im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x$46$im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$46$im$95$s_, x$46$re_, x$46$im$95$m_] := Block[{t$95$0 = N[(N[((-x$46$im$95$m) * x$46$im$95$m), $MachinePrecision] * x$46$im$95$m), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im$95$m * x$46$im$95$m), $MachinePrecision]), $MachinePrecision] * x$46$im$95$m), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im$95$m), $MachinePrecision] + N[(x$46$im$95$m * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]}, N[(x$46$im$95$s * If[LessEqual[t$95$1, -1e-306], t$95$0, If[LessEqual[t$95$1, Infinity], N[(N[(N[(3.0 * x$46$im$95$m), $MachinePrecision] * x$46$re), $MachinePrecision] * x$46$re), $MachinePrecision], t$95$0]]), $MachinePrecision]]]
\begin{array}{l}
x.im\_m = \left|x.im\right|
\\
x.im\_s = \mathsf{copysign}\left(1, x.im\right)

\\
\begin{array}{l}
t_0 := \left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\
t_1 := \left(x.re \cdot x.re - x.im\_m \cdot x.im\_m\right) \cdot x.im\_m + \left(x.re \cdot x.im\_m + x.im\_m \cdot x.re\right) \cdot x.re\\
x.im\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq -1 \cdot 10^{-306}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;t\_1 \leq \infty:\\
\;\;\;\;\left(\left(3 \cdot x.im\_m\right) \cdot x.re\right) \cdot x.re\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < -1.00000000000000003e-306 or +inf.0 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re))

    1. Initial program 69.6%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around 0

      \[\leadsto \color{blue}{x.im \cdot \left(-1 \cdot {x.im}^{2} + \left(2 \cdot {x.re}^{2} + {x.re}^{2}\right)\right)} \]
    4. Applied rewrites84.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(3, x.re \cdot x.re, -x.im \cdot x.im\right) \cdot x.im} \]
    5. Taylor expanded in x.re around 0

      \[\leadsto \left(-1 \cdot {x.im}^{2}\right) \cdot x.im \]
    6. Applied rewrites57.6%

      \[\leadsto \left(\left(-x.im\right) \cdot x.im\right) \cdot x.im \]

    if -1.00000000000000003e-306 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < +inf.0

    1. Initial program 94.1%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.re around inf

      \[\leadsto \color{blue}{{x.re}^{2} \cdot \left(x.im + 2 \cdot x.im\right)} \]
    4. Applied rewrites58.4%

      \[\leadsto \color{blue}{\left(3 \cdot x.im\right) \cdot \left(x.re \cdot x.re\right)} \]
    5. Applied rewrites64.1%

      \[\leadsto \color{blue}{\left(\left(3 \cdot x.im\right) \cdot x.re\right) \cdot x.re} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 96.0% accurate, 0.4× speedup?

\[\begin{array}{l} x.im\_m = \left|x.im\right| \\ x.im\_s = \mathsf{copysign}\left(1, x.im\right) \\ \begin{array}{l} t_0 := \left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\ t_1 := \left(x.re \cdot x.re - x.im\_m \cdot x.im\_m\right) \cdot x.im\_m + \left(x.re \cdot x.im\_m + x.im\_m \cdot x.re\right) \cdot x.re\\ x.im\_s \cdot \begin{array}{l} \mathbf{if}\;t\_1 \leq -1 \cdot 10^{-306}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;t\_1 \leq \infty:\\ \;\;\;\;\left(x.im\_m \cdot x.re\right) \cdot \left(3 \cdot x.re\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \end{array} \]
x.im\_m = (fabs.f64 x.im)
x.im\_s = (copysign.f64 #s(literal 1 binary64) x.im)
(FPCore (x.im_s x.re x.im_m)
 :precision binary64
 (let* ((t_0 (* (* (- x.im_m) x.im_m) x.im_m))
        (t_1
         (+
          (* (- (* x.re x.re) (* x.im_m x.im_m)) x.im_m)
          (* (+ (* x.re x.im_m) (* x.im_m x.re)) x.re))))
   (*
    x.im_s
    (if (<= t_1 -1e-306)
      t_0
      (if (<= t_1 INFINITY) (* (* x.im_m x.re) (* 3.0 x.re)) t_0)))))
x.im\_m = fabs(x_46_im);
x.im\_s = copysign(1.0, x_46_im);
double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	double t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	double tmp;
	if (t_1 <= -1e-306) {
		tmp = t_0;
	} else if (t_1 <= ((double) INFINITY)) {
		tmp = (x_46_im_m * x_46_re) * (3.0 * x_46_re);
	} else {
		tmp = t_0;
	}
	return x_46_im_s * tmp;
}
x.im\_m = Math.abs(x_46_im);
x.im\_s = Math.copySign(1.0, x_46_im);
public static double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	double t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	double tmp;
	if (t_1 <= -1e-306) {
		tmp = t_0;
	} else if (t_1 <= Double.POSITIVE_INFINITY) {
		tmp = (x_46_im_m * x_46_re) * (3.0 * x_46_re);
	} else {
		tmp = t_0;
	}
	return x_46_im_s * tmp;
}
x.im\_m = math.fabs(x_46_im)
x.im\_s = math.copysign(1.0, x_46_im)
def code(x_46_im_s, x_46_re, x_46_im_m):
	t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m
	t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re)
	tmp = 0
	if t_1 <= -1e-306:
		tmp = t_0
	elif t_1 <= math.inf:
		tmp = (x_46_im_m * x_46_re) * (3.0 * x_46_re)
	else:
		tmp = t_0
	return x_46_im_s * tmp
x.im\_m = abs(x_46_im)
x.im\_s = copysign(1.0, x_46_im)
function code(x_46_im_s, x_46_re, x_46_im_m)
	t_0 = Float64(Float64(Float64(-x_46_im_m) * x_46_im_m) * x_46_im_m)
	t_1 = Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im_m * x_46_im_m)) * x_46_im_m) + Float64(Float64(Float64(x_46_re * x_46_im_m) + Float64(x_46_im_m * x_46_re)) * x_46_re))
	tmp = 0.0
	if (t_1 <= -1e-306)
		tmp = t_0;
	elseif (t_1 <= Inf)
		tmp = Float64(Float64(x_46_im_m * x_46_re) * Float64(3.0 * x_46_re));
	else
		tmp = t_0;
	end
	return Float64(x_46_im_s * tmp)
end
x.im\_m = abs(x_46_im);
x.im\_s = sign(x_46_im) * abs(1.0);
function tmp_2 = code(x_46_im_s, x_46_re, x_46_im_m)
	t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	tmp = 0.0;
	if (t_1 <= -1e-306)
		tmp = t_0;
	elseif (t_1 <= Inf)
		tmp = (x_46_im_m * x_46_re) * (3.0 * x_46_re);
	else
		tmp = t_0;
	end
	tmp_2 = x_46_im_s * tmp;
end
x.im\_m = N[Abs[x$46$im], $MachinePrecision]
x.im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x$46$im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$46$im$95$s_, x$46$re_, x$46$im$95$m_] := Block[{t$95$0 = N[(N[((-x$46$im$95$m) * x$46$im$95$m), $MachinePrecision] * x$46$im$95$m), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im$95$m * x$46$im$95$m), $MachinePrecision]), $MachinePrecision] * x$46$im$95$m), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im$95$m), $MachinePrecision] + N[(x$46$im$95$m * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]}, N[(x$46$im$95$s * If[LessEqual[t$95$1, -1e-306], t$95$0, If[LessEqual[t$95$1, Infinity], N[(N[(x$46$im$95$m * x$46$re), $MachinePrecision] * N[(3.0 * x$46$re), $MachinePrecision]), $MachinePrecision], t$95$0]]), $MachinePrecision]]]
\begin{array}{l}
x.im\_m = \left|x.im\right|
\\
x.im\_s = \mathsf{copysign}\left(1, x.im\right)

\\
\begin{array}{l}
t_0 := \left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\
t_1 := \left(x.re \cdot x.re - x.im\_m \cdot x.im\_m\right) \cdot x.im\_m + \left(x.re \cdot x.im\_m + x.im\_m \cdot x.re\right) \cdot x.re\\
x.im\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq -1 \cdot 10^{-306}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;t\_1 \leq \infty:\\
\;\;\;\;\left(x.im\_m \cdot x.re\right) \cdot \left(3 \cdot x.re\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < -1.00000000000000003e-306 or +inf.0 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re))

    1. Initial program 69.6%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around 0

      \[\leadsto \color{blue}{x.im \cdot \left(-1 \cdot {x.im}^{2} + \left(2 \cdot {x.re}^{2} + {x.re}^{2}\right)\right)} \]
    4. Applied rewrites84.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(3, x.re \cdot x.re, -x.im \cdot x.im\right) \cdot x.im} \]
    5. Taylor expanded in x.re around 0

      \[\leadsto \left(-1 \cdot {x.im}^{2}\right) \cdot x.im \]
    6. Applied rewrites57.6%

      \[\leadsto \left(\left(-x.im\right) \cdot x.im\right) \cdot x.im \]

    if -1.00000000000000003e-306 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < +inf.0

    1. Initial program 94.1%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.re around inf

      \[\leadsto \color{blue}{{x.re}^{2} \cdot \left(x.im + 2 \cdot x.im\right)} \]
    4. Applied rewrites58.4%

      \[\leadsto \color{blue}{\left(3 \cdot x.im\right) \cdot \left(x.re \cdot x.re\right)} \]
    5. Applied rewrites64.1%

      \[\leadsto \color{blue}{\left(\left(3 \cdot x.im\right) \cdot x.re\right) \cdot x.re} \]
    6. Applied rewrites64.1%

      \[\leadsto \left(\left(x.re \cdot x.im\right) \cdot 3\right) \cdot x.re \]
    7. Applied rewrites64.1%

      \[\leadsto \color{blue}{\left(x.im \cdot x.re\right) \cdot \left(3 \cdot x.re\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 90.2% accurate, 0.4× speedup?

\[\begin{array}{l} x.im\_m = \left|x.im\right| \\ x.im\_s = \mathsf{copysign}\left(1, x.im\right) \\ \begin{array}{l} t_0 := \left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\ t_1 := \left(x.re \cdot x.re - x.im\_m \cdot x.im\_m\right) \cdot x.im\_m + \left(x.re \cdot x.im\_m + x.im\_m \cdot x.re\right) \cdot x.re\\ x.im\_s \cdot \begin{array}{l} \mathbf{if}\;t\_1 \leq -1 \cdot 10^{-306}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;t\_1 \leq \infty:\\ \;\;\;\;\left(3 \cdot x.im\_m\right) \cdot \left(x.re \cdot x.re\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \end{array} \]
x.im\_m = (fabs.f64 x.im)
x.im\_s = (copysign.f64 #s(literal 1 binary64) x.im)
(FPCore (x.im_s x.re x.im_m)
 :precision binary64
 (let* ((t_0 (* (* (- x.im_m) x.im_m) x.im_m))
        (t_1
         (+
          (* (- (* x.re x.re) (* x.im_m x.im_m)) x.im_m)
          (* (+ (* x.re x.im_m) (* x.im_m x.re)) x.re))))
   (*
    x.im_s
    (if (<= t_1 -1e-306)
      t_0
      (if (<= t_1 INFINITY) (* (* 3.0 x.im_m) (* x.re x.re)) t_0)))))
x.im\_m = fabs(x_46_im);
x.im\_s = copysign(1.0, x_46_im);
double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	double t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	double tmp;
	if (t_1 <= -1e-306) {
		tmp = t_0;
	} else if (t_1 <= ((double) INFINITY)) {
		tmp = (3.0 * x_46_im_m) * (x_46_re * x_46_re);
	} else {
		tmp = t_0;
	}
	return x_46_im_s * tmp;
}
x.im\_m = Math.abs(x_46_im);
x.im\_s = Math.copySign(1.0, x_46_im);
public static double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	double t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	double tmp;
	if (t_1 <= -1e-306) {
		tmp = t_0;
	} else if (t_1 <= Double.POSITIVE_INFINITY) {
		tmp = (3.0 * x_46_im_m) * (x_46_re * x_46_re);
	} else {
		tmp = t_0;
	}
	return x_46_im_s * tmp;
}
x.im\_m = math.fabs(x_46_im)
x.im\_s = math.copysign(1.0, x_46_im)
def code(x_46_im_s, x_46_re, x_46_im_m):
	t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m
	t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re)
	tmp = 0
	if t_1 <= -1e-306:
		tmp = t_0
	elif t_1 <= math.inf:
		tmp = (3.0 * x_46_im_m) * (x_46_re * x_46_re)
	else:
		tmp = t_0
	return x_46_im_s * tmp
x.im\_m = abs(x_46_im)
x.im\_s = copysign(1.0, x_46_im)
function code(x_46_im_s, x_46_re, x_46_im_m)
	t_0 = Float64(Float64(Float64(-x_46_im_m) * x_46_im_m) * x_46_im_m)
	t_1 = Float64(Float64(Float64(Float64(x_46_re * x_46_re) - Float64(x_46_im_m * x_46_im_m)) * x_46_im_m) + Float64(Float64(Float64(x_46_re * x_46_im_m) + Float64(x_46_im_m * x_46_re)) * x_46_re))
	tmp = 0.0
	if (t_1 <= -1e-306)
		tmp = t_0;
	elseif (t_1 <= Inf)
		tmp = Float64(Float64(3.0 * x_46_im_m) * Float64(x_46_re * x_46_re));
	else
		tmp = t_0;
	end
	return Float64(x_46_im_s * tmp)
end
x.im\_m = abs(x_46_im);
x.im\_s = sign(x_46_im) * abs(1.0);
function tmp_2 = code(x_46_im_s, x_46_re, x_46_im_m)
	t_0 = (-x_46_im_m * x_46_im_m) * x_46_im_m;
	t_1 = (((x_46_re * x_46_re) - (x_46_im_m * x_46_im_m)) * x_46_im_m) + (((x_46_re * x_46_im_m) + (x_46_im_m * x_46_re)) * x_46_re);
	tmp = 0.0;
	if (t_1 <= -1e-306)
		tmp = t_0;
	elseif (t_1 <= Inf)
		tmp = (3.0 * x_46_im_m) * (x_46_re * x_46_re);
	else
		tmp = t_0;
	end
	tmp_2 = x_46_im_s * tmp;
end
x.im\_m = N[Abs[x$46$im], $MachinePrecision]
x.im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x$46$im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$46$im$95$s_, x$46$re_, x$46$im$95$m_] := Block[{t$95$0 = N[(N[((-x$46$im$95$m) * x$46$im$95$m), $MachinePrecision] * x$46$im$95$m), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(x$46$re * x$46$re), $MachinePrecision] - N[(x$46$im$95$m * x$46$im$95$m), $MachinePrecision]), $MachinePrecision] * x$46$im$95$m), $MachinePrecision] + N[(N[(N[(x$46$re * x$46$im$95$m), $MachinePrecision] + N[(x$46$im$95$m * x$46$re), $MachinePrecision]), $MachinePrecision] * x$46$re), $MachinePrecision]), $MachinePrecision]}, N[(x$46$im$95$s * If[LessEqual[t$95$1, -1e-306], t$95$0, If[LessEqual[t$95$1, Infinity], N[(N[(3.0 * x$46$im$95$m), $MachinePrecision] * N[(x$46$re * x$46$re), $MachinePrecision]), $MachinePrecision], t$95$0]]), $MachinePrecision]]]
\begin{array}{l}
x.im\_m = \left|x.im\right|
\\
x.im\_s = \mathsf{copysign}\left(1, x.im\right)

\\
\begin{array}{l}
t_0 := \left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\\
t_1 := \left(x.re \cdot x.re - x.im\_m \cdot x.im\_m\right) \cdot x.im\_m + \left(x.re \cdot x.im\_m + x.im\_m \cdot x.re\right) \cdot x.re\\
x.im\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq -1 \cdot 10^{-306}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;t\_1 \leq \infty:\\
\;\;\;\;\left(3 \cdot x.im\_m\right) \cdot \left(x.re \cdot x.re\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < -1.00000000000000003e-306 or +inf.0 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re))

    1. Initial program 69.6%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around 0

      \[\leadsto \color{blue}{x.im \cdot \left(-1 \cdot {x.im}^{2} + \left(2 \cdot {x.re}^{2} + {x.re}^{2}\right)\right)} \]
    4. Applied rewrites84.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(3, x.re \cdot x.re, -x.im \cdot x.im\right) \cdot x.im} \]
    5. Taylor expanded in x.re around 0

      \[\leadsto \left(-1 \cdot {x.im}^{2}\right) \cdot x.im \]
    6. Applied rewrites57.6%

      \[\leadsto \left(\left(-x.im\right) \cdot x.im\right) \cdot x.im \]

    if -1.00000000000000003e-306 < (+.f64 (*.f64 (-.f64 (*.f64 x.re x.re) (*.f64 x.im x.im)) x.im) (*.f64 (+.f64 (*.f64 x.re x.im) (*.f64 x.im x.re)) x.re)) < +inf.0

    1. Initial program 94.1%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.re around inf

      \[\leadsto \color{blue}{{x.re}^{2} \cdot \left(x.im + 2 \cdot x.im\right)} \]
    4. Applied rewrites58.4%

      \[\leadsto \color{blue}{\left(3 \cdot x.im\right) \cdot \left(x.re \cdot x.re\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 92.4% accurate, 1.3× speedup?

\[\begin{array}{l} x.im\_m = \left|x.im\right| \\ x.im\_s = \mathsf{copysign}\left(1, x.im\right) \\ x.im\_s \cdot \begin{array}{l} \mathbf{if}\;x.re \leq 5 \cdot 10^{+153}:\\ \;\;\;\;\mathsf{fma}\left(3, x.re \cdot x.re, -x.im\_m \cdot x.im\_m\right) \cdot x.im\_m\\ \mathbf{else}:\\ \;\;\;\;\left(\left(3 \cdot x.im\_m\right) \cdot x.re\right) \cdot x.re\\ \end{array} \end{array} \]
x.im\_m = (fabs.f64 x.im)
x.im\_s = (copysign.f64 #s(literal 1 binary64) x.im)
(FPCore (x.im_s x.re x.im_m)
 :precision binary64
 (*
  x.im_s
  (if (<= x.re 5e+153)
    (* (fma 3.0 (* x.re x.re) (- (* x.im_m x.im_m))) x.im_m)
    (* (* (* 3.0 x.im_m) x.re) x.re))))
x.im\_m = fabs(x_46_im);
x.im\_s = copysign(1.0, x_46_im);
double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	double tmp;
	if (x_46_re <= 5e+153) {
		tmp = fma(3.0, (x_46_re * x_46_re), -(x_46_im_m * x_46_im_m)) * x_46_im_m;
	} else {
		tmp = ((3.0 * x_46_im_m) * x_46_re) * x_46_re;
	}
	return x_46_im_s * tmp;
}
x.im\_m = abs(x_46_im)
x.im\_s = copysign(1.0, x_46_im)
function code(x_46_im_s, x_46_re, x_46_im_m)
	tmp = 0.0
	if (x_46_re <= 5e+153)
		tmp = Float64(fma(3.0, Float64(x_46_re * x_46_re), Float64(-Float64(x_46_im_m * x_46_im_m))) * x_46_im_m);
	else
		tmp = Float64(Float64(Float64(3.0 * x_46_im_m) * x_46_re) * x_46_re);
	end
	return Float64(x_46_im_s * tmp)
end
x.im\_m = N[Abs[x$46$im], $MachinePrecision]
x.im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x$46$im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$46$im$95$s_, x$46$re_, x$46$im$95$m_] := N[(x$46$im$95$s * If[LessEqual[x$46$re, 5e+153], N[(N[(3.0 * N[(x$46$re * x$46$re), $MachinePrecision] + (-N[(x$46$im$95$m * x$46$im$95$m), $MachinePrecision])), $MachinePrecision] * x$46$im$95$m), $MachinePrecision], N[(N[(N[(3.0 * x$46$im$95$m), $MachinePrecision] * x$46$re), $MachinePrecision] * x$46$re), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x.im\_m = \left|x.im\right|
\\
x.im\_s = \mathsf{copysign}\left(1, x.im\right)

\\
x.im\_s \cdot \begin{array}{l}
\mathbf{if}\;x.re \leq 5 \cdot 10^{+153}:\\
\;\;\;\;\mathsf{fma}\left(3, x.re \cdot x.re, -x.im\_m \cdot x.im\_m\right) \cdot x.im\_m\\

\mathbf{else}:\\
\;\;\;\;\left(\left(3 \cdot x.im\_m\right) \cdot x.re\right) \cdot x.re\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x.re < 5.00000000000000018e153

    1. Initial program 86.3%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.im around 0

      \[\leadsto \color{blue}{x.im \cdot \left(-1 \cdot {x.im}^{2} + \left(2 \cdot {x.re}^{2} + {x.re}^{2}\right)\right)} \]
    4. Applied rewrites94.2%

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

    if 5.00000000000000018e153 < x.re

    1. Initial program 53.2%

      \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
    2. Add Preprocessing
    3. Taylor expanded in x.re around inf

      \[\leadsto \color{blue}{{x.re}^{2} \cdot \left(x.im + 2 \cdot x.im\right)} \]
    4. Applied rewrites69.3%

      \[\leadsto \color{blue}{\left(3 \cdot x.im\right) \cdot \left(x.re \cdot x.re\right)} \]
    5. Applied rewrites93.5%

      \[\leadsto \color{blue}{\left(\left(3 \cdot x.im\right) \cdot x.re\right) \cdot x.re} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 58.2% accurate, 3.1× speedup?

\[\begin{array}{l} x.im\_m = \left|x.im\right| \\ x.im\_s = \mathsf{copysign}\left(1, x.im\right) \\ x.im\_s \cdot \left(\left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\right) \end{array} \]
x.im\_m = (fabs.f64 x.im)
x.im\_s = (copysign.f64 #s(literal 1 binary64) x.im)
(FPCore (x.im_s x.re x.im_m)
 :precision binary64
 (* x.im_s (* (* (- x.im_m) x.im_m) x.im_m)))
x.im\_m = fabs(x_46_im);
x.im\_s = copysign(1.0, x_46_im);
double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	return x_46_im_s * ((-x_46_im_m * x_46_im_m) * x_46_im_m);
}
x.im\_m =     private
x.im\_s =     private
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_46im_s, x_46re, x_46im_m)
use fmin_fmax_functions
    real(8), intent (in) :: x_46im_s
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im_m
    code = x_46im_s * ((-x_46im_m * x_46im_m) * x_46im_m)
end function
x.im\_m = Math.abs(x_46_im);
x.im\_s = Math.copySign(1.0, x_46_im);
public static double code(double x_46_im_s, double x_46_re, double x_46_im_m) {
	return x_46_im_s * ((-x_46_im_m * x_46_im_m) * x_46_im_m);
}
x.im\_m = math.fabs(x_46_im)
x.im\_s = math.copysign(1.0, x_46_im)
def code(x_46_im_s, x_46_re, x_46_im_m):
	return x_46_im_s * ((-x_46_im_m * x_46_im_m) * x_46_im_m)
x.im\_m = abs(x_46_im)
x.im\_s = copysign(1.0, x_46_im)
function code(x_46_im_s, x_46_re, x_46_im_m)
	return Float64(x_46_im_s * Float64(Float64(Float64(-x_46_im_m) * x_46_im_m) * x_46_im_m))
end
x.im\_m = abs(x_46_im);
x.im\_s = sign(x_46_im) * abs(1.0);
function tmp = code(x_46_im_s, x_46_re, x_46_im_m)
	tmp = x_46_im_s * ((-x_46_im_m * x_46_im_m) * x_46_im_m);
end
x.im\_m = N[Abs[x$46$im], $MachinePrecision]
x.im\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x$46$im]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$46$im$95$s_, x$46$re_, x$46$im$95$m_] := N[(x$46$im$95$s * N[(N[((-x$46$im$95$m) * x$46$im$95$m), $MachinePrecision] * x$46$im$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x.im\_m = \left|x.im\right|
\\
x.im\_s = \mathsf{copysign}\left(1, x.im\right)

\\
x.im\_s \cdot \left(\left(\left(-x.im\_m\right) \cdot x.im\_m\right) \cdot x.im\_m\right)
\end{array}
Derivation
  1. Initial program 82.3%

    \[\left(x.re \cdot x.re - x.im \cdot x.im\right) \cdot x.im + \left(x.re \cdot x.im + x.im \cdot x.re\right) \cdot x.re \]
  2. Add Preprocessing
  3. Taylor expanded in x.im around 0

    \[\leadsto \color{blue}{x.im \cdot \left(-1 \cdot {x.im}^{2} + \left(2 \cdot {x.re}^{2} + {x.re}^{2}\right)\right)} \]
  4. Applied rewrites89.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(3, x.re \cdot x.re, -x.im \cdot x.im\right) \cdot x.im} \]
  5. Taylor expanded in x.re around 0

    \[\leadsto \left(-1 \cdot {x.im}^{2}\right) \cdot x.im \]
  6. Applied rewrites61.4%

    \[\leadsto \left(\left(-x.im\right) \cdot x.im\right) \cdot x.im \]
  7. Add Preprocessing

Developer Target 1: 91.1% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \left(x.re \cdot x.im\right) \cdot \left(2 \cdot x.re\right) + \left(x.im \cdot \left(x.re - x.im\right)\right) \cdot \left(x.re + x.im\right) \end{array} \]
(FPCore (x.re x.im)
 :precision binary64
 (+ (* (* x.re x.im) (* 2.0 x.re)) (* (* x.im (- x.re x.im)) (+ x.re x.im))))
double code(double x_46_re, double x_46_im) {
	return ((x_46_re * x_46_im) * (2.0 * x_46_re)) + ((x_46_im * (x_46_re - x_46_im)) * (x_46_re + x_46_im));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_46re, x_46im)
use fmin_fmax_functions
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    code = ((x_46re * x_46im) * (2.0d0 * x_46re)) + ((x_46im * (x_46re - x_46im)) * (x_46re + x_46im))
end function
public static double code(double x_46_re, double x_46_im) {
	return ((x_46_re * x_46_im) * (2.0 * x_46_re)) + ((x_46_im * (x_46_re - x_46_im)) * (x_46_re + x_46_im));
}
def code(x_46_re, x_46_im):
	return ((x_46_re * x_46_im) * (2.0 * x_46_re)) + ((x_46_im * (x_46_re - x_46_im)) * (x_46_re + x_46_im))
function code(x_46_re, x_46_im)
	return Float64(Float64(Float64(x_46_re * x_46_im) * Float64(2.0 * x_46_re)) + Float64(Float64(x_46_im * Float64(x_46_re - x_46_im)) * Float64(x_46_re + x_46_im)))
end
function tmp = code(x_46_re, x_46_im)
	tmp = ((x_46_re * x_46_im) * (2.0 * x_46_re)) + ((x_46_im * (x_46_re - x_46_im)) * (x_46_re + x_46_im));
end
code[x$46$re_, x$46$im_] := N[(N[(N[(x$46$re * x$46$im), $MachinePrecision] * N[(2.0 * x$46$re), $MachinePrecision]), $MachinePrecision] + N[(N[(x$46$im * N[(x$46$re - x$46$im), $MachinePrecision]), $MachinePrecision] * N[(x$46$re + x$46$im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x.re \cdot x.im\right) \cdot \left(2 \cdot x.re\right) + \left(x.im \cdot \left(x.re - x.im\right)\right) \cdot \left(x.re + x.im\right)
\end{array}

Reproduce

?
herbie shell --seed 2025036 -o generate:simplify -o generate:proofs
(FPCore (x.re x.im)
  :name "math.cube on complex, imaginary part"
  :precision binary64

  :alt
  (! :herbie-platform default (+ (* (* x.re x.im) (* 2 x.re)) (* (* x.im (- x.re x.im)) (+ x.re x.im))))

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