SynthBasics:oscSampleBasedAux from YampaSynth-0.2

Percentage Accurate: 100.0% → 100.0%
Time: 2.6s
Alternatives: 4
Speedup: 1.1×

Specification

?
\[x + y \cdot \left(z - x\right) \]
(FPCore (x y z) :precision binary64 (+ x (* y (- z x))))
double code(double x, double y, double z) {
	return x + (y * (z - x));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z)
use fmin_fmax_functions
    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]
x + y \cdot \left(z - x\right)

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 4 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?

\[x + y \cdot \left(z - x\right) \]
(FPCore (x y z) :precision binary64 (+ x (* y (- z x))))
double code(double x, double y, double z) {
	return x + (y * (z - x));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, y, z)
use fmin_fmax_functions
    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]
x + y \cdot \left(z - x\right)

Alternative 1: 100.0% accurate, 1.1× speedup?

\[\mathsf{fma}\left(z - x, y, x\right) \]
(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]
\mathsf{fma}\left(z - x, y, x\right)
Derivation
  1. Initial program 100.0%

    \[x + y \cdot \left(z - x\right) \]
  2. 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. add-flipN/A

      \[\leadsto \color{blue}{y \cdot \left(z - x\right) - \left(\mathsf{neg}\left(x\right)\right)} \]
    4. sub-flipN/A

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

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

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

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

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

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

Alternative 2: 98.7% accurate, 0.7× speedup?

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

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

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


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

    1. Initial program 100.0%

      \[x + y \cdot \left(z - x\right) \]
    2. 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. add-flipN/A

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

        \[\leadsto \color{blue}{y \cdot \left(z - x\right)} - \left(\mathsf{neg}\left(x\right)\right) \]
      5. lift--.f64N/A

        \[\leadsto y \cdot \color{blue}{\left(z - x\right)} - \left(\mathsf{neg}\left(x\right)\right) \]
      6. sub-flipN/A

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

        \[\leadsto \color{blue}{\left(z \cdot y + \left(\mathsf{neg}\left(x\right)\right) \cdot y\right)} - \left(\mathsf{neg}\left(x\right)\right) \]
      8. associate--l+N/A

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

        \[\leadsto z \cdot y + \left(\color{blue}{y \cdot \left(\mathsf{neg}\left(x\right)\right)} - \left(\mathsf{neg}\left(x\right)\right)\right) \]
      10. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, y \cdot \left(\mathsf{neg}\left(x\right)\right) - \left(\mathsf{neg}\left(x\right)\right)\right)} \]
      11. add-flip-revN/A

        \[\leadsto \mathsf{fma}\left(z, y, \color{blue}{y \cdot \left(\mathsf{neg}\left(x\right)\right) + x}\right) \]
      12. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(z, y, \color{blue}{x + y \cdot \left(\mathsf{neg}\left(x\right)\right)}\right) \]
      13. fp-cancel-sign-sub-invN/A

        \[\leadsto \mathsf{fma}\left(z, y, \color{blue}{x - \left(\mathsf{neg}\left(y\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)}\right) \]
      14. lower--.f64N/A

        \[\leadsto \mathsf{fma}\left(z, y, \color{blue}{x - \left(\mathsf{neg}\left(y\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)}\right) \]
      15. distribute-lft-neg-outN/A

        \[\leadsto \mathsf{fma}\left(z, y, x - \color{blue}{\left(\mathsf{neg}\left(y \cdot \left(\mathsf{neg}\left(x\right)\right)\right)\right)}\right) \]
      16. distribute-rgt-neg-outN/A

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

        \[\leadsto \mathsf{fma}\left(z, y, x - y \cdot \color{blue}{x}\right) \]
      18. lower-*.f6498.7%

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

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

      \[\leadsto \color{blue}{x - x \cdot y} \]
    5. Step-by-step derivation
      1. lower--.f64N/A

        \[\leadsto x - \color{blue}{x \cdot y} \]
      2. lower-*.f6461.4%

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

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

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

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

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

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

    if -980 < y < 1.66e7

    1. Initial program 100.0%

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

      \[\leadsto x + y \cdot \color{blue}{z} \]
    3. Step-by-step derivation
      1. Applied rewrites75.6%

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

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

          \[\leadsto \color{blue}{y \cdot z + x} \]
        3. add-flipN/A

          \[\leadsto \color{blue}{y \cdot z - \left(\mathsf{neg}\left(x\right)\right)} \]
        4. sub-flipN/A

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

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

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

          \[\leadsto z \cdot y + \color{blue}{x} \]
        8. lower-fma.f6475.7%

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
    4. Recombined 2 regimes into one program.
    5. Add Preprocessing

    Alternative 3: 75.7% accurate, 1.6× speedup?

    \[\mathsf{fma}\left(z, y, x\right) \]
    (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]
    
    \mathsf{fma}\left(z, y, x\right)
    
    Derivation
    1. Initial program 100.0%

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

      \[\leadsto x + y \cdot \color{blue}{z} \]
    3. Step-by-step derivation
      1. Applied rewrites75.6%

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

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

          \[\leadsto \color{blue}{y \cdot z + x} \]
        3. add-flipN/A

          \[\leadsto \color{blue}{y \cdot z - \left(\mathsf{neg}\left(x\right)\right)} \]
        4. sub-flipN/A

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

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

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

          \[\leadsto z \cdot y + \color{blue}{x} \]
        8. lower-fma.f6475.7%

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

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

      Alternative 4: 42.4% accurate, 2.4× speedup?

      \[y \cdot z \]
      (FPCore (x y z) :precision binary64 (* y z))
      double code(double x, double y, double z) {
      	return y * z;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      real(8) function code(x, y, z)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8), intent (in) :: z
          code = y * z
      end function
      
      public static double code(double x, double y, double z) {
      	return y * z;
      }
      
      def code(x, y, z):
      	return y * z
      
      function code(x, y, z)
      	return Float64(y * z)
      end
      
      function tmp = code(x, y, z)
      	tmp = y * z;
      end
      
      code[x_, y_, z_] := N[(y * z), $MachinePrecision]
      
      y \cdot z
      
      Derivation
      1. Initial program 100.0%

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

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

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

        \[\leadsto \color{blue}{y \cdot z} \]
      5. Add Preprocessing

      Reproduce

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