SynthBasics:moogVCF from YampaSynth-0.2

Percentage Accurate: 93.6% → 98.6%
Time: 11.8s
Alternatives: 12
Speedup: 1.0×

Specification

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

\\
x + \left(y \cdot z\right) \cdot \left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\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 12 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: 93.6% accurate, 1.0× speedup?

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

\\
x + \left(y \cdot z\right) \cdot \left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right)
\end{array}

Alternative 1: 98.6% accurate, 1.0× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \begin{array}{l} \mathbf{if}\;y\_m \leq 5 \cdot 10^{+232}:\\ \;\;\;\;\mathsf{fma}\left(z \cdot \left(\tanh \left(\frac{t}{y\_m}\right) - \tanh \left(\frac{x}{y\_m}\right)\right), y\_m, x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t - x, z, x\right)\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m z t)
 :precision binary64
 (if (<= y_m 5e+232)
   (fma (* z (- (tanh (/ t y_m)) (tanh (/ x y_m)))) y_m x)
   (fma (- t x) z x)))
y_m = fabs(y);
double code(double x, double y_m, double z, double t) {
	double tmp;
	if (y_m <= 5e+232) {
		tmp = fma((z * (tanh((t / y_m)) - tanh((x / y_m)))), y_m, x);
	} else {
		tmp = fma((t - x), z, x);
	}
	return tmp;
}
y_m = abs(y)
function code(x, y_m, z, t)
	tmp = 0.0
	if (y_m <= 5e+232)
		tmp = fma(Float64(z * Float64(tanh(Float64(t / y_m)) - tanh(Float64(x / y_m)))), y_m, x);
	else
		tmp = fma(Float64(t - x), z, x);
	end
	return tmp
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_, z_, t_] := If[LessEqual[y$95$m, 5e+232], N[(N[(z * N[(N[Tanh[N[(t / y$95$m), $MachinePrecision]], $MachinePrecision] - N[Tanh[N[(x / y$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y$95$m + x), $MachinePrecision], N[(N[(t - x), $MachinePrecision] * z + x), $MachinePrecision]]
\begin{array}{l}
y_m = \left|y\right|

\\
\begin{array}{l}
\mathbf{if}\;y\_m \leq 5 \cdot 10^{+232}:\\
\;\;\;\;\mathsf{fma}\left(z \cdot \left(\tanh \left(\frac{t}{y\_m}\right) - \tanh \left(\frac{x}{y\_m}\right)\right), y\_m, x\right)\\

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


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

    1. Initial program 96.8%

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

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

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

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

        \[\leadsto \color{blue}{\left(y \cdot z\right)} \cdot \left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right) + x \]
      5. associate-*l*N/A

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

        \[\leadsto \color{blue}{\left(z \cdot \left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right)\right) \cdot y} + x \]
      7. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(z \cdot \left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right), y, x\right)} \]
      8. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right) \cdot z}, y, x\right) \]
      9. lower-*.f6498.1

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right) \cdot z}, y, x\right) \]
    4. Applied rewrites98.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\tanh \left(\frac{t}{y}\right) - \tanh \left(\frac{x}{y}\right)\right) \cdot z, y, x\right)} \]

    if 4.99999999999999987e232 < y

    1. Initial program 69.8%

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

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

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

        \[\leadsto \color{blue}{\left(t - x\right) \cdot z} + x \]
      3. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(t - x, z, x\right)} \]
      4. lower--.f6497.7

        \[\leadsto \mathsf{fma}\left(\color{blue}{t - x}, z, x\right) \]
    5. Applied rewrites97.7%

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

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

Alternative 2: 67.9% accurate, 1.6× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ \begin{array}{l} \mathbf{if}\;y\_m \leq 4 \cdot 10^{-248}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left(-x\right) \cdot x}{x + t}, z, x\right)\\ \mathbf{elif}\;y\_m \leq 1.3 \cdot 10^{+71}:\\ \;\;\;\;\left(\frac{t}{y\_m} - \tanh \left(\frac{x}{y\_m}\right)\right) \cdot \left(z \cdot y\_m\right) + x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t - x, z, x\right)\\ \end{array} \end{array} \]
y_m = (fabs.f64 y)
(FPCore (x y_m z t)
 :precision binary64
 (if (<= y_m 4e-248)
   (fma (/ (* (- x) x) (+ x t)) z x)
   (if (<= y_m 1.3e+71)
     (+ (* (- (/ t y_m) (tanh (/ x y_m))) (* z y_m)) x)
     (fma (- t x) z x))))
y_m = fabs(y);
double code(double x, double y_m, double z, double t) {
	double tmp;
	if (y_m <= 4e-248) {
		tmp = fma(((-x * x) / (x + t)), z, x);
	} else if (y_m <= 1.3e+71) {
		tmp = (((t / y_m) - tanh((x / y_m))) * (z * y_m)) + x;
	} else {
		tmp = fma((t - x), z, x);
	}
	return tmp;
}
y_m = abs(y)
function code(x, y_m, z, t)
	tmp = 0.0
	if (y_m <= 4e-248)
		tmp = fma(Float64(Float64(Float64(-x) * x) / Float64(x + t)), z, x);
	elseif (y_m <= 1.3e+71)
		tmp = Float64(Float64(Float64(Float64(t / y_m) - tanh(Float64(x / y_m))) * Float64(z * y_m)) + x);
	else
		tmp = fma(Float64(t - x), z, x);
	end
	return tmp
end
y_m = N[Abs[y], $MachinePrecision]
code[x_, y$95$m_, z_, t_] := If[LessEqual[y$95$m, 4e-248], N[(N[(N[((-x) * x), $MachinePrecision] / N[(x + t), $MachinePrecision]), $MachinePrecision] * z + x), $MachinePrecision], If[LessEqual[y$95$m, 1.3e+71], N[(N[(N[(N[(t / y$95$m), $MachinePrecision] - N[Tanh[N[(x / y$95$m), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(z * y$95$m), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision], N[(N[(t - x), $MachinePrecision] * z + x), $MachinePrecision]]]
\begin{array}{l}
y_m = \left|y\right|

\\
\begin{array}{l}
\mathbf{if}\;y\_m \leq 4 \cdot 10^{-248}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\left(-x\right) \cdot x}{x + t}, z, x\right)\\

\mathbf{elif}\;y\_m \leq 1.3 \cdot 10^{+71}:\\
\;\;\;\;\left(\frac{t}{y\_m} - \tanh \left(\frac{x}{y\_m}\right)\right) \cdot \left(z \cdot y\_m\right) + x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < 3.99999999999999992e-248

    1. Initial program 100.0%

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

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

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

        \[\leadsto \color{blue}{\left(t - x\right) \cdot z} + x \]
      3. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(t - x, z, x\right)} \]
      4. lower--.f6433.6

        \[\leadsto \mathsf{fma}\left(\color{blue}{t - x}, z, x\right) \]
    5. Applied rewrites33.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(t - x, z, x\right)} \]
    6. Step-by-step derivation
      1. Applied rewrites22.5%

        \[\leadsto \mathsf{fma}\left(\frac{\left(x + t\right) \cdot \left(t - x\right)}{x + t}, z, x\right) \]
      2. Taylor expanded in x around inf

        \[\leadsto \mathsf{fma}\left(\frac{-1 \cdot {x}^{2}}{x + t}, z, x\right) \]
      3. Step-by-step derivation
        1. Applied rewrites47.3%

          \[\leadsto \mathsf{fma}\left(\frac{\left(-x\right) \cdot x}{x + t}, z, x\right) \]

        if 3.99999999999999992e-248 < y < 1.29999999999999996e71

        1. Initial program 99.5%

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

          \[\leadsto x + \left(y \cdot z\right) \cdot \left(\color{blue}{\frac{t}{y}} - \tanh \left(\frac{x}{y}\right)\right) \]
        4. Step-by-step derivation
          1. lower-/.f6459.1

            \[\leadsto x + \left(y \cdot z\right) \cdot \left(\color{blue}{\frac{t}{y}} - \tanh \left(\frac{x}{y}\right)\right) \]
        5. Applied rewrites59.1%

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

        if 1.29999999999999996e71 < y

        1. Initial program 83.7%

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

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

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

            \[\leadsto \color{blue}{\left(t - x\right) \cdot z} + x \]
          3. lower-fma.f64N/A

            \[\leadsto \color{blue}{\mathsf{fma}\left(t - x, z, x\right)} \]
          4. lower--.f6486.0

            \[\leadsto \mathsf{fma}\left(\color{blue}{t - x}, z, x\right) \]
        5. Applied rewrites86.0%

          \[\leadsto \color{blue}{\mathsf{fma}\left(t - x, z, x\right)} \]
      4. Recombined 3 regimes into one program.
      5. Final simplification67.9%

        \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 4 \cdot 10^{-248}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\left(-x\right) \cdot x}{x + t}, z, x\right)\\ \mathbf{elif}\;y \leq 1.3 \cdot 10^{+71}:\\ \;\;\;\;\left(\frac{t}{y} - \tanh \left(\frac{x}{y}\right)\right) \cdot \left(z \cdot y\right) + x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t - x, z, x\right)\\ \end{array} \]
      6. Add Preprocessing

      Developer Target 1: 97.4% accurate, 1.0× speedup?

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

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (x y z t)
        :name "SynthBasics:moogVCF from YampaSynth-0.2"
        :precision binary64
      
        :alt
        (! :herbie-platform default (+ x (* y (* z (- (tanh (/ t y)) (tanh (/ x y)))))))
      
        (+ x (* (* y z) (- (tanh (/ t y)) (tanh (/ x y))))))