FastMath dist4

Percentage Accurate: 87.8% → 98.4%
Time: 4.0s
Alternatives: 9
Speedup: 1.7×

Specification

?
\[\begin{array}{l} \\ \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))
double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(d1, d2, d3, d4)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
end function
public static double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
def code(d1, d2, d3, d4):
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
function code(d1, d2, d3, d4)
	return Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1))
end
function tmp = code(d1, d2, d3, d4)
	tmp = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
end
code[d1_, d2_, d3_, d4_] := N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 87.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))
double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(d1, d2, d3, d4)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
end function
public static double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
def code(d1, d2, d3, d4):
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
function code(d1, d2, d3, d4)
	return Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1))
end
function tmp = code(d1, d2, d3, d4)
	tmp = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
end
code[d1_, d2_, d3_, d4_] := N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 98.4% 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 \infty:\\ \;\;\;\;d1 \cdot \left(d4 + d2\right) - \mathsf{fma}\left(d3, d1, d1 \cdot d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)) INFINITY)
   (- (* d1 (+ d4 d2)) (fma d3 d1 (* d1 d1)))
   (* d1 (- (- d2 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)) <= ((double) INFINITY)) {
		tmp = (d1 * (d4 + d2)) - fma(d3, d1, (d1 * d1));
	} else {
		tmp = d1 * ((d2 - 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)) <= Inf)
		tmp = Float64(Float64(d1 * Float64(d4 + d2)) - fma(d3, d1, Float64(d1 * d1)));
	else
		tmp = Float64(d1 * Float64(Float64(d2 - 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], Infinity], N[(N[(d1 * N[(d4 + d2), $MachinePrecision]), $MachinePrecision] - N[(d3 * d1 + N[(d1 * d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d2 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \leq \infty:\\
\;\;\;\;d1 \cdot \left(d4 + d2\right) - \mathsf{fma}\left(d3, d1, d1 \cdot d1\right)\\

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


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

    1. Initial program 100.0%

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

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

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

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

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

        \[\leadsto \left(\left(d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      6. 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 \cdot d1 \]
      7. 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 \cdot d1 \]
      8. 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 \cdot d1 \]
      9. +-commutativeN/A

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

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

        \[\leadsto \left(\left(-1 \cdot \left(d1 \cdot d3\right) + d1 \cdot d2\right) + \color{blue}{d1 \cdot d4}\right) - d1 \cdot d1 \]
      12. 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 \cdot d1 \]
      13. lift-*.f64N/A

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

        \[\leadsto \left(-1 \cdot \left(d1 \cdot d3\right) + \left(d1 \cdot d2 + d1 \cdot d4\right)\right) - \color{blue}{{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} \]
      19. associate--r+N/A

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

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

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

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

    1. Initial program 0.0%

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

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

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

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

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

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

        \[\leadsto \left(d2 - d3\right) \cdot d1 + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d1 \]
      6. distribute-lft-neg-inN/A

        \[\leadsto \left(d2 - d3\right) \cdot d1 + \left(\mathsf{neg}\left(d1 \cdot d1\right)\right) \]
      7. pow2N/A

        \[\leadsto \left(d2 - d3\right) \cdot d1 + \left(\mathsf{neg}\left({d1}^{2}\right)\right) \]
      8. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, \color{blue}{d1}, \mathsf{neg}\left({d1}^{2}\right)\right) \]
      9. lower--.f64N/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \mathsf{neg}\left({d1}^{2}\right)\right) \]
      10. pow2N/A

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

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(\mathsf{neg}\left(d1\right)\right) \cdot d1\right) \]
      12. mul-1-negN/A

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

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(-1 \cdot d1\right) \cdot d1\right) \]
      14. mul-1-negN/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(\mathsf{neg}\left(d1\right)\right) \cdot d1\right) \]
      15. lower-neg.f6462.8

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(-d1\right) \cdot d1\right) \]
    5. Applied rewrites62.8%

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

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

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

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

        \[\leadsto \left(d2 - d3\right) \cdot d1 + \left(\mathsf{neg}\left(d1\right)\right) \cdot \color{blue}{d1} \]
      5. distribute-rgt-outN/A

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\left(d2 - d3\right) + \left(\mathsf{neg}\left(d1\right)\right)\right) \]
      11. lift-neg.f6490.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \leq \infty:\\ \;\;\;\;d1 \cdot \left(d4 + d2\right) - \mathsf{fma}\left(d3, d1, d1 \cdot d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d3\right) - d1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 86.3% accurate, 1.2× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d1 \leq -7.8 \cdot 10^{+145} \lor \neg \left(d1 \leq 1.22 \cdot 10^{+94}\right):\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (or (<= d1 -7.8e+145) (not (<= d1 1.22e+94)))
   (* (- d1) d1)
   (* (- (+ d4 d2) d3) d1)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d1 <= -7.8e+145) || !(d1 <= 1.22e+94)) {
		tmp = -d1 * d1;
	} else {
		tmp = ((d4 + d2) - d3) * d1;
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(d1, d2, d3, d4)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if ((d1 <= (-7.8d+145)) .or. (.not. (d1 <= 1.22d+94))) then
        tmp = -d1 * d1
    else
        tmp = ((d4 + d2) - d3) * d1
    end if
    code = tmp
end function
assert d1 < d2 && d2 < d3 && d3 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d1 <= -7.8e+145) || !(d1 <= 1.22e+94)) {
		tmp = -d1 * d1;
	} else {
		tmp = ((d4 + d2) - d3) * d1;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if (d1 <= -7.8e+145) or not (d1 <= 1.22e+94):
		tmp = -d1 * d1
	else:
		tmp = ((d4 + d2) - d3) * d1
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if ((d1 <= -7.8e+145) || !(d1 <= 1.22e+94))
		tmp = Float64(Float64(-d1) * d1);
	else
		tmp = Float64(Float64(Float64(d4 + d2) - d3) * d1);
	end
	return tmp
end
d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if ((d1 <= -7.8e+145) || ~((d1 <= 1.22e+94)))
		tmp = -d1 * d1;
	else
		tmp = ((d4 + d2) - d3) * d1;
	end
	tmp_2 = tmp;
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d1, -7.8e+145], N[Not[LessEqual[d1, 1.22e+94]], $MachinePrecision]], N[((-d1) * d1), $MachinePrecision], N[(N[(N[(d4 + d2), $MachinePrecision] - d3), $MachinePrecision] * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d1 \leq -7.8 \cdot 10^{+145} \lor \neg \left(d1 \leq 1.22 \cdot 10^{+94}\right):\\
\;\;\;\;\left(-d1\right) \cdot d1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d1 < -7.7999999999999995e145 or 1.22e94 < d1

    1. Initial program 54.3%

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

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
    4. Step-by-step derivation
      1. 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.f6477.8

        \[\leadsto \left(-d1\right) \cdot d1 \]
    5. Applied rewrites77.8%

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

    if -7.7999999999999995e145 < d1 < 1.22e94

    1. Initial program 100.0%

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

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

        \[\leadsto \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-+.f6490.6

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
    5. Applied rewrites90.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d1 \leq -7.8 \cdot 10^{+145} \lor \neg \left(d1 \leq 1.22 \cdot 10^{+94}\right):\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(\left(d4 + d2\right) - d3\right) \cdot d1\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 66.3% accurate, 1.4× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d1 \leq -4.2 \cdot 10^{+117} \lor \neg \left(d1 \leq 8 \cdot 10^{+93}\right):\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d2 + d4\right) \cdot d1\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (or (<= d1 -4.2e+117) (not (<= d1 8e+93)))
   (* (- d1) d1)
   (* (+ d2 d4) d1)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d1 <= -4.2e+117) || !(d1 <= 8e+93)) {
		tmp = -d1 * d1;
	} else {
		tmp = (d2 + d4) * d1;
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(d1, d2, d3, d4)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if ((d1 <= (-4.2d+117)) .or. (.not. (d1 <= 8d+93))) then
        tmp = -d1 * d1
    else
        tmp = (d2 + d4) * d1
    end if
    code = tmp
end function
assert d1 < d2 && d2 < d3 && d3 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d1 <= -4.2e+117) || !(d1 <= 8e+93)) {
		tmp = -d1 * d1;
	} else {
		tmp = (d2 + d4) * d1;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if (d1 <= -4.2e+117) or not (d1 <= 8e+93):
		tmp = -d1 * d1
	else:
		tmp = (d2 + d4) * d1
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if ((d1 <= -4.2e+117) || !(d1 <= 8e+93))
		tmp = Float64(Float64(-d1) * d1);
	else
		tmp = Float64(Float64(d2 + d4) * d1);
	end
	return tmp
end
d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if ((d1 <= -4.2e+117) || ~((d1 <= 8e+93)))
		tmp = -d1 * d1;
	else
		tmp = (d2 + d4) * d1;
	end
	tmp_2 = tmp;
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d1, -4.2e+117], N[Not[LessEqual[d1, 8e+93]], $MachinePrecision]], N[((-d1) * d1), $MachinePrecision], N[(N[(d2 + d4), $MachinePrecision] * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d1 \leq -4.2 \cdot 10^{+117} \lor \neg \left(d1 \leq 8 \cdot 10^{+93}\right):\\
\;\;\;\;\left(-d1\right) \cdot d1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d1 < -4.2000000000000002e117 or 8.00000000000000035e93 < d1

    1. Initial program 56.6%

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

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
    4. Step-by-step derivation
      1. 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.f6476.0

        \[\leadsto \left(-d1\right) \cdot d1 \]
    5. Applied rewrites76.0%

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

    if -4.2000000000000002e117 < d1 < 8.00000000000000035e93

    1. Initial program 100.0%

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

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

        \[\leadsto \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-+.f6491.5

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

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

      \[\leadsto \left(d2 + d4\right) \cdot d1 \]
    7. Step-by-step derivation
      1. lower-+.f6461.1

        \[\leadsto \left(d2 + d4\right) \cdot d1 \]
    8. Applied rewrites61.1%

      \[\leadsto \left(d2 + d4\right) \cdot d1 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification66.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d1 \leq -4.2 \cdot 10^{+117} \lor \neg \left(d1 \leq 8 \cdot 10^{+93}\right):\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d2 + d4\right) \cdot d1\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 74.1% accurate, 1.4× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -3.5 \cdot 10^{+28}:\\ \;\;\;\;\left(d2 - d3\right) \cdot d1\\ \mathbf{elif}\;d2 \leq -5.5 \cdot 10^{-29}:\\ \;\;\;\;\left(-d1\right) \cdot d1\\ \mathbf{else}:\\ \;\;\;\;\left(d4 - d3\right) \cdot d1\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d2 -3.5e+28)
   (* (- d2 d3) d1)
   (if (<= d2 -5.5e-29) (* (- d1) d1) (* (- d4 d3) d1))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -3.5e+28) {
		tmp = (d2 - d3) * d1;
	} else if (d2 <= -5.5e-29) {
		tmp = -d1 * d1;
	} else {
		tmp = (d4 - d3) * d1;
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

\mathbf{elif}\;d2 \leq -5.5 \cdot 10^{-29}:\\
\;\;\;\;\left(-d1\right) \cdot d1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d2 < -3.5e28

    1. Initial program 73.4%

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

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

        \[\leadsto \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-+.f6487.6

        \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
    5. Applied rewrites87.6%

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

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

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

      if -3.5e28 < d2 < -5.4999999999999999e-29

      1. Initial program 72.2%

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

        \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
      4. Step-by-step derivation
        1. 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.f6462.4

          \[\leadsto \left(-d1\right) \cdot d1 \]
      5. Applied rewrites62.4%

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

      if -5.4999999999999999e-29 < d2

      1. Initial program 87.9%

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

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

          \[\leadsto \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-+.f6475.5

          \[\leadsto \left(\left(d4 + d2\right) - d3\right) \cdot d1 \]
      5. Applied rewrites75.5%

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

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

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

      Alternative 5: 53.5% accurate, 1.5× speedup?

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

        1. Initial program 69.4%

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

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

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

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

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

        if -7.49999999999999922e103 < d2 < -2.1000000000000001e-170

        1. Initial program 81.7%

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

          \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
        4. Step-by-step derivation
          1. 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.f6451.4

            \[\leadsto \left(-d1\right) \cdot d1 \]
        5. Applied rewrites51.4%

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

        if -2.1000000000000001e-170 < d2

        1. Initial program 88.4%

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

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

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

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

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

      Alternative 6: 93.5% accurate, 1.7× speedup?

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

        1. Initial program 83.6%

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

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

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

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

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

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

            \[\leadsto \left(d2 - d3\right) \cdot d1 + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \cdot d1 \]
          6. distribute-lft-neg-inN/A

            \[\leadsto \left(d2 - d3\right) \cdot d1 + \left(\mathsf{neg}\left(d1 \cdot d1\right)\right) \]
          7. pow2N/A

            \[\leadsto \left(d2 - d3\right) \cdot d1 + \left(\mathsf{neg}\left({d1}^{2}\right)\right) \]
          8. lower-fma.f64N/A

            \[\leadsto \mathsf{fma}\left(d2 - d3, \color{blue}{d1}, \mathsf{neg}\left({d1}^{2}\right)\right) \]
          9. lower--.f64N/A

            \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \mathsf{neg}\left({d1}^{2}\right)\right) \]
          10. pow2N/A

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

            \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(\mathsf{neg}\left(d1\right)\right) \cdot d1\right) \]
          12. mul-1-negN/A

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

            \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(-1 \cdot d1\right) \cdot d1\right) \]
          14. mul-1-negN/A

            \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(\mathsf{neg}\left(d1\right)\right) \cdot d1\right) \]
          15. lower-neg.f6480.9

            \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \left(-d1\right) \cdot d1\right) \]
        5. Applied rewrites80.9%

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

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

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

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

            \[\leadsto \left(d2 - d3\right) \cdot d1 + \left(\mathsf{neg}\left(d1\right)\right) \cdot \color{blue}{d1} \]
          5. distribute-rgt-outN/A

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

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

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

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

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

            \[\leadsto d1 \cdot \left(\left(d2 - d3\right) + \left(\mathsf{neg}\left(d1\right)\right)\right) \]
          11. lift-neg.f6487.1

            \[\leadsto d1 \cdot \left(\left(d2 - d3\right) + \left(-d1\right)\right) \]
        7. Applied rewrites87.1%

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

        if 4.99999999999999991e76 < d4

        1. Initial program 81.3%

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

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

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

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

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

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

      Alternative 7: 72.9% accurate, 2.0× speedup?

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

        1. Initial program 83.7%

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

          \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
        4. 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-+.f6473.4

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

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

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

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

          if 8.7999999999999996e79 < d4

          1. Initial program 80.9%

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

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

              \[\leadsto \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.6

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

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

            \[\leadsto \left(d2 + d4\right) \cdot d1 \]
          7. Step-by-step derivation
            1. lower-+.f6474.0

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

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

        Alternative 8: 50.8% accurate, 2.5× speedup?

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

          1. Initial program 74.1%

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

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

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

              \[\leadsto d2 \cdot \color{blue}{d1} \]
          5. Applied rewrites51.9%

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

          if -6.49999999999999988e-32 < d2

          1. Initial program 87.7%

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

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

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

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

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

        Alternative 9: 32.2% accurate, 5.0× speedup?

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

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

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

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

            \[\leadsto d2 \cdot \color{blue}{d1} \]
        5. Applied rewrites30.9%

          \[\leadsto \color{blue}{d2 \cdot d1} \]
        6. Add Preprocessing

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

        \[\begin{array}{l} \\ d1 \cdot \left(\left(\left(d2 - d3\right) + d4\right) - d1\right) \end{array} \]
        (FPCore (d1 d2 d3 d4) :precision binary64 (* d1 (- (+ (- d2 d3) d4) d1)))
        double code(double d1, double d2, double d3, double d4) {
        	return d1 * (((d2 - d3) + d4) - d1);
        }
        
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(d1, d2, d3, d4)
        use fmin_fmax_functions
            real(8), intent (in) :: d1
            real(8), intent (in) :: d2
            real(8), intent (in) :: d3
            real(8), intent (in) :: d4
            code = d1 * (((d2 - d3) + d4) - d1)
        end function
        
        public static double code(double d1, double d2, double d3, double d4) {
        	return d1 * (((d2 - d3) + d4) - d1);
        }
        
        def code(d1, d2, d3, d4):
        	return d1 * (((d2 - d3) + d4) - d1)
        
        function code(d1, d2, d3, d4)
        	return Float64(d1 * Float64(Float64(Float64(d2 - d3) + d4) - d1))
        end
        
        function tmp = code(d1, d2, d3, d4)
        	tmp = d1 * (((d2 - d3) + d4) - d1);
        end
        
        code[d1_, d2_, d3_, d4_] := N[(d1 * N[(N[(N[(d2 - d3), $MachinePrecision] + d4), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        d1 \cdot \left(\left(\left(d2 - d3\right) + d4\right) - d1\right)
        \end{array}
        

        Reproduce

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