FastMath dist4

Percentage Accurate: 88.0% → 98.3%
Time: 37.2s
Alternatives: 14
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);
}
real(8) function code(d1, d2, d3, d4)
    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 14 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.0% 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);
}
real(8) function code(d1, d2, d3, d4)
    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.3% accurate, 0.6× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;\left(\left(d2 \cdot d1 - d1 \cdot d3\right) + d1 \cdot d4\right) - d1 \cdot d1 \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(d1, d4 - d1, d1 \cdot \left(d2 - d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\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 d1) (* d1 d3)) (* d1 d4)) (* d1 d1)) INFINITY)
   (fma d1 (- d4 d1) (* d1 (- d2 d3)))
   (* d1 (- (- d2 d1) d3))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (((((d2 * d1) - (d1 * d3)) + (d1 * d4)) - (d1 * d1)) <= ((double) INFINITY)) {
		tmp = fma(d1, (d4 - d1), (d1 * (d2 - d3)));
	} else {
		tmp = d1 * ((d2 - d1) - d3);
	}
	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(d2 * d1) - Float64(d1 * d3)) + Float64(d1 * d4)) - Float64(d1 * d1)) <= Inf)
		tmp = fma(d1, Float64(d4 - d1), Float64(d1 * Float64(d2 - d3)));
	else
		tmp = Float64(d1 * Float64(Float64(d2 - d1) - d3));
	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[(d2 * d1), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d1 * d4), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision], Infinity], N[(d1 * N[(d4 - d1), $MachinePrecision] + N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d2 - d1), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(\left(d2 \cdot d1 - d1 \cdot d3\right) + d1 \cdot d4\right) - d1 \cdot d1 \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(d1, d4 - d1, d1 \cdot \left(d2 - d3\right)\right)\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\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. associate--l+N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
      12. --lowering--.f6489.7

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

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

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

Alternative 2: 76.7% accurate, 1.0× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq -7.4 \cdot 10^{-156}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d4 \leq 1.6 \cdot 10^{-239}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 3.4 \cdot 10^{+49}:\\ \;\;\;\;d1 \cdot \left(\left(-d1\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\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 (<= d4 -7.4e-156)
   (* d1 (- d2 d1))
   (if (<= d4 1.6e-239)
     (* d1 (- d2 d3))
     (if (<= d4 3.4e+49) (* d1 (- (- d1) d3)) (* d1 (- d4 d3))))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= -7.4e-156) {
		tmp = d1 * (d2 - d1);
	} else if (d4 <= 1.6e-239) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 3.4e+49) {
		tmp = d1 * (-d1 - d3);
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3, d4)
    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 <= (-7.4d-156)) then
        tmp = d1 * (d2 - d1)
    else if (d4 <= 1.6d-239) then
        tmp = d1 * (d2 - d3)
    else if (d4 <= 3.4d+49) then
        tmp = d1 * (-d1 - d3)
    else
        tmp = d1 * (d4 - d3)
    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 <= -7.4e-156) {
		tmp = d1 * (d2 - d1);
	} else if (d4 <= 1.6e-239) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 3.4e+49) {
		tmp = d1 * (-d1 - d3);
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= -7.4e-156:
		tmp = d1 * (d2 - d1)
	elif d4 <= 1.6e-239:
		tmp = d1 * (d2 - d3)
	elif d4 <= 3.4e+49:
		tmp = d1 * (-d1 - d3)
	else:
		tmp = d1 * (d4 - d3)
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= -7.4e-156)
		tmp = Float64(d1 * Float64(d2 - d1));
	elseif (d4 <= 1.6e-239)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d4 <= 3.4e+49)
		tmp = Float64(d1 * Float64(Float64(-d1) - d3));
	else
		tmp = Float64(d1 * Float64(d4 - d3));
	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 <= -7.4e-156)
		tmp = d1 * (d2 - d1);
	elseif (d4 <= 1.6e-239)
		tmp = d1 * (d2 - d3);
	elseif (d4 <= 3.4e+49)
		tmp = d1 * (-d1 - d3);
	else
		tmp = d1 * (d4 - d3);
	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, -7.4e-156], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 1.6e-239], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 3.4e+49], N[(d1 * N[((-d1) - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq -7.4 \cdot 10^{-156}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

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

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

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


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

    1. Initial program 77.9%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
      12. --lowering--.f6471.3

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
      2. --lowering--.f6451.1

        \[\leadsto d1 \cdot \color{blue}{\left(d2 - d1\right)} \]
    8. Simplified51.1%

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

    if -7.4000000000000001e-156 < d4 < 1.6e-239

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 - d3\right)\right)} \]
      4. --lowering--.f6472.8

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

    if 1.6e-239 < d4 < 3.4000000000000001e49

    1. Initial program 88.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 0

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

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

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d4 - d3\right) - d1\right)} \]
      7. --lowering--.f6477.4

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

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

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

        \[\leadsto d1 \cdot \left(\color{blue}{\left(\mathsf{neg}\left(d3\right)\right)} - d1\right) \]
      2. neg-lowering-neg.f6471.8

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

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

    if 3.4000000000000001e49 < d4

    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 d1 around 0

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 - d3\right)\right)} \]
      4. --lowering--.f6494.7

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

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right)} \]
      2. --lowering--.f6484.6

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

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

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

Alternative 3: 73.4% accurate, 1.1× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d1\right)\\ \mathbf{if}\;d4 \leq -6.8 \cdot 10^{-156}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d4 \leq 1.65 \cdot 10^{-248}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 2.8 \cdot 10^{+59}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d1))))
   (if (<= d4 -6.8e-156)
     t_0
     (if (<= d4 1.65e-248)
       (* d1 (- d2 d3))
       (if (<= d4 2.8e+59) t_0 (* d1 (- d4 d3)))))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d1);
	double tmp;
	if (d4 <= -6.8e-156) {
		tmp = t_0;
	} else if (d4 <= 1.65e-248) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 2.8e+59) {
		tmp = t_0;
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3, d4)
    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 * (d2 - d1)
    if (d4 <= (-6.8d-156)) then
        tmp = t_0
    else if (d4 <= 1.65d-248) then
        tmp = d1 * (d2 - d3)
    else if (d4 <= 2.8d+59) then
        tmp = t_0
    else
        tmp = d1 * (d4 - d3)
    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 * (d2 - d1);
	double tmp;
	if (d4 <= -6.8e-156) {
		tmp = t_0;
	} else if (d4 <= 1.65e-248) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 2.8e+59) {
		tmp = t_0;
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d1)
	tmp = 0
	if d4 <= -6.8e-156:
		tmp = t_0
	elif d4 <= 1.65e-248:
		tmp = d1 * (d2 - d3)
	elif d4 <= 2.8e+59:
		tmp = t_0
	else:
		tmp = d1 * (d4 - d3)
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d1))
	tmp = 0.0
	if (d4 <= -6.8e-156)
		tmp = t_0;
	elseif (d4 <= 1.65e-248)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d4 <= 2.8e+59)
		tmp = t_0;
	else
		tmp = Float64(d1 * Float64(d4 - d3));
	end
	return tmp
end
d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d1);
	tmp = 0.0;
	if (d4 <= -6.8e-156)
		tmp = t_0;
	elseif (d4 <= 1.65e-248)
		tmp = d1 * (d2 - d3);
	elseif (d4 <= 2.8e+59)
		tmp = t_0;
	else
		tmp = d1 * (d4 - d3);
	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[(d2 - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d4, -6.8e-156], t$95$0, If[LessEqual[d4, 1.65e-248], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 2.8e+59], t$95$0, N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d1\right)\\
\mathbf{if}\;d4 \leq -6.8 \cdot 10^{-156}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;d4 \leq 2.8 \cdot 10^{+59}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < -6.79999999999999981e-156 or 1.6500000000000001e-248 < d4 < 2.7999999999999998e59

    1. Initial program 82.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. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
      12. --lowering--.f6479.4

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
      2. --lowering--.f6458.3

        \[\leadsto d1 \cdot \color{blue}{\left(d2 - d1\right)} \]
    8. Simplified58.3%

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

    if -6.79999999999999981e-156 < d4 < 1.6500000000000001e-248

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 - d3\right)\right)} \]
      4. --lowering--.f6471.7

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
      2. --lowering--.f6471.7

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

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

    if 2.7999999999999998e59 < d4

    1. Initial program 87.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 0

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d3\right)} \]
      2. --lowering--.f6485.8

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

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

Alternative 4: 71.1% accurate, 1.1× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d1 \leq -8 \cdot 10^{+109}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d1 \leq 2 \cdot 10^{-133}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d1 \leq 3.8 \cdot 10^{+106}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \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 (<= d1 -8e+109)
   (* d1 (- d2 d1))
   (if (<= d1 2e-133)
     (* d1 (- d2 d3))
     (if (<= d1 3.8e+106) (* d1 (+ d2 d4)) (* d1 (- d4 d1))))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d1 <= -8e+109) {
		tmp = d1 * (d2 - d1);
	} else if (d1 <= 2e-133) {
		tmp = d1 * (d2 - d3);
	} else if (d1 <= 3.8e+106) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = d1 * (d4 - d1);
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3, d4)
    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 <= (-8d+109)) then
        tmp = d1 * (d2 - d1)
    else if (d1 <= 2d-133) then
        tmp = d1 * (d2 - d3)
    else if (d1 <= 3.8d+106) then
        tmp = d1 * (d2 + d4)
    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 (d1 <= -8e+109) {
		tmp = d1 * (d2 - d1);
	} else if (d1 <= 2e-133) {
		tmp = d1 * (d2 - d3);
	} else if (d1 <= 3.8e+106) {
		tmp = d1 * (d2 + d4);
	} 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 d1 <= -8e+109:
		tmp = d1 * (d2 - d1)
	elif d1 <= 2e-133:
		tmp = d1 * (d2 - d3)
	elif d1 <= 3.8e+106:
		tmp = d1 * (d2 + d4)
	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 (d1 <= -8e+109)
		tmp = Float64(d1 * Float64(d2 - d1));
	elseif (d1 <= 2e-133)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d1 <= 3.8e+106)
		tmp = Float64(d1 * Float64(d2 + d4));
	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 (d1 <= -8e+109)
		tmp = d1 * (d2 - d1);
	elseif (d1 <= 2e-133)
		tmp = d1 * (d2 - d3);
	elseif (d1 <= 3.8e+106)
		tmp = d1 * (d2 + d4);
	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[d1, -8e+109], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d1, 2e-133], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d1, 3.8e+106], N[(d1 * N[(d2 + d4), $MachinePrecision]), $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}\;d1 \leq -8 \cdot 10^{+109}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

\mathbf{elif}\;d1 \leq 2 \cdot 10^{-133}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

\mathbf{elif}\;d1 \leq 3.8 \cdot 10^{+106}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d1 < -7.99999999999999985e109

    1. Initial program 54.9%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
      12. --lowering--.f6494.3

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
      2. --lowering--.f6488.7

        \[\leadsto d1 \cdot \color{blue}{\left(d2 - d1\right)} \]
    8. Simplified88.7%

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

    if -7.99999999999999985e109 < d1 < 2.0000000000000001e-133

    1. Initial program 99.1%

      \[\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. *-lowering-*.f64N/A

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 - d3\right)\right)} \]
      4. --lowering--.f6493.9

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
      2. --lowering--.f6464.0

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

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

    if 2.0000000000000001e-133 < d1 < 3.7999999999999998e106

    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. *-lowering-*.f64N/A

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

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

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

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
      3. +-lowering-+.f6474.0

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    8. Simplified74.0%

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

    if 3.7999999999999998e106 < d1

    1. Initial program 68.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 0

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

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

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d4 - d3\right) - d1\right)} \]
      7. --lowering--.f64100.0

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

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

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

        \[\leadsto d1 \cdot \left(\color{blue}{d4} - d1\right) \]
    8. Recombined 4 regimes into one program.
    9. Final simplification74.8%

      \[\leadsto \begin{array}{l} \mathbf{if}\;d1 \leq -8 \cdot 10^{+109}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d1 \leq 2 \cdot 10^{-133}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d1 \leq 3.8 \cdot 10^{+106}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \end{array} \]
    10. Add Preprocessing

    Alternative 5: 69.8% accurate, 1.1× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d1\right)\\ \mathbf{if}\;d1 \leq -1.85 \cdot 10^{+118}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d1 \leq 1.65 \cdot 10^{-133}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d1 \leq 7 \cdot 10^{+102}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \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 (- d2 d1))))
       (if (<= d1 -1.85e+118)
         t_0
         (if (<= d1 1.65e-133)
           (* d1 (- d2 d3))
           (if (<= d1 7e+102) (* d1 (+ d2 d4)) t_0)))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double t_0 = d1 * (d2 - d1);
    	double tmp;
    	if (d1 <= -1.85e+118) {
    		tmp = t_0;
    	} else if (d1 <= 1.65e-133) {
    		tmp = d1 * (d2 - d3);
    	} else if (d1 <= 7e+102) {
    		tmp = d1 * (d2 + d4);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    real(8) function code(d1, d2, d3, d4)
        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 * (d2 - d1)
        if (d1 <= (-1.85d+118)) then
            tmp = t_0
        else if (d1 <= 1.65d-133) then
            tmp = d1 * (d2 - d3)
        else if (d1 <= 7d+102) then
            tmp = d1 * (d2 + d4)
        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 * (d2 - d1);
    	double tmp;
    	if (d1 <= -1.85e+118) {
    		tmp = t_0;
    	} else if (d1 <= 1.65e-133) {
    		tmp = d1 * (d2 - d3);
    	} else if (d1 <= 7e+102) {
    		tmp = d1 * (d2 + d4);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	t_0 = d1 * (d2 - d1)
    	tmp = 0
    	if d1 <= -1.85e+118:
    		tmp = t_0
    	elif d1 <= 1.65e-133:
    		tmp = d1 * (d2 - d3)
    	elif d1 <= 7e+102:
    		tmp = d1 * (d2 + d4)
    	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(d2 - d1))
    	tmp = 0.0
    	if (d1 <= -1.85e+118)
    		tmp = t_0;
    	elseif (d1 <= 1.65e-133)
    		tmp = Float64(d1 * Float64(d2 - d3));
    	elseif (d1 <= 7e+102)
    		tmp = Float64(d1 * Float64(d2 + d4));
    	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 * (d2 - d1);
    	tmp = 0.0;
    	if (d1 <= -1.85e+118)
    		tmp = t_0;
    	elseif (d1 <= 1.65e-133)
    		tmp = d1 * (d2 - d3);
    	elseif (d1 <= 7e+102)
    		tmp = d1 * (d2 + d4);
    	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[(d2 - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d1, -1.85e+118], t$95$0, If[LessEqual[d1, 1.65e-133], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d1, 7e+102], N[(d1 * N[(d2 + d4), $MachinePrecision]), $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(d2 - d1\right)\\
    \mathbf{if}\;d1 \leq -1.85 \cdot 10^{+118}:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;d1 \leq 1.65 \cdot 10^{-133}:\\
    \;\;\;\;d1 \cdot \left(d2 - d3\right)\\
    
    \mathbf{elif}\;d1 \leq 7 \cdot 10^{+102}:\\
    \;\;\;\;d1 \cdot \left(d2 + d4\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_0\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if d1 < -1.84999999999999993e118 or 7.00000000000000021e102 < d1

      1. Initial program 62.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. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
        12. --lowering--.f6493.1

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
      7. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
        2. --lowering--.f6483.6

          \[\leadsto d1 \cdot \color{blue}{\left(d2 - d1\right)} \]
      8. Simplified83.6%

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

      if -1.84999999999999993e118 < d1 < 1.65000000000000005e-133

      1. Initial program 99.1%

        \[\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. *-lowering-*.f64N/A

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 - d3\right)\right)} \]
        4. --lowering--.f6493.9

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
      7. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
        2. --lowering--.f6464.0

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

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

      if 1.65000000000000005e-133 < d1 < 7.00000000000000021e102

      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. *-lowering-*.f64N/A

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 - d3\right)\right)} \]
        4. --lowering--.f6497.3

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
        3. +-lowering-+.f6475.0

          \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
      8. Simplified75.0%

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

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

    Alternative 6: 90.2% accurate, 1.2× speedup?

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

      1. Initial program 51.2%

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
        12. --lowering--.f6495.3

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
      7. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
        2. --lowering--.f6493.0

          \[\leadsto d1 \cdot \color{blue}{\left(d2 - d1\right)} \]
      8. Simplified93.0%

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

      if -1.45000000000000003e141 < d1 < 2.7000000000000001e107

      1. Initial program 98.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 0

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

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

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

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

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

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

      if 2.7000000000000001e107 < d1

      1. Initial program 68.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 0

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

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

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(\left(d4 - d3\right) - d1\right)} \]
        7. --lowering--.f64100.0

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

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

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

          \[\leadsto d1 \cdot \left(\color{blue}{\left(\mathsf{neg}\left(d3\right)\right)} - d1\right) \]
        2. neg-lowering-neg.f6491.5

          \[\leadsto d1 \cdot \left(\color{blue}{\left(-d3\right)} - d1\right) \]
      8. Simplified91.5%

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

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

    Alternative 7: 66.5% accurate, 1.4× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(-d1\right)\\ \mathbf{if}\;d1 \leq -5.2 \cdot 10^{+139}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d1 \leq 9.6 \cdot 10^{+106}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \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 (- d1))))
       (if (<= d1 -5.2e+139) t_0 (if (<= d1 9.6e+106) (* d1 (+ d2 d4)) t_0))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double t_0 = d1 * -d1;
    	double tmp;
    	if (d1 <= -5.2e+139) {
    		tmp = t_0;
    	} else if (d1 <= 9.6e+106) {
    		tmp = d1 * (d2 + d4);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    real(8) function code(d1, d2, d3, d4)
        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 * -d1
        if (d1 <= (-5.2d+139)) then
            tmp = t_0
        else if (d1 <= 9.6d+106) then
            tmp = d1 * (d2 + d4)
        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 * -d1;
    	double tmp;
    	if (d1 <= -5.2e+139) {
    		tmp = t_0;
    	} else if (d1 <= 9.6e+106) {
    		tmp = d1 * (d2 + d4);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	t_0 = d1 * -d1
    	tmp = 0
    	if d1 <= -5.2e+139:
    		tmp = t_0
    	elif d1 <= 9.6e+106:
    		tmp = d1 * (d2 + d4)
    	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(-d1))
    	tmp = 0.0
    	if (d1 <= -5.2e+139)
    		tmp = t_0;
    	elseif (d1 <= 9.6e+106)
    		tmp = Float64(d1 * Float64(d2 + d4));
    	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 * -d1;
    	tmp = 0.0;
    	if (d1 <= -5.2e+139)
    		tmp = t_0;
    	elseif (d1 <= 9.6e+106)
    		tmp = d1 * (d2 + d4);
    	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 * (-d1)), $MachinePrecision]}, If[LessEqual[d1, -5.2e+139], t$95$0, If[LessEqual[d1, 9.6e+106], N[(d1 * N[(d2 + d4), $MachinePrecision]), $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(-d1\right)\\
    \mathbf{if}\;d1 \leq -5.2 \cdot 10^{+139}:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;d1 \leq 9.6 \cdot 10^{+106}:\\
    \;\;\;\;d1 \cdot \left(d2 + d4\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_0\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if d1 < -5.20000000000000044e139 or 9.6000000000000002e106 < d1

      1. Initial program 60.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 inf

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

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \]
        6. neg-lowering-neg.f6483.6

          \[\leadsto d1 \cdot \color{blue}{\left(-d1\right)} \]
      5. Simplified83.6%

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

      if -5.20000000000000044e139 < d1 < 9.6000000000000002e106

      1. Initial program 98.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 0

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

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
        3. +-lowering-+.f6463.2

          \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
      8. Simplified63.2%

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

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

    Alternative 8: 53.7% accurate, 1.5× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 3.6 \cdot 10^{-261}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d4 \leq 1.3 \cdot 10^{+50}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \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 3.6e-261) (* d2 d1) (if (<= d4 1.3e+50) (* d1 (- d1)) (* d1 d4))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double tmp;
    	if (d4 <= 3.6e-261) {
    		tmp = d2 * d1;
    	} else if (d4 <= 1.3e+50) {
    		tmp = d1 * -d1;
    	} else {
    		tmp = d1 * d4;
    	}
    	return tmp;
    }
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    real(8) function code(d1, d2, d3, d4)
        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 <= 3.6d-261) then
            tmp = d2 * d1
        else if (d4 <= 1.3d+50) then
            tmp = d1 * -d1
        else
            tmp = d1 * d4
        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 <= 3.6e-261) {
    		tmp = d2 * d1;
    	} else if (d4 <= 1.3e+50) {
    		tmp = d1 * -d1;
    	} else {
    		tmp = d1 * d4;
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	tmp = 0
    	if d4 <= 3.6e-261:
    		tmp = d2 * d1
    	elif d4 <= 1.3e+50:
    		tmp = d1 * -d1
    	else:
    		tmp = d1 * d4
    	return tmp
    
    d1, d2, d3, d4 = sort([d1, d2, d3, d4])
    function code(d1, d2, d3, d4)
    	tmp = 0.0
    	if (d4 <= 3.6e-261)
    		tmp = Float64(d2 * d1);
    	elseif (d4 <= 1.3e+50)
    		tmp = Float64(d1 * Float64(-d1));
    	else
    		tmp = Float64(d1 * d4);
    	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 <= 3.6e-261)
    		tmp = d2 * d1;
    	elseif (d4 <= 1.3e+50)
    		tmp = d1 * -d1;
    	else
    		tmp = d1 * d4;
    	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, 3.6e-261], N[(d2 * d1), $MachinePrecision], If[LessEqual[d4, 1.3e+50], N[(d1 * (-d1)), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]
    
    \begin{array}{l}
    [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;d4 \leq 3.6 \cdot 10^{-261}:\\
    \;\;\;\;d2 \cdot d1\\
    
    \mathbf{elif}\;d4 \leq 1.3 \cdot 10^{+50}:\\
    \;\;\;\;d1 \cdot \left(-d1\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;d1 \cdot d4\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if d4 < 3.59999999999999999e-261

      1. Initial program 81.8%

        \[\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. *-lowering-*.f6427.0

          \[\leadsto \color{blue}{d1 \cdot d2} \]
      5. Simplified27.0%

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

      if 3.59999999999999999e-261 < d4 < 1.3000000000000001e50

      1. Initial program 89.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. unpow2N/A

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)} \]
        6. neg-lowering-neg.f6450.5

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

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

      if 1.3000000000000001e50 < d4

      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. *-lowering-*.f6465.5

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

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

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

    Alternative 9: 94.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 9.5 \cdot 10^{+58}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\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 (<= d4 9.5e+58) (* d1 (- (- d2 d1) d3)) (* d1 (- (- d4 d3) d1))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double tmp;
    	if (d4 <= 9.5e+58) {
    		tmp = d1 * ((d2 - d1) - d3);
    	} else {
    		tmp = d1 * ((d4 - d3) - d1);
    	}
    	return tmp;
    }
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    real(8) function code(d1, d2, d3, d4)
        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 <= 9.5d+58) then
            tmp = d1 * ((d2 - d1) - d3)
        else
            tmp = d1 * ((d4 - d3) - d1)
        end if
        code = tmp
    end function
    
    assert d1 < d2 && d2 < d3 && d3 < d4;
    public static double code(double d1, double d2, double d3, double d4) {
    	double tmp;
    	if (d4 <= 9.5e+58) {
    		tmp = d1 * ((d2 - d1) - d3);
    	} else {
    		tmp = d1 * ((d4 - d3) - d1);
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	tmp = 0
    	if d4 <= 9.5e+58:
    		tmp = d1 * ((d2 - d1) - d3)
    	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 (d4 <= 9.5e+58)
    		tmp = Float64(d1 * Float64(Float64(d2 - d1) - d3));
    	else
    		tmp = Float64(d1 * Float64(Float64(d4 - d3) - d1));
    	end
    	return tmp
    end
    
    d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
    function tmp_2 = code(d1, d2, d3, d4)
    	tmp = 0.0;
    	if (d4 <= 9.5e+58)
    		tmp = d1 * ((d2 - d1) - d3);
    	else
    		tmp = d1 * ((d4 - d3) - d1);
    	end
    	tmp_2 = tmp;
    end
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 9.5e+58], N[(d1 * N[(N[(d2 - d1), $MachinePrecision] - d3), $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}\;d4 \leq 9.5 \cdot 10^{+58}:\\
    \;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\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 d4 < 9.5000000000000002e58

      1. Initial program 84.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      if 9.5000000000000002e58 < d4

      1. Initial program 87.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 0

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

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

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(\left(d4 - d3\right) - d1\right)} \]
        7. --lowering--.f6492.9

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

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

    Alternative 10: 93.8% 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^{+23}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d3\right)\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 (<= d4 5e+23) (* d1 (- (- d2 d1) d3)) (* d1 (+ d2 (- d4 d3)))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double tmp;
    	if (d4 <= 5e+23) {
    		tmp = d1 * ((d2 - d1) - d3);
    	} else {
    		tmp = d1 * (d2 + (d4 - d3));
    	}
    	return tmp;
    }
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    real(8) function code(d1, d2, d3, d4)
        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+23) then
            tmp = d1 * ((d2 - d1) - d3)
        else
            tmp = d1 * (d2 + (d4 - d3))
        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+23) {
    		tmp = d1 * ((d2 - d1) - d3);
    	} else {
    		tmp = d1 * (d2 + (d4 - d3));
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	tmp = 0
    	if d4 <= 5e+23:
    		tmp = d1 * ((d2 - d1) - d3)
    	else:
    		tmp = d1 * (d2 + (d4 - d3))
    	return tmp
    
    d1, d2, d3, d4 = sort([d1, d2, d3, d4])
    function code(d1, d2, d3, d4)
    	tmp = 0.0
    	if (d4 <= 5e+23)
    		tmp = Float64(d1 * Float64(Float64(d2 - d1) - d3));
    	else
    		tmp = Float64(d1 * Float64(d2 + Float64(d4 - d3)));
    	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+23)
    		tmp = d1 * ((d2 - d1) - d3);
    	else
    		tmp = d1 * (d2 + (d4 - d3));
    	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+23], N[(d1 * N[(N[(d2 - d1), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d2 + N[(d4 - d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;d4 \leq 5 \cdot 10^{+23}:\\
    \;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d3\right)\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if d4 < 4.9999999999999999e23

      1. Initial program 83.5%

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
        12. --lowering--.f6485.0

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

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

      if 4.9999999999999999e23 < d4

      1. Initial program 88.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. *-lowering-*.f64N/A

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

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

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

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

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

    Alternative 11: 98.4% accurate, 1.7× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \mathsf{fma}\left(d2, d1, d1 \cdot \left(\left(d4 - d1\right) - d3\right)\right) \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 (fma d2 d1 (* d1 (- (- d4 d1) d3))))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	return fma(d2, d1, (d1 * ((d4 - d1) - d3)));
    }
    
    d1, d2, d3, d4 = sort([d1, d2, d3, d4])
    function code(d1, d2, d3, d4)
    	return fma(d2, d1, Float64(d1 * Float64(Float64(d4 - d1) - d3)))
    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 + N[(d1 * N[(N[(d4 - d1), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
    \\
    \mathsf{fma}\left(d2, d1, d1 \cdot \left(\left(d4 - d1\right) - d3\right)\right)
    \end{array}
    
    Derivation
    1. Initial program 84.7%

      \[\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. associate--l+N/A

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\color{blue}{\left(\mathsf{neg}\left(d3\right)\right)} + \left(d4 - d1\right)\right)\right) \]
      12. --lowering--.f6499.2

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(d2, d1, d1 \cdot \left(\left(-d3\right) + \left(d4 - d1\right)\right)\right)} \]
    5. Final simplification99.2%

      \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\left(d4 - d1\right) - d3\right)\right) \]
    6. Add Preprocessing

    Alternative 12: 70.8% accurate, 2.0× speedup?

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

      1. Initial program 83.5%

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \left(\color{blue}{\left(d2 - d1\right)} - d3\right) \]
        12. --lowering--.f6485.0

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
      7. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \color{blue}{d1 \cdot \left(d2 - d1\right)} \]
        2. --lowering--.f6460.0

          \[\leadsto d1 \cdot \color{blue}{\left(d2 - d1\right)} \]
      8. Simplified60.0%

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

      if 2.8e22 < d4

      1. Initial program 88.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. *-lowering-*.f64N/A

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

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

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

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

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

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

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

          \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
        3. +-lowering-+.f6477.8

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

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

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

    Alternative 13: 50.2% accurate, 2.5× speedup?

    \[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 3.2 \cdot 10^{+59}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \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 3.2e+59) (* d2 d1) (* d1 d4)))
    assert(d1 < d2 && d2 < d3 && d3 < d4);
    double code(double d1, double d2, double d3, double d4) {
    	double tmp;
    	if (d4 <= 3.2e+59) {
    		tmp = d2 * d1;
    	} else {
    		tmp = d1 * d4;
    	}
    	return tmp;
    }
    
    NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
    real(8) function code(d1, d2, d3, d4)
        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 <= 3.2d+59) then
            tmp = d2 * d1
        else
            tmp = d1 * d4
        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 <= 3.2e+59) {
    		tmp = d2 * d1;
    	} else {
    		tmp = d1 * d4;
    	}
    	return tmp;
    }
    
    [d1, d2, d3, d4] = sort([d1, d2, d3, d4])
    def code(d1, d2, d3, d4):
    	tmp = 0
    	if d4 <= 3.2e+59:
    		tmp = d2 * d1
    	else:
    		tmp = d1 * d4
    	return tmp
    
    d1, d2, d3, d4 = sort([d1, d2, d3, d4])
    function code(d1, d2, d3, d4)
    	tmp = 0.0
    	if (d4 <= 3.2e+59)
    		tmp = Float64(d2 * d1);
    	else
    		tmp = Float64(d1 * d4);
    	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 <= 3.2e+59)
    		tmp = d2 * d1;
    	else
    		tmp = d1 * d4;
    	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, 3.2e+59], N[(d2 * d1), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]
    
    \begin{array}{l}
    [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;d4 \leq 3.2 \cdot 10^{+59}:\\
    \;\;\;\;d2 \cdot d1\\
    
    \mathbf{else}:\\
    \;\;\;\;d1 \cdot d4\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if d4 < 3.19999999999999982e59

      1. Initial program 84.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. *-lowering-*.f6428.3

          \[\leadsto \color{blue}{d1 \cdot d2} \]
      5. Simplified28.3%

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

      if 3.19999999999999982e59 < d4

      1. Initial program 87.2%

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

        \[\leadsto \color{blue}{d1 \cdot d4} \]
      4. Step-by-step derivation
        1. *-lowering-*.f6466.0

          \[\leadsto \color{blue}{d1 \cdot d4} \]
      5. Simplified66.0%

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

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

    Alternative 14: 30.9% 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.
    real(8) function code(d1, d2, d3, d4)
        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 84.7%

      \[\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. *-lowering-*.f6426.3

        \[\leadsto \color{blue}{d1 \cdot d2} \]
    5. Simplified26.3%

      \[\leadsto \color{blue}{d1 \cdot d2} \]
    6. Final simplification26.3%

      \[\leadsto d2 \cdot d1 \]
    7. 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);
    }
    
    real(8) function code(d1, d2, d3, d4)
        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 2024204 
    (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)))