FastMath dist4

Percentage Accurate: 88.2% → 97.0%
Time: 3.2s
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: 88.2% 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: 97.0% accurate, 0.5× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \leq 5 \cdot 10^{+282}:\\ \;\;\;\;\mathsf{fma}\left(d4, d1, \left(d2 - d3\right) \cdot d1\right) - d1 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d4 - 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
 (if (<= (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)) 5e+282)
   (- (fma d4 d1 (* (- d2 d3) d1)) (* d1 d1))
   (* d1 (- (- d4 d3) d1))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (((((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)) <= 5e+282) {
		tmp = fma(d4, d1, ((d2 - d3) * d1)) - (d1 * d1);
	} else {
		tmp = d1 * ((d4 - d3) - d1);
	}
	return tmp;
}
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1)) <= 5e+282)
		tmp = Float64(fma(d4, d1, Float64(Float64(d2 - d3) * d1)) - Float64(d1 * d1));
	else
		tmp = Float64(d1 * Float64(Float64(d4 - d3) - d1));
	end
	return tmp
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision], 5e+282], N[(N[(d4 * d1 + N[(N[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d4 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \leq 5 \cdot 10^{+282}:\\
\;\;\;\;\mathsf{fma}\left(d4, d1, \left(d2 - d3\right) \cdot d1\right) - d1 \cdot d1\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(\left(d4 - 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)) < 4.99999999999999978e282

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 4.99999999999999978e282 < (-.f64 (+.f64 (-.f64 (*.f64 d1 d2) (*.f64 d1 d3)) (*.f64 d4 d1)) (*.f64 d1 d1))

    1. Initial program 28.9%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
      9. fp-cancel-sub-sign-invN/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
      10. mul-1-negN/A

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

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

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

        \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
      14. associate-+r+N/A

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

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

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
      17. mul-1-negN/A

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
      18. fp-cancel-sub-sign-invN/A

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 + d1 \cdot d4\right) - d1 \cdot d3\right)} - {d1}^{2} \]
    3. Applied rewrites49.0%

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

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
    5. Step-by-step derivation
      1. pow2N/A

        \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
      2. associate-+r-N/A

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

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      5. associate--l+N/A

        \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
      6. fp-cancel-sub-sign-invN/A

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      8. associate-*r*N/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      9. +-commutativeN/A

        \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
      10. associate-+r+N/A

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      12. associate-*r*N/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      13. mul-1-negN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      14. fp-cancel-sub-sign-invN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      15. *-commutativeN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      16. pow2N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
    6. Applied rewrites81.7%

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

Alternative 2: 92.1% accurate, 1.2× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(\left(d4 - d3\right) - d1\right)\\ \mathbf{if}\;d1 \leq -4.2 \cdot 10^{+43}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d1 \leq 5.2 \cdot 10^{+112}:\\ \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \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 (- (- d4 d3) d1))))
   (if (<= d1 -4.2e+43)
     t_0
     (if (<= d1 5.2e+112) (* (- (+ d4 d2) d3) d1) t_0))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * ((d4 - d3) - d1);
	double tmp;
	if (d1 <= -4.2e+43) {
		tmp = t_0;
	} else if (d1 <= 5.2e+112) {
		tmp = ((d4 + d2) - d3) * d1;
	} else {
		tmp = t_0;
	}
	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) :: t_0
    real(8) :: tmp
    t_0 = d1 * ((d4 - d3) - d1)
    if (d1 <= (-4.2d+43)) then
        tmp = t_0
    else if (d1 <= 5.2d+112) then
        tmp = ((d4 + d2) - d3) * d1
    else
        tmp = t_0
    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 t_0 = d1 * ((d4 - d3) - d1);
	double tmp;
	if (d1 <= -4.2e+43) {
		tmp = t_0;
	} else if (d1 <= 5.2e+112) {
		tmp = ((d4 + d2) - d3) * d1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * ((d4 - d3) - d1)
	tmp = 0
	if d1 <= -4.2e+43:
		tmp = t_0
	elif d1 <= 5.2e+112:
		tmp = ((d4 + d2) - d3) * d1
	else:
		tmp = t_0
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(Float64(d4 - d3) - d1))
	tmp = 0.0
	if (d1 <= -4.2e+43)
		tmp = t_0;
	elseif (d1 <= 5.2e+112)
		tmp = Float64(Float64(Float64(d4 + d2) - d3) * d1);
	else
		tmp = t_0;
	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 * ((d4 - d3) - d1);
	tmp = 0.0;
	if (d1 <= -4.2e+43)
		tmp = t_0;
	elseif (d1 <= 5.2e+112)
		tmp = ((d4 + d2) - d3) * d1;
	else
		tmp = t_0;
	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[(d1 * N[(N[(d4 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d1, -4.2e+43], t$95$0, If[LessEqual[d1, 5.2e+112], N[(N[(N[(d4 + d2), $MachinePrecision] - d3), $MachinePrecision] * d1), $MachinePrecision], t$95$0]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(\left(d4 - d3\right) - d1\right)\\
\mathbf{if}\;d1 \leq -4.2 \cdot 10^{+43}:\\
\;\;\;\;t\_0\\

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

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d1 < -4.20000000000000003e43 or 5.2000000000000001e112 < d1

    1. Initial program 68.6%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
      9. fp-cancel-sub-sign-invN/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
      10. mul-1-negN/A

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

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

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

        \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
      14. associate-+r+N/A

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

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

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
      17. mul-1-negN/A

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
      18. fp-cancel-sub-sign-invN/A

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
    5. Step-by-step derivation
      1. pow2N/A

        \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
      2. associate-+r-N/A

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

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      5. associate--l+N/A

        \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
      6. fp-cancel-sub-sign-invN/A

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      8. associate-*r*N/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      9. +-commutativeN/A

        \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
      10. associate-+r+N/A

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      12. associate-*r*N/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      13. mul-1-negN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      14. fp-cancel-sub-sign-invN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      15. *-commutativeN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      16. pow2N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
    6. Applied rewrites89.3%

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

    if -4.20000000000000003e43 < d1 < 5.2000000000000001e112

    1. Initial program 99.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-+.f6493.8

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

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

Alternative 3: 89.2% accurate, 1.6× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -1.5 \cdot 10^{+131}:\\ \;\;\;\;\mathsf{fma}\left(d2, d1, d4 \cdot d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d4 - 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
 (if (<= d2 -1.5e+131) (fma d2 d1 (* d4 d1)) (* d1 (- (- d4 d3) d1))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -1.5e+131) {
		tmp = fma(d2, d1, (d4 * d1));
	} else {
		tmp = d1 * ((d4 - d3) - d1);
	}
	return tmp;
}
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -1.5e+131)
		tmp = fma(d2, d1, Float64(d4 * d1));
	else
		tmp = Float64(d1 * Float64(Float64(d4 - d3) - d1));
	end
	return tmp
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -1.5e+131], N[(d2 * d1 + N[(d4 * d1), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d4 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -1.5 \cdot 10^{+131}:\\
\;\;\;\;\mathsf{fma}\left(d2, d1, d4 \cdot d1\right)\\

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


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

    1. Initial program 82.7%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
      9. fp-cancel-sub-sign-invN/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
      10. mul-1-negN/A

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

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

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

        \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
      14. associate-+r+N/A

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

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

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
      17. mul-1-negN/A

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
      18. fp-cancel-sub-sign-invN/A

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 + d1 \cdot d4\right) - d1 \cdot d3\right)} - {d1}^{2} \]
    3. Applied rewrites88.7%

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

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot \color{blue}{d1}\right) \]
      2. lift-*.f6481.3

        \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot \color{blue}{d1}\right) \]
    6. Applied rewrites81.3%

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

    if -1.5000000000000001e131 < d2

    1. Initial program 90.2%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
      9. fp-cancel-sub-sign-invN/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
      10. mul-1-negN/A

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

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

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

        \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
      14. associate-+r+N/A

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

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

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
      17. mul-1-negN/A

        \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
      18. fp-cancel-sub-sign-invN/A

        \[\leadsto \color{blue}{\left(\left(d1 \cdot d2 + d1 \cdot d4\right) - d1 \cdot d3\right)} - {d1}^{2} \]
    3. Applied rewrites92.6%

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

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
    5. Step-by-step derivation
      1. pow2N/A

        \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
      2. associate-+r-N/A

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

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      5. associate--l+N/A

        \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
      6. fp-cancel-sub-sign-invN/A

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      8. associate-*r*N/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      9. +-commutativeN/A

        \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
      10. associate-+r+N/A

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

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      12. associate-*r*N/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      13. mul-1-negN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      14. fp-cancel-sub-sign-invN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      15. *-commutativeN/A

        \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
      16. pow2N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
    6. Applied rewrites92.1%

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

Alternative 4: 71.0% accurate, 1.0× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -4.2 \cdot 10^{+117}:\\ \;\;\;\;\left(d2 - d3\right) \cdot d1\\ \mathbf{elif}\;d3 \leq -8.5 \cdot 10^{-108}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 3.8 \cdot 10^{+115}:\\ \;\;\;\;\mathsf{fma}\left(d2, d1, d4 \cdot d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(-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
 (if (<= d3 -4.2e+117)
   (* (- d2 d3) d1)
   (if (<= d3 -8.5e-108)
     (* d1 (- d4 d1))
     (if (<= d3 3.8e+115) (fma d2 d1 (* d4 d1)) (* d1 (- (- d3) d1))))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d3 <= -4.2e+117) {
		tmp = (d2 - d3) * d1;
	} else if (d3 <= -8.5e-108) {
		tmp = d1 * (d4 - d1);
	} else if (d3 <= 3.8e+115) {
		tmp = fma(d2, d1, (d4 * d1));
	} else {
		tmp = d1 * (-d3 - d1);
	}
	return tmp;
}
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d3 <= -4.2e+117)
		tmp = Float64(Float64(d2 - d3) * d1);
	elseif (d3 <= -8.5e-108)
		tmp = Float64(d1 * Float64(d4 - d1));
	elseif (d3 <= 3.8e+115)
		tmp = fma(d2, d1, Float64(d4 * d1));
	else
		tmp = Float64(d1 * Float64(Float64(-d3) - d1));
	end
	return tmp
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[d3, -4.2e+117], N[(N[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision], If[LessEqual[d3, -8.5e-108], N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 3.8e+115], N[(d2 * d1 + N[(d4 * d1), $MachinePrecision]), $MachinePrecision], N[(d1 * N[((-d3) - d1), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -4.2 \cdot 10^{+117}:\\
\;\;\;\;\left(d2 - d3\right) \cdot d1\\

\mathbf{elif}\;d3 \leq -8.5 \cdot 10^{-108}:\\
\;\;\;\;d1 \cdot \left(d4 - d1\right)\\

\mathbf{elif}\;d3 \leq 3.8 \cdot 10^{+115}:\\
\;\;\;\;\mathsf{fma}\left(d2, d1, d4 \cdot d1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d3 < -4.2000000000000002e117

    1. Initial program 82.5%

      \[\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-+.f6493.3

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

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

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

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

      if -4.2000000000000002e117 < d3 < -8.49999999999999986e-108

      1. Initial program 88.9%

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

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

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

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

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

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

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

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

          \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
        9. fp-cancel-sub-sign-invN/A

          \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
        10. mul-1-negN/A

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

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

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

          \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
        14. associate-+r+N/A

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

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

          \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
        17. mul-1-negN/A

          \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
        18. fp-cancel-sub-sign-invN/A

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
      5. Step-by-step derivation
        1. pow2N/A

          \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
        2. associate-+r-N/A

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

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

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        5. associate--l+N/A

          \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
        6. fp-cancel-sub-sign-invN/A

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

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        8. associate-*r*N/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        9. +-commutativeN/A

          \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
        10. associate-+r+N/A

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

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        12. associate-*r*N/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        13. mul-1-negN/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        14. fp-cancel-sub-sign-invN/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        15. *-commutativeN/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        16. pow2N/A

          \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
      6. Applied rewrites75.1%

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

        \[\leadsto d1 \cdot \left(d4 - \color{blue}{d1}\right) \]
      8. Step-by-step derivation
        1. lower--.f6461.9

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

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

      if -8.49999999999999986e-108 < d3 < 3.8000000000000001e115

      1. Initial program 90.9%

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

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

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

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

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

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

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

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

          \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
        9. fp-cancel-sub-sign-invN/A

          \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
        10. mul-1-negN/A

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

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

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

          \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
        14. associate-+r+N/A

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

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

          \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
        17. mul-1-negN/A

          \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
        18. fp-cancel-sub-sign-invN/A

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

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

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

          \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot \color{blue}{d1}\right) \]
        2. lift-*.f6467.8

          \[\leadsto \mathsf{fma}\left(d2, d1, d4 \cdot \color{blue}{d1}\right) \]
      6. Applied rewrites67.8%

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

      if 3.8000000000000001e115 < d3

      1. Initial program 83.9%

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

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

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

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

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

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

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

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

          \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
        9. fp-cancel-sub-sign-invN/A

          \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
        10. mul-1-negN/A

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

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

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

          \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
        14. associate-+r+N/A

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

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

          \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
        17. mul-1-negN/A

          \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
        18. fp-cancel-sub-sign-invN/A

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
      5. Step-by-step derivation
        1. pow2N/A

          \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
        2. associate-+r-N/A

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

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

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        5. associate--l+N/A

          \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
        6. fp-cancel-sub-sign-invN/A

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

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        8. associate-*r*N/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        9. +-commutativeN/A

          \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
        10. associate-+r+N/A

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

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        12. associate-*r*N/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        13. mul-1-negN/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        14. fp-cancel-sub-sign-invN/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        15. *-commutativeN/A

          \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
        16. pow2N/A

          \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
      6. Applied rewrites87.8%

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

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

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

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

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

    Alternative 5: 70.6% accurate, 1.1× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -4.2 \cdot 10^{+117}:\\ \;\;\;\;\left(d2 - d3\right) \cdot d1\\ \mathbf{elif}\;d3 \leq -8.5 \cdot 10^{-108}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 3.8 \cdot 10^{+115}:\\ \;\;\;\;\left(d4 + d2\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(-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
     (if (<= d3 -4.2e+117)
       (* (- d2 d3) d1)
       (if (<= d3 -8.5e-108)
         (* d1 (- d4 d1))
         (if (<= d3 3.8e+115) (* (+ d4 d2) d1) (* d1 (- (- d3) d1))))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double tmp;
    	if (d3 <= -4.2e+117) {
    		tmp = (d2 - d3) * d1;
    	} else if (d3 <= -8.5e-108) {
    		tmp = d1 * (d4 - d1);
    	} else if (d3 <= 3.8e+115) {
    		tmp = (d4 + d2) * d1;
    	} else {
    		tmp = d1 * (-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 (d3 <= (-4.2d+117)) then
            tmp = (d2 - d3) * d1
        else if (d3 <= (-8.5d-108)) then
            tmp = d1 * (d4 - d1)
        else if (d3 <= 3.8d+115) then
            tmp = (d4 + d2) * d1
        else
            tmp = d1 * (-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 (d3 <= -4.2e+117) {
    		tmp = (d2 - d3) * d1;
    	} else if (d3 <= -8.5e-108) {
    		tmp = d1 * (d4 - d1);
    	} else if (d3 <= 3.8e+115) {
    		tmp = (d4 + d2) * d1;
    	} else {
    		tmp = d1 * (-d3 - d1);
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	tmp = 0
    	if d3 <= -4.2e+117:
    		tmp = (d2 - d3) * d1
    	elif d3 <= -8.5e-108:
    		tmp = d1 * (d4 - d1)
    	elif d3 <= 3.8e+115:
    		tmp = (d4 + d2) * d1
    	else:
    		tmp = d1 * (-d3 - d1)
    	return tmp
    
    d1, d2, d3, d4 = sort([d1, d2, d3, d4])
    function code(d1, d2, d3, d4)
    	tmp = 0.0
    	if (d3 <= -4.2e+117)
    		tmp = Float64(Float64(d2 - d3) * d1);
    	elseif (d3 <= -8.5e-108)
    		tmp = Float64(d1 * Float64(d4 - d1));
    	elseif (d3 <= 3.8e+115)
    		tmp = Float64(Float64(d4 + d2) * d1);
    	else
    		tmp = Float64(d1 * Float64(Float64(-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 (d3 <= -4.2e+117)
    		tmp = (d2 - d3) * d1;
    	elseif (d3 <= -8.5e-108)
    		tmp = d1 * (d4 - d1);
    	elseif (d3 <= 3.8e+115)
    		tmp = (d4 + d2) * d1;
    	else
    		tmp = d1 * (-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[d3, -4.2e+117], N[(N[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision], If[LessEqual[d3, -8.5e-108], N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 3.8e+115], N[(N[(d4 + d2), $MachinePrecision] * d1), $MachinePrecision], N[(d1 * N[((-d3) - d1), $MachinePrecision]), $MachinePrecision]]]]
    
    \begin{array}{l}
    [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;d3 \leq -4.2 \cdot 10^{+117}:\\
    \;\;\;\;\left(d2 - d3\right) \cdot d1\\
    
    \mathbf{elif}\;d3 \leq -8.5 \cdot 10^{-108}:\\
    \;\;\;\;d1 \cdot \left(d4 - d1\right)\\
    
    \mathbf{elif}\;d3 \leq 3.8 \cdot 10^{+115}:\\
    \;\;\;\;\left(d4 + d2\right) \cdot d1\\
    
    \mathbf{else}:\\
    \;\;\;\;d1 \cdot \left(\left(-d3\right) - d1\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 4 regimes
    2. if d3 < -4.2000000000000002e117

      1. Initial program 82.5%

        \[\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-+.f6493.3

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

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

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

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

        if -4.2000000000000002e117 < d3 < -8.49999999999999986e-108

        1. Initial program 88.9%

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

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

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

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

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

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

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

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

            \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
          9. fp-cancel-sub-sign-invN/A

            \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
          10. mul-1-negN/A

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

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

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

            \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
          14. associate-+r+N/A

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

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

            \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
          17. mul-1-negN/A

            \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
          18. fp-cancel-sub-sign-invN/A

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

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

          \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
        5. Step-by-step derivation
          1. pow2N/A

            \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
          2. associate-+r-N/A

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

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

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          5. associate--l+N/A

            \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
          6. fp-cancel-sub-sign-invN/A

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

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          8. associate-*r*N/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          9. +-commutativeN/A

            \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
          10. associate-+r+N/A

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

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          12. associate-*r*N/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          13. mul-1-negN/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          14. fp-cancel-sub-sign-invN/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          15. *-commutativeN/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          16. pow2N/A

            \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
        6. Applied rewrites75.1%

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

          \[\leadsto d1 \cdot \left(d4 - \color{blue}{d1}\right) \]
        8. Step-by-step derivation
          1. lower--.f6461.9

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

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

        if -8.49999999999999986e-108 < d3 < 3.8000000000000001e115

        1. Initial program 90.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-+.f6474.1

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

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

          \[\leadsto \left(d2 + d4\right) \cdot d1 \]
        6. Step-by-step derivation
          1. +-commutativeN/A

            \[\leadsto \left(d4 + d2\right) \cdot d1 \]
          2. lift-+.f6468.2

            \[\leadsto \left(d4 + d2\right) \cdot d1 \]
        7. Applied rewrites68.2%

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

        if 3.8000000000000001e115 < d3

        1. Initial program 83.9%

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

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

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

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

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

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

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

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

            \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
          9. fp-cancel-sub-sign-invN/A

            \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
          10. mul-1-negN/A

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

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

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

            \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
          14. associate-+r+N/A

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

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

            \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
          17. mul-1-negN/A

            \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
          18. fp-cancel-sub-sign-invN/A

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

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

          \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
        5. Step-by-step derivation
          1. pow2N/A

            \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
          2. associate-+r-N/A

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

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

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          5. associate--l+N/A

            \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
          6. fp-cancel-sub-sign-invN/A

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

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          8. associate-*r*N/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          9. +-commutativeN/A

            \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
          10. associate-+r+N/A

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

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          12. associate-*r*N/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          13. mul-1-negN/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          14. fp-cancel-sub-sign-invN/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          15. *-commutativeN/A

            \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
          16. pow2N/A

            \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
        6. Applied rewrites87.8%

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

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

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

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

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

      Alternative 6: 70.4% accurate, 1.1× speedup?

      \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := \left(d2 - d3\right) \cdot d1\\ \mathbf{if}\;d3 \leq -4.2 \cdot 10^{+117}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d3 \leq -8.5 \cdot 10^{-108}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 0.00034:\\ \;\;\;\;\left(d4 + d2\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \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 (* (- d2 d3) d1)))
         (if (<= d3 -4.2e+117)
           t_0
           (if (<= d3 -8.5e-108)
             (* d1 (- d4 d1))
             (if (<= d3 0.00034) (* (+ d4 d2) d1) t_0)))))
      assert(d1 < d2 && d2 < d3 && d3 < d4);
      double code(double d1, double d2, double d3, double d4) {
      	double t_0 = (d2 - d3) * d1;
      	double tmp;
      	if (d3 <= -4.2e+117) {
      		tmp = t_0;
      	} else if (d3 <= -8.5e-108) {
      		tmp = d1 * (d4 - d1);
      	} else if (d3 <= 0.00034) {
      		tmp = (d4 + d2) * d1;
      	} else {
      		tmp = t_0;
      	}
      	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) :: t_0
          real(8) :: tmp
          t_0 = (d2 - d3) * d1
          if (d3 <= (-4.2d+117)) then
              tmp = t_0
          else if (d3 <= (-8.5d-108)) then
              tmp = d1 * (d4 - d1)
          else if (d3 <= 0.00034d0) then
              tmp = (d4 + d2) * d1
          else
              tmp = t_0
          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 t_0 = (d2 - d3) * d1;
      	double tmp;
      	if (d3 <= -4.2e+117) {
      		tmp = t_0;
      	} else if (d3 <= -8.5e-108) {
      		tmp = d1 * (d4 - d1);
      	} else if (d3 <= 0.00034) {
      		tmp = (d4 + d2) * d1;
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
      def code(d1, d2, d3, d4):
      	t_0 = (d2 - d3) * d1
      	tmp = 0
      	if d3 <= -4.2e+117:
      		tmp = t_0
      	elif d3 <= -8.5e-108:
      		tmp = d1 * (d4 - d1)
      	elif d3 <= 0.00034:
      		tmp = (d4 + d2) * d1
      	else:
      		tmp = t_0
      	return tmp
      
      d1, d2, d3, d4 = sort([d1, d2, d3, d4])
      function code(d1, d2, d3, d4)
      	t_0 = Float64(Float64(d2 - d3) * d1)
      	tmp = 0.0
      	if (d3 <= -4.2e+117)
      		tmp = t_0;
      	elseif (d3 <= -8.5e-108)
      		tmp = Float64(d1 * Float64(d4 - d1));
      	elseif (d3 <= 0.00034)
      		tmp = Float64(Float64(d4 + d2) * d1);
      	else
      		tmp = t_0;
      	end
      	return tmp
      end
      
      d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
      function tmp_2 = code(d1, d2, d3, d4)
      	t_0 = (d2 - d3) * d1;
      	tmp = 0.0;
      	if (d3 <= -4.2e+117)
      		tmp = t_0;
      	elseif (d3 <= -8.5e-108)
      		tmp = d1 * (d4 - d1);
      	elseif (d3 <= 0.00034)
      		tmp = (d4 + d2) * d1;
      	else
      		tmp = t_0;
      	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[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision]}, If[LessEqual[d3, -4.2e+117], t$95$0, If[LessEqual[d3, -8.5e-108], N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 0.00034], N[(N[(d4 + d2), $MachinePrecision] * d1), $MachinePrecision], t$95$0]]]]
      
      \begin{array}{l}
      [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
      \\
      \begin{array}{l}
      t_0 := \left(d2 - d3\right) \cdot d1\\
      \mathbf{if}\;d3 \leq -4.2 \cdot 10^{+117}:\\
      \;\;\;\;t\_0\\
      
      \mathbf{elif}\;d3 \leq -8.5 \cdot 10^{-108}:\\
      \;\;\;\;d1 \cdot \left(d4 - d1\right)\\
      
      \mathbf{elif}\;d3 \leq 0.00034:\\
      \;\;\;\;\left(d4 + d2\right) \cdot d1\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_0\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if d3 < -4.2000000000000002e117 or 3.4e-4 < d3

        1. Initial program 84.7%

          \[\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-+.f6489.9

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

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

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

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

          if -4.2000000000000002e117 < d3 < -8.49999999999999986e-108

          1. Initial program 88.9%

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

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

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

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

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

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

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

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

              \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
            9. fp-cancel-sub-sign-invN/A

              \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
            10. mul-1-negN/A

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

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

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

              \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
            14. associate-+r+N/A

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

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

              \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
            17. mul-1-negN/A

              \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
            18. fp-cancel-sub-sign-invN/A

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

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

            \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
          5. Step-by-step derivation
            1. pow2N/A

              \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
            2. associate-+r-N/A

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

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

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            5. associate--l+N/A

              \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
            6. fp-cancel-sub-sign-invN/A

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

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            8. associate-*r*N/A

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            9. +-commutativeN/A

              \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
            10. associate-+r+N/A

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

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            12. associate-*r*N/A

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            13. mul-1-negN/A

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            14. fp-cancel-sub-sign-invN/A

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            15. *-commutativeN/A

              \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
            16. pow2N/A

              \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
          6. Applied rewrites75.1%

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

            \[\leadsto d1 \cdot \left(d4 - \color{blue}{d1}\right) \]
          8. Step-by-step derivation
            1. lower--.f6461.9

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

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

          if -8.49999999999999986e-108 < d3 < 3.4e-4

          1. Initial program 91.3%

            \[\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-+.f6472.7

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

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

            \[\leadsto \left(d2 + d4\right) \cdot d1 \]
          6. Step-by-step derivation
            1. +-commutativeN/A

              \[\leadsto \left(d4 + d2\right) \cdot d1 \]
            2. lift-+.f6471.0

              \[\leadsto \left(d4 + d2\right) \cdot d1 \]
          7. Applied rewrites71.0%

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

        Alternative 7: 70.0% accurate, 2.0× speedup?

        \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -4.15 \cdot 10^{-144}:\\ \;\;\;\;\left(d2 - d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - 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
         (if (<= d2 -4.15e-144) (* (- d2 d3) d1) (* d1 (- d4 d1))))
        assert(d1 < d2 && d2 < d3 && d3 < d4);
        double code(double d1, double d2, double d3, double d4) {
        	double tmp;
        	if (d2 <= -4.15e-144) {
        		tmp = (d2 - d3) * d1;
        	} else {
        		tmp = d1 * (d4 - d1);
        	}
        	return tmp;
        }
        
        NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(d1, d2, d3, d4)
        use fmin_fmax_functions
            real(8), intent (in) :: d1
            real(8), intent (in) :: d2
            real(8), intent (in) :: d3
            real(8), intent (in) :: d4
            real(8) :: tmp
            if (d2 <= (-4.15d-144)) then
                tmp = (d2 - d3) * d1
            else
                tmp = d1 * (d4 - d1)
            end if
            code = tmp
        end function
        
        assert d1 < d2 && d2 < d3 && d3 < d4;
        public static double code(double d1, double d2, double d3, double d4) {
        	double tmp;
        	if (d2 <= -4.15e-144) {
        		tmp = (d2 - d3) * d1;
        	} else {
        		tmp = d1 * (d4 - d1);
        	}
        	return tmp;
        }
        
        [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
        def code(d1, d2, d3, d4):
        	tmp = 0
        	if d2 <= -4.15e-144:
        		tmp = (d2 - d3) * d1
        	else:
        		tmp = d1 * (d4 - d1)
        	return tmp
        
        d1, d2, d3, d4 = sort([d1, d2, d3, d4])
        function code(d1, d2, d3, d4)
        	tmp = 0.0
        	if (d2 <= -4.15e-144)
        		tmp = Float64(Float64(d2 - d3) * d1);
        	else
        		tmp = Float64(d1 * Float64(d4 - d1));
        	end
        	return tmp
        end
        
        d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
        function tmp_2 = code(d1, d2, d3, d4)
        	tmp = 0.0;
        	if (d2 <= -4.15e-144)
        		tmp = (d2 - d3) * d1;
        	else
        		tmp = d1 * (d4 - d1);
        	end
        	tmp_2 = tmp;
        end
        
        NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
        code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -4.15e-144], N[(N[(d2 - d3), $MachinePrecision] * d1), $MachinePrecision], N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision]]
        
        \begin{array}{l}
        [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
        \\
        \begin{array}{l}
        \mathbf{if}\;d2 \leq -4.15 \cdot 10^{-144}:\\
        \;\;\;\;\left(d2 - d3\right) \cdot d1\\
        
        \mathbf{else}:\\
        \;\;\;\;d1 \cdot \left(d4 - d1\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if d2 < -4.1500000000000001e-144

          1. Initial program 86.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-+.f6482.1

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

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

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

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

            if -4.1500000000000001e-144 < d2

            1. Initial program 90.2%

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

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

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

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

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

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

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

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

                \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
              9. fp-cancel-sub-sign-invN/A

                \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
              10. mul-1-negN/A

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

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

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

                \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
              14. associate-+r+N/A

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

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

                \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
              17. mul-1-negN/A

                \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
              18. fp-cancel-sub-sign-invN/A

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

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

              \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
            5. Step-by-step derivation
              1. pow2N/A

                \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
              2. associate-+r-N/A

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

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

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              5. associate--l+N/A

                \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
              6. fp-cancel-sub-sign-invN/A

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

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              8. associate-*r*N/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              9. +-commutativeN/A

                \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
              10. associate-+r+N/A

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

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              12. associate-*r*N/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              13. mul-1-negN/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              14. fp-cancel-sub-sign-invN/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              15. *-commutativeN/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              16. pow2N/A

                \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
            6. Applied rewrites99.5%

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

              \[\leadsto d1 \cdot \left(d4 - \color{blue}{d1}\right) \]
            8. Step-by-step derivation
              1. lower--.f6473.0

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

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

          Alternative 8: 65.4% accurate, 1.2× speedup?

          \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d4 - d1\right)\\ \mathbf{if}\;d2 \leq -1.8 \cdot 10^{+46}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d2 \leq -3.6 \cdot 10^{-78}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d2 \leq -4.15 \cdot 10^{-144}:\\ \;\;\;\;\left(-d3\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \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 (- d4 d1))))
             (if (<= d2 -1.8e+46)
               (* d2 d1)
               (if (<= d2 -3.6e-78) t_0 (if (<= d2 -4.15e-144) (* (- d3) d1) t_0)))))
          assert(d1 < d2 && d2 < d3 && d3 < d4);
          double code(double d1, double d2, double d3, double d4) {
          	double t_0 = d1 * (d4 - d1);
          	double tmp;
          	if (d2 <= -1.8e+46) {
          		tmp = d2 * d1;
          	} else if (d2 <= -3.6e-78) {
          		tmp = t_0;
          	} else if (d2 <= -4.15e-144) {
          		tmp = -d3 * d1;
          	} else {
          		tmp = t_0;
          	}
          	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) :: t_0
              real(8) :: tmp
              t_0 = d1 * (d4 - d1)
              if (d2 <= (-1.8d+46)) then
                  tmp = d2 * d1
              else if (d2 <= (-3.6d-78)) then
                  tmp = t_0
              else if (d2 <= (-4.15d-144)) then
                  tmp = -d3 * d1
              else
                  tmp = t_0
              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 t_0 = d1 * (d4 - d1);
          	double tmp;
          	if (d2 <= -1.8e+46) {
          		tmp = d2 * d1;
          	} else if (d2 <= -3.6e-78) {
          		tmp = t_0;
          	} else if (d2 <= -4.15e-144) {
          		tmp = -d3 * d1;
          	} else {
          		tmp = t_0;
          	}
          	return tmp;
          }
          
          [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
          def code(d1, d2, d3, d4):
          	t_0 = d1 * (d4 - d1)
          	tmp = 0
          	if d2 <= -1.8e+46:
          		tmp = d2 * d1
          	elif d2 <= -3.6e-78:
          		tmp = t_0
          	elif d2 <= -4.15e-144:
          		tmp = -d3 * d1
          	else:
          		tmp = t_0
          	return tmp
          
          d1, d2, d3, d4 = sort([d1, d2, d3, d4])
          function code(d1, d2, d3, d4)
          	t_0 = Float64(d1 * Float64(d4 - d1))
          	tmp = 0.0
          	if (d2 <= -1.8e+46)
          		tmp = Float64(d2 * d1);
          	elseif (d2 <= -3.6e-78)
          		tmp = t_0;
          	elseif (d2 <= -4.15e-144)
          		tmp = Float64(Float64(-d3) * d1);
          	else
          		tmp = t_0;
          	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 * (d4 - d1);
          	tmp = 0.0;
          	if (d2 <= -1.8e+46)
          		tmp = d2 * d1;
          	elseif (d2 <= -3.6e-78)
          		tmp = t_0;
          	elseif (d2 <= -4.15e-144)
          		tmp = -d3 * d1;
          	else
          		tmp = t_0;
          	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[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d2, -1.8e+46], N[(d2 * d1), $MachinePrecision], If[LessEqual[d2, -3.6e-78], t$95$0, If[LessEqual[d2, -4.15e-144], N[((-d3) * d1), $MachinePrecision], t$95$0]]]]
          
          \begin{array}{l}
          [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
          \\
          \begin{array}{l}
          t_0 := d1 \cdot \left(d4 - d1\right)\\
          \mathbf{if}\;d2 \leq -1.8 \cdot 10^{+46}:\\
          \;\;\;\;d2 \cdot d1\\
          
          \mathbf{elif}\;d2 \leq -3.6 \cdot 10^{-78}:\\
          \;\;\;\;t\_0\\
          
          \mathbf{elif}\;d2 \leq -4.15 \cdot 10^{-144}:\\
          \;\;\;\;\left(-d3\right) \cdot d1\\
          
          \mathbf{else}:\\
          \;\;\;\;t\_0\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if d2 < -1.7999999999999999e46

            1. Initial program 83.6%

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

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

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

                \[\leadsto d2 \cdot \color{blue}{d1} \]
            4. Applied rewrites65.6%

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

            if -1.7999999999999999e46 < d2 < -3.6000000000000002e-78 or -4.1500000000000001e-144 < d2

            1. Initial program 90.6%

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

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

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

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

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

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

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

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

                \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{{d1}^{2}} \]
              9. fp-cancel-sub-sign-invN/A

                \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1\right)\right) \cdot d3\right)} + d4 \cdot d1\right) - {d1}^{2} \]
              10. mul-1-negN/A

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

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

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

                \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - {d1}^{2} \]
              14. associate-+r+N/A

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

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

                \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(-1 \cdot d1\right) \cdot d3}\right) - {d1}^{2} \]
              17. mul-1-negN/A

                \[\leadsto \left(\left(d1 \cdot d2 + d1 \cdot d4\right) + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d3\right) - {d1}^{2} \]
              18. fp-cancel-sub-sign-invN/A

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

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

              \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right) - {d1}^{2}} \]
            5. Step-by-step derivation
              1. pow2N/A

                \[\leadsto d1 \cdot \left(d4 - \color{blue}{d3}\right) - {d1}^{2} \]
              2. associate-+r-N/A

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

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

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              5. associate--l+N/A

                \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
              6. fp-cancel-sub-sign-invN/A

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

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              8. associate-*r*N/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              9. +-commutativeN/A

                \[\leadsto \color{blue}{d1} \cdot \left(d4 - d3\right) - {d1}^{2} \]
              10. associate-+r+N/A

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

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              12. associate-*r*N/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              13. mul-1-negN/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              14. fp-cancel-sub-sign-invN/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              15. *-commutativeN/A

                \[\leadsto d1 \cdot \left(d4 - d3\right) - {d1}^{2} \]
              16. pow2N/A

                \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} - {d1}^{2} \]
            6. Applied rewrites96.2%

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

              \[\leadsto d1 \cdot \left(d4 - \color{blue}{d1}\right) \]
            8. Step-by-step derivation
              1. lower--.f6468.9

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

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

            if -3.6000000000000002e-78 < d2 < -4.1500000000000001e-144

            1. Initial program 93.9%

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

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

                \[\leadsto \mathsf{neg}\left(d1 \cdot d3\right) \]
              2. *-commutativeN/A

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

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

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

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

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

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

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

          Alternative 9: 53.1% accurate, 1.0× speedup?

          \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := \left(-d3\right) \cdot d1\\ \mathbf{if}\;d2 \leq -1.5 \cdot 10^{+46}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d2 \leq -4.1 \cdot 10^{-147}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d2 \leq 4.5 \cdot 10^{-303}:\\ \;\;\;\;d4 \cdot d1\\ \mathbf{elif}\;d2 \leq 1.26 \cdot 10^{-169}:\\ \;\;\;\;t\_0\\ \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
           (let* ((t_0 (* (- d3) d1)))
             (if (<= d2 -1.5e+46)
               (* d2 d1)
               (if (<= d2 -4.1e-147)
                 t_0
                 (if (<= d2 4.5e-303) (* d4 d1) (if (<= d2 1.26e-169) t_0 (* d4 d1)))))))
          assert(d1 < d2 && d2 < d3 && d3 < d4);
          double code(double d1, double d2, double d3, double d4) {
          	double t_0 = -d3 * d1;
          	double tmp;
          	if (d2 <= -1.5e+46) {
          		tmp = d2 * d1;
          	} else if (d2 <= -4.1e-147) {
          		tmp = t_0;
          	} else if (d2 <= 4.5e-303) {
          		tmp = d4 * d1;
          	} else if (d2 <= 1.26e-169) {
          		tmp = t_0;
          	} 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) :: t_0
              real(8) :: tmp
              t_0 = -d3 * d1
              if (d2 <= (-1.5d+46)) then
                  tmp = d2 * d1
              else if (d2 <= (-4.1d-147)) then
                  tmp = t_0
              else if (d2 <= 4.5d-303) then
                  tmp = d4 * d1
              else if (d2 <= 1.26d-169) then
                  tmp = t_0
              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 t_0 = -d3 * d1;
          	double tmp;
          	if (d2 <= -1.5e+46) {
          		tmp = d2 * d1;
          	} else if (d2 <= -4.1e-147) {
          		tmp = t_0;
          	} else if (d2 <= 4.5e-303) {
          		tmp = d4 * d1;
          	} else if (d2 <= 1.26e-169) {
          		tmp = t_0;
          	} else {
          		tmp = d4 * d1;
          	}
          	return tmp;
          }
          
          [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
          def code(d1, d2, d3, d4):
          	t_0 = -d3 * d1
          	tmp = 0
          	if d2 <= -1.5e+46:
          		tmp = d2 * d1
          	elif d2 <= -4.1e-147:
          		tmp = t_0
          	elif d2 <= 4.5e-303:
          		tmp = d4 * d1
          	elif d2 <= 1.26e-169:
          		tmp = t_0
          	else:
          		tmp = d4 * d1
          	return tmp
          
          d1, d2, d3, d4 = sort([d1, d2, d3, d4])
          function code(d1, d2, d3, d4)
          	t_0 = Float64(Float64(-d3) * d1)
          	tmp = 0.0
          	if (d2 <= -1.5e+46)
          		tmp = Float64(d2 * d1);
          	elseif (d2 <= -4.1e-147)
          		tmp = t_0;
          	elseif (d2 <= 4.5e-303)
          		tmp = Float64(d4 * d1);
          	elseif (d2 <= 1.26e-169)
          		tmp = t_0;
          	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)
          	t_0 = -d3 * d1;
          	tmp = 0.0;
          	if (d2 <= -1.5e+46)
          		tmp = d2 * d1;
          	elseif (d2 <= -4.1e-147)
          		tmp = t_0;
          	elseif (d2 <= 4.5e-303)
          		tmp = d4 * d1;
          	elseif (d2 <= 1.26e-169)
          		tmp = t_0;
          	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_] := Block[{t$95$0 = N[((-d3) * d1), $MachinePrecision]}, If[LessEqual[d2, -1.5e+46], N[(d2 * d1), $MachinePrecision], If[LessEqual[d2, -4.1e-147], t$95$0, If[LessEqual[d2, 4.5e-303], N[(d4 * d1), $MachinePrecision], If[LessEqual[d2, 1.26e-169], t$95$0, N[(d4 * d1), $MachinePrecision]]]]]]
          
          \begin{array}{l}
          [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
          \\
          \begin{array}{l}
          t_0 := \left(-d3\right) \cdot d1\\
          \mathbf{if}\;d2 \leq -1.5 \cdot 10^{+46}:\\
          \;\;\;\;d2 \cdot d1\\
          
          \mathbf{elif}\;d2 \leq -4.1 \cdot 10^{-147}:\\
          \;\;\;\;t\_0\\
          
          \mathbf{elif}\;d2 \leq 4.5 \cdot 10^{-303}:\\
          \;\;\;\;d4 \cdot d1\\
          
          \mathbf{elif}\;d2 \leq 1.26 \cdot 10^{-169}:\\
          \;\;\;\;t\_0\\
          
          \mathbf{else}:\\
          \;\;\;\;d4 \cdot d1\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if d2 < -1.50000000000000012e46

            1. Initial program 83.6%

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

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

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

                \[\leadsto d2 \cdot \color{blue}{d1} \]
            4. Applied rewrites65.6%

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

            if -1.50000000000000012e46 < d2 < -4.1e-147 or 4.5000000000000001e-303 < d2 < 1.26e-169

            1. Initial program 92.3%

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

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

                \[\leadsto \mathsf{neg}\left(d1 \cdot d3\right) \]
              2. *-commutativeN/A

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

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

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

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

                \[\leadsto \left(\mathsf{neg}\left(d3\right)\right) \cdot d1 \]
              7. lower-neg.f6438.1

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

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

            if -4.1e-147 < d2 < 4.5000000000000001e-303 or 1.26e-169 < d2

            1. Initial program 89.7%

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

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

                \[\leadsto d4 \cdot \color{blue}{d1} \]
              2. lift-*.f6449.2

                \[\leadsto d4 \cdot \color{blue}{d1} \]
            4. Applied rewrites49.2%

              \[\leadsto \color{blue}{d4 \cdot d1} \]
          3. Recombined 3 regimes into one program.
          4. Add Preprocessing

          Alternative 10: 52.0% accurate, 1.7× speedup?

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

            1. Initial program 89.8%

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

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

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

                \[\leadsto d2 \cdot \color{blue}{d1} \]
            4. Applied rewrites48.3%

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

            if 1.1999999999999999e-136 < d4 < 8.19999999999999972e95

            1. Initial program 90.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-inN/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.f6438.8

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

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

            if 8.19999999999999972e95 < d4

            1. Initial program 83.8%

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

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

                \[\leadsto d4 \cdot \color{blue}{d1} \]
              2. lift-*.f6471.5

                \[\leadsto d4 \cdot \color{blue}{d1} \]
            4. Applied rewrites71.5%

              \[\leadsto \color{blue}{d4 \cdot d1} \]
          3. Recombined 3 regimes into one program.
          4. Add Preprocessing

          Alternative 11: 50.0% accurate, 2.7× speedup?

          \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -3.3 \cdot 10^{-52}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d4 \cdot d1\\ \end{array} \end{array} \]
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          (FPCore (d1 d2 d3 d4)
           :precision binary64
           (if (<= d2 -3.3e-52) (* d2 d1) (* d4 d1)))
          assert(d1 < d2 && d2 < d3 && d3 < d4);
          double code(double d1, double d2, double d3, double d4) {
          	double tmp;
          	if (d2 <= -3.3e-52) {
          		tmp = d2 * d1;
          	} else {
          		tmp = d4 * d1;
          	}
          	return tmp;
          }
          
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          module fmin_fmax_functions
              implicit none
              private
              public fmax
              public fmin
          
              interface fmax
                  module procedure fmax88
                  module procedure fmax44
                  module procedure fmax84
                  module procedure fmax48
              end interface
              interface fmin
                  module procedure fmin88
                  module procedure fmin44
                  module procedure fmin84
                  module procedure fmin48
              end interface
          contains
              real(8) function fmax88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(4) function fmax44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(8) function fmax84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmax48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
              end function
              real(8) function fmin88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(4) function fmin44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(8) function fmin84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmin48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
              end function
          end module
          
          real(8) function code(d1, d2, d3, d4)
          use fmin_fmax_functions
              real(8), intent (in) :: d1
              real(8), intent (in) :: d2
              real(8), intent (in) :: d3
              real(8), intent (in) :: d4
              real(8) :: tmp
              if (d2 <= (-3.3d-52)) then
                  tmp = d2 * d1
              else
                  tmp = d4 * d1
              end if
              code = tmp
          end function
          
          assert d1 < d2 && d2 < d3 && d3 < d4;
          public static double code(double d1, double d2, double d3, double d4) {
          	double tmp;
          	if (d2 <= -3.3e-52) {
          		tmp = d2 * d1;
          	} else {
          		tmp = d4 * d1;
          	}
          	return tmp;
          }
          
          [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
          def code(d1, d2, d3, d4):
          	tmp = 0
          	if d2 <= -3.3e-52:
          		tmp = d2 * d1
          	else:
          		tmp = d4 * d1
          	return tmp
          
          d1, d2, d3, d4 = sort([d1, d2, d3, d4])
          function code(d1, d2, d3, d4)
          	tmp = 0.0
          	if (d2 <= -3.3e-52)
          		tmp = Float64(d2 * d1);
          	else
          		tmp = Float64(d4 * d1);
          	end
          	return tmp
          end
          
          d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
          function tmp_2 = code(d1, d2, d3, d4)
          	tmp = 0.0;
          	if (d2 <= -3.3e-52)
          		tmp = d2 * d1;
          	else
          		tmp = d4 * d1;
          	end
          	tmp_2 = tmp;
          end
          
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -3.3e-52], N[(d2 * d1), $MachinePrecision], N[(d4 * d1), $MachinePrecision]]
          
          \begin{array}{l}
          [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
          \\
          \begin{array}{l}
          \mathbf{if}\;d2 \leq -3.3 \cdot 10^{-52}:\\
          \;\;\;\;d2 \cdot d1\\
          
          \mathbf{else}:\\
          \;\;\;\;d4 \cdot d1\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if d2 < -3.29999999999999995e-52

            1. Initial program 85.6%

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

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

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

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

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

            if -3.29999999999999995e-52 < d2

            1. Initial program 90.8%

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

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

                \[\leadsto d4 \cdot \color{blue}{d1} \]
              2. lift-*.f6445.3

                \[\leadsto d4 \cdot \color{blue}{d1} \]
            4. Applied rewrites45.3%

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

          Alternative 12: 31.0% accurate, 5.3× speedup?

          \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ d2 \cdot d1 \end{array} \]
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          (FPCore (d1 d2 d3 d4) :precision binary64 (* d2 d1))
          assert(d1 < d2 && d2 < d3 && d3 < d4);
          double code(double d1, double d2, double d3, double d4) {
          	return d2 * d1;
          }
          
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          module fmin_fmax_functions
              implicit none
              private
              public fmax
              public fmin
          
              interface fmax
                  module procedure fmax88
                  module procedure fmax44
                  module procedure fmax84
                  module procedure fmax48
              end interface
              interface fmin
                  module procedure fmin88
                  module procedure fmin44
                  module procedure fmin84
                  module procedure fmin48
              end interface
          contains
              real(8) function fmax88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(4) function fmax44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(8) function fmax84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmax48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
              end function
              real(8) function fmin88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(4) function fmin44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(8) function fmin84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmin48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
              end function
          end module
          
          real(8) function code(d1, d2, d3, d4)
          use fmin_fmax_functions
              real(8), intent (in) :: d1
              real(8), intent (in) :: d2
              real(8), intent (in) :: d3
              real(8), intent (in) :: d4
              code = d2 * d1
          end function
          
          assert d1 < d2 && d2 < d3 && d3 < d4;
          public static double code(double d1, double d2, double d3, double d4) {
          	return d2 * d1;
          }
          
          [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
          def code(d1, d2, d3, d4):
          	return d2 * d1
          
          d1, d2, d3, d4 = sort([d1, d2, d3, d4])
          function code(d1, d2, d3, d4)
          	return Float64(d2 * d1)
          end
          
          d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
          function tmp = code(d1, d2, d3, d4)
          	tmp = d2 * d1;
          end
          
          NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
          code[d1_, d2_, d3_, d4_] := N[(d2 * d1), $MachinePrecision]
          
          \begin{array}{l}
          [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
          \\
          d2 \cdot d1
          \end{array}
          
          Derivation
          1. Initial program 88.2%

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

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

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

              \[\leadsto d2 \cdot \color{blue}{d1} \]
          4. Applied rewrites31.0%

            \[\leadsto \color{blue}{d2 \cdot d1} \]
          5. 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 2025119 
          (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)))