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

Percentage Accurate: 99.9% → 100.0%
Time: 3.3s
Alternatives: 5
Speedup: 1.8×

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 5 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: 100.0% accurate, 1.3× speedup?

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

\\
\mathsf{fma}\left(y \cdot 2, y, \mathsf{fma}\left(y, y, x \cdot x\right)\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. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{2 \cdot y}, y, x \cdot x + y \cdot y\right) \]
    11. lower-*.f6499.9

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

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

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

      \[\leadsto \mathsf{fma}\left(2 \cdot y, y, \color{blue}{y \cdot y} + x \cdot x\right) \]
    15. lower-fma.f6499.9

      \[\leadsto \mathsf{fma}\left(2 \cdot y, y, \color{blue}{\mathsf{fma}\left(y, y, x \cdot x\right)}\right) \]
  4. Applied rewrites99.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(2 \cdot y, y, \mathsf{fma}\left(y, y, x \cdot x\right)\right)} \]
  5. Final simplification99.9%

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

Alternative 2: 81.2% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1.06 \cdot 10^{+67}:\\ \;\;\;\;\mathsf{fma}\left(y, y + y, y \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* x x) 1.06e+67) (fma y (+ y y) (* y y)) (* x x)))
double code(double x, double y) {
	double tmp;
	if ((x * x) <= 1.06e+67) {
		tmp = fma(y, (y + y), (y * y));
	} else {
		tmp = x * x;
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (Float64(x * x) <= 1.06e+67)
		tmp = fma(y, Float64(y + y), Float64(y * y));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 1.06e+67], N[(y * N[(y + y), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1.06 \cdot 10^{+67}:\\
\;\;\;\;\mathsf{fma}\left(y, y + y, y \cdot y\right)\\

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


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

    1. Initial program 99.7%

      \[\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 0

      \[\leadsto \color{blue}{2 \cdot {y}^{2} + {y}^{2}} \]
    4. Step-by-step derivation
      1. distribute-lft1-inN/A

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

        \[\leadsto \color{blue}{3} \cdot {y}^{2} \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{3 \cdot {y}^{2}} \]
      4. unpow2N/A

        \[\leadsto 3 \cdot \color{blue}{\left(y \cdot y\right)} \]
      5. lower-*.f6487.0

        \[\leadsto 3 \cdot \color{blue}{\left(y \cdot y\right)} \]
    5. Applied rewrites87.0%

      \[\leadsto \color{blue}{3 \cdot \left(y \cdot y\right)} \]
    6. Step-by-step derivation
      1. Applied rewrites87.2%

        \[\leadsto \mathsf{fma}\left(y, \color{blue}{y + y}, y \cdot y\right) \]

      if 1.0599999999999999e67 < (*.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. Add Preprocessing
      3. Step-by-step derivation
        1. lift-+.f64N/A

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

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

          \[\leadsto \left(\color{blue}{\left(\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right)}{x \cdot x - y \cdot y} - \frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)} + y \cdot y\right) + y \cdot y \]
        4. sub-negN/A

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

          \[\leadsto \left(\left(\color{blue}{\left(x \cdot x\right) \cdot \frac{x \cdot x}{x \cdot x - y \cdot y}} + \left(\mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right)\right) + y \cdot y\right) + y \cdot y \]
        6. lower-fma.f64N/A

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

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \color{blue}{\frac{x \cdot x}{x \cdot x - y \cdot y}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        8. lift-*.f64N/A

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{x \cdot x} - y \cdot y}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        9. lift-*.f64N/A

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{x \cdot x - \color{blue}{y \cdot y}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        10. difference-of-squaresN/A

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        11. *-commutativeN/A

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

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

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{\left(x - y\right)} \cdot \left(x + y\right)}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        14. +-commutativeN/A

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

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

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

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\left(x - y\right) \cdot \left(y + x\right)}, -\color{blue}{\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}}\right) + y \cdot y\right) + y \cdot y \]
      4. Applied rewrites27.4%

        \[\leadsto \left(\color{blue}{\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\left(x - y\right) \cdot \left(y + x\right)}, -\frac{{y}^{4}}{\left(x - y\right) \cdot \left(y + x\right)}\right)} + y \cdot y\right) + y \cdot y \]
      5. Taylor expanded in x around inf

        \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + -1 \cdot \frac{y + -1 \cdot y}{x}\right)} \]
      6. Step-by-step derivation
        1. associate-*r/N/A

          \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{\frac{-1 \cdot \left(y + -1 \cdot y\right)}{x}}\right) \]
        2. mul-1-negN/A

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

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{\left(-1 + 1\right) \cdot y}\right)}{x}\right) \]
        4. metadata-evalN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{0} \cdot y\right)}{x}\right) \]
        5. mul0-lftN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{0}\right)}{x}\right) \]
        6. metadata-evalN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\color{blue}{0}}{x}\right) \]
        7. mul0-lftN/A

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

          \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{0 \cdot \frac{y}{x}}\right) \]
        9. mul0-lftN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{0}\right) \]
        10. metadata-evalN/A

          \[\leadsto {x}^{2} \cdot \color{blue}{1} \]
        11. *-rgt-identityN/A

          \[\leadsto \color{blue}{{x}^{2}} \]
        12. unpow2N/A

          \[\leadsto \color{blue}{x \cdot x} \]
        13. lower-*.f6493.6

          \[\leadsto \color{blue}{x \cdot x} \]
      7. Applied rewrites93.6%

        \[\leadsto \color{blue}{x \cdot x} \]
    7. Recombined 2 regimes into one program.
    8. Add Preprocessing

    Alternative 3: 81.1% accurate, 1.4× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1.06 \cdot 10^{+67}:\\ \;\;\;\;\left(y \cdot y\right) \cdot 3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= (* x x) 1.06e+67) (* (* y y) 3.0) (* x x)))
    double code(double x, double y) {
    	double tmp;
    	if ((x * x) <= 1.06e+67) {
    		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) <= 1.06d+67) 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) <= 1.06e+67) {
    		tmp = (y * y) * 3.0;
    	} else {
    		tmp = x * x;
    	}
    	return tmp;
    }
    
    def code(x, y):
    	tmp = 0
    	if (x * x) <= 1.06e+67:
    		tmp = (y * y) * 3.0
    	else:
    		tmp = x * x
    	return tmp
    
    function code(x, y)
    	tmp = 0.0
    	if (Float64(x * x) <= 1.06e+67)
    		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) <= 1.06e+67)
    		tmp = (y * y) * 3.0;
    	else
    		tmp = x * x;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 1.06e+67], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], N[(x * x), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;x \cdot x \leq 1.06 \cdot 10^{+67}:\\
    \;\;\;\;\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) < 1.0599999999999999e67

      1. Initial program 99.7%

        \[\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 0

        \[\leadsto \color{blue}{2 \cdot {y}^{2} + {y}^{2}} \]
      4. Step-by-step derivation
        1. distribute-lft1-inN/A

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

          \[\leadsto \color{blue}{3} \cdot {y}^{2} \]
        3. lower-*.f64N/A

          \[\leadsto \color{blue}{3 \cdot {y}^{2}} \]
        4. unpow2N/A

          \[\leadsto 3 \cdot \color{blue}{\left(y \cdot y\right)} \]
        5. lower-*.f6487.0

          \[\leadsto 3 \cdot \color{blue}{\left(y \cdot y\right)} \]
      5. Applied rewrites87.0%

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

      if 1.0599999999999999e67 < (*.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. Add Preprocessing
      3. Step-by-step derivation
        1. lift-+.f64N/A

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

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

          \[\leadsto \left(\color{blue}{\left(\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right)}{x \cdot x - y \cdot y} - \frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)} + y \cdot y\right) + y \cdot y \]
        4. sub-negN/A

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

          \[\leadsto \left(\left(\color{blue}{\left(x \cdot x\right) \cdot \frac{x \cdot x}{x \cdot x - y \cdot y}} + \left(\mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right)\right) + y \cdot y\right) + y \cdot y \]
        6. lower-fma.f64N/A

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

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \color{blue}{\frac{x \cdot x}{x \cdot x - y \cdot y}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        8. lift-*.f64N/A

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{x \cdot x} - y \cdot y}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        9. lift-*.f64N/A

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{x \cdot x - \color{blue}{y \cdot y}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        10. difference-of-squaresN/A

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        11. *-commutativeN/A

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

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

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{\left(x - y\right)} \cdot \left(x + y\right)}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
        14. +-commutativeN/A

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

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

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

          \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\left(x - y\right) \cdot \left(y + x\right)}, -\color{blue}{\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}}\right) + y \cdot y\right) + y \cdot y \]
      4. Applied rewrites27.4%

        \[\leadsto \left(\color{blue}{\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\left(x - y\right) \cdot \left(y + x\right)}, -\frac{{y}^{4}}{\left(x - y\right) \cdot \left(y + x\right)}\right)} + y \cdot y\right) + y \cdot y \]
      5. Taylor expanded in x around inf

        \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + -1 \cdot \frac{y + -1 \cdot y}{x}\right)} \]
      6. Step-by-step derivation
        1. associate-*r/N/A

          \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{\frac{-1 \cdot \left(y + -1 \cdot y\right)}{x}}\right) \]
        2. mul-1-negN/A

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

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{\left(-1 + 1\right) \cdot y}\right)}{x}\right) \]
        4. metadata-evalN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{0} \cdot y\right)}{x}\right) \]
        5. mul0-lftN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{0}\right)}{x}\right) \]
        6. metadata-evalN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \frac{\color{blue}{0}}{x}\right) \]
        7. mul0-lftN/A

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

          \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{0 \cdot \frac{y}{x}}\right) \]
        9. mul0-lftN/A

          \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{0}\right) \]
        10. metadata-evalN/A

          \[\leadsto {x}^{2} \cdot \color{blue}{1} \]
        11. *-rgt-identityN/A

          \[\leadsto \color{blue}{{x}^{2}} \]
        12. unpow2N/A

          \[\leadsto \color{blue}{x \cdot x} \]
        13. lower-*.f6493.6

          \[\leadsto \color{blue}{x \cdot x} \]
      7. Applied rewrites93.6%

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

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

    Alternative 4: 99.9% accurate, 1.8× speedup?

    \[\begin{array}{l} \\ \mathsf{fma}\left(3, y \cdot y, x \cdot x\right) \end{array} \]
    (FPCore (x y) :precision binary64 (fma 3.0 (* y y) (* x x)))
    double code(double x, double y) {
    	return fma(3.0, (y * y), (x * x));
    }
    
    function code(x, y)
    	return fma(3.0, Float64(y * y), Float64(x * x))
    end
    
    code[x_, y_] := N[(3.0 * N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{fma}\left(3, y \cdot y, 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. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{3} \cdot \left(y \cdot y\right) + x \cdot x \]
      10. lower-fma.f6499.8

        \[\leadsto \color{blue}{\mathsf{fma}\left(3, y \cdot y, x \cdot x\right)} \]
    4. Applied rewrites99.8%

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

    Alternative 5: 57.8% 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. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

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

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

        \[\leadsto \left(\color{blue}{\left(\frac{\left(x \cdot x\right) \cdot \left(x \cdot x\right)}{x \cdot x - y \cdot y} - \frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)} + y \cdot y\right) + y \cdot y \]
      4. sub-negN/A

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

        \[\leadsto \left(\left(\color{blue}{\left(x \cdot x\right) \cdot \frac{x \cdot x}{x \cdot x - y \cdot y}} + \left(\mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right)\right) + y \cdot y\right) + y \cdot y \]
      6. lower-fma.f64N/A

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

        \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \color{blue}{\frac{x \cdot x}{x \cdot x - y \cdot y}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
      8. lift-*.f64N/A

        \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{x \cdot x} - y \cdot y}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
      9. lift-*.f64N/A

        \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{x \cdot x - \color{blue}{y \cdot y}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
      10. difference-of-squaresN/A

        \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
      11. *-commutativeN/A

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

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

        \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\color{blue}{\left(x - y\right)} \cdot \left(x + y\right)}, \mathsf{neg}\left(\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}\right)\right) + y \cdot y\right) + y \cdot y \]
      14. +-commutativeN/A

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

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

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

        \[\leadsto \left(\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\left(x - y\right) \cdot \left(y + x\right)}, -\color{blue}{\frac{\left(y \cdot y\right) \cdot \left(y \cdot y\right)}{x \cdot x - y \cdot y}}\right) + y \cdot y\right) + y \cdot y \]
    4. Applied rewrites36.9%

      \[\leadsto \left(\color{blue}{\mathsf{fma}\left(x \cdot x, \frac{x \cdot x}{\left(x - y\right) \cdot \left(y + x\right)}, -\frac{{y}^{4}}{\left(x - y\right) \cdot \left(y + x\right)}\right)} + y \cdot y\right) + y \cdot y \]
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + -1 \cdot \frac{y + -1 \cdot y}{x}\right)} \]
    6. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{\frac{-1 \cdot \left(y + -1 \cdot y\right)}{x}}\right) \]
      2. mul-1-negN/A

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

        \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{\left(-1 + 1\right) \cdot y}\right)}{x}\right) \]
      4. metadata-evalN/A

        \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{0} \cdot y\right)}{x}\right) \]
      5. mul0-lftN/A

        \[\leadsto {x}^{2} \cdot \left(1 + \frac{\mathsf{neg}\left(\color{blue}{0}\right)}{x}\right) \]
      6. metadata-evalN/A

        \[\leadsto {x}^{2} \cdot \left(1 + \frac{\color{blue}{0}}{x}\right) \]
      7. mul0-lftN/A

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

        \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{0 \cdot \frac{y}{x}}\right) \]
      9. mul0-lftN/A

        \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{0}\right) \]
      10. metadata-evalN/A

        \[\leadsto {x}^{2} \cdot \color{blue}{1} \]
      11. *-rgt-identityN/A

        \[\leadsto \color{blue}{{x}^{2}} \]
      12. unpow2N/A

        \[\leadsto \color{blue}{x \cdot x} \]
      13. lower-*.f6453.9

        \[\leadsto \color{blue}{x \cdot x} \]
    7. Applied rewrites53.9%

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

    Developer Target 1: 99.9% accurate, 1.5× 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 2024304 
    (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)))