FastMath dist4

Percentage Accurate: 87.3% → 98.7%
Time: 25.4s
Alternatives: 10
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 10 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.3% 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.7% 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.35 \cdot 10^{+154}:\\ \;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(d2 - d3, d1, d1 \cdot \left(d4 - d1\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 (<= d1 -1.35e+154)
   (* d1 (- (- d2 d1) d3))
   (fma (- d2 d3) d1 (* d1 (- d4 d1)))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d1 <= -1.35e+154) {
		tmp = d1 * ((d2 - d1) - d3);
	} else {
		tmp = fma((d2 - d3), d1, (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 <= -1.35e+154)
		tmp = Float64(d1 * Float64(Float64(d2 - d1) - d3));
	else
		tmp = fma(Float64(d2 - d3), d1, Float64(d1 * Float64(d4 - 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[d1, -1.35e+154], N[(d1 * N[(N[(d2 - d1), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision], N[(N[(d2 - d3), $MachinePrecision] * d1 + N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d1 \leq -1.35 \cdot 10^{+154}:\\
\;\;\;\;d1 \cdot \left(\left(d2 - d1\right) - d3\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d1 < -1.35000000000000003e154

    1. Initial program 36.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 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--.f6496.7

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

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

    if -1.35000000000000003e154 < d1

    1. Initial program 93.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. associate--l+N/A

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

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

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

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

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

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

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

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

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

Alternative 2: 94.3% accurate, 1.7× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 1000000000000:\\ \;\;\;\;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 1000000000000.0) (* 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 <= 1000000000000.0) {
		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 <= 1000000000000.0d0) 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 <= 1000000000000.0) {
		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 <= 1000000000000.0:
		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 <= 1000000000000.0)
		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 <= 1000000000000.0)
		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, 1000000000000.0], 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 1000000000000:\\
\;\;\;\;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 < 1e12

    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 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--.f6481.1

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

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

    if 1e12 < d4

    1. Initial program 80.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 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--.f6484.9

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

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

Alternative 3: 90.9% accurate, 1.7× speedup?

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

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


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

    1. Initial program 89.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 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.8

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

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

    if 1.54999999999999996e113 < d4

    1. Initial program 71.4%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, \left(d2 + d4\right) - d3, 0\right)} \]
      11. associate--l+N/A

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

        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d2 + \left(d4 - d3\right)}, 0\right) \]
      13. --lowering--.f6488.1

        \[\leadsto \mathsf{fma}\left(d1, d2 + \color{blue}{\left(d4 - d3\right)}, 0\right) \]
    5. Simplified88.1%

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

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(0 - \left(d3 - d4\right)\right)} \]
      5. neg-sub0N/A

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(\mathsf{neg}\left(\left(d3 - d4\right)\right)\right)} \]
      9. neg-sub0N/A

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

        \[\leadsto d1 \cdot \color{blue}{\left(\left(0 - d3\right) + d4\right)} \]
      11. neg-sub0N/A

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

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

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

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

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

Alternative 4: 98.5% 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 - d3\right) - d1\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 d3) d1))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	return fma(d2, d1, (d1 * ((d4 - d3) - d1)));
}
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	return fma(d2, d1, Float64(d1 * Float64(Float64(d4 - d3) - 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 + N[(d1 * N[(N[(d4 - d3), $MachinePrecision] - d1), $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 - d3\right) - d1\right)\right)
\end{array}
Derivation
  1. Initial program 86.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. sub-negN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 75.3% accurate, 2.0× speedup?

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

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


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

    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 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--.f6481.1

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

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

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

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

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

    if 6.5e15 < d4

    1. Initial program 80.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. distribute-lft-out--N/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, \left(d2 + d4\right) - d3, 0\right)} \]
      11. associate--l+N/A

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

        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d2 + \left(d4 - d3\right)}, 0\right) \]
      13. --lowering--.f6488.9

        \[\leadsto \mathsf{fma}\left(d1, d2 + \color{blue}{\left(d4 - d3\right)}, 0\right) \]
    5. Simplified88.9%

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

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

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(0 - \left(d3 - d4\right)\right)} \]
      5. neg-sub0N/A

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

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(\mathsf{neg}\left(\left(d3 - d4\right)\right)\right)} \]
      9. neg-sub0N/A

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

        \[\leadsto d1 \cdot \color{blue}{\left(\left(0 - d3\right) + d4\right)} \]
      11. neg-sub0N/A

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

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

        \[\leadsto d1 \cdot \color{blue}{\left(d4 - d3\right)} \]
      14. --lowering--.f6475.5

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

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

Alternative 6: 73.4% accurate, 2.0× speedup?

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

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


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

    1. Initial program 89.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--.f6480.7

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

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

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

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

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

    if 5.00000000000000005e54 < d4

    1. Initial program 78.5%

      \[\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. distribute-lft-out--N/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, \left(d2 + d4\right) - d3, 0\right)} \]
      11. associate--l+N/A

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

        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d2 + \left(d4 - d3\right)}, 0\right) \]
      13. --lowering--.f6487.7

        \[\leadsto \mathsf{fma}\left(d1, d2 + \color{blue}{\left(d4 - d3\right)}, 0\right) \]
    5. Simplified87.7%

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

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

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

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

Alternative 7: 70.5% accurate, 2.0× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 5500000:\\ \;\;\;\;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 5500000.0) (* 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 <= 5500000.0) {
		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 <= 5500000.0d0) 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 <= 5500000.0) {
		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 <= 5500000.0:
		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 <= 5500000.0)
		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 <= 5500000.0)
		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, 5500000.0], 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 5500000:\\
\;\;\;\;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 < 5.5e6

    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 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--.f6481.1

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

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

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

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

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

    if 5.5e6 < d4

    1. Initial program 80.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. distribute-lft-out--N/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, \left(d2 + d4\right) - d3, 0\right)} \]
      11. associate--l+N/A

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

        \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d2 + \left(d4 - d3\right)}, 0\right) \]
      13. --lowering--.f6488.9

        \[\leadsto \mathsf{fma}\left(d1, d2 + \color{blue}{\left(d4 - d3\right)}, 0\right) \]
    5. Simplified88.9%

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

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

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

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

Alternative 8: 50.5% accurate, 2.5× speedup?

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

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


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

    1. Initial program 83.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. +-rgt-identityN/A

        \[\leadsto \color{blue}{d1 \cdot d2 + 0} \]
      2. accelerator-lowering-fma.f6463.8

        \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d2, 0\right)} \]
    5. Simplified63.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(d1, d2, 0\right)} \]
    6. Step-by-step derivation
      1. +-rgt-identityN/A

        \[\leadsto \color{blue}{d1 \cdot d2} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{d2 \cdot d1} \]
      3. *-lowering-*.f6463.8

        \[\leadsto \color{blue}{d2 \cdot d1} \]
    7. Applied egg-rr63.8%

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

    if -5.19999999999999963e72 < d2

    1. Initial program 87.6%

      \[\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. sub-negN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{d1 \cdot d4} \]
    6. Step-by-step derivation
      1. *-lowering-*.f6438.2

        \[\leadsto \color{blue}{d1 \cdot d4} \]
    7. Simplified38.2%

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

Alternative 9: 56.8% accurate, 3.3× speedup?

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(d1, \left(d2 + d4\right) - d3, 0\right)} \]
    11. associate--l+N/A

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

      \[\leadsto \mathsf{fma}\left(d1, \color{blue}{d2 + \left(d4 - d3\right)}, 0\right) \]
    13. --lowering--.f6483.7

      \[\leadsto \mathsf{fma}\left(d1, d2 + \color{blue}{\left(d4 - d3\right)}, 0\right) \]
  5. Simplified83.7%

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

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

    \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]
  9. Final simplification61.1%

    \[\leadsto d1 \cdot \left(d2 + d4\right) \]
  10. Add Preprocessing

Alternative 10: 32.1% accurate, 5.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{d1 \cdot d4} \]
  6. Step-by-step derivation
    1. *-lowering-*.f6434.3

      \[\leadsto \color{blue}{d1 \cdot d4} \]
  7. Simplified34.3%

    \[\leadsto \color{blue}{d1 \cdot d4} \]
  8. 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 2024198 
(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)))