FastMath dist4

Percentage Accurate: 87.9% → 98.2%
Time: 3.0s
Alternatives: 12
Speedup: 1.6×

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}

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

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1\\ \mathbf{if}\;t\_0 \leq \infty:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\ \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
 (let* ((t_0 (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1))))
   (if (<= t_0 INFINITY) t_0 (* d1 (- (- d2 d3) d1)))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
	double tmp;
	if (t_0 <= ((double) INFINITY)) {
		tmp = t_0;
	} else {
		tmp = d1 * ((d2 - d3) - d1);
	}
	return tmp;
}
assert d1 < d2 && d2 < d3 && d3 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
	double tmp;
	if (t_0 <= Double.POSITIVE_INFINITY) {
		tmp = t_0;
	} else {
		tmp = d1 * ((d2 - d3) - d1);
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	t_0 = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
	tmp = 0
	if t_0 <= math.inf:
		tmp = t_0
	else:
		tmp = d1 * ((d2 - d3) - d1)
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1))
	tmp = 0.0
	if (t_0 <= Inf)
		tmp = t_0;
	else
		tmp = Float64(d1 * Float64(Float64(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)
	t_0 = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
	tmp = 0.0;
	if (t_0 <= Inf)
		tmp = t_0;
	else
		tmp = d1 * ((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_] := Block[{t$95$0 = N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], t$95$0, N[(d1 * N[(N[(d2 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
t_0 := \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1\\
\mathbf{if}\;t\_0 \leq \infty:\\
\;\;\;\;t\_0\\

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


\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 87.9%

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

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

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

Alternative 2: 93.3% accurate, 1.6× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 8.3 \cdot 10^{+83}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\ \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 8.3e+83) (* d1 (- (- d2 d3) 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 <= 8.3e+83) {
		tmp = d1 * ((d2 - d3) - 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 <= 8.3d+83) then
        tmp = d1 * ((d2 - d3) - 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 <= 8.3e+83) {
		tmp = d1 * ((d2 - d3) - 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 <= 8.3e+83:
		tmp = d1 * ((d2 - d3) - 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 <= 8.3e+83)
		tmp = Float64(d1 * Float64(Float64(d2 - d3) - 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 <= 8.3e+83)
		tmp = d1 * ((d2 - d3) - 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, 8.3e+83], N[(d1 * N[(N[(d2 - d3), $MachinePrecision] - d1), $MachinePrecision]), $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 8.3 \cdot 10^{+83}:\\
\;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\

\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 < 8.2999999999999996e83

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

    if 8.2999999999999996e83 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
      5. lower-+.f6480.7

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
    4. Applied rewrites80.7%

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

Alternative 3: 90.7% accurate, 1.6× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 4.5 \cdot 10^{+94}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\ \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 4.5e+94) (* d1 (- (- d2 d3) 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 <= 4.5e+94) {
		tmp = d1 * ((d2 - d3) - 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 <= 4.5d+94) then
        tmp = d1 * ((d2 - d3) - 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 <= 4.5e+94) {
		tmp = d1 * ((d2 - d3) - 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 <= 4.5e+94:
		tmp = d1 * ((d2 - d3) - 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 <= 4.5e+94)
		tmp = Float64(d1 * Float64(Float64(d2 - d3) - 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 <= 4.5e+94)
		tmp = d1 * ((d2 - d3) - 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, 4.5e+94], N[(d1 * N[(N[(d2 - d3), $MachinePrecision] - d1), $MachinePrecision]), $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 4.5 \cdot 10^{+94}:\\
\;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d4 < 4.49999999999999972e94

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

    if 4.49999999999999972e94 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
      5. lower-+.f6480.7

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
    4. Applied rewrites80.7%

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

      \[\leadsto \left(d4 - d3\right) \cdot d1 \]
    6. Step-by-step derivation
      1. lower--.f6457.0

        \[\leadsto \left(d4 - d3\right) \cdot d1 \]
    7. Applied rewrites57.0%

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

Alternative 4: 76.6% accurate, 1.4× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 2.8 \cdot 10^{-182}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+83}:\\ \;\;\;\;d1 \cdot \left(\left(-d3\right) - d1\right)\\ \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.8e-182)
   (* d1 (- d2 d3))
   (if (<= d4 8.6e+83) (* d1 (- (- d3) 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.8e-182) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 8.6e+83) {
		tmp = d1 * (-d3 - 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.8d-182) then
        tmp = d1 * (d2 - d3)
    else if (d4 <= 8.6d+83) then
        tmp = d1 * (-d3 - 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.8e-182) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 8.6e+83) {
		tmp = d1 * (-d3 - 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.8e-182:
		tmp = d1 * (d2 - d3)
	elif d4 <= 8.6e+83:
		tmp = d1 * (-d3 - 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.8e-182)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d4 <= 8.6e+83)
		tmp = Float64(d1 * Float64(Float64(-d3) - 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.8e-182)
		tmp = d1 * (d2 - d3);
	elseif (d4 <= 8.6e+83)
		tmp = d1 * (-d3 - 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.8e-182], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 8.6e+83], N[(d1 * N[((-d3) - d1), $MachinePrecision]), $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.8 \cdot 10^{-182}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < 2.79999999999999993e-182

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto d1 \cdot \left(d2 - \color{blue}{d3}\right) \]
    6. Step-by-step derivation
      1. lift--.f6455.6

        \[\leadsto d1 \cdot \left(d2 - d3\right) \]
    7. Applied rewrites55.6%

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

    if 2.79999999999999993e-182 < d4 < 8.6e83

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto d1 \cdot \left(-1 \cdot d3 - d1\right) \]
    6. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto d1 \cdot \left(\left(\mathsf{neg}\left(d3\right)\right) - d1\right) \]
      2. lower-neg.f6454.5

        \[\leadsto d1 \cdot \left(\left(-d3\right) - d1\right) \]
    7. Applied rewrites54.5%

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

    if 8.6e83 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
      5. lower-+.f6480.7

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
    4. Applied rewrites80.7%

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

      \[\leadsto \left(d4 - d3\right) \cdot d1 \]
    6. Step-by-step derivation
      1. lower--.f6457.0

        \[\leadsto \left(d4 - d3\right) \cdot d1 \]
    7. Applied rewrites57.0%

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

Alternative 5: 74.0% accurate, 1.5× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 1300000000000:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 1.8 \cdot 10^{+53}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \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 1300000000000.0)
   (* d1 (- d2 d3))
   (if (<= d4 1.8e+53) (* d1 (- d2 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 <= 1300000000000.0) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 1.8e+53) {
		tmp = d1 * (d2 - 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 <= 1300000000000.0d0) then
        tmp = d1 * (d2 - d3)
    else if (d4 <= 1.8d+53) then
        tmp = d1 * (d2 - 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 <= 1300000000000.0) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 1.8e+53) {
		tmp = d1 * (d2 - 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 <= 1300000000000.0:
		tmp = d1 * (d2 - d3)
	elif d4 <= 1.8e+53:
		tmp = d1 * (d2 - 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 <= 1300000000000.0)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d4 <= 1.8e+53)
		tmp = Float64(d1 * Float64(d2 - 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 <= 1300000000000.0)
		tmp = d1 * (d2 - d3);
	elseif (d4 <= 1.8e+53)
		tmp = d1 * (d2 - 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, 1300000000000.0], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 1.8e+53], N[(d1 * N[(d2 - d1), $MachinePrecision]), $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 1300000000000:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

\mathbf{elif}\;d4 \leq 1.8 \cdot 10^{+53}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < 1.3e12

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto d1 \cdot \left(d2 - \color{blue}{d3}\right) \]
    6. Step-by-step derivation
      1. lift--.f6455.6

        \[\leadsto d1 \cdot \left(d2 - d3\right) \]
    7. Applied rewrites55.6%

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

    if 1.3e12 < d4 < 1.8e53

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto d1 \cdot \left(d2 - \color{blue}{d1}\right) \]
    6. Step-by-step derivation
      1. lower--.f6453.5

        \[\leadsto d1 \cdot \left(d2 - d1\right) \]
    7. Applied rewrites53.5%

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

    if 1.8e53 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
      5. lower-+.f6480.7

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
    4. Applied rewrites80.7%

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

      \[\leadsto \left(d4 - d3\right) \cdot d1 \]
    6. Step-by-step derivation
      1. lower--.f6457.0

        \[\leadsto \left(d4 - d3\right) \cdot d1 \]
    7. Applied rewrites57.0%

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

Alternative 6: 72.5% accurate, 2.0× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 8500000000000:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - 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 (<= d4 8500000000000.0) (* d1 (- d2 d3)) (* (- d4 d1) d1)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 8500000000000.0) {
		tmp = d1 * (d2 - d3);
	} else {
		tmp = (d4 - 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 (d4 <= 8500000000000.0d0) then
        tmp = d1 * (d2 - d3)
    else
        tmp = (d4 - 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 (d4 <= 8500000000000.0) {
		tmp = d1 * (d2 - d3);
	} else {
		tmp = (d4 - d1) * d1;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 8500000000000.0:
		tmp = d1 * (d2 - d3)
	else:
		tmp = (d4 - d1) * d1
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 8500000000000.0)
		tmp = Float64(d1 * Float64(d2 - d3));
	else
		tmp = Float64(Float64(d4 - 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 (d4 <= 8500000000000.0)
		tmp = d1 * (d2 - d3);
	else
		tmp = (d4 - 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[d4, 8500000000000.0], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], N[(N[(d4 - d1), $MachinePrecision] * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 8500000000000:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d4 < 8.5e12

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto d1 \cdot \left(d2 - \color{blue}{d3}\right) \]
    6. Step-by-step derivation
      1. lift--.f6455.6

        \[\leadsto d1 \cdot \left(d2 - d3\right) \]
    7. Applied rewrites55.6%

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

    if 8.5e12 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
      5. pow2N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
      6. distribute-rgt-out--N/A

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
      8. lower--.f6475.4

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
    4. Applied rewrites75.4%

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

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

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      2. lower-*.f64N/A

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      3. lift--.f6454.9

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
    7. Applied rewrites54.9%

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

Alternative 7: 71.1% accurate, 2.0× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 1.85 \cdot 10^{+53}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - 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 (<= d4 1.85e+53) (* d1 (- d2 d1)) (* (- d4 d1) d1)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 1.85e+53) {
		tmp = d1 * (d2 - d1);
	} else {
		tmp = (d4 - 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 (d4 <= 1.85d+53) then
        tmp = d1 * (d2 - d1)
    else
        tmp = (d4 - 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 (d4 <= 1.85e+53) {
		tmp = d1 * (d2 - d1);
	} else {
		tmp = (d4 - d1) * d1;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 1.85e+53:
		tmp = d1 * (d2 - d1)
	else:
		tmp = (d4 - d1) * d1
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 1.85e+53)
		tmp = Float64(d1 * Float64(d2 - d1));
	else
		tmp = Float64(Float64(d4 - 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 (d4 <= 1.85e+53)
		tmp = d1 * (d2 - d1);
	else
		tmp = (d4 - 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[d4, 1.85e+53], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], N[(N[(d4 - d1), $MachinePrecision] * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 1.85 \cdot 10^{+53}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d4 < 1.85e53

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto d1 \cdot \left(d2 - \color{blue}{d1}\right) \]
    6. Step-by-step derivation
      1. lower--.f6453.5

        \[\leadsto d1 \cdot \left(d2 - d1\right) \]
    7. Applied rewrites53.5%

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

    if 1.85e53 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
      5. pow2N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
      6. distribute-rgt-out--N/A

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
      8. lower--.f6475.4

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
    4. Applied rewrites75.4%

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

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

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      2. lower-*.f64N/A

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      3. lift--.f6454.9

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
    7. Applied rewrites54.9%

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

Alternative 8: 67.2% accurate, 2.0× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -8.5 \cdot 10^{+145}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - 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 (<= d2 -8.5e+145) (* d2 d1) (* (- d4 d1) d1)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -8.5e+145) {
		tmp = d2 * d1;
	} else {
		tmp = (d4 - 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 (d2 <= (-8.5d+145)) then
        tmp = d2 * d1
    else
        tmp = (d4 - 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 (d2 <= -8.5e+145) {
		tmp = d2 * d1;
	} else {
		tmp = (d4 - d1) * d1;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -8.5e+145:
		tmp = d2 * d1
	else:
		tmp = (d4 - d1) * d1
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -8.5e+145)
		tmp = Float64(d2 * d1);
	else
		tmp = Float64(Float64(d4 - 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 (d2 <= -8.5e+145)
		tmp = d2 * d1;
	else
		tmp = (d4 - 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[d2, -8.5e+145], N[(d2 * d1), $MachinePrecision], N[(N[(d4 - d1), $MachinePrecision] * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -8.5 \cdot 10^{+145}:\\
\;\;\;\;d2 \cdot d1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d2 < -8.49999999999999977e145

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

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

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

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

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

    if -8.49999999999999977e145 < d2

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
      5. pow2N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
      6. distribute-rgt-out--N/A

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
      8. lower--.f6475.4

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
    4. Applied rewrites75.4%

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

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

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      2. lower-*.f64N/A

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      3. lift--.f6454.9

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
    7. Applied rewrites54.9%

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

Alternative 9: 53.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 4.2 \cdot 10^{-244}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 1400000000000:\\ \;\;\;\;\left(-d1\right) \cdot d3\\ \mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+83}:\\ \;\;\;\;\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 4.2e-244)
   (* d2 d1)
   (if (<= d4 1400000000000.0)
     (* (- d1) d3)
     (if (<= d4 8.6e+83) (* (- 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 <= 4.2e-244) {
		tmp = d2 * d1;
	} else if (d4 <= 1400000000000.0) {
		tmp = -d1 * d3;
	} else if (d4 <= 8.6e+83) {
		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 <= 4.2d-244) then
        tmp = d2 * d1
    else if (d4 <= 1400000000000.0d0) then
        tmp = -d1 * d3
    else if (d4 <= 8.6d+83) 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 <= 4.2e-244) {
		tmp = d2 * d1;
	} else if (d4 <= 1400000000000.0) {
		tmp = -d1 * d3;
	} else if (d4 <= 8.6e+83) {
		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 <= 4.2e-244:
		tmp = d2 * d1
	elif d4 <= 1400000000000.0:
		tmp = -d1 * d3
	elif d4 <= 8.6e+83:
		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 <= 4.2e-244)
		tmp = Float64(d2 * d1);
	elseif (d4 <= 1400000000000.0)
		tmp = Float64(Float64(-d1) * d3);
	elseif (d4 <= 8.6e+83)
		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 <= 4.2e-244)
		tmp = d2 * d1;
	elseif (d4 <= 1400000000000.0)
		tmp = -d1 * d3;
	elseif (d4 <= 8.6e+83)
		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, 4.2e-244], N[(d2 * d1), $MachinePrecision], If[LessEqual[d4, 1400000000000.0], N[((-d1) * d3), $MachinePrecision], If[LessEqual[d4, 8.6e+83], 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 4.2 \cdot 10^{-244}:\\
\;\;\;\;d2 \cdot d1\\

\mathbf{elif}\;d4 \leq 1400000000000:\\
\;\;\;\;\left(-d1\right) \cdot d3\\

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

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


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

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

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

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

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

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

    if 4.20000000000000003e-244 < d4 < 1.4e12

    1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
    4. Applied rewrites76.4%

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

      \[\leadsto \color{blue}{-1 \cdot \left(d1 \cdot d3\right)} \]
    6. Step-by-step derivation
      1. associate-*r*N/A

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

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

        \[\leadsto \left(\mathsf{neg}\left(d1\right)\right) \cdot d3 \]
      4. lift-neg.f6431.3

        \[\leadsto \left(-d1\right) \cdot d3 \]
    7. Applied rewrites31.3%

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

    if 1.4e12 < d4 < 8.6e83

    1. Initial program 87.9%

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

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

        \[\leadsto \mathsf{neg}\left({d1}^{2}\right) \]
      2. pow2N/A

        \[\leadsto \mathsf{neg}\left(d1 \cdot d1\right) \]
      3. distribute-lft-neg-outN/A

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

        \[\leadsto \left(-1 \cdot d1\right) \cdot d1 \]
      5. lower-*.f64N/A

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

        \[\leadsto \left(\mathsf{neg}\left(d1\right)\right) \cdot d1 \]
      7. lower-neg.f6431.8

        \[\leadsto \left(-d1\right) \cdot d1 \]
    4. Applied rewrites31.8%

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

    if 8.6e83 < d4

    1. Initial program 87.9%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
      5. pow2N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
      6. distribute-rgt-out--N/A

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
      8. lower--.f6475.4

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
    4. Applied rewrites75.4%

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

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

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      2. lower-*.f64N/A

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      3. lift--.f6454.9

        \[\leadsto \left(d4 - d1\right) \cdot d1 \]
    7. Applied rewrites54.9%

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

      \[\leadsto d4 \cdot d1 \]
    9. Step-by-step derivation
      1. Applied rewrites31.5%

        \[\leadsto d4 \cdot d1 \]
    10. Recombined 4 regimes into one program.
    11. Add Preprocessing

    Alternative 10: 52.6% accurate, 1.7× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 2.8 \cdot 10^{-182}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+83}:\\ \;\;\;\;\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.8e-182) (* d2 d1) (if (<= d4 8.6e+83) (* (- 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.8e-182) {
    		tmp = d2 * d1;
    	} else if (d4 <= 8.6e+83) {
    		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.8d-182) then
            tmp = d2 * d1
        else if (d4 <= 8.6d+83) 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.8e-182) {
    		tmp = d2 * d1;
    	} else if (d4 <= 8.6e+83) {
    		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.8e-182:
    		tmp = d2 * d1
    	elif d4 <= 8.6e+83:
    		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.8e-182)
    		tmp = Float64(d2 * d1);
    	elseif (d4 <= 8.6e+83)
    		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.8e-182)
    		tmp = d2 * d1;
    	elseif (d4 <= 8.6e+83)
    		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.8e-182], N[(d2 * d1), $MachinePrecision], If[LessEqual[d4, 8.6e+83], 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.8 \cdot 10^{-182}:\\
    \;\;\;\;d2 \cdot d1\\
    
    \mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+83}:\\
    \;\;\;\;\left(-d1\right) \cdot d1\\
    
    \mathbf{else}:\\
    \;\;\;\;d4 \cdot d1\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if d4 < 2.79999999999999993e-182

      1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
      4. Applied rewrites76.4%

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

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

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

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

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

      if 2.79999999999999993e-182 < d4 < 8.6e83

      1. Initial program 87.9%

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

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

          \[\leadsto \mathsf{neg}\left({d1}^{2}\right) \]
        2. pow2N/A

          \[\leadsto \mathsf{neg}\left(d1 \cdot d1\right) \]
        3. distribute-lft-neg-outN/A

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

          \[\leadsto \left(-1 \cdot d1\right) \cdot d1 \]
        5. lower-*.f64N/A

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

          \[\leadsto \left(\mathsf{neg}\left(d1\right)\right) \cdot d1 \]
        7. lower-neg.f6431.8

          \[\leadsto \left(-d1\right) \cdot d1 \]
      4. Applied rewrites31.8%

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

      if 8.6e83 < d4

      1. Initial program 87.9%

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

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

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

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

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

          \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
        5. pow2N/A

          \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
        6. distribute-rgt-out--N/A

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

          \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
        8. lower--.f6475.4

          \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
      4. Applied rewrites75.4%

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

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

          \[\leadsto \left(d4 - d1\right) \cdot d1 \]
        2. lower-*.f64N/A

          \[\leadsto \left(d4 - d1\right) \cdot d1 \]
        3. lift--.f6454.9

          \[\leadsto \left(d4 - d1\right) \cdot d1 \]
      7. Applied rewrites54.9%

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

        \[\leadsto d4 \cdot d1 \]
      9. Step-by-step derivation
        1. Applied rewrites31.5%

          \[\leadsto d4 \cdot d1 \]
      10. Recombined 3 regimes into one program.
      11. Add Preprocessing

      Alternative 11: 50.1% accurate, 2.7× speedup?

      \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 2 \cdot 10^{+53}:\\ \;\;\;\;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 (<= d4 2e+53) (* d2 d1) (* d4 d1)))
      assert(d1 < d2 && d2 < d3 && d3 < d4);
      double code(double d1, double d2, double d3, double d4) {
      	double tmp;
      	if (d4 <= 2e+53) {
      		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 (d4 <= 2d+53) 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 (d4 <= 2e+53) {
      		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 d4 <= 2e+53:
      		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 (d4 <= 2e+53)
      		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 (d4 <= 2e+53)
      		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[d4, 2e+53], 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}\;d4 \leq 2 \cdot 10^{+53}:\\
      \;\;\;\;d2 \cdot d1\\
      
      \mathbf{else}:\\
      \;\;\;\;d4 \cdot d1\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if d4 < 2e53

        1. Initial program 87.9%

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

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

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

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

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

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

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

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

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

            \[\leadsto d1 \cdot \left(\left(d2 - d3\right) - d1\right) \]
        4. Applied rewrites76.4%

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

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

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

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

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

        if 2e53 < d4

        1. Initial program 87.9%

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

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

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

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

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

            \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
          5. pow2N/A

            \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
          6. distribute-rgt-out--N/A

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

            \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
          8. lower--.f6475.4

            \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
        4. Applied rewrites75.4%

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

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

            \[\leadsto \left(d4 - d1\right) \cdot d1 \]
          2. lower-*.f64N/A

            \[\leadsto \left(d4 - d1\right) \cdot d1 \]
          3. lift--.f6454.9

            \[\leadsto \left(d4 - d1\right) \cdot d1 \]
        7. Applied rewrites54.9%

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

          \[\leadsto d4 \cdot d1 \]
        9. Step-by-step derivation
          1. Applied rewrites31.5%

            \[\leadsto d4 \cdot d1 \]
        10. Recombined 2 regimes into one program.
        11. Add Preprocessing

        Alternative 12: 31.5% accurate, 5.3× speedup?

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

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

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

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

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

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

            \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - {d1}^{2}\right) \]
          5. pow2N/A

            \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
          6. distribute-rgt-out--N/A

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

            \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
          8. lower--.f6475.4

            \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(d4 - d1\right)\right) \]
        4. Applied rewrites75.4%

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

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

            \[\leadsto \left(d4 - d1\right) \cdot d1 \]
          2. lower-*.f64N/A

            \[\leadsto \left(d4 - d1\right) \cdot d1 \]
          3. lift--.f6454.9

            \[\leadsto \left(d4 - d1\right) \cdot d1 \]
        7. Applied rewrites54.9%

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

          \[\leadsto d4 \cdot d1 \]
        9. Step-by-step derivation
          1. Applied rewrites31.5%

            \[\leadsto d4 \cdot d1 \]
          2. Add Preprocessing

          Developer Target 1: 100.0% accurate, 1.7× 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 2025127 
          (FPCore (d1 d2 d3 d4)
            :name "FastMath dist4"
            :precision binary64
          
            :alt
            (! :herbie-platform c (* d1 (- (+ (- d2 d3) d4) d1)))
          
            (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))