FastMath dist

Percentage Accurate: 97.8% → 99.0%
Time: 2.2s
Alternatives: 4
Speedup: 1.4×

Specification

?
\[\begin{array}{l} \\ d1 \cdot d2 + d1 \cdot d3 \end{array} \]
(FPCore (d1 d2 d3) :precision binary64 (+ (* d1 d2) (* d1 d3)))
double code(double d1, double d2, double d3) {
	return (d1 * d2) + (d1 * d3);
}
real(8) function code(d1, d2, d3)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    code = (d1 * d2) + (d1 * d3)
end function
public static double code(double d1, double d2, double d3) {
	return (d1 * d2) + (d1 * d3);
}
def code(d1, d2, d3):
	return (d1 * d2) + (d1 * d3)
function code(d1, d2, d3)
	return Float64(Float64(d1 * d2) + Float64(d1 * d3))
end
function tmp = code(d1, d2, d3)
	tmp = (d1 * d2) + (d1 * d3);
end
code[d1_, d2_, d3_] := N[(N[(d1 * d2), $MachinePrecision] + N[(d1 * d3), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot d2 + d1 \cdot d3
\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 4 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: 97.8% accurate, 1.0× speedup?

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

\\
d1 \cdot d2 + d1 \cdot d3
\end{array}

Alternative 1: 99.0% accurate, 0.1× speedup?

\[\begin{array}{l} [d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\ \\ \mathsf{fma}\left(d3, d1, d1 \cdot d2\right) \end{array} \]
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3) :precision binary64 (fma d3 d1 (* d1 d2)))
assert(d1 < d2 && d2 < d3);
double code(double d1, double d2, double d3) {
	return fma(d3, d1, (d1 * d2));
}
d1, d2, d3 = sort([d1, d2, d3])
function code(d1, d2, d3)
	return fma(d3, d1, Float64(d1 * d2))
end
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_] := N[(d3 * d1 + N[(d1 * d2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\
\\
\mathsf{fma}\left(d3, d1, d1 \cdot d2\right)
\end{array}
Derivation
  1. Initial program 99.2%

    \[d1 \cdot d2 + d1 \cdot d3 \]
  2. Step-by-step derivation
    1. distribute-lft-out100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. distribute-lft-in99.2%

      \[\leadsto \color{blue}{d1 \cdot d2 + d1 \cdot d3} \]
    2. +-commutative99.2%

      \[\leadsto \color{blue}{d1 \cdot d3 + d1 \cdot d2} \]
    3. *-commutative99.2%

      \[\leadsto \color{blue}{d3 \cdot d1} + d1 \cdot d2 \]
    4. fma-def100.0%

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(d3, d1, d1 \cdot d2\right)} \]
  7. Final simplification100.0%

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

Alternative 2: 84.0% accurate, 1.4× speedup?

\[\begin{array}{l} [d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq 8.8 \cdot 10^{-117}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d3 \cdot d1\\ \end{array} \end{array} \]
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3)
 :precision binary64
 (if (<= d3 8.8e-117) (* d1 d2) (* d3 d1)))
assert(d1 < d2 && d2 < d3);
double code(double d1, double d2, double d3) {
	double tmp;
	if (d3 <= 8.8e-117) {
		tmp = d1 * d2;
	} else {
		tmp = d3 * d1;
	}
	return tmp;
}
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8) :: tmp
    if (d3 <= 8.8d-117) then
        tmp = d1 * d2
    else
        tmp = d3 * d1
    end if
    code = tmp
end function
assert d1 < d2 && d2 < d3;
public static double code(double d1, double d2, double d3) {
	double tmp;
	if (d3 <= 8.8e-117) {
		tmp = d1 * d2;
	} else {
		tmp = d3 * d1;
	}
	return tmp;
}
[d1, d2, d3] = sort([d1, d2, d3])
def code(d1, d2, d3):
	tmp = 0
	if d3 <= 8.8e-117:
		tmp = d1 * d2
	else:
		tmp = d3 * d1
	return tmp
d1, d2, d3 = sort([d1, d2, d3])
function code(d1, d2, d3)
	tmp = 0.0
	if (d3 <= 8.8e-117)
		tmp = Float64(d1 * d2);
	else
		tmp = Float64(d3 * d1);
	end
	return tmp
end
d1, d2, d3 = num2cell(sort([d1, d2, d3])){:}
function tmp_2 = code(d1, d2, d3)
	tmp = 0.0;
	if (d3 <= 8.8e-117)
		tmp = d1 * d2;
	else
		tmp = d3 * d1;
	end
	tmp_2 = tmp;
end
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_] := If[LessEqual[d3, 8.8e-117], N[(d1 * d2), $MachinePrecision], N[(d3 * d1), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\
\\
\begin{array}{l}
\mathbf{if}\;d3 \leq 8.8 \cdot 10^{-117}:\\
\;\;\;\;d1 \cdot d2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d3 < 8.8000000000000004e-117

    1. Initial program 100.0%

      \[d1 \cdot d2 + d1 \cdot d3 \]
    2. Step-by-step derivation
      1. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in d2 around inf 65.6%

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

    if 8.8000000000000004e-117 < d3

    1. Initial program 97.7%

      \[d1 \cdot d2 + d1 \cdot d3 \]
    2. Step-by-step derivation
      1. distribute-lft-out99.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in d2 around 0 72.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq 8.8 \cdot 10^{-117}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d3 \cdot d1\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 100.0% accurate, 1.4× speedup?

\[\begin{array}{l} [d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\ \\ d1 \cdot \left(d3 + d2\right) \end{array} \]
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3) :precision binary64 (* d1 (+ d3 d2)))
assert(d1 < d2 && d2 < d3);
double code(double d1, double d2, double d3) {
	return d1 * (d3 + d2);
}
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    code = d1 * (d3 + d2)
end function
assert d1 < d2 && d2 < d3;
public static double code(double d1, double d2, double d3) {
	return d1 * (d3 + d2);
}
[d1, d2, d3] = sort([d1, d2, d3])
def code(d1, d2, d3):
	return d1 * (d3 + d2)
d1, d2, d3 = sort([d1, d2, d3])
function code(d1, d2, d3)
	return Float64(d1 * Float64(d3 + d2))
end
d1, d2, d3 = num2cell(sort([d1, d2, d3])){:}
function tmp = code(d1, d2, d3)
	tmp = d1 * (d3 + d2);
end
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_] := N[(d1 * N[(d3 + d2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\
\\
d1 \cdot \left(d3 + d2\right)
\end{array}
Derivation
  1. Initial program 99.2%

    \[d1 \cdot d2 + d1 \cdot d3 \]
  2. Step-by-step derivation
    1. distribute-lft-out100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
  4. Add Preprocessing
  5. Final simplification100.0%

    \[\leadsto d1 \cdot \left(d3 + d2\right) \]
  6. Add Preprocessing

Alternative 4: 53.8% accurate, 2.3× speedup?

\[\begin{array}{l} [d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\ \\ d1 \cdot d2 \end{array} \]
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3) :precision binary64 (* d1 d2))
assert(d1 < d2 && d2 < d3);
double code(double d1, double d2, double d3) {
	return d1 * d2;
}
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    code = d1 * d2
end function
assert d1 < d2 && d2 < d3;
public static double code(double d1, double d2, double d3) {
	return d1 * d2;
}
[d1, d2, d3] = sort([d1, d2, d3])
def code(d1, d2, d3):
	return d1 * d2
d1, d2, d3 = sort([d1, d2, d3])
function code(d1, d2, d3)
	return Float64(d1 * d2)
end
d1, d2, d3 = num2cell(sort([d1, d2, d3])){:}
function tmp = code(d1, d2, d3)
	tmp = d1 * d2;
end
NOTE: d1, d2, and d3 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_] := N[(d1 * d2), $MachinePrecision]
\begin{array}{l}
[d1, d2, d3] = \mathsf{sort}([d1, d2, d3])\\
\\
d1 \cdot d2
\end{array}
Derivation
  1. Initial program 99.2%

    \[d1 \cdot d2 + d1 \cdot d3 \]
  2. Step-by-step derivation
    1. distribute-lft-out100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{d1 \cdot \left(d2 + d3\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in d2 around inf 53.9%

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

    \[\leadsto d1 \cdot d2 \]
  7. Add Preprocessing

Developer target: 100.0% accurate, 1.4× speedup?

\[\begin{array}{l} \\ d1 \cdot \left(d2 + d3\right) \end{array} \]
(FPCore (d1 d2 d3) :precision binary64 (* d1 (+ d2 d3)))
double code(double d1, double d2, double d3) {
	return d1 * (d2 + d3);
}
real(8) function code(d1, d2, d3)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    code = d1 * (d2 + d3)
end function
public static double code(double d1, double d2, double d3) {
	return d1 * (d2 + d3);
}
def code(d1, d2, d3):
	return d1 * (d2 + d3)
function code(d1, d2, d3)
	return Float64(d1 * Float64(d2 + d3))
end
function tmp = code(d1, d2, d3)
	tmp = d1 * (d2 + d3);
end
code[d1_, d2_, d3_] := N[(d1 * N[(d2 + d3), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Reproduce

?
herbie shell --seed 2024019 
(FPCore (d1 d2 d3)
  :name "FastMath dist"
  :precision binary64

  :herbie-target
  (* d1 (+ d2 d3))

  (+ (* d1 d2) (* d1 d3)))