SynthBasics:oscSampleBasedAux from YampaSynth-0.2

Percentage Accurate: 100.0% → 100.0%
Time: 6.9s
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 \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. Add Preprocessing

Alternative 2: 99.0% accurate, 0.6× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -260 or 0.0749999999999999972 < 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--.f6498.3

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

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

    if -260 < y < 0.0749999999999999972

    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-*.f6499.2

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

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

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

Alternative 3: 84.6% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(z - x\right) \cdot y\\ \mathbf{if}\;y \leq -2.12 \cdot 10^{-69}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 205:\\ \;\;\;\;x - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* (- z x) y)))
   (if (<= y -2.12e-69) t_0 (if (<= y 205.0) (- x (* x y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = (z - x) * y;
	double tmp;
	if (y <= -2.12e-69) {
		tmp = t_0;
	} else if (y <= 205.0) {
		tmp = x - (x * y);
	} 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 = (z - x) * y
    if (y <= (-2.12d-69)) then
        tmp = t_0
    else if (y <= 205.0d0) then
        tmp = x - (x * y)
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = (z - x) * y;
	double tmp;
	if (y <= -2.12e-69) {
		tmp = t_0;
	} else if (y <= 205.0) {
		tmp = x - (x * y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = (z - x) * y
	tmp = 0
	if y <= -2.12e-69:
		tmp = t_0
	elif y <= 205.0:
		tmp = x - (x * y)
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(z - x) * y)
	tmp = 0.0
	if (y <= -2.12e-69)
		tmp = t_0;
	elseif (y <= 205.0)
		tmp = Float64(x - Float64(x * y));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = (z - x) * y;
	tmp = 0.0;
	if (y <= -2.12e-69)
		tmp = t_0;
	elseif (y <= 205.0)
		tmp = x - (x * y);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(z - x), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -2.12e-69], t$95$0, If[LessEqual[y, 205.0], N[(x - N[(x * y), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

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

\mathbf{elif}\;y \leq 205:\\
\;\;\;\;x - x \cdot y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -2.11999999999999993e-69 or 205 < 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--.f6498.1

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

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

    if -2.11999999999999993e-69 < y < 205

    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-*.f6471.4

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

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

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

Alternative 4: 52.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y \cdot \left(-x\right)\\ \mathbf{if}\;x \leq -9.7 \cdot 10^{-11}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 100000000000:\\ \;\;\;\;z \cdot y\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (- x))))
   (if (<= x -9.7e-11) t_0 (if (<= x 100000000000.0) (* z y) t_0))))
double code(double x, double y, double z) {
	double t_0 = y * -x;
	double tmp;
	if (x <= -9.7e-11) {
		tmp = t_0;
	} else if (x <= 100000000000.0) {
		tmp = z * y;
	} 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
    if (x <= (-9.7d-11)) then
        tmp = t_0
    else if (x <= 100000000000.0d0) then
        tmp = z * y
    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;
	double tmp;
	if (x <= -9.7e-11) {
		tmp = t_0;
	} else if (x <= 100000000000.0) {
		tmp = z * y;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = y * -x
	tmp = 0
	if x <= -9.7e-11:
		tmp = t_0
	elif x <= 100000000000.0:
		tmp = z * y
	else:
		tmp = t_0
	return tmp
function code(x, y, z)
	t_0 = Float64(y * Float64(-x))
	tmp = 0.0
	if (x <= -9.7e-11)
		tmp = t_0;
	elseif (x <= 100000000000.0)
		tmp = Float64(z * y);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = y * -x;
	tmp = 0.0;
	if (x <= -9.7e-11)
		tmp = t_0;
	elseif (x <= 100000000000.0)
		tmp = z * y;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * (-x)), $MachinePrecision]}, If[LessEqual[x, -9.7e-11], t$95$0, If[LessEqual[x, 100000000000.0], N[(z * y), $MachinePrecision], t$95$0]]]
\begin{array}{l}

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -9.7000000000000001e-11 or 1e11 < x

    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--.f6453.9

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

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

      \[\leadsto y \cdot \left(-1 \cdot \color{blue}{x}\right) \]
    7. Step-by-step derivation
      1. Applied rewrites44.4%

        \[\leadsto y \cdot \left(-x\right) \]

      if -9.7000000000000001e-11 < x < 1e11

      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-*.f6460.3

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

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -9.7 \cdot 10^{-11}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq 100000000000:\\ \;\;\;\;z \cdot y\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \end{array} \]
    10. Add Preprocessing

    Alternative 5: 65.7% accurate, 1.3× speedup?

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

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

      \[\leadsto \color{blue}{y \cdot \left(z - x\right)} \]
    6. Final simplification64.1%

      \[\leadsto \left(z - x\right) \cdot y \]
    7. Add Preprocessing

    Alternative 6: 41.9% 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-*.f6439.3

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

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

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

    Reproduce

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