SynthBasics:oscSampleBasedAux from YampaSynth-0.2

Percentage Accurate: 100.0% → 100.0%
Time: 5.3s
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(z - x, y, x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma (- z x) y x))
double code(double x, double y, double z) {
	return fma((z - x), y, x);
}
function code(x, y, z)
	return fma(Float64(z - x), y, x)
end
code[x_, y_, z_] := N[(N[(z - x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(z - x, 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 x + y \cdot \color{blue}{\left(z - x\right)} \]
    2. lift-*.f64N/A

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

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

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

      \[\leadsto \color{blue}{\left(z - x\right) \cdot y} + x \]
    6. 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. Add Preprocessing

Alternative 2: 98.5% accurate, 0.6× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -9.2e16 or 0.0105000000000000007 < 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(z - x\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

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

        \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
    5. Applied rewrites100.0%

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

    if -9.2e16 < y < 0.0105000000000000007

    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. lower-*.f6498.7

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

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

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

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

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

        \[\leadsto \color{blue}{z \cdot y} + x \]
      5. lower-fma.f6498.7

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

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

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

Alternative 3: 76.1% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -8.4 \cdot 10^{+229}:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\

\mathbf{elif}\;y \leq -3.7 \cdot 10^{+28}:\\
\;\;\;\;x \cdot \left(-y\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\


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

    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. lower-*.f6482.9

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

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

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

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

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

        \[\leadsto \color{blue}{z \cdot y} + x \]
      5. lower-fma.f6482.9

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
    7. Applied rewrites82.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]

    if -8.3999999999999995e229 < y < -3.6999999999999999e28

    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(z - x\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

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

        \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{y \cdot \left(z - x\right)} \]
    6. Taylor expanded in z around 0

      \[\leadsto y \cdot \color{blue}{\left(-1 \cdot x\right)} \]
    7. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto y \cdot \color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \]
      2. lower-neg.f6467.0

        \[\leadsto y \cdot \color{blue}{\left(-x\right)} \]
    8. Applied rewrites67.0%

      \[\leadsto y \cdot \color{blue}{\left(-x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification80.6%

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

Alternative 4: 76.0% accurate, 1.7× speedup?

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

\\
\mathsf{fma}\left(z, y, x\right)
\end{array}
Derivation
  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. lower-*.f6476.1

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

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

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

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

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

      \[\leadsto \color{blue}{z \cdot y} + x \]
    5. lower-fma.f6476.1

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
  7. Applied rewrites76.1%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
  8. Add Preprocessing

Alternative 5: 42.5% accurate, 2.0× speedup?

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

\\
z \cdot y
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{y \cdot z} \]
  4. Step-by-step derivation
    1. lower-*.f6443.6

      \[\leadsto \color{blue}{y \cdot z} \]
  5. Applied rewrites43.6%

    \[\leadsto \color{blue}{y \cdot z} \]
  6. Final simplification43.6%

    \[\leadsto z \cdot y \]
  7. Add Preprocessing

Alternative 6: 3.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ x \cdot y \end{array} \]
(FPCore (x y z) :precision binary64 (* x y))
double code(double x, double y, double z) {
	return x * y;
}
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
end function
public static double code(double x, double y, double z) {
	return x * y;
}
def code(x, y, z):
	return x * y
function code(x, y, z)
	return Float64(x * y)
end
function tmp = code(x, y, z)
	tmp = x * y;
end
code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}

\\
x \cdot y
\end{array}
Derivation
  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(z - x\right)} \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \color{blue}{y \cdot \left(z - x\right)} \]
    2. lower--.f6466.9

      \[\leadsto y \cdot \color{blue}{\left(z - x\right)} \]
  5. Applied rewrites66.9%

    \[\leadsto \color{blue}{y \cdot \left(z - x\right)} \]
  6. Taylor expanded in z around 0

    \[\leadsto y \cdot \color{blue}{\left(-1 \cdot x\right)} \]
  7. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto y \cdot \color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \]
    2. lower-neg.f6427.6

      \[\leadsto y \cdot \color{blue}{\left(-x\right)} \]
  8. Applied rewrites27.6%

    \[\leadsto y \cdot \color{blue}{\left(-x\right)} \]
  9. Step-by-step derivation
    1. neg-sub0N/A

      \[\leadsto y \cdot \color{blue}{\left(0 - x\right)} \]
    2. flip3--N/A

      \[\leadsto y \cdot \color{blue}{\frac{{0}^{3} - {x}^{3}}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)}} \]
    3. metadata-evalN/A

      \[\leadsto y \cdot \frac{\color{blue}{0} - {x}^{3}}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    4. sub0-negN/A

      \[\leadsto y \cdot \frac{\color{blue}{\mathsf{neg}\left({x}^{3}\right)}}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    5. sqr-powN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\color{blue}{{x}^{\left(\frac{3}{2}\right)} \cdot {x}^{\left(\frac{3}{2}\right)}}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    6. pow-prod-downN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\color{blue}{{\left(x \cdot x\right)}^{\left(\frac{3}{2}\right)}}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    7. sqr-negN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left({\color{blue}{\left(\left(\mathsf{neg}\left(x\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)\right)}}^{\left(\frac{3}{2}\right)}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    8. lift-neg.f64N/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left({\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \cdot \left(\mathsf{neg}\left(x\right)\right)\right)}^{\left(\frac{3}{2}\right)}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    9. lift-neg.f64N/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left({\left(\left(\mathsf{neg}\left(x\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)}^{\left(\frac{3}{2}\right)}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    10. pow-prod-downN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\color{blue}{{\left(\mathsf{neg}\left(x\right)\right)}^{\left(\frac{3}{2}\right)} \cdot {\left(\mathsf{neg}\left(x\right)\right)}^{\left(\frac{3}{2}\right)}}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    11. sqr-powN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\color{blue}{{\left(\mathsf{neg}\left(x\right)\right)}^{3}}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    12. lift-neg.f64N/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left({\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}}^{3}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    13. cube-negN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left({x}^{3}\right)\right)}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    14. sub0-negN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\color{blue}{\left(0 - {x}^{3}\right)}\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    15. metadata-evalN/A

      \[\leadsto y \cdot \frac{\mathsf{neg}\left(\left(\color{blue}{{0}^{3}} - {x}^{3}\right)\right)}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)} \]
    16. distribute-neg-fracN/A

      \[\leadsto y \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{{0}^{3} - {x}^{3}}{0 \cdot 0 + \left(x \cdot x + 0 \cdot x\right)}\right)\right)} \]
    17. flip3--N/A

      \[\leadsto y \cdot \left(\mathsf{neg}\left(\color{blue}{\left(0 - x\right)}\right)\right) \]
    18. neg-sub0N/A

      \[\leadsto y \cdot \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right)\right) \]
    19. remove-double-negN/A

      \[\leadsto y \cdot \color{blue}{x} \]
  10. Applied rewrites5.6%

    \[\leadsto \color{blue}{y \cdot x} \]
  11. Final simplification5.6%

    \[\leadsto x \cdot y \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024216 
(FPCore (x y z)
  :name "SynthBasics:oscSampleBasedAux from YampaSynth-0.2"
  :precision binary64
  (+ x (* y (- z x))))