Linear.Matrix:fromQuaternion from linear-1.19.1.3, A

Percentage Accurate: 95.5% → 100.0%
Time: 2.2s
Alternatives: 4
Speedup: 1.6×

Specification

?
\[\begin{array}{l} \\ 2 \cdot \left(x \cdot x - x \cdot y\right) \end{array} \]
(FPCore (x y) :precision binary64 (* 2.0 (- (* x x) (* x y))))
double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
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, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 2.0d0 * ((x * x) - (x * y))
end function
public static double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
def code(x, y):
	return 2.0 * ((x * x) - (x * y))
function code(x, y)
	return Float64(2.0 * Float64(Float64(x * x) - Float64(x * y)))
end
function tmp = code(x, y)
	tmp = 2.0 * ((x * x) - (x * y));
end
code[x_, y_] := N[(2.0 * N[(N[(x * x), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

\[\begin{array}{l} \\ 2 \cdot \left(x \cdot x - x \cdot y\right) \end{array} \]
(FPCore (x y) :precision binary64 (* 2.0 (- (* x x) (* x y))))
double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
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, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 2.0d0 * ((x * x) - (x * y))
end function
public static double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
def code(x, y):
	return 2.0 * ((x * x) - (x * y))
function code(x, y)
	return Float64(2.0 * Float64(Float64(x * x) - Float64(x * y)))
end
function tmp = code(x, y)
	tmp = 2.0 * ((x * x) - (x * y));
end
code[x_, y_] := N[(2.0 * N[(N[(x * x), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 100.0% accurate, 1.6× speedup?

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

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

    \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-*.f64N/A

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

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

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

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

      \[\leadsto \color{blue}{\left(x \cdot x - x \cdot y\right) \cdot 2} \]
    6. distribute-lft-out--N/A

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

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

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

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

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

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

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
    13. lower-*.f64100.0

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x \cdot 2\right)} \]
  5. Step-by-step derivation
    1. lift-*.f64N/A

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

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

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x + x\right)} \]
    4. lower-+.f64100.0

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x + x\right)} \]
  6. Applied rewrites100.0%

    \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x + x\right)} \]
  7. Add Preprocessing

Alternative 2: 78.6% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;2 \cdot \left(x \cdot x - x \cdot y\right) \leq 5 \cdot 10^{-170}:\\ \;\;\;\;\left(-2 \cdot y\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(x + x\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (* 2.0 (- (* x x) (* x y))) 5e-170) (* (* -2.0 y) x) (* x (+ x x))))
double code(double x, double y) {
	double tmp;
	if ((2.0 * ((x * x) - (x * y))) <= 5e-170) {
		tmp = (-2.0 * y) * x;
	} else {
		tmp = x * (x + x);
	}
	return tmp;
}
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, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((2.0d0 * ((x * x) - (x * y))) <= 5d-170) then
        tmp = ((-2.0d0) * y) * x
    else
        tmp = x * (x + x)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((2.0 * ((x * x) - (x * y))) <= 5e-170) {
		tmp = (-2.0 * y) * x;
	} else {
		tmp = x * (x + x);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (2.0 * ((x * x) - (x * y))) <= 5e-170:
		tmp = (-2.0 * y) * x
	else:
		tmp = x * (x + x)
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(2.0 * Float64(Float64(x * x) - Float64(x * y))) <= 5e-170)
		tmp = Float64(Float64(-2.0 * y) * x);
	else
		tmp = Float64(x * Float64(x + x));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((2.0 * ((x * x) - (x * y))) <= 5e-170)
		tmp = (-2.0 * y) * x;
	else
		tmp = x * (x + x);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(2.0 * N[(N[(x * x), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e-170], N[(N[(-2.0 * y), $MachinePrecision] * x), $MachinePrecision], N[(x * N[(x + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;2 \cdot \left(x \cdot x - x \cdot y\right) \leq 5 \cdot 10^{-170}:\\
\;\;\;\;\left(-2 \cdot y\right) \cdot x\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(x + x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 2 binary64) (-.f64 (*.f64 x x) (*.f64 x y))) < 5.0000000000000001e-170

    1. Initial program 99.9%

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

      \[\leadsto \color{blue}{-2 \cdot \left(x \cdot y\right)} \]
    4. Step-by-step derivation
      1. Applied rewrites93.0%

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

          \[\leadsto \left(-2 \cdot y\right) \cdot \color{blue}{x} \]

        if 5.0000000000000001e-170 < (*.f64 #s(literal 2 binary64) (-.f64 (*.f64 x x) (*.f64 x y)))

        1. Initial program 91.3%

          \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
        2. Add Preprocessing
        3. Step-by-step derivation
          1. lift-*.f64N/A

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

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

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

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

            \[\leadsto \color{blue}{\left(x \cdot x - x \cdot y\right) \cdot 2} \]
          6. distribute-lft-out--N/A

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

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

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

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

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

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

            \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
          13. lower-*.f64100.0

            \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
        4. Applied rewrites100.0%

          \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x \cdot 2\right)} \]
        5. Taylor expanded in x around inf

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

            \[\leadsto \color{blue}{x} \cdot \left(x \cdot 2\right) \]
          2. Step-by-step derivation
            1. lift-*.f64N/A

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

              \[\leadsto x \cdot \color{blue}{\left(2 \cdot x\right)} \]
            3. count-2-revN/A

              \[\leadsto x \cdot \color{blue}{\left(x + x\right)} \]
            4. lift-+.f6476.4

              \[\leadsto x \cdot \color{blue}{\left(x + x\right)} \]
          3. Applied rewrites76.4%

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

        Alternative 3: 78.6% accurate, 0.5× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;2 \cdot \left(x \cdot x - x \cdot y\right) \leq 5 \cdot 10^{-170}:\\ \;\;\;\;-2 \cdot \left(y \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(x + x\right)\\ \end{array} \end{array} \]
        (FPCore (x y)
         :precision binary64
         (if (<= (* 2.0 (- (* x x) (* x y))) 5e-170) (* -2.0 (* y x)) (* x (+ x x))))
        double code(double x, double y) {
        	double tmp;
        	if ((2.0 * ((x * x) - (x * y))) <= 5e-170) {
        		tmp = -2.0 * (y * x);
        	} else {
        		tmp = x * (x + x);
        	}
        	return tmp;
        }
        
        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, y)
        use fmin_fmax_functions
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            real(8) :: tmp
            if ((2.0d0 * ((x * x) - (x * y))) <= 5d-170) then
                tmp = (-2.0d0) * (y * x)
            else
                tmp = x * (x + x)
            end if
            code = tmp
        end function
        
        public static double code(double x, double y) {
        	double tmp;
        	if ((2.0 * ((x * x) - (x * y))) <= 5e-170) {
        		tmp = -2.0 * (y * x);
        	} else {
        		tmp = x * (x + x);
        	}
        	return tmp;
        }
        
        def code(x, y):
        	tmp = 0
        	if (2.0 * ((x * x) - (x * y))) <= 5e-170:
        		tmp = -2.0 * (y * x)
        	else:
        		tmp = x * (x + x)
        	return tmp
        
        function code(x, y)
        	tmp = 0.0
        	if (Float64(2.0 * Float64(Float64(x * x) - Float64(x * y))) <= 5e-170)
        		tmp = Float64(-2.0 * Float64(y * x));
        	else
        		tmp = Float64(x * Float64(x + x));
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y)
        	tmp = 0.0;
        	if ((2.0 * ((x * x) - (x * y))) <= 5e-170)
        		tmp = -2.0 * (y * x);
        	else
        		tmp = x * (x + x);
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_] := If[LessEqual[N[(2.0 * N[(N[(x * x), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e-170], N[(-2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision], N[(x * N[(x + x), $MachinePrecision]), $MachinePrecision]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;2 \cdot \left(x \cdot x - x \cdot y\right) \leq 5 \cdot 10^{-170}:\\
        \;\;\;\;-2 \cdot \left(y \cdot x\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;x \cdot \left(x + x\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if (*.f64 #s(literal 2 binary64) (-.f64 (*.f64 x x) (*.f64 x y))) < 5.0000000000000001e-170

          1. Initial program 99.9%

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

            \[\leadsto \color{blue}{-2 \cdot \left(x \cdot y\right)} \]
          4. Step-by-step derivation
            1. Applied rewrites93.0%

              \[\leadsto \color{blue}{-2 \cdot \left(y \cdot x\right)} \]

            if 5.0000000000000001e-170 < (*.f64 #s(literal 2 binary64) (-.f64 (*.f64 x x) (*.f64 x y)))

            1. Initial program 91.3%

              \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
            2. Add Preprocessing
            3. Step-by-step derivation
              1. lift-*.f64N/A

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

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

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

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

                \[\leadsto \color{blue}{\left(x \cdot x - x \cdot y\right) \cdot 2} \]
              6. distribute-lft-out--N/A

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

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

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

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

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

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

                \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
              13. lower-*.f64100.0

                \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
            4. Applied rewrites100.0%

              \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x \cdot 2\right)} \]
            5. Taylor expanded in x around inf

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

                \[\leadsto \color{blue}{x} \cdot \left(x \cdot 2\right) \]
              2. Step-by-step derivation
                1. lift-*.f64N/A

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

                  \[\leadsto x \cdot \color{blue}{\left(2 \cdot x\right)} \]
                3. count-2-revN/A

                  \[\leadsto x \cdot \color{blue}{\left(x + x\right)} \]
                4. lift-+.f6476.4

                  \[\leadsto x \cdot \color{blue}{\left(x + x\right)} \]
              3. Applied rewrites76.4%

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

            Alternative 4: 58.9% accurate, 2.1× speedup?

            \[\begin{array}{l} \\ x \cdot \left(x + x\right) \end{array} \]
            (FPCore (x y) :precision binary64 (* x (+ x x)))
            double code(double x, double y) {
            	return x * (x + x);
            }
            
            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, y)
            use fmin_fmax_functions
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                code = x * (x + x)
            end function
            
            public static double code(double x, double y) {
            	return x * (x + x);
            }
            
            def code(x, y):
            	return x * (x + x)
            
            function code(x, y)
            	return Float64(x * Float64(x + x))
            end
            
            function tmp = code(x, y)
            	tmp = x * (x + x);
            end
            
            code[x_, y_] := N[(x * N[(x + x), $MachinePrecision]), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            x \cdot \left(x + x\right)
            \end{array}
            
            Derivation
            1. Initial program 94.9%

              \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
            2. Add Preprocessing
            3. Step-by-step derivation
              1. lift-*.f64N/A

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

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

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

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

                \[\leadsto \color{blue}{\left(x \cdot x - x \cdot y\right) \cdot 2} \]
              6. distribute-lft-out--N/A

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

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

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

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

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

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

                \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
              13. lower-*.f64100.0

                \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(x \cdot 2\right)} \]
            4. Applied rewrites100.0%

              \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x \cdot 2\right)} \]
            5. Taylor expanded in x around inf

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

                \[\leadsto \color{blue}{x} \cdot \left(x \cdot 2\right) \]
              2. Step-by-step derivation
                1. lift-*.f64N/A

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

                  \[\leadsto x \cdot \color{blue}{\left(2 \cdot x\right)} \]
                3. count-2-revN/A

                  \[\leadsto x \cdot \color{blue}{\left(x + x\right)} \]
                4. lift-+.f6458.4

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

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

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

              \[\begin{array}{l} \\ \left(x \cdot 2\right) \cdot \left(x - y\right) \end{array} \]
              (FPCore (x y) :precision binary64 (* (* x 2.0) (- x y)))
              double code(double x, double y) {
              	return (x * 2.0) * (x - y);
              }
              
              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, y)
              use fmin_fmax_functions
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  code = (x * 2.0d0) * (x - y)
              end function
              
              public static double code(double x, double y) {
              	return (x * 2.0) * (x - y);
              }
              
              def code(x, y):
              	return (x * 2.0) * (x - y)
              
              function code(x, y)
              	return Float64(Float64(x * 2.0) * Float64(x - y))
              end
              
              function tmp = code(x, y)
              	tmp = (x * 2.0) * (x - y);
              end
              
              code[x_, y_] := N[(N[(x * 2.0), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              \left(x \cdot 2\right) \cdot \left(x - y\right)
              \end{array}
              

              Reproduce

              ?
              herbie shell --seed 2025026 
              (FPCore (x y)
                :name "Linear.Matrix:fromQuaternion from linear-1.19.1.3, A"
                :precision binary64
              
                :alt
                (! :herbie-platform default (* (* x 2) (- x y)))
              
                (* 2.0 (- (* x x) (* x y))))