FastMath dist4

Percentage Accurate: 87.5% → 98.4%
Time: 7.5s
Alternatives: 8
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 8 alternatives:

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

Initial Program: 87.5% 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.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 88.3%

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(d2, d1, \left(\mathsf{neg}\left(\color{blue}{d1 \cdot d3}\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
    14. 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) \]
    15. lift-*.f64N/A

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

      \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right)\right) \]
    17. 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) \]
    18. 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) \]
    19. lower-*.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) \]
    20. lower-+.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) \]
  4. Applied rewrites98.8%

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

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

Alternative 2: 98.4% 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(d4 - d3\right) - d1\right)\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= (- (+ (- (* d2 d1) (* d1 d3)) (* d1 d4)) (* d1 d1)) INFINITY)
   (fma d1 (- d4 d1) (* d1 (- d2 d3)))
   (* d1 (- (- d4 d3) d1))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (((((d2 * d1) - (d1 * d3)) + (d1 * d4)) - (d1 * d1)) <= ((double) INFINITY)) {
		tmp = fma(d1, (d4 - d1), (d1 * (d2 - 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 (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(d4 - d3) - d1));
	end
	return tmp
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[N[(N[(N[(N[(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[(d4 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(\left(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(d4 - d3\right) - d1\right)\\


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

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(d1, d4 - d1, d1 \cdot \color{blue}{\left(d2 - d3\right)}\right) \]
    4. Applied rewrites100.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 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. lower-*.f64N/A

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

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

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

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

    \[\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(d4 - d3\right) - d1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 52.9% accurate, 1.2× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -4 \cdot 10^{+126}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d2 \leq -2 \cdot 10^{+90}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d2 \leq -1 \cdot 10^{-278}:\\ \;\;\;\;-d1 \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 (<= d2 -4e+126)
   (* d2 d1)
   (if (<= d2 -2e+90)
     (* d1 (- d3))
     (if (<= d2 -1e-278) (- (* d1 d1)) (* d1 d4)))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -4e+126) {
		tmp = d2 * d1;
	} else if (d2 <= -2e+90) {
		tmp = d1 * -d3;
	} else if (d2 <= -1e-278) {
		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 (d2 <= (-4d+126)) then
        tmp = d2 * d1
    else if (d2 <= (-2d+90)) then
        tmp = d1 * -d3
    else if (d2 <= (-1d-278)) 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 (d2 <= -4e+126) {
		tmp = d2 * d1;
	} else if (d2 <= -2e+90) {
		tmp = d1 * -d3;
	} else if (d2 <= -1e-278) {
		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 d2 <= -4e+126:
		tmp = d2 * d1
	elif d2 <= -2e+90:
		tmp = d1 * -d3
	elif d2 <= -1e-278:
		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 (d2 <= -4e+126)
		tmp = Float64(d2 * d1);
	elseif (d2 <= -2e+90)
		tmp = Float64(d1 * Float64(-d3));
	elseif (d2 <= -1e-278)
		tmp = Float64(-Float64(d1 * 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 (d2 <= -4e+126)
		tmp = d2 * d1;
	elseif (d2 <= -2e+90)
		tmp = d1 * -d3;
	elseif (d2 <= -1e-278)
		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[d2, -4e+126], N[(d2 * d1), $MachinePrecision], If[LessEqual[d2, -2e+90], N[(d1 * (-d3)), $MachinePrecision], If[LessEqual[d2, -1e-278], (-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}\;d2 \leq -4 \cdot 10^{+126}:\\
\;\;\;\;d2 \cdot d1\\

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

\mathbf{elif}\;d2 \leq -1 \cdot 10^{-278}:\\
\;\;\;\;-d1 \cdot d1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d2 < -3.9999999999999997e126

    1. Initial program 93.9%

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

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

        \[\leadsto \color{blue}{d1 \cdot d2} \]
    5. Applied rewrites82.1%

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

    if -3.9999999999999997e126 < d2 < -1.99999999999999993e90

    1. Initial program 87.5%

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

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

        \[\leadsto \color{blue}{\mathsf{neg}\left(d1 \cdot d3\right)} \]
      2. distribute-rgt-neg-inN/A

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(\mathsf{neg}\left(d3\right)\right)} \]
      6. lower-neg.f6427.2

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

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

    if -1.99999999999999993e90 < d2 < -9.99999999999999938e-279

    1. Initial program 91.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Taylor expanded in 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. lower-*.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. lower-neg.f6449.5

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

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

    if -9.99999999999999938e-279 < d2

    1. Initial program 84.6%

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

      \[\leadsto \color{blue}{d1 \cdot d4} \]
    4. Step-by-step derivation
      1. lower-*.f6427.5

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

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

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

Alternative 4: 88.0% accurate, 1.2× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d1 < -9.9999999999999993e158 or 2.0000000000000002e172 < d1

    1. Initial program 63.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 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. lower-*.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. lower-neg.f6489.2

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

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

    if -9.9999999999999993e158 < d1 < 2.0000000000000002e172

    1. Initial program 96.8%

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

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    4. Step-by-step derivation
      1. lower-*.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. lower-+.f64N/A

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

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

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

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

Alternative 5: 52.8% accurate, 1.5× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -2 \cdot 10^{+132}:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d2 \leq -1 \cdot 10^{-278}:\\ \;\;\;\;-d1 \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 (<= d2 -2e+132) (* d2 d1) (if (<= d2 -1e-278) (- (* d1 d1)) (* d1 d4))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -2e+132) {
		tmp = d2 * d1;
	} else if (d2 <= -1e-278) {
		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 (d2 <= (-2d+132)) then
        tmp = d2 * d1
    else if (d2 <= (-1d-278)) 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 (d2 <= -2e+132) {
		tmp = d2 * d1;
	} else if (d2 <= -1e-278) {
		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 d2 <= -2e+132:
		tmp = d2 * d1
	elif d2 <= -1e-278:
		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 (d2 <= -2e+132)
		tmp = Float64(d2 * d1);
	elseif (d2 <= -1e-278)
		tmp = Float64(-Float64(d1 * 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 (d2 <= -2e+132)
		tmp = d2 * d1;
	elseif (d2 <= -1e-278)
		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[d2, -2e+132], N[(d2 * d1), $MachinePrecision], If[LessEqual[d2, -1e-278], (-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}\;d2 \leq -2 \cdot 10^{+132}:\\
\;\;\;\;d2 \cdot d1\\

\mathbf{elif}\;d2 \leq -1 \cdot 10^{-278}:\\
\;\;\;\;-d1 \cdot d1\\

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


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

    1. Initial program 96.6%

      \[\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. lower-*.f6486.7

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

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

    if -1.99999999999999998e132 < d2 < -9.99999999999999938e-279

    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 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. lower-*.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. lower-neg.f6446.2

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

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

    if -9.99999999999999938e-279 < d2

    1. Initial program 84.6%

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

      \[\leadsto \color{blue}{d1 \cdot d4} \]
    4. Step-by-step derivation
      1. lower-*.f6427.5

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

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

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

Alternative 6: 93.5% accurate, 1.7× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -5 \cdot 10^{+84}:\\ \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d4 - d3\right) - d1\right)\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d2 -5e+84) (* d1 (+ d2 (- d4 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 (d2 <= -5e+84) {
		tmp = d1 * (d2 + (d4 - 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 (d2 <= (-5d+84)) then
        tmp = d1 * (d2 + (d4 - 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 (d2 <= -5e+84) {
		tmp = d1 * (d2 + (d4 - 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 d2 <= -5e+84:
		tmp = d1 * (d2 + (d4 - 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 (d2 <= -5e+84)
		tmp = Float64(d1 * Float64(d2 + Float64(d4 - 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 (d2 <= -5e+84)
		tmp = d1 * (d2 + (d4 - 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[d2, -5e+84], N[(d1 * N[(d2 + N[(d4 - d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d4 - d3), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -5 \cdot 10^{+84}:\\
\;\;\;\;d1 \cdot \left(d2 + \left(d4 - d3\right)\right)\\

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


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

    1. Initial program 93.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. lower-*.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. lower-+.f64N/A

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

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

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

    if -5.0000000000000001e84 < d2

    1. Initial program 87.3%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Taylor expanded in 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. lower-*.f64N/A

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

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

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

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

Alternative 7: 48.3% accurate, 2.5× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -4 \cdot 10^{+126}:\\ \;\;\;\;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 (<= d2 -4e+126) (* d2 d1) (* d1 d4)))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -4e+126) {
		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 (d2 <= (-4d+126)) 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 (d2 <= -4e+126) {
		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 d2 <= -4e+126:
		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 (d2 <= -4e+126)
		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 (d2 <= -4e+126)
		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[d2, -4e+126], 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}\;d2 \leq -4 \cdot 10^{+126}:\\
\;\;\;\;d2 \cdot d1\\

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


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

    1. Initial program 93.9%

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

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

        \[\leadsto \color{blue}{d1 \cdot d2} \]
    5. Applied rewrites82.1%

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

    if -3.9999999999999997e126 < d2

    1. Initial program 87.4%

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

      \[\leadsto \color{blue}{d1 \cdot d4} \]
    4. Step-by-step derivation
      1. lower-*.f6430.9

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

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

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

Alternative 8: 31.4% 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 88.3%

    \[\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. lower-*.f6434.6

      \[\leadsto \color{blue}{d1 \cdot d2} \]
  5. Applied rewrites34.6%

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

    \[\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 2024213 
(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)))