Examples.Basics.ProofTests:f4 from sbv-4.4

Percentage Accurate: 94.1% → 99.9%
Time: 3.6s
Alternatives: 6
Speedup: 1.5×

Specification

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

\\
\left(x \cdot x + \left(x \cdot 2\right) \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 6 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 94.1% accurate, 1.0× speedup?

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

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

Alternative 1: 99.9% accurate, 1.1× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 4.8 \cdot 10^{+151}:\\ \;\;\;\;\mathsf{fma}\left(y, y, x \cdot \mathsf{fma}\left(y, 2, x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot y\\ \end{array} \end{array} \]
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
 :precision binary64
 (if (<= y 4.8e+151) (fma y y (* x (fma y 2.0 x))) (* y y)))
assert(x < y);
double code(double x, double y) {
	double tmp;
	if (y <= 4.8e+151) {
		tmp = fma(y, y, (x * fma(y, 2.0, x)));
	} else {
		tmp = y * y;
	}
	return tmp;
}
x, y = sort([x, y])
function code(x, y)
	tmp = 0.0
	if (y <= 4.8e+151)
		tmp = fma(y, y, Float64(x * fma(y, 2.0, x)));
	else
		tmp = Float64(y * y);
	end
	return tmp
end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := If[LessEqual[y, 4.8e+151], N[(y * y + N[(x * N[(y * 2.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.8 \cdot 10^{+151}:\\
\;\;\;\;\mathsf{fma}\left(y, y, x \cdot \mathsf{fma}\left(y, 2, x\right)\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 4.8000000000000002e151

    1. Initial program 94.1%

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

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

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

        \[\leadsto \color{blue}{y \cdot y} + \left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) \]
      4. lower-fma.f6494.1

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(y, y, x \cdot \left(\color{blue}{y \cdot 2} + x\right)\right) \]
      14. lower-fma.f6497.7

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

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

    if 4.8000000000000002e151 < y

    1. Initial program 87.9%

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

      \[\leadsto \color{blue}{{y}^{2}} \]
    4. Step-by-step derivation
      1. Applied rewrites100.0%

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

    Alternative 2: 87.4% accurate, 1.5× speedup?

    \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 3.4 \cdot 10^{-158}:\\ \;\;\;\;\left(\left(x + y\right) + y\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot y\\ \end{array} \end{array} \]
    NOTE: x and y should be sorted in increasing order before calling this function.
    (FPCore (x y)
     :precision binary64
     (if (<= y 3.4e-158) (* (+ (+ x y) y) x) (* y y)))
    assert(x < y);
    double code(double x, double y) {
    	double tmp;
    	if (y <= 3.4e-158) {
    		tmp = ((x + y) + y) * x;
    	} else {
    		tmp = y * y;
    	}
    	return tmp;
    }
    
    NOTE: x and y should be sorted in increasing order before calling this function.
    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 (y <= 3.4d-158) then
            tmp = ((x + y) + y) * x
        else
            tmp = y * y
        end if
        code = tmp
    end function
    
    assert x < y;
    public static double code(double x, double y) {
    	double tmp;
    	if (y <= 3.4e-158) {
    		tmp = ((x + y) + y) * x;
    	} else {
    		tmp = y * y;
    	}
    	return tmp;
    }
    
    [x, y] = sort([x, y])
    def code(x, y):
    	tmp = 0
    	if y <= 3.4e-158:
    		tmp = ((x + y) + y) * x
    	else:
    		tmp = y * y
    	return tmp
    
    x, y = sort([x, y])
    function code(x, y)
    	tmp = 0.0
    	if (y <= 3.4e-158)
    		tmp = Float64(Float64(Float64(x + y) + y) * x);
    	else
    		tmp = Float64(y * y);
    	end
    	return tmp
    end
    
    x, y = num2cell(sort([x, y])){:}
    function tmp_2 = code(x, y)
    	tmp = 0.0;
    	if (y <= 3.4e-158)
    		tmp = ((x + y) + y) * x;
    	else
    		tmp = y * y;
    	end
    	tmp_2 = tmp;
    end
    
    NOTE: x and y should be sorted in increasing order before calling this function.
    code[x_, y_] := If[LessEqual[y, 3.4e-158], N[(N[(N[(x + y), $MachinePrecision] + y), $MachinePrecision] * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
    
    \begin{array}{l}
    [x, y] = \mathsf{sort}([x, y])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq 3.4 \cdot 10^{-158}:\\
    \;\;\;\;\left(\left(x + y\right) + y\right) \cdot x\\
    
    \mathbf{else}:\\
    \;\;\;\;y \cdot y\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < 3.3999999999999999e-158

      1. Initial program 93.9%

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

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

          \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, x\right) \cdot x} \]
        2. Step-by-step derivation
          1. Applied rewrites66.5%

            \[\leadsto \left(\left(x + y\right) + y\right) \cdot x \]

          if 3.3999999999999999e-158 < y

          1. Initial program 92.3%

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

            \[\leadsto \color{blue}{{y}^{2}} \]
          4. Step-by-step derivation
            1. Applied rewrites72.5%

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

          Alternative 3: 100.0% accurate, 1.5× speedup?

          \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \mathsf{fma}\left(y, \mathsf{fma}\left(2, x, y\right), x \cdot x\right) \end{array} \]
          NOTE: x and y should be sorted in increasing order before calling this function.
          (FPCore (x y) :precision binary64 (fma y (fma 2.0 x y) (* x x)))
          assert(x < y);
          double code(double x, double y) {
          	return fma(y, fma(2.0, x, y), (x * x));
          }
          
          x, y = sort([x, y])
          function code(x, y)
          	return fma(y, fma(2.0, x, y), Float64(x * x))
          end
          
          NOTE: x and y should be sorted in increasing order before calling this function.
          code[x_, y_] := N[(y * N[(2.0 * x + y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
          
          \begin{array}{l}
          [x, y] = \mathsf{sort}([x, y])\\
          \\
          \mathsf{fma}\left(y, \mathsf{fma}\left(2, x, y\right), x \cdot x\right)
          \end{array}
          
          Derivation
          1. Initial program 93.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

          Alternative 4: 87.2% accurate, 1.8× speedup?

          \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 3.4 \cdot 10^{-158}:\\ \;\;\;\;\left(x + y\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot y\\ \end{array} \end{array} \]
          NOTE: x and y should be sorted in increasing order before calling this function.
          (FPCore (x y) :precision binary64 (if (<= y 3.4e-158) (* (+ x y) x) (* y y)))
          assert(x < y);
          double code(double x, double y) {
          	double tmp;
          	if (y <= 3.4e-158) {
          		tmp = (x + y) * x;
          	} else {
          		tmp = y * y;
          	}
          	return tmp;
          }
          
          NOTE: x and y should be sorted in increasing order before calling this function.
          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 (y <= 3.4d-158) then
                  tmp = (x + y) * x
              else
                  tmp = y * y
              end if
              code = tmp
          end function
          
          assert x < y;
          public static double code(double x, double y) {
          	double tmp;
          	if (y <= 3.4e-158) {
          		tmp = (x + y) * x;
          	} else {
          		tmp = y * y;
          	}
          	return tmp;
          }
          
          [x, y] = sort([x, y])
          def code(x, y):
          	tmp = 0
          	if y <= 3.4e-158:
          		tmp = (x + y) * x
          	else:
          		tmp = y * y
          	return tmp
          
          x, y = sort([x, y])
          function code(x, y)
          	tmp = 0.0
          	if (y <= 3.4e-158)
          		tmp = Float64(Float64(x + y) * x);
          	else
          		tmp = Float64(y * y);
          	end
          	return tmp
          end
          
          x, y = num2cell(sort([x, y])){:}
          function tmp_2 = code(x, y)
          	tmp = 0.0;
          	if (y <= 3.4e-158)
          		tmp = (x + y) * x;
          	else
          		tmp = y * y;
          	end
          	tmp_2 = tmp;
          end
          
          NOTE: x and y should be sorted in increasing order before calling this function.
          code[x_, y_] := If[LessEqual[y, 3.4e-158], N[(N[(x + y), $MachinePrecision] * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
          
          \begin{array}{l}
          [x, y] = \mathsf{sort}([x, y])\\
          \\
          \begin{array}{l}
          \mathbf{if}\;y \leq 3.4 \cdot 10^{-158}:\\
          \;\;\;\;\left(x + y\right) \cdot x\\
          
          \mathbf{else}:\\
          \;\;\;\;y \cdot y\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if y < 3.3999999999999999e-158

            1. Initial program 93.9%

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

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

                \[\leadsto \color{blue}{\mathsf{fma}\left(2, y, x\right) \cdot x} \]
              2. Step-by-step derivation
                1. Applied rewrites66.5%

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

                  \[\leadsto \left(x + y\right) \cdot x \]
                3. Step-by-step derivation
                  1. Applied rewrites66.1%

                    \[\leadsto \left(x + y\right) \cdot x \]

                  if 3.3999999999999999e-158 < y

                  1. Initial program 92.3%

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

                    \[\leadsto \color{blue}{{y}^{2}} \]
                  4. Step-by-step derivation
                    1. Applied rewrites72.5%

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

                  Alternative 5: 87.2% accurate, 2.2× speedup?

                  \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 3.4 \cdot 10^{-158}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot y\\ \end{array} \end{array} \]
                  NOTE: x and y should be sorted in increasing order before calling this function.
                  (FPCore (x y) :precision binary64 (if (<= y 3.4e-158) (* x x) (* y y)))
                  assert(x < y);
                  double code(double x, double y) {
                  	double tmp;
                  	if (y <= 3.4e-158) {
                  		tmp = x * x;
                  	} else {
                  		tmp = y * y;
                  	}
                  	return tmp;
                  }
                  
                  NOTE: x and y should be sorted in increasing order before calling this function.
                  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 (y <= 3.4d-158) then
                          tmp = x * x
                      else
                          tmp = y * y
                      end if
                      code = tmp
                  end function
                  
                  assert x < y;
                  public static double code(double x, double y) {
                  	double tmp;
                  	if (y <= 3.4e-158) {
                  		tmp = x * x;
                  	} else {
                  		tmp = y * y;
                  	}
                  	return tmp;
                  }
                  
                  [x, y] = sort([x, y])
                  def code(x, y):
                  	tmp = 0
                  	if y <= 3.4e-158:
                  		tmp = x * x
                  	else:
                  		tmp = y * y
                  	return tmp
                  
                  x, y = sort([x, y])
                  function code(x, y)
                  	tmp = 0.0
                  	if (y <= 3.4e-158)
                  		tmp = Float64(x * x);
                  	else
                  		tmp = Float64(y * y);
                  	end
                  	return tmp
                  end
                  
                  x, y = num2cell(sort([x, y])){:}
                  function tmp_2 = code(x, y)
                  	tmp = 0.0;
                  	if (y <= 3.4e-158)
                  		tmp = x * x;
                  	else
                  		tmp = y * y;
                  	end
                  	tmp_2 = tmp;
                  end
                  
                  NOTE: x and y should be sorted in increasing order before calling this function.
                  code[x_, y_] := If[LessEqual[y, 3.4e-158], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
                  
                  \begin{array}{l}
                  [x, y] = \mathsf{sort}([x, y])\\
                  \\
                  \begin{array}{l}
                  \mathbf{if}\;y \leq 3.4 \cdot 10^{-158}:\\
                  \;\;\;\;x \cdot x\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;y \cdot y\\
                  
                  
                  \end{array}
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if y < 3.3999999999999999e-158

                    1. Initial program 93.9%

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

                      \[\leadsto \color{blue}{{x}^{2}} \]
                    4. Step-by-step derivation
                      1. Applied rewrites64.7%

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

                      if 3.3999999999999999e-158 < y

                      1. Initial program 92.3%

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

                        \[\leadsto \color{blue}{{y}^{2}} \]
                      4. Step-by-step derivation
                        1. Applied rewrites72.5%

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

                      Alternative 6: 57.4% accurate, 4.5× speedup?

                      \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ x \cdot x \end{array} \]
                      NOTE: x and y should be sorted in increasing order before calling this function.
                      (FPCore (x y) :precision binary64 (* x x))
                      assert(x < y);
                      double code(double x, double y) {
                      	return x * x;
                      }
                      
                      NOTE: x and y should be sorted in increasing order before calling this function.
                      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
                      end function
                      
                      assert x < y;
                      public static double code(double x, double y) {
                      	return x * x;
                      }
                      
                      [x, y] = sort([x, y])
                      def code(x, y):
                      	return x * x
                      
                      x, y = sort([x, y])
                      function code(x, y)
                      	return Float64(x * x)
                      end
                      
                      x, y = num2cell(sort([x, y])){:}
                      function tmp = code(x, y)
                      	tmp = x * x;
                      end
                      
                      NOTE: x and y should be sorted in increasing order before calling this function.
                      code[x_, y_] := N[(x * x), $MachinePrecision]
                      
                      \begin{array}{l}
                      [x, y] = \mathsf{sort}([x, y])\\
                      \\
                      x \cdot x
                      \end{array}
                      
                      Derivation
                      1. Initial program 93.3%

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

                        \[\leadsto \color{blue}{{x}^{2}} \]
                      4. Step-by-step derivation
                        1. Applied rewrites54.8%

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

                        Developer Target 1: 94.1% accurate, 1.0× speedup?

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

                        Reproduce

                        ?
                        herbie shell --seed 2025018 
                        (FPCore (x y)
                          :name "Examples.Basics.ProofTests:f4 from sbv-4.4"
                          :precision binary64
                        
                          :alt
                          (! :herbie-platform default (+ (* x x) (+ (* y y) (* (* x y) 2))))
                        
                          (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))