FastMath test2

Percentage Accurate: 99.7% → 100.0%
Time: 2.8s
Alternatives: 3
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 3 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.7% 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: 100.0% accurate, 2.4× speedup?

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

\\
\left(30 + d2\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. Add Preprocessing

Alternative 3: 50.8% 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-*.f6451.0

      \[\leadsto \color{blue}{30 \cdot d1} \]
  5. Applied rewrites51.0%

    \[\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 2024308 
(FPCore (d1 d2)
  :name "FastMath test2"
  :precision binary64

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

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