FastMath dist4

Percentage Accurate: 88.8% → 98.5%
Time: 7.3s
Alternatives: 12
Speedup: 1.7×

Specification

?
\[\begin{array}{l} \\ \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))
double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
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(d1, d2, d3, d4)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
end function
public static double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
def code(d1, d2, d3, d4):
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
function code(d1, d2, d3, d4)
	return Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1))
end
function tmp = code(d1, d2, d3, d4)
	tmp = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
end
code[d1_, d2_, d3_, d4_] := N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1
\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 12 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: 88.8% accurate, 1.0× speedup?

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

\\
\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1
\end{array}

Alternative 1: 98.5% accurate, 0.6× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\left(d2 - d3\right) - d1\right) \cdot d1\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)) INFINITY)
   (fma d1 (- d4 d1) (* (- d2 d3) d1))
   (* (- (- d2 d3) d1) d1)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (((((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)) <= ((double) INFINITY)) {
		tmp = fma(d1, (d4 - d1), ((d2 - d3) * d1));
	} else {
		tmp = ((d2 - d3) - d1) * d1;
	}
	return tmp;
}
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1)) <= Inf)
		tmp = fma(d1, Float64(d4 - d1), Float64(Float64(d2 - d3) * d1));
	else
		tmp = Float64(Float64(Float64(d2 - d3) - d1) * d1);
	end
	return tmp
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision], Infinity], N[(d1 * N[(d4 - d1), $MachinePrecision] + N[(N[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision]), $MachinePrecision], N[(N[(N[(d2 - d3), $MachinePrecision] - d1), $MachinePrecision] * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)\\

\mathbf{else}:\\
\;\;\;\;\left(\left(d2 - d3\right) - d1\right) \cdot d1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (+.f64 (-.f64 (*.f64 d1 d2) (*.f64 d1 d3)) (*.f64 d4 d1)) (*.f64 d1 d1)) < +inf.0

    1. Initial program 100.0%

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

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1} \]
      2. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right)} - d1 \cdot d1 \]
      3. associate--l+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      4. +-commutativeN/A

        \[\leadsto \color{blue}{\left(d4 \cdot d1 - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right)} \]
      5. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
      6. lift-*.f64N/A

        \[\leadsto \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
      7. distribute-rgt-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - d1 \cdot d3\right)} \]
      9. lower--.f64100.0

        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d4 - d1}, d1 \cdot d2 - d1 \cdot d3\right) \]
      10. lift--.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2 - d1 \cdot d3}\right) \]
      11. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2} - d1 \cdot d3\right) \]
      12. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) \]
      13. distribute-lft-out--N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot \left(d2 - d3\right)}\right) \]
      14. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
      15. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
      16. lower--.f64100.0

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right)} \cdot d1\right) \]
    4. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)} \]

    if +inf.0 < (-.f64 (+.f64 (-.f64 (*.f64 d1 d2) (*.f64 d1 d3)) (*.f64 d4 d1)) (*.f64 d1 d1))

    1. Initial program 0.0%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Taylor expanded in d4 around 0

      \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
    4. Step-by-step derivation
      1. associate--r+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
      2. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
      3. unpow2N/A

        \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
      4. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
      5. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
      6. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
      7. lower--.f64N/A

        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
      8. lower--.f6484.6

        \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
    5. Applied rewrites84.6%

      \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 51.9% accurate, 1.2× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 5.2 \cdot 10^{+50}:\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+129}:\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d4 -2.3e-163)
   (* d2 d1)
   (if (<= d4 5.2e+50)
     (* (- d3) d1)
     (if (<= d4 1.6e+129) (* (- d1) d1) (* d4 d1)))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= -2.3e-163) {
		tmp = d2 * d1;
	} else if (d4 <= 5.2e+50) {
		tmp = -d3 * d1;
	} else if (d4 <= 1.6e+129) {
		tmp = -d1 * d1;
	} else {
		tmp = d4 * d1;
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if (d4 <= (-2.3d-163)) then
        tmp = d2 * d1
    else if (d4 <= 5.2d+50) then
        tmp = -d3 * d1
    else if (d4 <= 1.6d+129) then
        tmp = -d1 * d1
    else
        tmp = d4 * d1
    end if
    code = tmp
end function
assert d1 < d2 && d2 < d3 && d3 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= -2.3e-163) {
		tmp = d2 * d1;
	} else if (d4 <= 5.2e+50) {
		tmp = -d3 * d1;
	} else if (d4 <= 1.6e+129) {
		tmp = -d1 * d1;
	} else {
		tmp = d4 * d1;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= -2.3e-163:
		tmp = d2 * d1
	elif d4 <= 5.2e+50:
		tmp = -d3 * d1
	elif d4 <= 1.6e+129:
		tmp = -d1 * d1
	else:
		tmp = d4 * d1
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= -2.3e-163)
		tmp = Float64(d2 * d1);
	elseif (d4 <= 5.2e+50)
		tmp = Float64(Float64(-d3) * d1);
	elseif (d4 <= 1.6e+129)
		tmp = Float64(Float64(-d1) * d1);
	else
		tmp = Float64(d4 * d1);
	end
	return tmp
end
d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= -2.3e-163)
		tmp = d2 * d1;
	elseif (d4 <= 5.2e+50)
		tmp = -d3 * d1;
	elseif (d4 <= 1.6e+129)
		tmp = -d1 * d1;
	else
		tmp = d4 * d1;
	end
	tmp_2 = tmp;
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, -2.3e-163], N[(d2 * d1), $MachinePrecision], If[LessEqual[d4, 5.2e+50], N[((-d3) * d1), $MachinePrecision], If[LessEqual[d4, 1.6e+129], N[((-d1) * d1), $MachinePrecision], N[(d4 * d1), $MachinePrecision]]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\
\;\;\;\;d2 \cdot d1\\

\mathbf{elif}\;d4 \leq 5.2 \cdot 10^{+50}:\\
\;\;\;\;\left(-d3\right) \cdot d1\\

\mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+129}:\\
\;\;\;\;\left(-d1\right) \cdot d1\\

\mathbf{else}:\\
\;\;\;\;d4 \cdot d1\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d4 < -2.2999999999999999e-163

    1. Initial program 92.3%

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

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1} \]
      2. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right)} - d1 \cdot d1 \]
      3. associate--l+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      4. +-commutativeN/A

        \[\leadsto \color{blue}{\left(d4 \cdot d1 - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right)} \]
      5. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
      6. lift-*.f64N/A

        \[\leadsto \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
      7. distribute-rgt-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - d1 \cdot d3\right)} \]
      9. lower--.f6497.1

        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d4 - d1}, d1 \cdot d2 - d1 \cdot d3\right) \]
      10. lift--.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2 - d1 \cdot d3}\right) \]
      11. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2} - d1 \cdot d3\right) \]
      12. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) \]
      13. distribute-lft-out--N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot \left(d2 - d3\right)}\right) \]
      14. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
      15. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
      16. lower--.f6497.1

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right)} \cdot d1\right) \]
    4. Applied rewrites97.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)} \]
    5. Taylor expanded in d2 around inf

      \[\leadsto \color{blue}{d1 \cdot d2} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{d2 \cdot d1} \]
      2. lower-*.f6422.8

        \[\leadsto \color{blue}{d2 \cdot d1} \]
    7. Applied rewrites22.8%

      \[\leadsto \color{blue}{d2 \cdot d1} \]

    if -2.2999999999999999e-163 < d4 < 5.2000000000000004e50

    1. Initial program 90.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Taylor expanded in d4 around 0

      \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
    4. Step-by-step derivation
      1. associate--r+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
      2. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
      3. unpow2N/A

        \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
      4. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
      5. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
      6. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
      7. lower--.f64N/A

        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
      8. lower--.f6492.4

        \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
    5. Applied rewrites92.4%

      \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
    6. Taylor expanded in d1 around 0

      \[\leadsto \left(d2 - d3\right) \cdot d1 \]
    7. Step-by-step derivation
      1. Applied rewrites67.1%

        \[\leadsto \left(d2 - d3\right) \cdot d1 \]
      2. Taylor expanded in d2 around 0

        \[\leadsto \left(-1 \cdot d3\right) \cdot d1 \]
      3. Step-by-step derivation
        1. Applied rewrites46.4%

          \[\leadsto \left(-d3\right) \cdot d1 \]

        if 5.2000000000000004e50 < d4 < 1.6000000000000001e129

        1. Initial program 94.7%

          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
        2. Add Preprocessing
        3. Taylor expanded in d1 around inf

          \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
        4. Step-by-step derivation
          1. unpow2N/A

            \[\leadsto -1 \cdot \color{blue}{\left(d1 \cdot d1\right)} \]
          2. associate-*r*N/A

            \[\leadsto \color{blue}{\left(-1 \cdot d1\right) \cdot d1} \]
          3. lower-*.f64N/A

            \[\leadsto \color{blue}{\left(-1 \cdot d1\right) \cdot d1} \]
          4. mul-1-negN/A

            \[\leadsto \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d1 \]
          5. lower-neg.f6434.1

            \[\leadsto \color{blue}{\left(-d1\right)} \cdot d1 \]
        5. Applied rewrites34.1%

          \[\leadsto \color{blue}{\left(-d1\right) \cdot d1} \]

        if 1.6000000000000001e129 < d4

        1. Initial program 80.8%

          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
        2. Add Preprocessing
        3. Taylor expanded in d4 around inf

          \[\leadsto \color{blue}{d1 \cdot d4} \]
        4. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \color{blue}{d4 \cdot d1} \]
          2. lower-*.f6472.8

            \[\leadsto \color{blue}{d4 \cdot d1} \]
        5. Applied rewrites72.8%

          \[\leadsto \color{blue}{d4 \cdot d1} \]
      4. Recombined 4 regimes into one program.
      5. Final simplification40.8%

        \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 5.2 \cdot 10^{+50}:\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+129}:\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \]
      6. Add Preprocessing

      Alternative 3: 88.6% accurate, 1.2× speedup?

      \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d1 \leq -1.5 \cdot 10^{+151}:\\ \;\;\;\;\left(\left(-d3\right) - d1\right) \cdot d1\\ \mathbf{elif}\;d1 \leq 8 \cdot 10^{+93}:\\ \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d2 - d1\right) \cdot d1\\ \end{array} \end{array} \]
      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
      (FPCore (d1 d2 d3 d4)
       :precision binary64
       (if (<= d1 -1.5e+151)
         (* (- (- d3) d1) d1)
         (if (<= d1 8e+93) (* (- (+ d4 d2) d3) d1) (* (- d2 d1) d1))))
      assert(d1 < d2 && d2 < d3 && d3 < d4);
      double code(double d1, double d2, double d3, double d4) {
      	double tmp;
      	if (d1 <= -1.5e+151) {
      		tmp = (-d3 - d1) * d1;
      	} else if (d1 <= 8e+93) {
      		tmp = ((d4 + d2) - d3) * d1;
      	} else {
      		tmp = (d2 - d1) * d1;
      	}
      	return tmp;
      }
      
      NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
      use fmin_fmax_functions
          real(8), intent (in) :: d1
          real(8), intent (in) :: d2
          real(8), intent (in) :: d3
          real(8), intent (in) :: d4
          real(8) :: tmp
          if (d1 <= (-1.5d+151)) then
              tmp = (-d3 - d1) * d1
          else if (d1 <= 8d+93) then
              tmp = ((d4 + d2) - d3) * d1
          else
              tmp = (d2 - d1) * d1
          end if
          code = tmp
      end function
      
      assert d1 < d2 && d2 < d3 && d3 < d4;
      public static double code(double d1, double d2, double d3, double d4) {
      	double tmp;
      	if (d1 <= -1.5e+151) {
      		tmp = (-d3 - d1) * d1;
      	} else if (d1 <= 8e+93) {
      		tmp = ((d4 + d2) - d3) * d1;
      	} else {
      		tmp = (d2 - d1) * d1;
      	}
      	return tmp;
      }
      
      [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
      def code(d1, d2, d3, d4):
      	tmp = 0
      	if d1 <= -1.5e+151:
      		tmp = (-d3 - d1) * d1
      	elif d1 <= 8e+93:
      		tmp = ((d4 + d2) - d3) * d1
      	else:
      		tmp = (d2 - d1) * d1
      	return tmp
      
      d1, d2, d3, d4 = sort([d1, d2, d3, d4])
      function code(d1, d2, d3, d4)
      	tmp = 0.0
      	if (d1 <= -1.5e+151)
      		tmp = Float64(Float64(Float64(-d3) - d1) * d1);
      	elseif (d1 <= 8e+93)
      		tmp = Float64(Float64(Float64(d4 + d2) - d3) * d1);
      	else
      		tmp = Float64(Float64(d2 - d1) * d1);
      	end
      	return tmp
      end
      
      d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
      function tmp_2 = code(d1, d2, d3, d4)
      	tmp = 0.0;
      	if (d1 <= -1.5e+151)
      		tmp = (-d3 - d1) * d1;
      	elseif (d1 <= 8e+93)
      		tmp = ((d4 + d2) - d3) * d1;
      	else
      		tmp = (d2 - d1) * d1;
      	end
      	tmp_2 = tmp;
      end
      
      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
      code[d1_, d2_, d3_, d4_] := If[LessEqual[d1, -1.5e+151], N[(N[((-d3) - d1), $MachinePrecision] * d1), $MachinePrecision], If[LessEqual[d1, 8e+93], N[(N[(N[(d4 + d2), $MachinePrecision] - d3), $MachinePrecision] * d1), $MachinePrecision], N[(N[(d2 - d1), $MachinePrecision] * d1), $MachinePrecision]]]
      
      \begin{array}{l}
      [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
      \\
      \begin{array}{l}
      \mathbf{if}\;d1 \leq -1.5 \cdot 10^{+151}:\\
      \;\;\;\;\left(\left(-d3\right) - d1\right) \cdot d1\\
      
      \mathbf{elif}\;d1 \leq 8 \cdot 10^{+93}:\\
      \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\
      
      \mathbf{else}:\\
      \;\;\;\;\left(d2 - d1\right) \cdot d1\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if d1 < -1.5e151

        1. Initial program 67.6%

          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
        2. Add Preprocessing
        3. Taylor expanded in d4 around 0

          \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
        4. Step-by-step derivation
          1. associate--r+N/A

            \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
          2. distribute-lft-out--N/A

            \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
          3. unpow2N/A

            \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
          4. distribute-lft-out--N/A

            \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
          5. *-commutativeN/A

            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
          6. lower-*.f64N/A

            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
          7. lower--.f64N/A

            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
          8. lower--.f6497.1

            \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
        5. Applied rewrites97.1%

          \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
        6. Taylor expanded in d2 around 0

          \[\leadsto \left(-1 \cdot d3 - d1\right) \cdot d1 \]
        7. Step-by-step derivation
          1. Applied rewrites97.1%

            \[\leadsto \left(\left(-d3\right) - d1\right) \cdot d1 \]

          if -1.5e151 < d1 < 8.00000000000000035e93

          1. Initial program 98.8%

            \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
          2. Add Preprocessing
          3. Taylor expanded in d1 around 0

            \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
          4. Step-by-step derivation
            1. *-commutativeN/A

              \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
            2. lower-*.f64N/A

              \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
            3. lower--.f64N/A

              \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
            4. +-commutativeN/A

              \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
            5. lower-+.f6491.9

              \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
          5. Applied rewrites91.9%

            \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]

          if 8.00000000000000035e93 < d1

          1. Initial program 67.5%

            \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
          2. Add Preprocessing
          3. Taylor expanded in d4 around 0

            \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
          4. Step-by-step derivation
            1. associate--r+N/A

              \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
            2. distribute-lft-out--N/A

              \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
            3. unpow2N/A

              \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
            4. distribute-lft-out--N/A

              \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
            5. *-commutativeN/A

              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
            6. lower-*.f64N/A

              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
            7. lower--.f64N/A

              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
            8. lower--.f6488.2

              \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
          5. Applied rewrites88.2%

            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
          6. Taylor expanded in d3 around 0

            \[\leadsto \left(d2 - d1\right) \cdot d1 \]
          7. Step-by-step derivation
            1. Applied rewrites83.2%

              \[\leadsto \left(d2 - d1\right) \cdot d1 \]
          8. Recombined 3 regimes into one program.
          9. Add Preprocessing

          Alternative 4: 74.4% accurate, 1.3× speedup?

          \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\ \;\;\;\;\left(d2 - d1\right) \cdot d1\\ \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+129}:\\ \;\;\;\;\left(\left(-d3\right) - d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - d3\right) \cdot d1\\ \end{array} \end{array} \]
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          (FPCore (d1 d2 d3 d4)
           :precision binary64
           (if (<= d4 -2.3e-163)
             (* (- d2 d1) d1)
             (if (<= d4 1.6e+129) (* (- (- d3) d1) d1) (* (- d4 d3) d1))))
          assert(d1 < d2 && d2 < d3 && d3 < d4);
          double code(double d1, double d2, double d3, double d4) {
          	double tmp;
          	if (d4 <= -2.3e-163) {
          		tmp = (d2 - d1) * d1;
          	} else if (d4 <= 1.6e+129) {
          		tmp = (-d3 - d1) * d1;
          	} else {
          		tmp = (d4 - d3) * d1;
          	}
          	return tmp;
          }
          
          NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
          use fmin_fmax_functions
              real(8), intent (in) :: d1
              real(8), intent (in) :: d2
              real(8), intent (in) :: d3
              real(8), intent (in) :: d4
              real(8) :: tmp
              if (d4 <= (-2.3d-163)) then
                  tmp = (d2 - d1) * d1
              else if (d4 <= 1.6d+129) then
                  tmp = (-d3 - d1) * d1
              else
                  tmp = (d4 - d3) * d1
              end if
              code = tmp
          end function
          
          assert d1 < d2 && d2 < d3 && d3 < d4;
          public static double code(double d1, double d2, double d3, double d4) {
          	double tmp;
          	if (d4 <= -2.3e-163) {
          		tmp = (d2 - d1) * d1;
          	} else if (d4 <= 1.6e+129) {
          		tmp = (-d3 - d1) * d1;
          	} else {
          		tmp = (d4 - d3) * d1;
          	}
          	return tmp;
          }
          
          [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
          def code(d1, d2, d3, d4):
          	tmp = 0
          	if d4 <= -2.3e-163:
          		tmp = (d2 - d1) * d1
          	elif d4 <= 1.6e+129:
          		tmp = (-d3 - d1) * d1
          	else:
          		tmp = (d4 - d3) * d1
          	return tmp
          
          d1, d2, d3, d4 = sort([d1, d2, d3, d4])
          function code(d1, d2, d3, d4)
          	tmp = 0.0
          	if (d4 <= -2.3e-163)
          		tmp = Float64(Float64(d2 - d1) * d1);
          	elseif (d4 <= 1.6e+129)
          		tmp = Float64(Float64(Float64(-d3) - d1) * d1);
          	else
          		tmp = Float64(Float64(d4 - d3) * d1);
          	end
          	return tmp
          end
          
          d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
          function tmp_2 = code(d1, d2, d3, d4)
          	tmp = 0.0;
          	if (d4 <= -2.3e-163)
          		tmp = (d2 - d1) * d1;
          	elseif (d4 <= 1.6e+129)
          		tmp = (-d3 - d1) * d1;
          	else
          		tmp = (d4 - d3) * d1;
          	end
          	tmp_2 = tmp;
          end
          
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, -2.3e-163], N[(N[(d2 - d1), $MachinePrecision] * d1), $MachinePrecision], If[LessEqual[d4, 1.6e+129], N[(N[((-d3) - d1), $MachinePrecision] * d1), $MachinePrecision], N[(N[(d4 - d3), $MachinePrecision] * d1), $MachinePrecision]]]
          
          \begin{array}{l}
          [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
          \\
          \begin{array}{l}
          \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\
          \;\;\;\;\left(d2 - d1\right) \cdot d1\\
          
          \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+129}:\\
          \;\;\;\;\left(\left(-d3\right) - d1\right) \cdot d1\\
          
          \mathbf{else}:\\
          \;\;\;\;\left(d4 - d3\right) \cdot d1\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if d4 < -2.2999999999999999e-163

            1. Initial program 92.3%

              \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
            2. Add Preprocessing
            3. Taylor expanded in d4 around 0

              \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
            4. Step-by-step derivation
              1. associate--r+N/A

                \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
              2. distribute-lft-out--N/A

                \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
              3. unpow2N/A

                \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
              4. distribute-lft-out--N/A

                \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
              5. *-commutativeN/A

                \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
              6. lower-*.f64N/A

                \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
              7. lower--.f64N/A

                \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
              8. lower--.f6472.0

                \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
            5. Applied rewrites72.0%

              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
            6. Taylor expanded in d3 around 0

              \[\leadsto \left(d2 - d1\right) \cdot d1 \]
            7. Step-by-step derivation
              1. Applied rewrites51.3%

                \[\leadsto \left(d2 - d1\right) \cdot d1 \]

              if -2.2999999999999999e-163 < d4 < 1.6000000000000001e129

              1. Initial program 91.4%

                \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
              2. Add Preprocessing
              3. Taylor expanded in d4 around 0

                \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
              4. Step-by-step derivation
                1. associate--r+N/A

                  \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                2. distribute-lft-out--N/A

                  \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                3. unpow2N/A

                  \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                4. distribute-lft-out--N/A

                  \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                5. *-commutativeN/A

                  \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                6. lower-*.f64N/A

                  \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                7. lower--.f64N/A

                  \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                8. lower--.f6490.1

                  \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
              5. Applied rewrites90.1%

                \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
              6. Taylor expanded in d2 around 0

                \[\leadsto \left(-1 \cdot d3 - d1\right) \cdot d1 \]
              7. Step-by-step derivation
                1. Applied rewrites65.5%

                  \[\leadsto \left(\left(-d3\right) - d1\right) \cdot d1 \]

                if 1.6000000000000001e129 < d4

                1. Initial program 80.8%

                  \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                2. Add Preprocessing
                3. Taylor expanded in d1 around 0

                  \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                4. Step-by-step derivation
                  1. *-commutativeN/A

                    \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                  2. lower-*.f64N/A

                    \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                  3. lower--.f64N/A

                    \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                  4. +-commutativeN/A

                    \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                  5. lower-+.f6493.6

                    \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                5. Applied rewrites93.6%

                  \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                6. Taylor expanded in d2 around 0

                  \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                7. Step-by-step derivation
                  1. Applied rewrites87.2%

                    \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                8. Recombined 3 regimes into one program.
                9. Add Preprocessing

                Alternative 5: 68.2% accurate, 1.4× speedup?

                \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -5.2 \cdot 10^{+110} \lor \neg \left(d3 \leq 6.5 \cdot 10^{+155}\right):\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d2 + d4\right) \cdot d1\\ \end{array} \end{array} \]
                NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                (FPCore (d1 d2 d3 d4)
                 :precision binary64
                 (if (or (<= d3 -5.2e+110) (not (<= d3 6.5e+155)))
                   (* (- d3) d1)
                   (* (+ d2 d4) d1)))
                assert(d1 < d2 && d2 < d3 && d3 < d4);
                double code(double d1, double d2, double d3, double d4) {
                	double tmp;
                	if ((d3 <= -5.2e+110) || !(d3 <= 6.5e+155)) {
                		tmp = -d3 * d1;
                	} else {
                		tmp = (d2 + d4) * d1;
                	}
                	return tmp;
                }
                
                NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                use fmin_fmax_functions
                    real(8), intent (in) :: d1
                    real(8), intent (in) :: d2
                    real(8), intent (in) :: d3
                    real(8), intent (in) :: d4
                    real(8) :: tmp
                    if ((d3 <= (-5.2d+110)) .or. (.not. (d3 <= 6.5d+155))) then
                        tmp = -d3 * d1
                    else
                        tmp = (d2 + d4) * d1
                    end if
                    code = tmp
                end function
                
                assert d1 < d2 && d2 < d3 && d3 < d4;
                public static double code(double d1, double d2, double d3, double d4) {
                	double tmp;
                	if ((d3 <= -5.2e+110) || !(d3 <= 6.5e+155)) {
                		tmp = -d3 * d1;
                	} else {
                		tmp = (d2 + d4) * d1;
                	}
                	return tmp;
                }
                
                [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                def code(d1, d2, d3, d4):
                	tmp = 0
                	if (d3 <= -5.2e+110) or not (d3 <= 6.5e+155):
                		tmp = -d3 * d1
                	else:
                		tmp = (d2 + d4) * d1
                	return tmp
                
                d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                function code(d1, d2, d3, d4)
                	tmp = 0.0
                	if ((d3 <= -5.2e+110) || !(d3 <= 6.5e+155))
                		tmp = Float64(Float64(-d3) * d1);
                	else
                		tmp = Float64(Float64(d2 + d4) * d1);
                	end
                	return tmp
                end
                
                d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                function tmp_2 = code(d1, d2, d3, d4)
                	tmp = 0.0;
                	if ((d3 <= -5.2e+110) || ~((d3 <= 6.5e+155)))
                		tmp = -d3 * d1;
                	else
                		tmp = (d2 + d4) * d1;
                	end
                	tmp_2 = tmp;
                end
                
                NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d3, -5.2e+110], N[Not[LessEqual[d3, 6.5e+155]], $MachinePrecision]], N[((-d3) * d1), $MachinePrecision], N[(N[(d2 + d4), $MachinePrecision] * d1), $MachinePrecision]]
                
                \begin{array}{l}
                [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                \\
                \begin{array}{l}
                \mathbf{if}\;d3 \leq -5.2 \cdot 10^{+110} \lor \neg \left(d3 \leq 6.5 \cdot 10^{+155}\right):\\
                \;\;\;\;\left(-d3\right) \cdot d1\\
                
                \mathbf{else}:\\
                \;\;\;\;\left(d2 + d4\right) \cdot d1\\
                
                
                \end{array}
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if d3 < -5.2e110 or 6.50000000000000046e155 < d3

                  1. Initial program 87.3%

                    \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                  2. Add Preprocessing
                  3. Taylor expanded in d4 around 0

                    \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
                  4. Step-by-step derivation
                    1. associate--r+N/A

                      \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                    2. distribute-lft-out--N/A

                      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                    3. unpow2N/A

                      \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                    4. distribute-lft-out--N/A

                      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                    5. *-commutativeN/A

                      \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                    6. lower-*.f64N/A

                      \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                    7. lower--.f64N/A

                      \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                    8. lower--.f6488.1

                      \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
                  5. Applied rewrites88.1%

                    \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                  6. Taylor expanded in d1 around 0

                    \[\leadsto \left(d2 - d3\right) \cdot d1 \]
                  7. Step-by-step derivation
                    1. Applied rewrites86.7%

                      \[\leadsto \left(d2 - d3\right) \cdot d1 \]
                    2. Taylor expanded in d2 around 0

                      \[\leadsto \left(-1 \cdot d3\right) \cdot d1 \]
                    3. Step-by-step derivation
                      1. Applied rewrites81.6%

                        \[\leadsto \left(-d3\right) \cdot d1 \]

                      if -5.2e110 < d3 < 6.50000000000000046e155

                      1. Initial program 90.8%

                        \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                      2. Add Preprocessing
                      3. Taylor expanded in d1 around 0

                        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                      4. Step-by-step derivation
                        1. *-commutativeN/A

                          \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                        2. lower-*.f64N/A

                          \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                        3. lower--.f64N/A

                          \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                        4. +-commutativeN/A

                          \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                        5. lower-+.f6472.6

                          \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                      5. Applied rewrites72.6%

                        \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                      6. Taylor expanded in d3 around 0

                        \[\leadsto d1 \cdot \color{blue}{\left(d2 + d4\right)} \]
                      7. Step-by-step derivation
                        1. Applied rewrites65.9%

                          \[\leadsto \left(d2 + d4\right) \cdot \color{blue}{d1} \]
                      8. Recombined 2 regimes into one program.
                      9. Final simplification70.2%

                        \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -5.2 \cdot 10^{+110} \lor \neg \left(d3 \leq 6.5 \cdot 10^{+155}\right):\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d2 + d4\right) \cdot d1\\ \end{array} \]
                      10. Add Preprocessing

                      Alternative 6: 74.9% accurate, 1.4× speedup?

                      \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -1 \cdot 10^{+35}:\\ \;\;\;\;\left(d2 - d3\right) \cdot d1\\ \mathbf{elif}\;d2 \leq -1.85 \cdot 10^{-24}:\\ \;\;\;\;\left(d2 - d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - d3\right) \cdot d1\\ \end{array} \end{array} \]
                      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                      (FPCore (d1 d2 d3 d4)
                       :precision binary64
                       (if (<= d2 -1e+35)
                         (* (- d2 d3) d1)
                         (if (<= d2 -1.85e-24) (* (- d2 d1) d1) (* (- d4 d3) d1))))
                      assert(d1 < d2 && d2 < d3 && d3 < d4);
                      double code(double d1, double d2, double d3, double d4) {
                      	double tmp;
                      	if (d2 <= -1e+35) {
                      		tmp = (d2 - d3) * d1;
                      	} else if (d2 <= -1.85e-24) {
                      		tmp = (d2 - d1) * d1;
                      	} else {
                      		tmp = (d4 - d3) * d1;
                      	}
                      	return tmp;
                      }
                      
                      NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                      use fmin_fmax_functions
                          real(8), intent (in) :: d1
                          real(8), intent (in) :: d2
                          real(8), intent (in) :: d3
                          real(8), intent (in) :: d4
                          real(8) :: tmp
                          if (d2 <= (-1d+35)) then
                              tmp = (d2 - d3) * d1
                          else if (d2 <= (-1.85d-24)) then
                              tmp = (d2 - d1) * d1
                          else
                              tmp = (d4 - d3) * d1
                          end if
                          code = tmp
                      end function
                      
                      assert d1 < d2 && d2 < d3 && d3 < d4;
                      public static double code(double d1, double d2, double d3, double d4) {
                      	double tmp;
                      	if (d2 <= -1e+35) {
                      		tmp = (d2 - d3) * d1;
                      	} else if (d2 <= -1.85e-24) {
                      		tmp = (d2 - d1) * d1;
                      	} else {
                      		tmp = (d4 - d3) * d1;
                      	}
                      	return tmp;
                      }
                      
                      [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                      def code(d1, d2, d3, d4):
                      	tmp = 0
                      	if d2 <= -1e+35:
                      		tmp = (d2 - d3) * d1
                      	elif d2 <= -1.85e-24:
                      		tmp = (d2 - d1) * d1
                      	else:
                      		tmp = (d4 - d3) * d1
                      	return tmp
                      
                      d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                      function code(d1, d2, d3, d4)
                      	tmp = 0.0
                      	if (d2 <= -1e+35)
                      		tmp = Float64(Float64(d2 - d3) * d1);
                      	elseif (d2 <= -1.85e-24)
                      		tmp = Float64(Float64(d2 - d1) * d1);
                      	else
                      		tmp = Float64(Float64(d4 - d3) * d1);
                      	end
                      	return tmp
                      end
                      
                      d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                      function tmp_2 = code(d1, d2, d3, d4)
                      	tmp = 0.0;
                      	if (d2 <= -1e+35)
                      		tmp = (d2 - d3) * d1;
                      	elseif (d2 <= -1.85e-24)
                      		tmp = (d2 - d1) * d1;
                      	else
                      		tmp = (d4 - d3) * d1;
                      	end
                      	tmp_2 = tmp;
                      end
                      
                      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                      code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -1e+35], N[(N[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision], If[LessEqual[d2, -1.85e-24], N[(N[(d2 - d1), $MachinePrecision] * d1), $MachinePrecision], N[(N[(d4 - d3), $MachinePrecision] * d1), $MachinePrecision]]]
                      
                      \begin{array}{l}
                      [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                      \\
                      \begin{array}{l}
                      \mathbf{if}\;d2 \leq -1 \cdot 10^{+35}:\\
                      \;\;\;\;\left(d2 - d3\right) \cdot d1\\
                      
                      \mathbf{elif}\;d2 \leq -1.85 \cdot 10^{-24}:\\
                      \;\;\;\;\left(d2 - d1\right) \cdot d1\\
                      
                      \mathbf{else}:\\
                      \;\;\;\;\left(d4 - d3\right) \cdot d1\\
                      
                      
                      \end{array}
                      \end{array}
                      
                      Derivation
                      1. Split input into 3 regimes
                      2. if d2 < -9.9999999999999997e34

                        1. Initial program 91.4%

                          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                        2. Add Preprocessing
                        3. Taylor expanded in d4 around 0

                          \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
                        4. Step-by-step derivation
                          1. associate--r+N/A

                            \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                          2. distribute-lft-out--N/A

                            \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                          3. unpow2N/A

                            \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                          4. distribute-lft-out--N/A

                            \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                          5. *-commutativeN/A

                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                          6. lower-*.f64N/A

                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                          7. lower--.f64N/A

                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                          8. lower--.f6481.6

                            \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
                        5. Applied rewrites81.6%

                          \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                        6. Taylor expanded in d1 around 0

                          \[\leadsto \left(d2 - d3\right) \cdot d1 \]
                        7. Step-by-step derivation
                          1. Applied rewrites61.0%

                            \[\leadsto \left(d2 - d3\right) \cdot d1 \]

                          if -9.9999999999999997e34 < d2 < -1.8499999999999999e-24

                          1. Initial program 99.8%

                            \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                          2. Add Preprocessing
                          3. Taylor expanded in d4 around 0

                            \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
                          4. Step-by-step derivation
                            1. associate--r+N/A

                              \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                            2. distribute-lft-out--N/A

                              \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                            3. unpow2N/A

                              \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                            4. distribute-lft-out--N/A

                              \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                            5. *-commutativeN/A

                              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                            6. lower-*.f64N/A

                              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                            7. lower--.f64N/A

                              \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                            8. lower--.f6476.0

                              \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
                          5. Applied rewrites76.0%

                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                          6. Taylor expanded in d3 around 0

                            \[\leadsto \left(d2 - d1\right) \cdot d1 \]
                          7. Step-by-step derivation
                            1. Applied rewrites63.9%

                              \[\leadsto \left(d2 - d1\right) \cdot d1 \]

                            if -1.8499999999999999e-24 < d2

                            1. Initial program 89.0%

                              \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                            2. Add Preprocessing
                            3. Taylor expanded in d1 around 0

                              \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                            4. Step-by-step derivation
                              1. *-commutativeN/A

                                \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                              2. lower-*.f64N/A

                                \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                              3. lower--.f64N/A

                                \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                              4. +-commutativeN/A

                                \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                              5. lower-+.f6479.3

                                \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                            5. Applied rewrites79.3%

                              \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                            6. Taylor expanded in d2 around 0

                              \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                            7. Step-by-step derivation
                              1. Applied rewrites66.1%

                                \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                            8. Recombined 3 regimes into one program.
                            9. Add Preprocessing

                            Alternative 7: 73.2% accurate, 1.4× speedup?

                            \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -3.4 \cdot 10^{+153}:\\ \;\;\;\;\left(d2 + d4\right) \cdot d1\\ \mathbf{elif}\;d2 \leq -1.85 \cdot 10^{-24}:\\ \;\;\;\;\left(d2 - d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - d3\right) \cdot d1\\ \end{array} \end{array} \]
                            NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                            (FPCore (d1 d2 d3 d4)
                             :precision binary64
                             (if (<= d2 -3.4e+153)
                               (* (+ d2 d4) d1)
                               (if (<= d2 -1.85e-24) (* (- d2 d1) d1) (* (- d4 d3) d1))))
                            assert(d1 < d2 && d2 < d3 && d3 < d4);
                            double code(double d1, double d2, double d3, double d4) {
                            	double tmp;
                            	if (d2 <= -3.4e+153) {
                            		tmp = (d2 + d4) * d1;
                            	} else if (d2 <= -1.85e-24) {
                            		tmp = (d2 - d1) * d1;
                            	} else {
                            		tmp = (d4 - d3) * d1;
                            	}
                            	return tmp;
                            }
                            
                            NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                            use fmin_fmax_functions
                                real(8), intent (in) :: d1
                                real(8), intent (in) :: d2
                                real(8), intent (in) :: d3
                                real(8), intent (in) :: d4
                                real(8) :: tmp
                                if (d2 <= (-3.4d+153)) then
                                    tmp = (d2 + d4) * d1
                                else if (d2 <= (-1.85d-24)) then
                                    tmp = (d2 - d1) * d1
                                else
                                    tmp = (d4 - d3) * d1
                                end if
                                code = tmp
                            end function
                            
                            assert d1 < d2 && d2 < d3 && d3 < d4;
                            public static double code(double d1, double d2, double d3, double d4) {
                            	double tmp;
                            	if (d2 <= -3.4e+153) {
                            		tmp = (d2 + d4) * d1;
                            	} else if (d2 <= -1.85e-24) {
                            		tmp = (d2 - d1) * d1;
                            	} else {
                            		tmp = (d4 - d3) * d1;
                            	}
                            	return tmp;
                            }
                            
                            [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                            def code(d1, d2, d3, d4):
                            	tmp = 0
                            	if d2 <= -3.4e+153:
                            		tmp = (d2 + d4) * d1
                            	elif d2 <= -1.85e-24:
                            		tmp = (d2 - d1) * d1
                            	else:
                            		tmp = (d4 - d3) * d1
                            	return tmp
                            
                            d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                            function code(d1, d2, d3, d4)
                            	tmp = 0.0
                            	if (d2 <= -3.4e+153)
                            		tmp = Float64(Float64(d2 + d4) * d1);
                            	elseif (d2 <= -1.85e-24)
                            		tmp = Float64(Float64(d2 - d1) * d1);
                            	else
                            		tmp = Float64(Float64(d4 - d3) * d1);
                            	end
                            	return tmp
                            end
                            
                            d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                            function tmp_2 = code(d1, d2, d3, d4)
                            	tmp = 0.0;
                            	if (d2 <= -3.4e+153)
                            		tmp = (d2 + d4) * d1;
                            	elseif (d2 <= -1.85e-24)
                            		tmp = (d2 - d1) * d1;
                            	else
                            		tmp = (d4 - d3) * d1;
                            	end
                            	tmp_2 = tmp;
                            end
                            
                            NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                            code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -3.4e+153], N[(N[(d2 + d4), $MachinePrecision] * d1), $MachinePrecision], If[LessEqual[d2, -1.85e-24], N[(N[(d2 - d1), $MachinePrecision] * d1), $MachinePrecision], N[(N[(d4 - d3), $MachinePrecision] * d1), $MachinePrecision]]]
                            
                            \begin{array}{l}
                            [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                            \\
                            \begin{array}{l}
                            \mathbf{if}\;d2 \leq -3.4 \cdot 10^{+153}:\\
                            \;\;\;\;\left(d2 + d4\right) \cdot d1\\
                            
                            \mathbf{elif}\;d2 \leq -1.85 \cdot 10^{-24}:\\
                            \;\;\;\;\left(d2 - d1\right) \cdot d1\\
                            
                            \mathbf{else}:\\
                            \;\;\;\;\left(d4 - d3\right) \cdot d1\\
                            
                            
                            \end{array}
                            \end{array}
                            
                            Derivation
                            1. Split input into 3 regimes
                            2. if d2 < -3.3999999999999997e153

                              1. Initial program 90.8%

                                \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                              2. Add Preprocessing
                              3. Taylor expanded in d1 around 0

                                \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                              4. Step-by-step derivation
                                1. *-commutativeN/A

                                  \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                2. lower-*.f64N/A

                                  \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                3. lower--.f64N/A

                                  \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                                4. +-commutativeN/A

                                  \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                5. lower-+.f6495.5

                                  \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                              5. Applied rewrites95.5%

                                \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                              6. Taylor expanded in d3 around 0

                                \[\leadsto d1 \cdot \color{blue}{\left(d2 + d4\right)} \]
                              7. Step-by-step derivation
                                1. Applied rewrites82.8%

                                  \[\leadsto \left(d2 + d4\right) \cdot \color{blue}{d1} \]

                                if -3.3999999999999997e153 < d2 < -1.8499999999999999e-24

                                1. Initial program 93.9%

                                  \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                2. Add Preprocessing
                                3. Taylor expanded in d4 around 0

                                  \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
                                4. Step-by-step derivation
                                  1. associate--r+N/A

                                    \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                                  2. distribute-lft-out--N/A

                                    \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                                  3. unpow2N/A

                                    \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                                  4. distribute-lft-out--N/A

                                    \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                                  5. *-commutativeN/A

                                    \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                  6. lower-*.f64N/A

                                    \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                  7. lower--.f64N/A

                                    \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                                  8. lower--.f6479.5

                                    \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
                                5. Applied rewrites79.5%

                                  \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                6. Taylor expanded in d3 around 0

                                  \[\leadsto \left(d2 - d1\right) \cdot d1 \]
                                7. Step-by-step derivation
                                  1. Applied rewrites57.4%

                                    \[\leadsto \left(d2 - d1\right) \cdot d1 \]

                                  if -1.8499999999999999e-24 < d2

                                  1. Initial program 89.0%

                                    \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                  2. Add Preprocessing
                                  3. Taylor expanded in d1 around 0

                                    \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                                  4. Step-by-step derivation
                                    1. *-commutativeN/A

                                      \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                    2. lower-*.f64N/A

                                      \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                    3. lower--.f64N/A

                                      \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                                    4. +-commutativeN/A

                                      \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                    5. lower-+.f6479.3

                                      \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                  5. Applied rewrites79.3%

                                    \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                                  6. Taylor expanded in d2 around 0

                                    \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                                  7. Step-by-step derivation
                                    1. Applied rewrites66.1%

                                      \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                                  8. Recombined 3 regimes into one program.
                                  9. Add Preprocessing

                                  Alternative 8: 53.2% accurate, 1.5× speedup?

                                  \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+60}:\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \end{array} \]
                                  NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                  (FPCore (d1 d2 d3 d4)
                                   :precision binary64
                                   (if (<= d4 -2.3e-163) (* d2 d1) (if (<= d4 1.6e+60) (* (- d3) d1) (* d4 d1))))
                                  assert(d1 < d2 && d2 < d3 && d3 < d4);
                                  double code(double d1, double d2, double d3, double d4) {
                                  	double tmp;
                                  	if (d4 <= -2.3e-163) {
                                  		tmp = d2 * d1;
                                  	} else if (d4 <= 1.6e+60) {
                                  		tmp = -d3 * d1;
                                  	} else {
                                  		tmp = d4 * d1;
                                  	}
                                  	return tmp;
                                  }
                                  
                                  NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                                  use fmin_fmax_functions
                                      real(8), intent (in) :: d1
                                      real(8), intent (in) :: d2
                                      real(8), intent (in) :: d3
                                      real(8), intent (in) :: d4
                                      real(8) :: tmp
                                      if (d4 <= (-2.3d-163)) then
                                          tmp = d2 * d1
                                      else if (d4 <= 1.6d+60) then
                                          tmp = -d3 * d1
                                      else
                                          tmp = d4 * d1
                                      end if
                                      code = tmp
                                  end function
                                  
                                  assert d1 < d2 && d2 < d3 && d3 < d4;
                                  public static double code(double d1, double d2, double d3, double d4) {
                                  	double tmp;
                                  	if (d4 <= -2.3e-163) {
                                  		tmp = d2 * d1;
                                  	} else if (d4 <= 1.6e+60) {
                                  		tmp = -d3 * d1;
                                  	} else {
                                  		tmp = d4 * d1;
                                  	}
                                  	return tmp;
                                  }
                                  
                                  [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                                  def code(d1, d2, d3, d4):
                                  	tmp = 0
                                  	if d4 <= -2.3e-163:
                                  		tmp = d2 * d1
                                  	elif d4 <= 1.6e+60:
                                  		tmp = -d3 * d1
                                  	else:
                                  		tmp = d4 * d1
                                  	return tmp
                                  
                                  d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                                  function code(d1, d2, d3, d4)
                                  	tmp = 0.0
                                  	if (d4 <= -2.3e-163)
                                  		tmp = Float64(d2 * d1);
                                  	elseif (d4 <= 1.6e+60)
                                  		tmp = Float64(Float64(-d3) * d1);
                                  	else
                                  		tmp = Float64(d4 * d1);
                                  	end
                                  	return tmp
                                  end
                                  
                                  d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                                  function tmp_2 = code(d1, d2, d3, d4)
                                  	tmp = 0.0;
                                  	if (d4 <= -2.3e-163)
                                  		tmp = d2 * d1;
                                  	elseif (d4 <= 1.6e+60)
                                  		tmp = -d3 * d1;
                                  	else
                                  		tmp = d4 * d1;
                                  	end
                                  	tmp_2 = tmp;
                                  end
                                  
                                  NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                  code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, -2.3e-163], N[(d2 * d1), $MachinePrecision], If[LessEqual[d4, 1.6e+60], N[((-d3) * d1), $MachinePrecision], N[(d4 * d1), $MachinePrecision]]]
                                  
                                  \begin{array}{l}
                                  [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                                  \\
                                  \begin{array}{l}
                                  \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\
                                  \;\;\;\;d2 \cdot d1\\
                                  
                                  \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+60}:\\
                                  \;\;\;\;\left(-d3\right) \cdot d1\\
                                  
                                  \mathbf{else}:\\
                                  \;\;\;\;d4 \cdot d1\\
                                  
                                  
                                  \end{array}
                                  \end{array}
                                  
                                  Derivation
                                  1. Split input into 3 regimes
                                  2. if d4 < -2.2999999999999999e-163

                                    1. Initial program 92.3%

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

                                        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1} \]
                                      2. lift-+.f64N/A

                                        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right)} - d1 \cdot d1 \]
                                      3. associate--l+N/A

                                        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
                                      4. +-commutativeN/A

                                        \[\leadsto \color{blue}{\left(d4 \cdot d1 - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right)} \]
                                      5. lift-*.f64N/A

                                        \[\leadsto \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                      6. lift-*.f64N/A

                                        \[\leadsto \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                      7. distribute-rgt-out--N/A

                                        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                      8. lower-fma.f64N/A

                                        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - d1 \cdot d3\right)} \]
                                      9. lower--.f6497.1

                                        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d4 - d1}, d1 \cdot d2 - d1 \cdot d3\right) \]
                                      10. lift--.f64N/A

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2 - d1 \cdot d3}\right) \]
                                      11. lift-*.f64N/A

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2} - d1 \cdot d3\right) \]
                                      12. lift-*.f64N/A

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) \]
                                      13. distribute-lft-out--N/A

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot \left(d2 - d3\right)}\right) \]
                                      14. *-commutativeN/A

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
                                      15. lower-*.f64N/A

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
                                      16. lower--.f6497.1

                                        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right)} \cdot d1\right) \]
                                    4. Applied rewrites97.1%

                                      \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)} \]
                                    5. Taylor expanded in d2 around inf

                                      \[\leadsto \color{blue}{d1 \cdot d2} \]
                                    6. Step-by-step derivation
                                      1. *-commutativeN/A

                                        \[\leadsto \color{blue}{d2 \cdot d1} \]
                                      2. lower-*.f6422.8

                                        \[\leadsto \color{blue}{d2 \cdot d1} \]
                                    7. Applied rewrites22.8%

                                      \[\leadsto \color{blue}{d2 \cdot d1} \]

                                    if -2.2999999999999999e-163 < d4 < 1.59999999999999995e60

                                    1. Initial program 90.1%

                                      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                    2. Add Preprocessing
                                    3. Taylor expanded in d4 around 0

                                      \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
                                    4. Step-by-step derivation
                                      1. associate--r+N/A

                                        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                                      2. distribute-lft-out--N/A

                                        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                                      3. unpow2N/A

                                        \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                                      4. distribute-lft-out--N/A

                                        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                                      5. *-commutativeN/A

                                        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                      6. lower-*.f64N/A

                                        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                      7. lower--.f64N/A

                                        \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                                      8. lower--.f6492.7

                                        \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
                                    5. Applied rewrites92.7%

                                      \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                    6. Taylor expanded in d1 around 0

                                      \[\leadsto \left(d2 - d3\right) \cdot d1 \]
                                    7. Step-by-step derivation
                                      1. Applied rewrites67.7%

                                        \[\leadsto \left(d2 - d3\right) \cdot d1 \]
                                      2. Taylor expanded in d2 around 0

                                        \[\leadsto \left(-1 \cdot d3\right) \cdot d1 \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites47.1%

                                          \[\leadsto \left(-d3\right) \cdot d1 \]

                                        if 1.59999999999999995e60 < d4

                                        1. Initial program 85.2%

                                          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                        2. Add Preprocessing
                                        3. Taylor expanded in d4 around inf

                                          \[\leadsto \color{blue}{d1 \cdot d4} \]
                                        4. Step-by-step derivation
                                          1. *-commutativeN/A

                                            \[\leadsto \color{blue}{d4 \cdot d1} \]
                                          2. lower-*.f6466.6

                                            \[\leadsto \color{blue}{d4 \cdot d1} \]
                                        5. Applied rewrites66.6%

                                          \[\leadsto \color{blue}{d4 \cdot d1} \]
                                      4. Recombined 3 regimes into one program.
                                      5. Final simplification41.9%

                                        \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -2.3 \cdot 10^{-163}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{+60}:\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \]
                                      6. Add Preprocessing

                                      Alternative 9: 92.0% accurate, 1.7× speedup?

                                      \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 1.8 \cdot 10^{+129}:\\ \;\;\;\;\left(\left(d2 - d3\right) - d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\ \end{array} \end{array} \]
                                      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                      (FPCore (d1 d2 d3 d4)
                                       :precision binary64
                                       (if (<= d4 1.8e+129) (* (- (- d2 d3) d1) d1) (* (- (+ d4 d2) d3) d1)))
                                      assert(d1 < d2 && d2 < d3 && d3 < d4);
                                      double code(double d1, double d2, double d3, double d4) {
                                      	double tmp;
                                      	if (d4 <= 1.8e+129) {
                                      		tmp = ((d2 - d3) - d1) * d1;
                                      	} else {
                                      		tmp = ((d4 + d2) - d3) * d1;
                                      	}
                                      	return tmp;
                                      }
                                      
                                      NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                                      use fmin_fmax_functions
                                          real(8), intent (in) :: d1
                                          real(8), intent (in) :: d2
                                          real(8), intent (in) :: d3
                                          real(8), intent (in) :: d4
                                          real(8) :: tmp
                                          if (d4 <= 1.8d+129) then
                                              tmp = ((d2 - d3) - d1) * d1
                                          else
                                              tmp = ((d4 + d2) - d3) * d1
                                          end if
                                          code = tmp
                                      end function
                                      
                                      assert d1 < d2 && d2 < d3 && d3 < d4;
                                      public static double code(double d1, double d2, double d3, double d4) {
                                      	double tmp;
                                      	if (d4 <= 1.8e+129) {
                                      		tmp = ((d2 - d3) - d1) * d1;
                                      	} else {
                                      		tmp = ((d4 + d2) - d3) * d1;
                                      	}
                                      	return tmp;
                                      }
                                      
                                      [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                                      def code(d1, d2, d3, d4):
                                      	tmp = 0
                                      	if d4 <= 1.8e+129:
                                      		tmp = ((d2 - d3) - d1) * d1
                                      	else:
                                      		tmp = ((d4 + d2) - d3) * d1
                                      	return tmp
                                      
                                      d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                                      function code(d1, d2, d3, d4)
                                      	tmp = 0.0
                                      	if (d4 <= 1.8e+129)
                                      		tmp = Float64(Float64(Float64(d2 - d3) - d1) * d1);
                                      	else
                                      		tmp = Float64(Float64(Float64(d4 + d2) - d3) * d1);
                                      	end
                                      	return tmp
                                      end
                                      
                                      d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                                      function tmp_2 = code(d1, d2, d3, d4)
                                      	tmp = 0.0;
                                      	if (d4 <= 1.8e+129)
                                      		tmp = ((d2 - d3) - d1) * d1;
                                      	else
                                      		tmp = ((d4 + d2) - d3) * d1;
                                      	end
                                      	tmp_2 = tmp;
                                      end
                                      
                                      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                      code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 1.8e+129], N[(N[(N[(d2 - d3), $MachinePrecision] - d1), $MachinePrecision] * d1), $MachinePrecision], N[(N[(N[(d4 + d2), $MachinePrecision] - d3), $MachinePrecision] * d1), $MachinePrecision]]
                                      
                                      \begin{array}{l}
                                      [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                                      \\
                                      \begin{array}{l}
                                      \mathbf{if}\;d4 \leq 1.8 \cdot 10^{+129}:\\
                                      \;\;\;\;\left(\left(d2 - d3\right) - d1\right) \cdot d1\\
                                      
                                      \mathbf{else}:\\
                                      \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\
                                      
                                      
                                      \end{array}
                                      \end{array}
                                      
                                      Derivation
                                      1. Split input into 2 regimes
                                      2. if d4 < 1.8000000000000001e129

                                        1. Initial program 91.8%

                                          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                        2. Add Preprocessing
                                        3. Taylor expanded in d4 around 0

                                          \[\leadsto \color{blue}{d1 \cdot d2 - \left(d1 \cdot d3 + {d1}^{2}\right)} \]
                                        4. Step-by-step derivation
                                          1. associate--r+N/A

                                            \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) - {d1}^{2}} \]
                                          2. distribute-lft-out--N/A

                                            \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} - {d1}^{2} \]
                                          3. unpow2N/A

                                            \[\leadsto d1 \cdot \left(d2 - d3\right) - \color{blue}{d1 \cdot d1} \]
                                          4. distribute-lft-out--N/A

                                            \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) - d1\right)} \]
                                          5. *-commutativeN/A

                                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                          6. lower-*.f64N/A

                                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]
                                          7. lower--.f64N/A

                                            \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \cdot d1 \]
                                          8. lower--.f6481.1

                                            \[\leadsto \left(\color{blue}{\left(d2 - d3\right)} - d1\right) \cdot d1 \]
                                        5. Applied rewrites81.1%

                                          \[\leadsto \color{blue}{\left(\left(d2 - d3\right) - d1\right) \cdot d1} \]

                                        if 1.8000000000000001e129 < d4

                                        1. Initial program 80.8%

                                          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                        2. Add Preprocessing
                                        3. Taylor expanded in d1 around 0

                                          \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                                        4. Step-by-step derivation
                                          1. *-commutativeN/A

                                            \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                          2. lower-*.f64N/A

                                            \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                          3. lower--.f64N/A

                                            \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                                          4. +-commutativeN/A

                                            \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                          5. lower-+.f6493.6

                                            \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                        5. Applied rewrites93.6%

                                          \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                                      3. Recombined 2 regimes into one program.
                                      4. Add Preprocessing

                                      Alternative 10: 72.3% accurate, 2.0× speedup?

                                      \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -4.2 \cdot 10^{+78}:\\ \;\;\;\;\left(d2 + d4\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - d3\right) \cdot d1\\ \end{array} \end{array} \]
                                      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                      (FPCore (d1 d2 d3 d4)
                                       :precision binary64
                                       (if (<= d2 -4.2e+78) (* (+ d2 d4) d1) (* (- d4 d3) d1)))
                                      assert(d1 < d2 && d2 < d3 && d3 < d4);
                                      double code(double d1, double d2, double d3, double d4) {
                                      	double tmp;
                                      	if (d2 <= -4.2e+78) {
                                      		tmp = (d2 + d4) * d1;
                                      	} else {
                                      		tmp = (d4 - d3) * d1;
                                      	}
                                      	return tmp;
                                      }
                                      
                                      NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                                      use fmin_fmax_functions
                                          real(8), intent (in) :: d1
                                          real(8), intent (in) :: d2
                                          real(8), intent (in) :: d3
                                          real(8), intent (in) :: d4
                                          real(8) :: tmp
                                          if (d2 <= (-4.2d+78)) then
                                              tmp = (d2 + d4) * d1
                                          else
                                              tmp = (d4 - d3) * d1
                                          end if
                                          code = tmp
                                      end function
                                      
                                      assert d1 < d2 && d2 < d3 && d3 < d4;
                                      public static double code(double d1, double d2, double d3, double d4) {
                                      	double tmp;
                                      	if (d2 <= -4.2e+78) {
                                      		tmp = (d2 + d4) * d1;
                                      	} else {
                                      		tmp = (d4 - d3) * d1;
                                      	}
                                      	return tmp;
                                      }
                                      
                                      [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                                      def code(d1, d2, d3, d4):
                                      	tmp = 0
                                      	if d2 <= -4.2e+78:
                                      		tmp = (d2 + d4) * d1
                                      	else:
                                      		tmp = (d4 - d3) * d1
                                      	return tmp
                                      
                                      d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                                      function code(d1, d2, d3, d4)
                                      	tmp = 0.0
                                      	if (d2 <= -4.2e+78)
                                      		tmp = Float64(Float64(d2 + d4) * d1);
                                      	else
                                      		tmp = Float64(Float64(d4 - d3) * d1);
                                      	end
                                      	return tmp
                                      end
                                      
                                      d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                                      function tmp_2 = code(d1, d2, d3, d4)
                                      	tmp = 0.0;
                                      	if (d2 <= -4.2e+78)
                                      		tmp = (d2 + d4) * d1;
                                      	else
                                      		tmp = (d4 - d3) * d1;
                                      	end
                                      	tmp_2 = tmp;
                                      end
                                      
                                      NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                      code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -4.2e+78], N[(N[(d2 + d4), $MachinePrecision] * d1), $MachinePrecision], N[(N[(d4 - d3), $MachinePrecision] * d1), $MachinePrecision]]
                                      
                                      \begin{array}{l}
                                      [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                                      \\
                                      \begin{array}{l}
                                      \mathbf{if}\;d2 \leq -4.2 \cdot 10^{+78}:\\
                                      \;\;\;\;\left(d2 + d4\right) \cdot d1\\
                                      
                                      \mathbf{else}:\\
                                      \;\;\;\;\left(d4 - d3\right) \cdot d1\\
                                      
                                      
                                      \end{array}
                                      \end{array}
                                      
                                      Derivation
                                      1. Split input into 2 regimes
                                      2. if d2 < -4.2000000000000002e78

                                        1. Initial program 89.9%

                                          \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                        2. Add Preprocessing
                                        3. Taylor expanded in d1 around 0

                                          \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                                        4. Step-by-step derivation
                                          1. *-commutativeN/A

                                            \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                          2. lower-*.f64N/A

                                            \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                          3. lower--.f64N/A

                                            \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                                          4. +-commutativeN/A

                                            \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                          5. lower-+.f6485.5

                                            \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                        5. Applied rewrites85.5%

                                          \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                                        6. Taylor expanded in d3 around 0

                                          \[\leadsto d1 \cdot \color{blue}{\left(d2 + d4\right)} \]
                                        7. Step-by-step derivation
                                          1. Applied rewrites73.8%

                                            \[\leadsto \left(d2 + d4\right) \cdot \color{blue}{d1} \]

                                          if -4.2000000000000002e78 < d2

                                          1. Initial program 89.8%

                                            \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                          2. Add Preprocessing
                                          3. Taylor expanded in d1 around 0

                                            \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
                                          4. Step-by-step derivation
                                            1. *-commutativeN/A

                                              \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                            2. lower-*.f64N/A

                                              \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right) \cdot d1} \]
                                            3. lower--.f64N/A

                                              \[\leadsto \color{blue}{\left(\left(d2 + d4\right) - d3\right)} \cdot d1 \]
                                            4. +-commutativeN/A

                                              \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                            5. lower-+.f6478.7

                                              \[\leadsto \left(\color{blue}{\left(d4 + d2\right)} - d3\right) \cdot d1 \]
                                          5. Applied rewrites78.7%

                                            \[\leadsto \color{blue}{\left(\left(d4 + d2\right) - d3\right) \cdot d1} \]
                                          6. Taylor expanded in d2 around 0

                                            \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                                          7. Step-by-step derivation
                                            1. Applied rewrites65.4%

                                              \[\leadsto \left(d4 - d3\right) \cdot d1 \]
                                          8. Recombined 2 regimes into one program.
                                          9. Add Preprocessing

                                          Alternative 11: 50.8% accurate, 2.5× speedup?

                                          \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -3.4 \cdot 10^{+20}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \end{array} \]
                                          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                          (FPCore (d1 d2 d3 d4)
                                           :precision binary64
                                           (if (<= d2 -3.4e+20) (* d2 d1) (* d4 d1)))
                                          assert(d1 < d2 && d2 < d3 && d3 < d4);
                                          double code(double d1, double d2, double d3, double d4) {
                                          	double tmp;
                                          	if (d2 <= -3.4e+20) {
                                          		tmp = d2 * d1;
                                          	} else {
                                          		tmp = d4 * d1;
                                          	}
                                          	return tmp;
                                          }
                                          
                                          NOTE: d1, d2, d3, and d4 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(d1, d2, d3, d4)
                                          use fmin_fmax_functions
                                              real(8), intent (in) :: d1
                                              real(8), intent (in) :: d2
                                              real(8), intent (in) :: d3
                                              real(8), intent (in) :: d4
                                              real(8) :: tmp
                                              if (d2 <= (-3.4d+20)) then
                                                  tmp = d2 * d1
                                              else
                                                  tmp = d4 * d1
                                              end if
                                              code = tmp
                                          end function
                                          
                                          assert d1 < d2 && d2 < d3 && d3 < d4;
                                          public static double code(double d1, double d2, double d3, double d4) {
                                          	double tmp;
                                          	if (d2 <= -3.4e+20) {
                                          		tmp = d2 * d1;
                                          	} else {
                                          		tmp = d4 * d1;
                                          	}
                                          	return tmp;
                                          }
                                          
                                          [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
                                          def code(d1, d2, d3, d4):
                                          	tmp = 0
                                          	if d2 <= -3.4e+20:
                                          		tmp = d2 * d1
                                          	else:
                                          		tmp = d4 * d1
                                          	return tmp
                                          
                                          d1, d2, d3, d4 = sort([d1, d2, d3, d4])
                                          function code(d1, d2, d3, d4)
                                          	tmp = 0.0
                                          	if (d2 <= -3.4e+20)
                                          		tmp = Float64(d2 * d1);
                                          	else
                                          		tmp = Float64(d4 * d1);
                                          	end
                                          	return tmp
                                          end
                                          
                                          d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
                                          function tmp_2 = code(d1, d2, d3, d4)
                                          	tmp = 0.0;
                                          	if (d2 <= -3.4e+20)
                                          		tmp = d2 * d1;
                                          	else
                                          		tmp = d4 * d1;
                                          	end
                                          	tmp_2 = tmp;
                                          end
                                          
                                          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
                                          code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -3.4e+20], N[(d2 * d1), $MachinePrecision], N[(d4 * d1), $MachinePrecision]]
                                          
                                          \begin{array}{l}
                                          [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
                                          \\
                                          \begin{array}{l}
                                          \mathbf{if}\;d2 \leq -3.4 \cdot 10^{+20}:\\
                                          \;\;\;\;d2 \cdot d1\\
                                          
                                          \mathbf{else}:\\
                                          \;\;\;\;d4 \cdot d1\\
                                          
                                          
                                          \end{array}
                                          \end{array}
                                          
                                          Derivation
                                          1. Split input into 2 regimes
                                          2. if d2 < -3.4e20

                                            1. Initial program 92.1%

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

                                                \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1} \]
                                              2. lift-+.f64N/A

                                                \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right)} - d1 \cdot d1 \]
                                              3. associate--l+N/A

                                                \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
                                              4. +-commutativeN/A

                                                \[\leadsto \color{blue}{\left(d4 \cdot d1 - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right)} \]
                                              5. lift-*.f64N/A

                                                \[\leadsto \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                              6. lift-*.f64N/A

                                                \[\leadsto \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                              7. distribute-rgt-out--N/A

                                                \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                              8. lower-fma.f64N/A

                                                \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - d1 \cdot d3\right)} \]
                                              9. lower--.f6492.1

                                                \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d4 - d1}, d1 \cdot d2 - d1 \cdot d3\right) \]
                                              10. lift--.f64N/A

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2 - d1 \cdot d3}\right) \]
                                              11. lift-*.f64N/A

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2} - d1 \cdot d3\right) \]
                                              12. lift-*.f64N/A

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) \]
                                              13. distribute-lft-out--N/A

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot \left(d2 - d3\right)}\right) \]
                                              14. *-commutativeN/A

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
                                              15. lower-*.f64N/A

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
                                              16. lower--.f6492.1

                                                \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right)} \cdot d1\right) \]
                                            4. Applied rewrites92.1%

                                              \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)} \]
                                            5. Taylor expanded in d2 around inf

                                              \[\leadsto \color{blue}{d1 \cdot d2} \]
                                            6. Step-by-step derivation
                                              1. *-commutativeN/A

                                                \[\leadsto \color{blue}{d2 \cdot d1} \]
                                              2. lower-*.f6442.7

                                                \[\leadsto \color{blue}{d2 \cdot d1} \]
                                            7. Applied rewrites42.7%

                                              \[\leadsto \color{blue}{d2 \cdot d1} \]

                                            if -3.4e20 < d2

                                            1. Initial program 89.2%

                                              \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
                                            2. Add Preprocessing
                                            3. Taylor expanded in d4 around inf

                                              \[\leadsto \color{blue}{d1 \cdot d4} \]
                                            4. Step-by-step derivation
                                              1. *-commutativeN/A

                                                \[\leadsto \color{blue}{d4 \cdot d1} \]
                                              2. lower-*.f6436.2

                                                \[\leadsto \color{blue}{d4 \cdot d1} \]
                                            5. Applied rewrites36.2%

                                              \[\leadsto \color{blue}{d4 \cdot d1} \]
                                          3. Recombined 2 regimes into one program.
                                          4. Final simplification37.5%

                                            \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -3.4 \cdot 10^{+20}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \]
                                          5. Add Preprocessing

                                          Alternative 12: 31.1% accurate, 5.0× speedup?

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

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

                                              \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1} \]
                                            2. lift-+.f64N/A

                                              \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right)} - d1 \cdot d1 \]
                                            3. associate--l+N/A

                                              \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
                                            4. +-commutativeN/A

                                              \[\leadsto \color{blue}{\left(d4 \cdot d1 - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right)} \]
                                            5. lift-*.f64N/A

                                              \[\leadsto \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                            6. lift-*.f64N/A

                                              \[\leadsto \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right) + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                            7. distribute-rgt-out--N/A

                                              \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} + \left(d1 \cdot d2 - d1 \cdot d3\right) \]
                                            8. lower-fma.f64N/A

                                              \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - d1 \cdot d3\right)} \]
                                            9. lower--.f6495.3

                                              \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d4 - d1}, d1 \cdot d2 - d1 \cdot d3\right) \]
                                            10. lift--.f64N/A

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2 - d1 \cdot d3}\right) \]
                                            11. lift-*.f64N/A

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot d2} - d1 \cdot d3\right) \]
                                            12. lift-*.f64N/A

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) \]
                                            13. distribute-lft-out--N/A

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{d1 \cdot \left(d2 - d3\right)}\right) \]
                                            14. *-commutativeN/A

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
                                            15. lower-*.f64N/A

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right) \cdot d1}\right) \]
                                            16. lower--.f6495.3

                                              \[\leadsto \mathsf{fma}\left(d1, d4 - d1, \color{blue}{\left(d2 - d3\right)} \cdot d1\right) \]
                                          4. Applied rewrites95.3%

                                            \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d4 - d1, \left(d2 - d3\right) \cdot d1\right)} \]
                                          5. Taylor expanded in d2 around inf

                                            \[\leadsto \color{blue}{d1 \cdot d2} \]
                                          6. Step-by-step derivation
                                            1. *-commutativeN/A

                                              \[\leadsto \color{blue}{d2 \cdot d1} \]
                                            2. lower-*.f6424.4

                                              \[\leadsto \color{blue}{d2 \cdot d1} \]
                                          7. Applied rewrites24.4%

                                            \[\leadsto \color{blue}{d2 \cdot d1} \]
                                          8. Final simplification24.4%

                                            \[\leadsto d2 \cdot d1 \]
                                          9. Add Preprocessing

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

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

                                          Reproduce

                                          ?
                                          herbie shell --seed 2024346 
                                          (FPCore (d1 d2 d3 d4)
                                            :name "FastMath dist4"
                                            :precision binary64
                                          
                                            :alt
                                            (! :herbie-platform default (* d1 (- (+ (- d2 d3) d4) d1)))
                                          
                                            (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))