Main:bigenough2 from A

Percentage Accurate: 100.0% → 100.0%
Time: 4.1s
Alternatives: 6
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ x + y \cdot \left(z + x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
	return x + (y * (z + x));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (y * (z + x))
end function
public static double code(double x, double y, double z) {
	return x + (y * (z + x));
}
def code(x, y, z):
	return x + (y * (z + x))
function code(x, y, z)
	return Float64(x + Float64(y * Float64(z + x)))
end
function tmp = code(x, y, z)
	tmp = x + (y * (z + x));
end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + y \cdot \left(z + x\right)
\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 6 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x + y \cdot \left(z + x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
	return x + (y * (z + x));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (y * (z + x))
end function
public static double code(double x, double y, double z) {
	return x + (y * (z + x));
}
def code(x, y, z):
	return x + (y * (z + x))
function code(x, y, z)
	return Float64(x + Float64(y * Float64(z + x)))
end
function tmp = code(x, y, z)
	tmp = x + (y * (z + x));
end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + y \cdot \left(z + x\right)
\end{array}

Alternative 1: 100.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x + z, y, x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma (+ x z) y x))
double code(double x, double y, double z) {
	return fma((x + z), y, x);
}
function code(x, y, z)
	return fma(Float64(x + z), y, x)
end
code[x_, y_, z_] := N[(N[(x + z), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x + z, y, x\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[x + y \cdot \left(z + x\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{x + y \cdot \left(z + x\right)} \]
    2. +-commutativeN/A

      \[\leadsto \color{blue}{y \cdot \left(z + x\right) + x} \]
    3. lift-*.f64N/A

      \[\leadsto \color{blue}{y \cdot \left(z + x\right)} + x \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{\left(z + x\right) \cdot y} + x \]
    5. lower-fma.f64100.0

      \[\leadsto \color{blue}{\mathsf{fma}\left(z + x, y, x\right)} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z + x, y, x\right)} \]
  5. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(x + z, y, x\right) \]
  6. Add Preprocessing

Alternative 2: 98.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y \cdot \left(x + z\right)\\ \mathbf{if}\;y \leq -1:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;y \cdot z + x\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (+ x z))))
   (if (<= y -1.0) t_0 (if (<= y 1.0) (+ (* y z) x) t_0))))
double code(double x, double y, double z) {
	double t_0 = y * (x + z);
	double tmp;
	if (y <= -1.0) {
		tmp = t_0;
	} else if (y <= 1.0) {
		tmp = (y * z) + x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = y * (x + z)
    if (y <= (-1.0d0)) then
        tmp = t_0
    else if (y <= 1.0d0) then
        tmp = (y * z) + x
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = y * (x + z);
	double tmp;
	if (y <= -1.0) {
		tmp = t_0;
	} else if (y <= 1.0) {
		tmp = (y * z) + x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = y * (x + z)
	tmp = 0
	if y <= -1.0:
		tmp = t_0
	elif y <= 1.0:
		tmp = (y * z) + x
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(y * Float64(x + z))
	tmp = 0.0
	if (y <= -1.0)
		tmp = t_0;
	elseif (y <= 1.0)
		tmp = Float64(Float64(y * z) + x);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = y * (x + z);
	tmp = 0.0;
	if (y <= -1.0)
		tmp = t_0;
	elseif (y <= 1.0)
		tmp = (y * z) + x;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.0], t$95$0, If[LessEqual[y, 1.0], N[(N[(y * z), $MachinePrecision] + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := y \cdot \left(x + z\right)\\
\mathbf{if}\;y \leq -1:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 1:\\
\;\;\;\;y \cdot z + x\\

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


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

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{y \cdot \left(x + z\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(x + z\right) \cdot y} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(x + z\right) \cdot y} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left(z + x\right)} \cdot y \]
      4. lower-+.f6499.1

        \[\leadsto \color{blue}{\left(z + x\right)} \cdot y \]
    5. Applied rewrites99.1%

      \[\leadsto \color{blue}{\left(z + x\right) \cdot y} \]

    if -1 < y < 1

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf

      \[\leadsto x + \color{blue}{y \cdot z} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto x + \color{blue}{z \cdot y} \]
      2. lower-*.f6498.4

        \[\leadsto x + \color{blue}{z \cdot y} \]
    5. Applied rewrites98.4%

      \[\leadsto x + \color{blue}{z \cdot y} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;y \cdot \left(x + z\right)\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;y \cdot z + x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(x + z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 82.1% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y \cdot \left(x + z\right)\\ \mathbf{if}\;y \leq -1.25 \cdot 10^{-152}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 550000000:\\ \;\;\;\;\mathsf{fma}\left(y, x, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (+ x z))))
   (if (<= y -1.25e-152) t_0 (if (<= y 550000000.0) (fma y x x) t_0))))
double code(double x, double y, double z) {
	double t_0 = y * (x + z);
	double tmp;
	if (y <= -1.25e-152) {
		tmp = t_0;
	} else if (y <= 550000000.0) {
		tmp = fma(y, x, x);
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(y * Float64(x + z))
	tmp = 0.0
	if (y <= -1.25e-152)
		tmp = t_0;
	elseif (y <= 550000000.0)
		tmp = fma(y, x, x);
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.25e-152], t$95$0, If[LessEqual[y, 550000000.0], N[(y * x + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := y \cdot \left(x + z\right)\\
\mathbf{if}\;y \leq -1.25 \cdot 10^{-152}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 550000000:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.2499999999999999e-152 or 5.5e8 < y

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{y \cdot \left(x + z\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(x + z\right) \cdot y} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(x + z\right) \cdot y} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left(z + x\right)} \cdot y \]
      4. lower-+.f6494.6

        \[\leadsto \color{blue}{\left(z + x\right)} \cdot y \]
    5. Applied rewrites94.6%

      \[\leadsto \color{blue}{\left(z + x\right) \cdot y} \]

    if -1.2499999999999999e-152 < y < 5.5e8

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

      \[\leadsto \color{blue}{x + x \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot y + x} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} + x \]
      3. lower-fma.f6480.5

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
    5. Applied rewrites80.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification89.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.25 \cdot 10^{-152}:\\ \;\;\;\;y \cdot \left(x + z\right)\\ \mathbf{elif}\;y \leq 550000000:\\ \;\;\;\;\mathsf{fma}\left(y, x, x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(x + z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 75.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -4.8 \cdot 10^{+49}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 2.3 \cdot 10^{+141}:\\ \;\;\;\;\mathsf{fma}\left(y, x, x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -4.8e+49) (* y z) (if (<= z 2.3e+141) (fma y x x) (* y z))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -4.8e+49) {
		tmp = y * z;
	} else if (z <= 2.3e+141) {
		tmp = fma(y, x, x);
	} else {
		tmp = y * z;
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (z <= -4.8e+49)
		tmp = Float64(y * z);
	elseif (z <= 2.3e+141)
		tmp = fma(y, x, x);
	else
		tmp = Float64(y * z);
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[z, -4.8e+49], N[(y * z), $MachinePrecision], If[LessEqual[z, 2.3e+141], N[(y * x + x), $MachinePrecision], N[(y * z), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -4.8 \cdot 10^{+49}:\\
\;\;\;\;y \cdot z\\

\mathbf{elif}\;z \leq 2.3 \cdot 10^{+141}:\\
\;\;\;\;\mathsf{fma}\left(y, x, x\right)\\

\mathbf{else}:\\
\;\;\;\;y \cdot z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -4.8e49 or 2.3000000000000002e141 < z

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in z around inf

      \[\leadsto \color{blue}{y \cdot z} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{z \cdot y} \]
      2. lower-*.f6481.9

        \[\leadsto \color{blue}{z \cdot y} \]
    5. Applied rewrites81.9%

      \[\leadsto \color{blue}{z \cdot y} \]

    if -4.8e49 < z < 2.3000000000000002e141

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

      \[\leadsto \color{blue}{x + x \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot y + x} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} + x \]
      3. lower-fma.f6477.0

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
    5. Applied rewrites77.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification78.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -4.8 \cdot 10^{+49}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 2.3 \cdot 10^{+141}:\\ \;\;\;\;\mathsf{fma}\left(y, x, x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 52.0% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4.8 \cdot 10^{+65}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;x \leq 1150:\\ \;\;\;\;y \cdot z\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -4.8e+65) (* y x) (if (<= x 1150.0) (* y z) (* y x))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -4.8e+65) {
		tmp = y * x;
	} else if (x <= 1150.0) {
		tmp = y * z;
	} else {
		tmp = y * x;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (x <= (-4.8d+65)) then
        tmp = y * x
    else if (x <= 1150.0d0) then
        tmp = y * z
    else
        tmp = y * x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -4.8e+65) {
		tmp = y * x;
	} else if (x <= 1150.0) {
		tmp = y * z;
	} else {
		tmp = y * x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if x <= -4.8e+65:
		tmp = y * x
	elif x <= 1150.0:
		tmp = y * z
	else:
		tmp = y * x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (x <= -4.8e+65)
		tmp = Float64(y * x);
	elseif (x <= 1150.0)
		tmp = Float64(y * z);
	else
		tmp = Float64(y * x);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -4.8e+65)
		tmp = y * x;
	elseif (x <= 1150.0)
		tmp = y * z;
	else
		tmp = y * x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[x, -4.8e+65], N[(y * x), $MachinePrecision], If[LessEqual[x, 1150.0], N[(y * z), $MachinePrecision], N[(y * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.8 \cdot 10^{+65}:\\
\;\;\;\;y \cdot x\\

\mathbf{elif}\;x \leq 1150:\\
\;\;\;\;y \cdot z\\

\mathbf{else}:\\
\;\;\;\;y \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -4.8000000000000003e65 or 1150 < x

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

      \[\leadsto \color{blue}{x + x \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot y + x} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} + x \]
      3. lower-fma.f6491.8

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
    5. Applied rewrites91.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
    6. Taylor expanded in y around inf

      \[\leadsto x \cdot \color{blue}{y} \]
    7. Step-by-step derivation
      1. Applied rewrites51.4%

        \[\leadsto y \cdot \color{blue}{x} \]

      if -4.8000000000000003e65 < x < 1150

      1. Initial program 100.0%

        \[x + y \cdot \left(z + x\right) \]
      2. Add Preprocessing
      3. Taylor expanded in z around inf

        \[\leadsto \color{blue}{y \cdot z} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{z \cdot y} \]
        2. lower-*.f6465.0

          \[\leadsto \color{blue}{z \cdot y} \]
      5. Applied rewrites65.0%

        \[\leadsto \color{blue}{z \cdot y} \]
    8. Recombined 2 regimes into one program.
    9. Final simplification59.4%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.8 \cdot 10^{+65}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;x \leq 1150:\\ \;\;\;\;y \cdot z\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]
    10. Add Preprocessing

    Alternative 6: 27.5% accurate, 2.0× speedup?

    \[\begin{array}{l} \\ y \cdot x \end{array} \]
    (FPCore (x y z) :precision binary64 (* y x))
    double code(double x, double y, double z) {
    	return y * x;
    }
    
    real(8) function code(x, y, z)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        code = y * x
    end function
    
    public static double code(double x, double y, double z) {
    	return y * x;
    }
    
    def code(x, y, z):
    	return y * x
    
    function code(x, y, z)
    	return Float64(y * x)
    end
    
    function tmp = code(x, y, z)
    	tmp = y * x;
    end
    
    code[x_, y_, z_] := N[(y * x), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    y \cdot x
    \end{array}
    
    Derivation
    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

      \[\leadsto \color{blue}{x + x \cdot y} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot y + x} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} + x \]
      3. lower-fma.f6459.2

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
    5. Applied rewrites59.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, x\right)} \]
    6. Taylor expanded in y around inf

      \[\leadsto x \cdot \color{blue}{y} \]
    7. Step-by-step derivation
      1. Applied rewrites30.4%

        \[\leadsto y \cdot \color{blue}{x} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024248 
      (FPCore (x y z)
        :name "Main:bigenough2 from A"
        :precision binary64
        (+ x (* y (+ z x))))