Examples.Basics.ProofTests:f4 from sbv-4.4

Percentage Accurate: 93.8% → 99.2%
Time: 8.4s
Alternatives: 4
Speedup: 1.1×

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 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: 93.8% 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.2% accurate, 1.1× speedup?

\[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 3.05 \cdot 10^{+119}:\\ \;\;\;\;\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 3.05e+119) (fma y y (* x (fma y 2.0 x))) (* y y)))
assert(x < y);
double code(double x, double y) {
	double tmp;
	if (y <= 3.05e+119) {
		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 <= 3.05e+119)
		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, 3.05e+119], 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 3.05 \cdot 10^{+119}:\\
\;\;\;\;\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 < 3.05e119

    1. Initial program 95.2%

      \[\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-varN/A

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

        \[\leadsto \left(\mathsf{Rewrite<=}\left(lift-var-spec, x\right) \cdot \color{blue}{x} + \left(x \cdot 2\right) \cdot y\right) + y \cdot y \]
      3. lift-*.f64N/A

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

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

        \[\leadsto \left(x \cdot x + \left(\mathsf{Rewrite<=}\left(lift-var-spec, x\right) \cdot \color{blue}{2}\right) \cdot y\right) + y \cdot y \]
      6. lift-*.f64N/A

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

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

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

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

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

        \[\leadsto \left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + \mathsf{Rewrite<=}\left(lift-var-spec, y\right) \cdot \color{blue}{y} \]
      12. lift-*.f64N/A

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

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

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

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

    if 3.05e119 < y

    1. Initial program 85.1%

      \[\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: 88.0% accurate, 1.5× speedup?

    \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 6.9 \cdot 10^{-119}:\\ \;\;\;\;\left(\left(y + y\right) + x\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 6.9e-119) (* (+ (+ y y) x) x) (* y y)))
    assert(x < y);
    double code(double x, double y) {
    	double tmp;
    	if (y <= 6.9e-119) {
    		tmp = ((y + y) + 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 <= 6.9d-119) then
            tmp = ((y + y) + 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 <= 6.9e-119) {
    		tmp = ((y + y) + x) * x;
    	} else {
    		tmp = y * y;
    	}
    	return tmp;
    }
    
    [x, y] = sort([x, y])
    def code(x, y):
    	tmp = 0
    	if y <= 6.9e-119:
    		tmp = ((y + y) + x) * x
    	else:
    		tmp = y * y
    	return tmp
    
    x, y = sort([x, y])
    function code(x, y)
    	tmp = 0.0
    	if (y <= 6.9e-119)
    		tmp = Float64(Float64(Float64(y + y) + 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 <= 6.9e-119)
    		tmp = ((y + y) + 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, 6.9e-119], N[(N[(N[(y + y), $MachinePrecision] + x), $MachinePrecision] * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
    
    \begin{array}{l}
    [x, y] = \mathsf{sort}([x, y])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq 6.9 \cdot 10^{-119}:\\
    \;\;\;\;\left(\left(y + y\right) + x\right) \cdot x\\
    
    \mathbf{else}:\\
    \;\;\;\;y \cdot y\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < 6.89999999999999953e-119

      1. Initial program 93.8%

        \[\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. Applied rewrites71.2%

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

      if 6.89999999999999953e-119 < y

      1. Initial program 92.6%

        \[\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 rewrites77.8%

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

      Alternative 3: 87.8% accurate, 2.2× speedup?

      \[\begin{array}{l} [x, y] = \mathsf{sort}([x, y])\\ \\ \begin{array}{l} \mathbf{if}\;y \leq 6.9 \cdot 10^{-119}:\\ \;\;\;\;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 6.9e-119) (* x x) (* y y)))
      assert(x < y);
      double code(double x, double y) {
      	double tmp;
      	if (y <= 6.9e-119) {
      		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 <= 6.9d-119) 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 <= 6.9e-119) {
      		tmp = x * x;
      	} else {
      		tmp = y * y;
      	}
      	return tmp;
      }
      
      [x, y] = sort([x, y])
      def code(x, y):
      	tmp = 0
      	if y <= 6.9e-119:
      		tmp = x * x
      	else:
      		tmp = y * y
      	return tmp
      
      x, y = sort([x, y])
      function code(x, y)
      	tmp = 0.0
      	if (y <= 6.9e-119)
      		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 <= 6.9e-119)
      		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, 6.9e-119], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
      
      \begin{array}{l}
      [x, y] = \mathsf{sort}([x, y])\\
      \\
      \begin{array}{l}
      \mathbf{if}\;y \leq 6.9 \cdot 10^{-119}:\\
      \;\;\;\;x \cdot x\\
      
      \mathbf{else}:\\
      \;\;\;\;y \cdot y\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if y < 6.89999999999999953e-119

        1. Initial program 93.8%

          \[\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 rewrites72.4%

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

          if 6.89999999999999953e-119 < y

          1. Initial program 92.6%

            \[\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 rewrites77.8%

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

          Alternative 4: 58.0% 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 rewrites58.9%

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

            Developer Target 1: 93.8% 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 2025021 
            (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)))