Expression 4, p15

Percentage Accurate: 100.0% → 100.0%
Time: 2.0s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\left(5 \leq a \land a \leq 10\right) \land \left(0 \leq b \land b \leq 0.001\right)\]
\[\begin{array}{l} \\ \left(a + b\right) \cdot \left(a + b\right) \end{array} \]
(FPCore (a b) :precision binary64 (* (+ a b) (+ a b)))
double code(double a, double b) {
	return (a + b) * (a + b);
}
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(a, b)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a + b) * (a + b)
end function
public static double code(double a, double b) {
	return (a + b) * (a + b);
}
def code(a, b):
	return (a + b) * (a + b)
function code(a, b)
	return Float64(Float64(a + b) * Float64(a + b))
end
function tmp = code(a, b)
	tmp = (a + b) * (a + b);
end
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * N[(a + b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(a + b\right) \cdot \left(a + b\right)
\end{array}

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

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

\\
\left(a + b\right) \cdot \left(a + b\right)
\end{array}

Alternative 1: 100.0% accurate, 0.7× speedup?

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

    \[\left(a + b\right) \cdot \left(a + b\right) \]
  2. Step-by-step derivation
    1. lift-*.f64N/A

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

      \[\leadsto \left(a + b\right) \cdot \color{blue}{\left(a + b\right)} \]
    3. distribute-rgt-inN/A

      \[\leadsto \color{blue}{a \cdot \left(a + b\right) + b \cdot \left(a + b\right)} \]
    4. add-flipN/A

      \[\leadsto \color{blue}{a \cdot \left(a + b\right) - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right)} \]
    5. lift-+.f64N/A

      \[\leadsto a \cdot \color{blue}{\left(a + b\right)} - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right) \]
    6. +-commutativeN/A

      \[\leadsto a \cdot \color{blue}{\left(b + a\right)} - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right) \]
    7. distribute-lft-inN/A

      \[\leadsto \color{blue}{\left(a \cdot b + a \cdot a\right)} - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right) \]
    8. associate--l+N/A

      \[\leadsto \color{blue}{a \cdot b + \left(a \cdot a - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right)\right)} \]
    9. *-commutativeN/A

      \[\leadsto \color{blue}{b \cdot a} + \left(a \cdot a - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right)\right) \]
    10. add-flipN/A

      \[\leadsto b \cdot a + \color{blue}{\left(a \cdot a + b \cdot \left(a + b\right)\right)} \]
    11. lift-+.f64N/A

      \[\leadsto b \cdot a + \left(a \cdot a + b \cdot \color{blue}{\left(a + b\right)}\right) \]
    12. +-commutativeN/A

      \[\leadsto b \cdot a + \left(a \cdot a + b \cdot \color{blue}{\left(b + a\right)}\right) \]
    13. distribute-rgt-inN/A

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(b, a, a \cdot a + \left(b \cdot b + a \cdot b\right)\right)} \]
    15. distribute-rgt-inN/A

      \[\leadsto \mathsf{fma}\left(b, a, a \cdot a + \color{blue}{b \cdot \left(b + a\right)}\right) \]
    16. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b, a, a \cdot a + b \cdot \color{blue}{\left(a + b\right)}\right) \]
    17. lift-+.f64N/A

      \[\leadsto \mathsf{fma}\left(b, a, a \cdot a + b \cdot \color{blue}{\left(a + b\right)}\right) \]
    18. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b, a, \color{blue}{b \cdot \left(a + b\right) + a \cdot a}\right) \]
    19. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b, a, \color{blue}{\left(a + b\right) \cdot b} + a \cdot a\right) \]
    20. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(b, a, \color{blue}{\mathsf{fma}\left(a + b, b, a \cdot a\right)}\right) \]
    21. lift-+.f64N/A

      \[\leadsto \mathsf{fma}\left(b, a, \mathsf{fma}\left(\color{blue}{a + b}, b, a \cdot a\right)\right) \]
    22. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b, a, \mathsf{fma}\left(\color{blue}{b + a}, b, a \cdot a\right)\right) \]
    23. lower-+.f64N/A

      \[\leadsto \mathsf{fma}\left(b, a, \mathsf{fma}\left(\color{blue}{b + a}, b, a \cdot a\right)\right) \]
    24. lower-*.f64100.0

      \[\leadsto \mathsf{fma}\left(b, a, \mathsf{fma}\left(b + a, b, \color{blue}{a \cdot a}\right)\right) \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(b, a, \mathsf{fma}\left(b + a, b, a \cdot a\right)\right)} \]
  4. Step-by-step derivation
    1. lift-fma.f64N/A

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

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

      \[\leadsto b \cdot a + \left(\color{blue}{\left(b + a\right) \cdot b} + a \cdot a\right) \]
    4. associate-+r+N/A

      \[\leadsto \color{blue}{\left(b \cdot a + \left(b + a\right) \cdot b\right) + a \cdot a} \]
    5. *-commutativeN/A

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(b, a + \left(b + a\right), a \cdot a\right)} \]
    9. lower-+.f64100.0

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

      \[\leadsto \mathsf{fma}\left(b, a + \color{blue}{\left(b + a\right)}, a \cdot a\right) \]
    11. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b, a + \color{blue}{\left(a + b\right)}, a \cdot a\right) \]
    12. lower-+.f64100.0

      \[\leadsto \mathsf{fma}\left(b, a + \color{blue}{\left(a + b\right)}, a \cdot a\right) \]
  5. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(b, a + \left(a + b\right), a \cdot a\right)} \]
  6. Step-by-step derivation
    1. lift-fma.f64N/A

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

      \[\leadsto \color{blue}{\left(a + \left(a + b\right)\right) \cdot b} + a \cdot a \]
    3. lower-fma.f64100.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(a + \left(a + b\right), b, a \cdot a\right)} \]
    4. lift-+.f64N/A

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

      \[\leadsto \mathsf{fma}\left(a + \color{blue}{\left(a + b\right)}, b, a \cdot a\right) \]
    6. associate-+r+N/A

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{2 \cdot a} + b, b, a \cdot a\right) \]
    8. lower-fma.f64100.0

      \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(2, a, b\right)}, b, a \cdot a\right) \]
  7. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(2, a, b\right), b, a \cdot a\right)} \]
  8. Add Preprocessing

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \left(a + b\right) \cdot \left(a + b\right) \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (* (+ a b) (+ a b)))
assert(a < b);
double code(double a, double b) {
	return (a + b) * (a + b);
}
NOTE: a and b 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(a, b)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a + b) * (a + b)
end function
assert a < b;
public static double code(double a, double b) {
	return (a + b) * (a + b);
}
[a, b] = sort([a, b])
def code(a, b):
	return (a + b) * (a + b)
a, b = sort([a, b])
function code(a, b)
	return Float64(Float64(a + b) * Float64(a + b))
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = (a + b) * (a + b);
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * N[(a + b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\left(a + b\right) \cdot \left(a + b\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(a + b\right) \cdot \left(a + b\right) \]
  2. Add Preprocessing

Alternative 3: 98.7% accurate, 1.4× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \left(a + b\right) \cdot b \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (* (+ a b) b))
assert(a < b);
double code(double a, double b) {
	return (a + b) * b;
}
NOTE: a and b 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(a, b)
use fmin_fmax_functions
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a + b) * b
end function
assert a < b;
public static double code(double a, double b) {
	return (a + b) * b;
}
[a, b] = sort([a, b])
def code(a, b):
	return (a + b) * b
a, b = sort([a, b])
function code(a, b)
	return Float64(Float64(a + b) * b)
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = (a + b) * b;
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * b), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\left(a + b\right) \cdot b
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(a + b\right) \cdot \left(a + b\right) \]
  2. Step-by-step derivation
    1. lift-*.f64N/A

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

      \[\leadsto \left(a + b\right) \cdot \color{blue}{\left(a + b\right)} \]
    3. distribute-rgt-inN/A

      \[\leadsto \color{blue}{a \cdot \left(a + b\right) + b \cdot \left(a + b\right)} \]
    4. add-flipN/A

      \[\leadsto \color{blue}{a \cdot \left(a + b\right) - \left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right)} \]
    5. sub-flipN/A

      \[\leadsto \color{blue}{a \cdot \left(a + b\right) + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right)\right)\right)} \]
    6. *-commutativeN/A

      \[\leadsto \color{blue}{\left(a + b\right) \cdot a} + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(b \cdot \left(a + b\right)\right)\right)\right)\right) \]
    7. distribute-rgt-neg-inN/A

      \[\leadsto \left(a + b\right) \cdot a + \left(\mathsf{neg}\left(\color{blue}{b \cdot \left(\mathsf{neg}\left(\left(a + b\right)\right)\right)}\right)\right) \]
    8. distribute-rgt-neg-outN/A

      \[\leadsto \left(a + b\right) \cdot a + \color{blue}{b \cdot \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\left(a + b\right)\right)\right)\right)\right)} \]
    9. remove-double-negN/A

      \[\leadsto \left(a + b\right) \cdot a + b \cdot \color{blue}{\left(a + b\right)} \]
    10. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(a + b, a, b \cdot \left(a + b\right)\right)} \]
    11. lift-+.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{a + b}, a, b \cdot \left(a + b\right)\right) \]
    12. +-commutativeN/A

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{b + a}, a, b \cdot \left(a + b\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b + a, a, \color{blue}{\left(a + b\right) \cdot b}\right) \]
    15. lower-*.f64100.0

      \[\leadsto \mathsf{fma}\left(b + a, a, \color{blue}{\left(a + b\right) \cdot b}\right) \]
    16. lift-+.f64N/A

      \[\leadsto \mathsf{fma}\left(b + a, a, \color{blue}{\left(a + b\right)} \cdot b\right) \]
    17. +-commutativeN/A

      \[\leadsto \mathsf{fma}\left(b + a, a, \color{blue}{\left(b + a\right)} \cdot b\right) \]
    18. lower-+.f64100.0

      \[\leadsto \mathsf{fma}\left(b + a, a, \color{blue}{\left(b + a\right)} \cdot b\right) \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(b + a, a, \left(b + a\right) \cdot b\right)} \]
  4. Taylor expanded in a around 0

    \[\leadsto \mathsf{fma}\left(\color{blue}{b}, a, \left(b + a\right) \cdot b\right) \]
  5. Step-by-step derivation
    1. Applied rewrites99.6%

      \[\leadsto \mathsf{fma}\left(\color{blue}{b}, a, \left(b + a\right) \cdot b\right) \]
    2. Taylor expanded in a around 0

      \[\leadsto \mathsf{fma}\left(b, a, \color{blue}{b} \cdot b\right) \]
    3. Step-by-step derivation
      1. Applied rewrites98.7%

        \[\leadsto \mathsf{fma}\left(b, a, \color{blue}{b} \cdot b\right) \]
      2. Step-by-step derivation
        1. lift-fma.f64N/A

          \[\leadsto \color{blue}{b \cdot a + b \cdot b} \]
        2. lift-*.f64N/A

          \[\leadsto b \cdot a + \color{blue}{b \cdot b} \]
        3. distribute-lft-outN/A

          \[\leadsto \color{blue}{b \cdot \left(a + b\right)} \]
        4. lift-+.f64N/A

          \[\leadsto b \cdot \color{blue}{\left(a + b\right)} \]
        5. *-commutativeN/A

          \[\leadsto \color{blue}{\left(a + b\right) \cdot b} \]
        6. lower-*.f6498.7

          \[\leadsto \color{blue}{\left(a + b\right) \cdot b} \]
      3. Applied rewrites98.7%

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

      Alternative 4: 98.7% accurate, 2.4× speedup?

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

        \[\left(a + b\right) \cdot \left(a + b\right) \]
      2. Taylor expanded in a around 0

        \[\leadsto \color{blue}{b} \cdot \left(a + b\right) \]
      3. Step-by-step derivation
        1. Applied rewrites98.7%

          \[\leadsto \color{blue}{b} \cdot \left(a + b\right) \]
        2. Taylor expanded in a around 0

          \[\leadsto b \cdot \color{blue}{b} \]
        3. Step-by-step derivation
          1. Applied rewrites98.7%

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

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

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

          Reproduce

          ?
          herbie shell --seed 2025150 
          (FPCore (a b)
            :name "Expression 4, p15"
            :precision binary64
            :pre (and (and (<= 5.0 a) (<= a 10.0)) (and (<= 0.0 b) (<= b 0.001)))
          
            :alt
            (! :herbie-platform c (+ (+ (+ (* b a) (* b b)) (* b a)) (* a a)))
          
            (* (+ a b) (+ a b)))