Linear.Quaternion:$c/ from linear-1.19.1.3, E

Percentage Accurate: 99.9% → 99.9%
Time: 9.7s
Alternatives: 8
Speedup: 1.7×

Specification

?
\[\begin{array}{l} \\ \left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
double code(double x, double y) {
	return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (((x * x) + (y * y)) + (y * y)) + (y * y)
end function
public static double code(double x, double y) {
	return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
def code(x, y):
	return (((x * x) + (y * y)) + (y * y)) + (y * y)
function code(x, y)
	return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y))
end
function tmp = code(x, y)
	tmp = (((x * x) + (y * y)) + (y * y)) + (y * y);
end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\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 8 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: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
double code(double x, double y) {
	return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (((x * x) + (y * y)) + (y * y)) + (y * y)
end function
public static double code(double x, double y) {
	return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
def code(x, y):
	return (((x * x) + (y * y)) + (y * y)) + (y * y)
function code(x, y)
	return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y))
end
function tmp = code(x, y)
	tmp = (((x * x) + (y * y)) + (y * y)) + (y * y);
end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\end{array}

Alternative 1: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y, y \cdot 2, x \cdot x + y \cdot y\right) \end{array} \]
(FPCore (x y) :precision binary64 (fma y (* y 2.0) (+ (* x x) (* y y))))
double code(double x, double y) {
	return fma(y, (y * 2.0), ((x * x) + (y * y)));
}
function code(x, y)
	return fma(y, Float64(y * 2.0), Float64(Float64(x * x) + Float64(y * y)))
end
code[x_, y_] := N[(y * N[(y * 2.0), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y, y \cdot 2, x \cdot x + y \cdot y\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
  2. Step-by-step derivation
    1. associate-+l+N/A

      \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
    2. associate-+l+N/A

      \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
    3. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
    5. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
    6. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    7. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    8. count-2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
    9. distribute-rgt1-inN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
    10. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    11. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    12. metadata-eval99.9%

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. associate-*r*N/A

      \[\leadsto x \cdot x + \left(y \cdot y\right) \cdot \color{blue}{3} \]
    2. *-commutativeN/A

      \[\leadsto x \cdot x + 3 \cdot \color{blue}{\left(y \cdot y\right)} \]
    3. metadata-evalN/A

      \[\leadsto x \cdot x + \left(2 + 1\right) \cdot \left(\color{blue}{y} \cdot y\right) \]
    4. distribute-rgt1-inN/A

      \[\leadsto x \cdot x + \left(y \cdot y + \color{blue}{2 \cdot \left(y \cdot y\right)}\right) \]
    5. count-2N/A

      \[\leadsto x \cdot x + \left(y \cdot y + \left(y \cdot y + \color{blue}{y \cdot y}\right)\right) \]
    6. associate-+l+N/A

      \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
    7. +-commutativeN/A

      \[\leadsto \left(y \cdot y + y \cdot y\right) + \color{blue}{\left(x \cdot x + y \cdot y\right)} \]
    8. distribute-lft-outN/A

      \[\leadsto y \cdot \left(y + y\right) + \left(\color{blue}{x \cdot x} + y \cdot y\right) \]
    9. fma-defineN/A

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{y + y}, x \cdot x + y \cdot y\right) \]
    10. fma-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma.f64}\left(y, \color{blue}{\left(y + y\right)}, \left(x \cdot x + y \cdot y\right)\right) \]
    11. count-2N/A

      \[\leadsto \mathsf{fma.f64}\left(y, \left(2 \cdot \color{blue}{y}\right), \left(x \cdot x + y \cdot y\right)\right) \]
    12. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma.f64}\left(y, \mathsf{*.f64}\left(2, \color{blue}{y}\right), \left(x \cdot x + y \cdot y\right)\right) \]
    13. +-lowering-+.f64N/A

      \[\leadsto \mathsf{fma.f64}\left(y, \mathsf{*.f64}\left(2, y\right), \mathsf{+.f64}\left(\left(x \cdot x\right), \left(y \cdot y\right)\right)\right) \]
    14. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma.f64}\left(y, \mathsf{*.f64}\left(2, y\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y\right)\right)\right) \]
    15. *-lowering-*.f64100.0%

      \[\leadsto \mathsf{fma.f64}\left(y, \mathsf{*.f64}\left(2, y\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, y\right)\right)\right) \]
  6. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y, 2 \cdot y, x \cdot x + y \cdot y\right)} \]
  7. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(y, y \cdot 2, x \cdot x + y \cdot y\right) \]
  8. Add Preprocessing

Alternative 2: 99.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y \cdot y, 3, x \cdot x\right) \end{array} \]
(FPCore (x y) :precision binary64 (fma (* y y) 3.0 (* x x)))
double code(double x, double y) {
	return fma((y * y), 3.0, (x * x));
}
function code(x, y)
	return fma(Float64(y * y), 3.0, Float64(x * x))
end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] * 3.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y \cdot y, 3, x \cdot x\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
  2. Step-by-step derivation
    1. associate-+l+N/A

      \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
    2. associate-+l+N/A

      \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
    3. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
    5. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
    6. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    7. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    8. count-2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
    9. distribute-rgt1-inN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
    10. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    11. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    12. metadata-eval99.9%

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto y \cdot \left(y \cdot 3\right) + \color{blue}{x \cdot x} \]
    2. associate-*r*N/A

      \[\leadsto \left(y \cdot y\right) \cdot 3 + \color{blue}{x} \cdot x \]
    3. fma-defineN/A

      \[\leadsto \mathsf{fma}\left(y \cdot y, \color{blue}{3}, x \cdot x\right) \]
    4. fma-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma.f64}\left(\left(y \cdot y\right), \color{blue}{3}, \left(x \cdot x\right)\right) \]
    5. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma.f64}\left(\mathsf{*.f64}\left(y, y\right), 3, \left(x \cdot x\right)\right) \]
    6. *-lowering-*.f6499.9%

      \[\leadsto \mathsf{fma.f64}\left(\mathsf{*.f64}\left(y, y\right), 3, \mathsf{*.f64}\left(x, x\right)\right) \]
  6. Applied egg-rr99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y \cdot y, 3, x \cdot x\right)} \]
  7. Add Preprocessing

Alternative 3: 89.3% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 2.7 \cdot 10^{-284}:\\ \;\;\;\;\frac{y \cdot \left(y \cdot 9\right)}{3}\\ \mathbf{else}:\\ \;\;\;\;x \cdot x + y \cdot y\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* x x) 2.7e-284) (/ (* y (* y 9.0)) 3.0) (+ (* x x) (* y y))))
double code(double x, double y) {
	double tmp;
	if ((x * x) <= 2.7e-284) {
		tmp = (y * (y * 9.0)) / 3.0;
	} else {
		tmp = (x * x) + (y * y);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x * x) <= 2.7d-284) then
        tmp = (y * (y * 9.0d0)) / 3.0d0
    else
        tmp = (x * x) + (y * y)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x * x) <= 2.7e-284) {
		tmp = (y * (y * 9.0)) / 3.0;
	} else {
		tmp = (x * x) + (y * y);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x * x) <= 2.7e-284:
		tmp = (y * (y * 9.0)) / 3.0
	else:
		tmp = (x * x) + (y * y)
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(x * x) <= 2.7e-284)
		tmp = Float64(Float64(y * Float64(y * 9.0)) / 3.0);
	else
		tmp = Float64(Float64(x * x) + Float64(y * y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x * x) <= 2.7e-284)
		tmp = (y * (y * 9.0)) / 3.0;
	else
		tmp = (x * x) + (y * y);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 2.7e-284], N[(N[(y * N[(y * 9.0), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision], N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 2.7 \cdot 10^{-284}:\\
\;\;\;\;\frac{y \cdot \left(y \cdot 9\right)}{3}\\

\mathbf{else}:\\
\;\;\;\;x \cdot x + y \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 2.69999999999999984e-284

    1. Initial program 99.7%

      \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
    2. Step-by-step derivation
      1. associate-+l+N/A

        \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
      2. associate-+l+N/A

        \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
      3. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
      5. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
      6. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      8. count-2N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
      9. distribute-rgt1-inN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      12. metadata-eval99.7%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
    3. Simplified99.7%

      \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 \cdot {y}^{2}} \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(3, \color{blue}{\left({y}^{2}\right)}\right) \]
      2. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(3, \left(y \cdot \color{blue}{y}\right)\right) \]
      3. *-lowering-*.f6498.4%

        \[\leadsto \mathsf{*.f64}\left(3, \mathsf{*.f64}\left(y, \color{blue}{y}\right)\right) \]
    7. Simplified98.4%

      \[\leadsto \color{blue}{3 \cdot \left(y \cdot y\right)} \]
    8. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \left(3 \cdot y\right) \cdot \color{blue}{y} \]
      2. *-commutativeN/A

        \[\leadsto \left(y \cdot 3\right) \cdot y \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\left(y \cdot 3\right), \color{blue}{y}\right) \]
      4. *-lowering-*.f6498.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(y, 3\right), y\right) \]
    9. Applied egg-rr98.3%

      \[\leadsto \color{blue}{\left(y \cdot 3\right) \cdot y} \]
    10. Step-by-step derivation
      1. associate-*l*N/A

        \[\leadsto y \cdot \color{blue}{\left(3 \cdot y\right)} \]
      2. metadata-evalN/A

        \[\leadsto y \cdot \left(\left(2 + 1\right) \cdot y\right) \]
      3. distribute-lft1-inN/A

        \[\leadsto y \cdot \left(2 \cdot y + \color{blue}{y}\right) \]
      4. distribute-lft-outN/A

        \[\leadsto y \cdot \left(2 \cdot y\right) + \color{blue}{y \cdot y} \]
      5. flip3-+N/A

        \[\leadsto \frac{{\left(y \cdot \left(2 \cdot y\right)\right)}^{3} + {\left(y \cdot y\right)}^{3}}{\color{blue}{\left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot \left(2 \cdot y\right)\right) + \left(\left(y \cdot y\right) \cdot \left(y \cdot y\right) - \left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot y\right)\right)}} \]
      6. clear-numN/A

        \[\leadsto \frac{1}{\color{blue}{\frac{\left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot \left(2 \cdot y\right)\right) + \left(\left(y \cdot y\right) \cdot \left(y \cdot y\right) - \left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot y\right)\right)}{{\left(y \cdot \left(2 \cdot y\right)\right)}^{3} + {\left(y \cdot y\right)}^{3}}}} \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(1, \color{blue}{\left(\frac{\left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot \left(2 \cdot y\right)\right) + \left(\left(y \cdot y\right) \cdot \left(y \cdot y\right) - \left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot y\right)\right)}{{\left(y \cdot \left(2 \cdot y\right)\right)}^{3} + {\left(y \cdot y\right)}^{3}}\right)}\right) \]
      8. clear-numN/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{\color{blue}{\frac{{\left(y \cdot \left(2 \cdot y\right)\right)}^{3} + {\left(y \cdot y\right)}^{3}}{\left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot \left(2 \cdot y\right)\right) + \left(\left(y \cdot y\right) \cdot \left(y \cdot y\right) - \left(y \cdot \left(2 \cdot y\right)\right) \cdot \left(y \cdot y\right)\right)}}}\right)\right) \]
      9. flip3-+N/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{y \cdot \left(2 \cdot y\right) + \color{blue}{y \cdot y}}\right)\right) \]
      10. distribute-lft-outN/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{y \cdot \color{blue}{\left(2 \cdot y + y\right)}}\right)\right) \]
      11. distribute-lft1-inN/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{y \cdot \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)}\right)\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{y \cdot \left(3 \cdot y\right)}\right)\right) \]
      13. associate-*l*N/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{\left(y \cdot 3\right) \cdot \color{blue}{y}}\right)\right) \]
      14. *-commutativeN/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{\left(3 \cdot y\right) \cdot y}\right)\right) \]
      15. associate-*r*N/A

        \[\leadsto \mathsf{/.f64}\left(1, \left(\frac{1}{3 \cdot \color{blue}{\left(y \cdot y\right)}}\right)\right) \]
    11. Applied egg-rr98.2%

      \[\leadsto \color{blue}{\frac{1}{\frac{0.3333333333333333}{y \cdot y}}} \]
    12. Step-by-step derivation
      1. clear-numN/A

        \[\leadsto \frac{y \cdot y}{\color{blue}{\frac{1}{3}}} \]
      2. div-invN/A

        \[\leadsto \left(y \cdot y\right) \cdot \color{blue}{\frac{1}{\frac{1}{3}}} \]
      3. metadata-evalN/A

        \[\leadsto \left(y \cdot y\right) \cdot 3 \]
      4. metadata-evalN/A

        \[\leadsto \left(y \cdot y\right) \cdot \frac{9}{\color{blue}{3}} \]
      5. associate-/l*N/A

        \[\leadsto \frac{\left(y \cdot y\right) \cdot 9}{\color{blue}{3}} \]
      6. associate-*r*N/A

        \[\leadsto \frac{y \cdot \left(y \cdot 9\right)}{3} \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\left(y \cdot \left(y \cdot 9\right)\right), \color{blue}{3}\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(y, \left(y \cdot 9\right)\right), 3\right) \]
      9. *-lowering-*.f6498.4%

        \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 9\right)\right), 3\right) \]
    13. Applied egg-rr98.4%

      \[\leadsto \color{blue}{\frac{y \cdot \left(y \cdot 9\right)}{3}} \]

    if 2.69999999999999984e-284 < (*.f64 x x)

    1. Initial program 99.9%

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

      \[\leadsto \mathsf{+.f64}\left(\color{blue}{\left({x}^{2}\right)}, \mathsf{*.f64}\left(y, y\right)\right) \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \mathsf{*.f64}\left(\color{blue}{y}, y\right)\right) \]
      2. *-lowering-*.f6488.5%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(\color{blue}{y}, y\right)\right) \]
    5. Simplified88.5%

      \[\leadsto \color{blue}{x \cdot x} + y \cdot y \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 89.3% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 4.1 \cdot 10^{-286}:\\ \;\;\;\;\left(y \cdot y\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x + y \cdot y\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* x x) 4.1e-286) (* (* y y) 3.0) (+ (* x x) (* y y))))
double code(double x, double y) {
	double tmp;
	if ((x * x) <= 4.1e-286) {
		tmp = (y * y) * 3.0;
	} else {
		tmp = (x * x) + (y * y);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x * x) <= 4.1d-286) then
        tmp = (y * y) * 3.0d0
    else
        tmp = (x * x) + (y * y)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x * x) <= 4.1e-286) {
		tmp = (y * y) * 3.0;
	} else {
		tmp = (x * x) + (y * y);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x * x) <= 4.1e-286:
		tmp = (y * y) * 3.0
	else:
		tmp = (x * x) + (y * y)
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(x * x) <= 4.1e-286)
		tmp = Float64(Float64(y * y) * 3.0);
	else
		tmp = Float64(Float64(x * x) + Float64(y * y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x * x) <= 4.1e-286)
		tmp = (y * y) * 3.0;
	else
		tmp = (x * x) + (y * y);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 4.1e-286], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 4.1 \cdot 10^{-286}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\

\mathbf{else}:\\
\;\;\;\;x \cdot x + y \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 4.1e-286

    1. Initial program 99.7%

      \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
    2. Step-by-step derivation
      1. associate-+l+N/A

        \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
      2. associate-+l+N/A

        \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
      3. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
      5. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
      6. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      8. count-2N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
      9. distribute-rgt1-inN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      12. metadata-eval99.7%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
    3. Simplified99.7%

      \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 \cdot {y}^{2}} \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(3, \color{blue}{\left({y}^{2}\right)}\right) \]
      2. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(3, \left(y \cdot \color{blue}{y}\right)\right) \]
      3. *-lowering-*.f6498.4%

        \[\leadsto \mathsf{*.f64}\left(3, \mathsf{*.f64}\left(y, \color{blue}{y}\right)\right) \]
    7. Simplified98.4%

      \[\leadsto \color{blue}{3 \cdot \left(y \cdot y\right)} \]

    if 4.1e-286 < (*.f64 x x)

    1. Initial program 99.9%

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

      \[\leadsto \mathsf{+.f64}\left(\color{blue}{\left({x}^{2}\right)}, \mathsf{*.f64}\left(y, y\right)\right) \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \mathsf{*.f64}\left(\color{blue}{y}, y\right)\right) \]
      2. *-lowering-*.f6488.5%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(\color{blue}{y}, y\right)\right) \]
    5. Simplified88.5%

      \[\leadsto \color{blue}{x \cdot x} + y \cdot y \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 4.1 \cdot 10^{-286}:\\ \;\;\;\;\left(y \cdot y\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x + y \cdot y\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 81.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 5.4 \cdot 10^{+118}:\\ \;\;\;\;\left(y \cdot y\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* x x) 5.4e+118) (* (* y y) 3.0) (* x x)))
double code(double x, double y) {
	double tmp;
	if ((x * x) <= 5.4e+118) {
		tmp = (y * y) * 3.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x * x) <= 5.4d+118) then
        tmp = (y * y) * 3.0d0
    else
        tmp = x * x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x * x) <= 5.4e+118) {
		tmp = (y * y) * 3.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x * x) <= 5.4e+118:
		tmp = (y * y) * 3.0
	else:
		tmp = x * x
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(x * x) <= 5.4e+118)
		tmp = Float64(Float64(y * y) * 3.0);
	else
		tmp = Float64(x * x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x * x) <= 5.4e+118)
		tmp = (y * y) * 3.0;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 5.4e+118], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 5.4 \cdot 10^{+118}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 5.4e118

    1. Initial program 99.8%

      \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
    2. Step-by-step derivation
      1. associate-+l+N/A

        \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
      2. associate-+l+N/A

        \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
      3. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
      5. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
      6. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      8. count-2N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
      9. distribute-rgt1-inN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      12. metadata-eval99.8%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 \cdot {y}^{2}} \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(3, \color{blue}{\left({y}^{2}\right)}\right) \]
      2. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(3, \left(y \cdot \color{blue}{y}\right)\right) \]
      3. *-lowering-*.f6482.6%

        \[\leadsto \mathsf{*.f64}\left(3, \mathsf{*.f64}\left(y, \color{blue}{y}\right)\right) \]
    7. Simplified82.6%

      \[\leadsto \color{blue}{3 \cdot \left(y \cdot y\right)} \]

    if 5.4e118 < (*.f64 x x)

    1. Initial program 100.0%

      \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
    2. Step-by-step derivation
      1. associate-+l+N/A

        \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
      2. associate-+l+N/A

        \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
      3. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
      5. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
      6. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      8. count-2N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
      9. distribute-rgt1-inN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      12. metadata-eval100.0%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{{x}^{2}} \]
    6. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto x \cdot \color{blue}{x} \]
      2. *-lowering-*.f6487.0%

        \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{x}\right) \]
    7. Simplified87.0%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification84.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 5.4 \cdot 10^{+118}:\\ \;\;\;\;\left(y \cdot y\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 74.6% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq 4.7 \cdot 10^{+235}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot y\\ \end{array} \end{array} \]
(FPCore (x y) :precision binary64 (if (<= (* y y) 4.7e+235) (* x x) (* y y)))
double code(double x, double y) {
	double tmp;
	if ((y * y) <= 4.7e+235) {
		tmp = x * x;
	} else {
		tmp = y * y;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y * y) <= 4.7d+235) then
        tmp = x * x
    else
        tmp = y * y
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y * y) <= 4.7e+235) {
		tmp = x * x;
	} else {
		tmp = y * y;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y * y) <= 4.7e+235:
		tmp = x * x
	else:
		tmp = y * y
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(y * y) <= 4.7e+235)
		tmp = Float64(x * x);
	else
		tmp = Float64(y * y);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y * y) <= 4.7e+235)
		tmp = x * x;
	else
		tmp = y * y;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 4.7e+235], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 4.7 \cdot 10^{+235}:\\
\;\;\;\;x \cdot x\\

\mathbf{else}:\\
\;\;\;\;y \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 4.6999999999999999e235

    1. Initial program 99.9%

      \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
    2. Step-by-step derivation
      1. associate-+l+N/A

        \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
      2. associate-+l+N/A

        \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
      3. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
      5. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
      6. distribute-lft-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
      8. count-2N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
      9. distribute-rgt1-inN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
      10. *-commutativeN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
      12. metadata-eval99.9%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{{x}^{2}} \]
    6. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto x \cdot \color{blue}{x} \]
      2. *-lowering-*.f6473.0%

        \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{x}\right) \]
    7. Simplified73.0%

      \[\leadsto \color{blue}{x \cdot x} \]

    if 4.6999999999999999e235 < (*.f64 y y)

    1. Initial program 99.9%

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

      \[\leadsto \mathsf{+.f64}\left(\color{blue}{\left({x}^{2}\right)}, \mathsf{*.f64}\left(y, y\right)\right) \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \mathsf{*.f64}\left(\color{blue}{y}, y\right)\right) \]
      2. *-lowering-*.f6485.1%

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(\color{blue}{y}, y\right)\right) \]
    5. Simplified85.1%

      \[\leadsto \color{blue}{x \cdot x} + y \cdot y \]
    6. Taylor expanded in x around 0

      \[\leadsto \color{blue}{{y}^{2}} \]
    7. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto y \cdot \color{blue}{y} \]
      2. *-lowering-*.f6483.9%

        \[\leadsto \mathsf{*.f64}\left(y, \color{blue}{y}\right) \]
    8. Simplified83.9%

      \[\leadsto \color{blue}{y \cdot y} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 99.9% accurate, 1.7× speedup?

\[\begin{array}{l} \\ x \cdot x + y \cdot \left(y \cdot 3\right) \end{array} \]
(FPCore (x y) :precision binary64 (+ (* x x) (* y (* y 3.0))))
double code(double x, double y) {
	return (x * x) + (y * (y * 3.0));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x * x) + (y * (y * 3.0d0))
end function
public static double code(double x, double y) {
	return (x * x) + (y * (y * 3.0));
}
def code(x, y):
	return (x * x) + (y * (y * 3.0))
function code(x, y)
	return Float64(Float64(x * x) + Float64(y * Float64(y * 3.0)))
end
function tmp = code(x, y)
	tmp = (x * x) + (y * (y * 3.0));
end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x + y \cdot \left(y \cdot 3\right)
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
  2. Step-by-step derivation
    1. associate-+l+N/A

      \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
    2. associate-+l+N/A

      \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
    3. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
    5. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
    6. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    7. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    8. count-2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
    9. distribute-rgt1-inN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
    10. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    11. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    12. metadata-eval99.9%

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 8: 57.6% accurate, 5.0× speedup?

\[\begin{array}{l} \\ x \cdot x \end{array} \]
(FPCore (x y) :precision binary64 (* x x))
double code(double x, double y) {
	return x * x;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * x
end function
public static double code(double x, double y) {
	return x * x;
}
def code(x, y):
	return x * x
function code(x, y)
	return Float64(x * x)
end
function tmp = code(x, y)
	tmp = x * x;
end
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
  2. Step-by-step derivation
    1. associate-+l+N/A

      \[\leadsto \left(x \cdot x + y \cdot y\right) + \color{blue}{\left(y \cdot y + y \cdot y\right)} \]
    2. associate-+l+N/A

      \[\leadsto x \cdot x + \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)} \]
    3. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\left(x \cdot x\right), \color{blue}{\left(y \cdot y + \left(y \cdot y + y \cdot y\right)\right)}\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(\color{blue}{y \cdot y} + \left(y \cdot y + y \cdot y\right)\right)\right) \]
    5. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot y + y \cdot \color{blue}{\left(y + y\right)}\right)\right) \]
    6. distribute-lft-outN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \left(y \cdot \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    7. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \color{blue}{\left(y + \left(y + y\right)\right)}\right)\right) \]
    8. count-2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y + 2 \cdot \color{blue}{y}\right)\right)\right) \]
    9. distribute-rgt1-inN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(\left(2 + 1\right) \cdot \color{blue}{y}\right)\right)\right) \]
    10. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \left(y \cdot \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    11. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, \color{blue}{\left(2 + 1\right)}\right)\right)\right) \]
    12. metadata-eval99.9%

      \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(x, x\right), \mathsf{*.f64}\left(y, \mathsf{*.f64}\left(y, 3\right)\right)\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x \cdot x + y \cdot \left(y \cdot 3\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in x around inf

    \[\leadsto \color{blue}{{x}^{2}} \]
  6. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto x \cdot \color{blue}{x} \]
    2. *-lowering-*.f6459.9%

      \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{x}\right) \]
  7. Simplified59.9%

    \[\leadsto \color{blue}{x \cdot x} \]
  8. Add Preprocessing

Developer Target 1: 99.9% accurate, 1.4× speedup?

\[\begin{array}{l} \\ x \cdot x + y \cdot \left(y + \left(y + y\right)\right) \end{array} \]
(FPCore (x y) :precision binary64 (+ (* x x) (* y (+ y (+ y y)))))
double code(double x, double y) {
	return (x * x) + (y * (y + (y + y)));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x * x) + (y * (y + (y + y)))
end function
public static double code(double x, double y) {
	return (x * x) + (y * (y + (y + y)));
}
def code(x, y):
	return (x * x) + (y * (y + (y + y)))
function code(x, y)
	return Float64(Float64(x * x) + Float64(y * Float64(y + Float64(y + y))))
end
function tmp = code(x, y)
	tmp = (x * x) + (y * (y + (y + y)));
end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * N[(y + N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x + y \cdot \left(y + \left(y + y\right)\right)
\end{array}

Reproduce

?
herbie shell --seed 2024145 
(FPCore (x y)
  :name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
  :precision binary64

  :alt
  (! :herbie-platform default (+ (* x x) (* y (+ y (+ y y)))))

  (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))