FastMath test2

Percentage Accurate: 99.6% → 100.0%
Time: 5.4s
Alternatives: 4
Speedup: 2.4×

Specification

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

\\
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20
\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: 99.6% accurate, 1.0× speedup?

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

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

Alternative 1: 100.0% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(d1, 30, d2 \cdot d1\right) \end{array} \]
(FPCore (d1 d2) :precision binary64 (fma d1 30.0 (* d2 d1)))
double code(double d1, double d2) {
	return fma(d1, 30.0, (d2 * d1));
}
function code(d1, d2)
	return fma(d1, 30.0, Float64(d2 * d1))
end
code[d1_, d2_] := N[(d1 * 30.0 + N[(d2 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(d1, 30, d2 \cdot d1\right)
\end{array}
Derivation
  1. Initial program 99.8%

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

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

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

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

      \[\leadsto d1 \cdot 10 + \color{blue}{\left(d1 \cdot 20 + d1 \cdot d2\right)} \]
    5. associate-+r+N/A

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

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(10 + 20\right)} + d1 \cdot d2 \]
    9. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(d1, 10 + 20, d1 \cdot d2\right)} \]
    10. metadata-eval100.0

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

      \[\leadsto \mathsf{fma}\left(d1, 30, \color{blue}{d1 \cdot d2}\right) \]
    12. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(d1, 30, \color{blue}{d2 \cdot d1}\right) \]
    13. lower-*.f64100.0

      \[\leadsto \mathsf{fma}\left(d1, 30, \color{blue}{d2 \cdot d1}\right) \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(d1, 30, d2 \cdot d1\right)} \]
  5. Add Preprocessing

Alternative 2: 97.8% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d2 \leq -30:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d2 \leq 30:\\ \;\;\;\;30 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d2 \cdot d1\\ \end{array} \end{array} \]
(FPCore (d1 d2)
 :precision binary64
 (if (<= d2 -30.0) (* d2 d1) (if (<= d2 30.0) (* 30.0 d1) (* d2 d1))))
double code(double d1, double d2) {
	double tmp;
	if (d2 <= -30.0) {
		tmp = d2 * d1;
	} else if (d2 <= 30.0) {
		tmp = 30.0 * d1;
	} else {
		tmp = d2 * d1;
	}
	return tmp;
}
real(8) function code(d1, d2)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8) :: tmp
    if (d2 <= (-30.0d0)) then
        tmp = d2 * d1
    else if (d2 <= 30.0d0) then
        tmp = 30.0d0 * d1
    else
        tmp = d2 * d1
    end if
    code = tmp
end function
public static double code(double d1, double d2) {
	double tmp;
	if (d2 <= -30.0) {
		tmp = d2 * d1;
	} else if (d2 <= 30.0) {
		tmp = 30.0 * d1;
	} else {
		tmp = d2 * d1;
	}
	return tmp;
}
def code(d1, d2):
	tmp = 0
	if d2 <= -30.0:
		tmp = d2 * d1
	elif d2 <= 30.0:
		tmp = 30.0 * d1
	else:
		tmp = d2 * d1
	return tmp
function code(d1, d2)
	tmp = 0.0
	if (d2 <= -30.0)
		tmp = Float64(d2 * d1);
	elseif (d2 <= 30.0)
		tmp = Float64(30.0 * d1);
	else
		tmp = Float64(d2 * d1);
	end
	return tmp
end
function tmp_2 = code(d1, d2)
	tmp = 0.0;
	if (d2 <= -30.0)
		tmp = d2 * d1;
	elseif (d2 <= 30.0)
		tmp = 30.0 * d1;
	else
		tmp = d2 * d1;
	end
	tmp_2 = tmp;
end
code[d1_, d2_] := If[LessEqual[d2, -30.0], N[(d2 * d1), $MachinePrecision], If[LessEqual[d2, 30.0], N[(30.0 * d1), $MachinePrecision], N[(d2 * d1), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -30:\\
\;\;\;\;d2 \cdot d1\\

\mathbf{elif}\;d2 \leq 30:\\
\;\;\;\;30 \cdot d1\\

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


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

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
      5. distribute-lft-outN/A

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

        \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
      7. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
      8. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      9. lower-*.f64N/A

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

        \[\leadsto \color{blue}{\left(20 + \left(10 + d2\right)\right)} \cdot d1 \]
      11. associate-+r+N/A

        \[\leadsto \color{blue}{\left(\left(20 + 10\right) + d2\right)} \cdot d1 \]
      12. metadata-evalN/A

        \[\leadsto \left(\color{blue}{30} + d2\right) \cdot d1 \]
      13. metadata-evalN/A

        \[\leadsto \left(\color{blue}{\left(10 + 20\right)} + d2\right) \cdot d1 \]
      14. lower-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(10 + 20\right) + d2\right)} \cdot d1 \]
      15. metadata-eval100.0

        \[\leadsto \left(\color{blue}{30} + d2\right) \cdot d1 \]
    4. Applied rewrites100.0%

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

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

        \[\leadsto \color{blue}{\left(30 + d2\right)} \cdot d1 \]
      3. flip-+N/A

        \[\leadsto \color{blue}{\frac{30 \cdot 30 - d2 \cdot d2}{30 - d2}} \cdot d1 \]
      4. associate-*l/N/A

        \[\leadsto \color{blue}{\frac{\left(30 \cdot 30 - d2 \cdot d2\right) \cdot d1}{30 - d2}} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\left(30 \cdot 30 - d2 \cdot d2\right) \cdot d1}{30 - d2}} \]
      6. lower-*.f64N/A

        \[\leadsto \frac{\color{blue}{\left(30 \cdot 30 - d2 \cdot d2\right) \cdot d1}}{30 - d2} \]
      7. lift-*.f64N/A

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

        \[\leadsto \frac{\color{blue}{\left(30 \cdot 30 - d2 \cdot d2\right)} \cdot d1}{30 - d2} \]
      9. metadata-evalN/A

        \[\leadsto \frac{\left(\color{blue}{900} - d2 \cdot d2\right) \cdot d1}{30 - d2} \]
      10. lower--.f6465.4

        \[\leadsto \frac{\left(900 - d2 \cdot d2\right) \cdot d1}{\color{blue}{30 - d2}} \]
    6. Applied rewrites65.4%

      \[\leadsto \color{blue}{\frac{\left(900 - d2 \cdot d2\right) \cdot d1}{30 - d2}} \]
    7. Taylor expanded in d2 around inf

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

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

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

    if -30 < d2 < 30

    1. Initial program 99.6%

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

      \[\leadsto \color{blue}{10 \cdot d1 + 20 \cdot d1} \]
    4. Step-by-step derivation
      1. distribute-rgt-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(10 + 20\right)} \]
      2. metadata-evalN/A

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

        \[\leadsto \color{blue}{30 \cdot d1} \]
      4. lower-*.f6497.9

        \[\leadsto \color{blue}{30 \cdot d1} \]
    5. Applied rewrites97.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -30:\\ \;\;\;\;d2 \cdot d1\\ \mathbf{elif}\;d2 \leq 30:\\ \;\;\;\;30 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d2 \cdot d1\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 100.0% accurate, 2.4× speedup?

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

\\
\left(d2 + 30\right) \cdot d1
\end{array}
Derivation
  1. Initial program 99.8%

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

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

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

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

      \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
    5. distribute-lft-outN/A

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

      \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
    7. distribute-lft-outN/A

      \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
    8. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
    9. lower-*.f64N/A

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

      \[\leadsto \color{blue}{\left(20 + \left(10 + d2\right)\right)} \cdot d1 \]
    11. associate-+r+N/A

      \[\leadsto \color{blue}{\left(\left(20 + 10\right) + d2\right)} \cdot d1 \]
    12. metadata-evalN/A

      \[\leadsto \left(\color{blue}{30} + d2\right) \cdot d1 \]
    13. metadata-evalN/A

      \[\leadsto \left(\color{blue}{\left(10 + 20\right)} + d2\right) \cdot d1 \]
    14. lower-+.f64N/A

      \[\leadsto \color{blue}{\left(\left(10 + 20\right) + d2\right)} \cdot d1 \]
    15. metadata-eval100.0

      \[\leadsto \left(\color{blue}{30} + d2\right) \cdot d1 \]
  4. Applied rewrites100.0%

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

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

Alternative 4: 51.0% accurate, 3.7× speedup?

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

\\
30 \cdot d1
\end{array}
Derivation
  1. Initial program 99.8%

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

    \[\leadsto \color{blue}{10 \cdot d1 + 20 \cdot d1} \]
  4. Step-by-step derivation
    1. distribute-rgt-outN/A

      \[\leadsto \color{blue}{d1 \cdot \left(10 + 20\right)} \]
    2. metadata-evalN/A

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

      \[\leadsto \color{blue}{30 \cdot d1} \]
    4. lower-*.f6454.2

      \[\leadsto \color{blue}{30 \cdot d1} \]
  5. Applied rewrites54.2%

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

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

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

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

Reproduce

?
herbie shell --seed 2024331 
(FPCore (d1 d2)
  :name "FastMath test2"
  :precision binary64

  :alt
  (! :herbie-platform default (* d1 (+ 30 d2)))

  (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))