SynthBasics:oscSampleBasedAux from YampaSynth-0.2

Percentage Accurate: 100.0% → 100.0%
Time: 6.9s
Alternatives: 5
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 5 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: 86.4% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
t_0 := x - x \cdot y\\
\mathbf{if}\;x \leq -1.85 \cdot 10^{+47}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.8500000000000002e47 or 0.419999999999999984 < x

    1. Initial program 100.0%

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

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

        \[\leadsto x \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(y\right)\right)}\right) \]
      2. unsub-negN/A

        \[\leadsto x \cdot \color{blue}{\left(1 - y\right)} \]
      3. distribute-lft-out--N/A

        \[\leadsto \color{blue}{x \cdot 1 - x \cdot y} \]
      4. *-rgt-identityN/A

        \[\leadsto \color{blue}{x} - x \cdot y \]
      5. lower--.f64N/A

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

        \[\leadsto x - \color{blue}{y \cdot x} \]
      7. lower-*.f6493.7

        \[\leadsto x - \color{blue}{y \cdot x} \]
    5. Applied rewrites93.7%

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

    if -1.8500000000000002e47 < x < 0.419999999999999984

    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-*.f6492.5

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

      \[\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.f6492.6

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

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

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

Alternative 3: 73.2% accurate, 0.6× speedup?

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

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

\mathbf{elif}\;z \leq 2.2 \cdot 10^{-139}:\\
\;\;\;\;y \cdot \left(-x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < 1.55e-292 or 2.2000000000000001e-139 < z

    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-*.f6486.6

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

      \[\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.f6486.6

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

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

    if 1.55e-292 < z < 2.2000000000000001e-139

    1. Initial program 100.0%

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

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

        \[\leadsto x \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(y\right)\right)}\right) \]
      2. unsub-negN/A

        \[\leadsto x \cdot \color{blue}{\left(1 - y\right)} \]
      3. distribute-lft-out--N/A

        \[\leadsto \color{blue}{x \cdot 1 - x \cdot y} \]
      4. *-rgt-identityN/A

        \[\leadsto \color{blue}{x} - x \cdot y \]
      5. lower--.f64N/A

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

        \[\leadsto x - \color{blue}{y \cdot x} \]
      7. lower-*.f6497.5

        \[\leadsto x - \color{blue}{y \cdot x} \]
    5. Applied rewrites97.5%

      \[\leadsto \color{blue}{x - y \cdot x} \]
    6. Taylor expanded in y around inf

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

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot y\right)} \]
      2. distribute-rgt-neg-inN/A

        \[\leadsto \color{blue}{x \cdot \left(\mathsf{neg}\left(y\right)\right)} \]
      3. mul-1-negN/A

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

        \[\leadsto \color{blue}{x \cdot \left(-1 \cdot y\right)} \]
      5. mul-1-negN/A

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

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

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

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

Alternative 4: 75.8% 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-*.f6478.1

      \[\leadsto x + \color{blue}{y \cdot z} \]
  5. Applied rewrites78.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.f6478.1

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

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

Alternative 5: 41.3% 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-*.f6442.3

      \[\leadsto \color{blue}{y \cdot z} \]
  5. Applied rewrites42.3%

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

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

Reproduce

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