Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3

Percentage Accurate: 98.0% → 100.0%
Time: 1.3s
Alternatives: 6
Speedup: 1.4×

Specification

?
\[x \cdot y + \left(1 - x\right) \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
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) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z):
	return (x * y) + ((1.0 - x) * z)
function code(x, y, z)
	return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z))
end
function tmp = code(x, y, z)
	tmp = (x * y) + ((1.0 - x) * z);
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * y) + (((1) - x) * z)
END code
x \cdot y + \left(1 - x\right) \cdot z

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: 98.0% accurate, 1.0× speedup?

\[x \cdot y + \left(1 - x\right) \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
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) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z):
	return (x * y) + ((1.0 - x) * z)
function code(x, y, z)
	return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z))
end
function tmp = code(x, y, z)
	tmp = (x * y) + ((1.0 - x) * z);
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * y) + (((1) - x) * z)
END code
x \cdot y + \left(1 - x\right) \cdot z

Alternative 1: 100.0% accurate, 1.4× speedup?

\[\mathsf{fma}\left(x, y - z, z\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fma x (- y z) z))
double code(double x, double y, double z) {
	return fma(x, (y - z), z);
}
function code(x, y, z)
	return fma(x, Float64(y - z), z)
end
code[x_, y_, z_] := N[(x * N[(y - z), $MachinePrecision] + z), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(x * (y - z)) + z
END code
\mathsf{fma}\left(x, y - z, z\right)
Derivation
  1. Initial program 98.0%

    \[x \cdot y + \left(1 - x\right) \cdot z \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \mathsf{fma}\left(-x, z - y, z\right) \]
    2. Step-by-step derivation
      1. Applied rewrites100.0%

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

      Alternative 2: 98.9% accurate, 0.9× speedup?

      \[\begin{array}{l} t_0 := x \cdot \left(y - z\right)\\ \mathbf{if}\;x \leq -14298.649587223466:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 0.0005660629841236394:\\ \;\;\;\;\mathsf{fma}\left(x, y, z\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
      (FPCore (x y z)
        :precision binary64
        :pre TRUE
        (let* ((t_0 (* x (- y z))))
        (if (<= x -14298.649587223466)
          t_0
          (if (<= x 0.0005660629841236394) (fma x y z) t_0))))
      double code(double x, double y, double z) {
      	double t_0 = x * (y - z);
      	double tmp;
      	if (x <= -14298.649587223466) {
      		tmp = t_0;
      	} else if (x <= 0.0005660629841236394) {
      		tmp = fma(x, y, z);
      	} else {
      		tmp = t_0;
      	}
      	return tmp;
      }
      
      function code(x, y, z)
      	t_0 = Float64(x * Float64(y - z))
      	tmp = 0.0
      	if (x <= -14298.649587223466)
      		tmp = t_0;
      	elseif (x <= 0.0005660629841236394)
      		tmp = fma(x, y, z);
      	else
      		tmp = t_0;
      	end
      	return tmp
      end
      
      code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -14298.649587223466], t$95$0, If[LessEqual[x, 0.0005660629841236394], N[(x * y + z), $MachinePrecision], t$95$0]]]
      
      f(x, y, z):
      	x in [-inf, +inf],
      	y in [-inf, +inf],
      	z in [-inf, +inf]
      code: THEORY
      BEGIN
      f(x, y, z: real): real =
      	LET t_0 = (x * (y - z)) IN
      		LET tmp_1 = IF (x <= (566062984123639448448550570702764161978848278522491455078125e-63)) THEN ((x * y) + z) ELSE t_0 ENDIF IN
      		LET tmp = IF (x <= (-14298649587223466369323432445526123046875e-36)) THEN t_0 ELSE tmp_1 ENDIF IN
      	tmp
      END code
      \begin{array}{l}
      t_0 := x \cdot \left(y - z\right)\\
      \mathbf{if}\;x \leq -14298.649587223466:\\
      \;\;\;\;t\_0\\
      
      \mathbf{elif}\;x \leq 0.0005660629841236394:\\
      \;\;\;\;\mathsf{fma}\left(x, y, z\right)\\
      
      \mathbf{else}:\\
      \;\;\;\;t\_0\\
      
      
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < -14298.649587223466 or 5.6606298412363945e-4 < x

        1. Initial program 98.0%

          \[x \cdot y + \left(1 - x\right) \cdot z \]
        2. Step-by-step derivation
          1. Applied rewrites100.0%

            \[\leadsto \mathsf{fma}\left(-x, z - y, z\right) \]
          2. Step-by-step derivation
            1. Applied rewrites100.0%

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

              \[\leadsto x \cdot \left(y - z\right) \]
            3. Step-by-step derivation
              1. Applied rewrites64.9%

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

              if -14298.649587223466 < x < 5.6606298412363945e-4

              1. Initial program 98.0%

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

                \[\leadsto x \cdot y + z \]
              3. Step-by-step derivation
                1. Applied rewrites75.9%

                  \[\leadsto x \cdot y + z \]
                2. Step-by-step derivation
                  1. Applied rewrites76.0%

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

                Alternative 3: 85.1% accurate, 0.9× speedup?

                \[\begin{array}{l} \mathbf{if}\;y \leq -6.742668220036118 \cdot 10^{+82}:\\ \;\;\;\;\mathsf{fma}\left(x, y, z\right)\\ \mathbf{elif}\;y \leq 3.07342713026798 \cdot 10^{-110}:\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, y, z\right)\\ \end{array} \]
                (FPCore (x y z)
                  :precision binary64
                  :pre TRUE
                  (if (<= y -6.742668220036118e+82)
                  (fma x y z)
                  (if (<= y 3.07342713026798e-110) (* z (- 1.0 x)) (fma x y z))))
                double code(double x, double y, double z) {
                	double tmp;
                	if (y <= -6.742668220036118e+82) {
                		tmp = fma(x, y, z);
                	} else if (y <= 3.07342713026798e-110) {
                		tmp = z * (1.0 - x);
                	} else {
                		tmp = fma(x, y, z);
                	}
                	return tmp;
                }
                
                function code(x, y, z)
                	tmp = 0.0
                	if (y <= -6.742668220036118e+82)
                		tmp = fma(x, y, z);
                	elseif (y <= 3.07342713026798e-110)
                		tmp = Float64(z * Float64(1.0 - x));
                	else
                		tmp = fma(x, y, z);
                	end
                	return tmp
                end
                
                code[x_, y_, z_] := If[LessEqual[y, -6.742668220036118e+82], N[(x * y + z), $MachinePrecision], If[LessEqual[y, 3.07342713026798e-110], N[(z * N[(1.0 - x), $MachinePrecision]), $MachinePrecision], N[(x * y + z), $MachinePrecision]]]
                
                f(x, y, z):
                	x in [-inf, +inf],
                	y in [-inf, +inf],
                	z in [-inf, +inf]
                code: THEORY
                BEGIN
                f(x, y, z: real): real =
                	LET tmp_1 = IF (y <= (3073427130267980153005627182190657993395790266346178807683912839381824716108030662403918387306509888781507111484986848945374899460134021774697330315701930108353347326619958155595922805617888049569222363859348691391916452399978367507736953548628895889670962497363109096337741021898182225413620471954345703125e-416)) THEN (z * ((1) - x)) ELSE ((x * y) + z) ENDIF IN
                	LET tmp = IF (y <= (-67426682200361176120268859113156867640057346737471876670647569701482367024229253120)) THEN ((x * y) + z) ELSE tmp_1 ENDIF IN
                	tmp
                END code
                \begin{array}{l}
                \mathbf{if}\;y \leq -6.742668220036118 \cdot 10^{+82}:\\
                \;\;\;\;\mathsf{fma}\left(x, y, z\right)\\
                
                \mathbf{elif}\;y \leq 3.07342713026798 \cdot 10^{-110}:\\
                \;\;\;\;z \cdot \left(1 - x\right)\\
                
                \mathbf{else}:\\
                \;\;\;\;\mathsf{fma}\left(x, y, z\right)\\
                
                
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if y < -6.7426682200361176e82 or 3.0734271302679802e-110 < y

                  1. Initial program 98.0%

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

                    \[\leadsto x \cdot y + z \]
                  3. Step-by-step derivation
                    1. Applied rewrites75.9%

                      \[\leadsto x \cdot y + z \]
                    2. Step-by-step derivation
                      1. Applied rewrites76.0%

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

                      if -6.7426682200361176e82 < y < 3.0734271302679802e-110

                      1. Initial program 98.0%

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

                        \[\leadsto z \cdot \left(1 - x\right) \]
                      3. Step-by-step derivation
                        1. Applied rewrites62.3%

                          \[\leadsto z \cdot \left(1 - x\right) \]
                      4. Recombined 2 regimes into one program.
                      5. Add Preprocessing

                      Alternative 4: 76.0% accurate, 2.0× speedup?

                      \[\mathsf{fma}\left(x, y, z\right) \]
                      (FPCore (x y z)
                        :precision binary64
                        :pre TRUE
                        (fma x y z))
                      double code(double x, double y, double z) {
                      	return fma(x, y, z);
                      }
                      
                      function code(x, y, z)
                      	return fma(x, y, z)
                      end
                      
                      code[x_, y_, z_] := N[(x * y + z), $MachinePrecision]
                      
                      f(x, y, z):
                      	x in [-inf, +inf],
                      	y in [-inf, +inf],
                      	z in [-inf, +inf]
                      code: THEORY
                      BEGIN
                      f(x, y, z: real): real =
                      	(x * y) + z
                      END code
                      \mathsf{fma}\left(x, y, z\right)
                      
                      Derivation
                      1. Initial program 98.0%

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

                        \[\leadsto x \cdot y + z \]
                      3. Step-by-step derivation
                        1. Applied rewrites75.9%

                          \[\leadsto x \cdot y + z \]
                        2. Step-by-step derivation
                          1. Applied rewrites76.0%

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

                          Alternative 5: 61.5% accurate, 1.1× speedup?

                          \[\begin{array}{l} \mathbf{if}\;x \leq -2.8368855981903414 \cdot 10^{-51}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 9.957315309628584 \cdot 10^{-34}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
                          (FPCore (x y z)
                            :precision binary64
                            :pre TRUE
                            (if (<= x -2.8368855981903414e-51)
                            (* x y)
                            (if (<= x 9.957315309628584e-34) z (* x y))))
                          double code(double x, double y, double z) {
                          	double tmp;
                          	if (x <= -2.8368855981903414e-51) {
                          		tmp = x * y;
                          	} else if (x <= 9.957315309628584e-34) {
                          		tmp = z;
                          	} else {
                          		tmp = x * y;
                          	}
                          	return tmp;
                          }
                          
                          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
                              real(8) :: tmp
                              if (x <= (-2.8368855981903414d-51)) then
                                  tmp = x * y
                              else if (x <= 9.957315309628584d-34) then
                                  tmp = z
                              else
                                  tmp = x * y
                              end if
                              code = tmp
                          end function
                          
                          public static double code(double x, double y, double z) {
                          	double tmp;
                          	if (x <= -2.8368855981903414e-51) {
                          		tmp = x * y;
                          	} else if (x <= 9.957315309628584e-34) {
                          		tmp = z;
                          	} else {
                          		tmp = x * y;
                          	}
                          	return tmp;
                          }
                          
                          def code(x, y, z):
                          	tmp = 0
                          	if x <= -2.8368855981903414e-51:
                          		tmp = x * y
                          	elif x <= 9.957315309628584e-34:
                          		tmp = z
                          	else:
                          		tmp = x * y
                          	return tmp
                          
                          function code(x, y, z)
                          	tmp = 0.0
                          	if (x <= -2.8368855981903414e-51)
                          		tmp = Float64(x * y);
                          	elseif (x <= 9.957315309628584e-34)
                          		tmp = z;
                          	else
                          		tmp = Float64(x * y);
                          	end
                          	return tmp
                          end
                          
                          function tmp_2 = code(x, y, z)
                          	tmp = 0.0;
                          	if (x <= -2.8368855981903414e-51)
                          		tmp = x * y;
                          	elseif (x <= 9.957315309628584e-34)
                          		tmp = z;
                          	else
                          		tmp = x * y;
                          	end
                          	tmp_2 = tmp;
                          end
                          
                          code[x_, y_, z_] := If[LessEqual[x, -2.8368855981903414e-51], N[(x * y), $MachinePrecision], If[LessEqual[x, 9.957315309628584e-34], z, N[(x * y), $MachinePrecision]]]
                          
                          f(x, y, z):
                          	x in [-inf, +inf],
                          	y in [-inf, +inf],
                          	z in [-inf, +inf]
                          code: THEORY
                          BEGIN
                          f(x, y, z: real): real =
                          	LET tmp_1 = IF (x <= (995731530962858429324473884117268777133337992305983115666935440622549596510538661930608296535272216942757950164377689361572265625e-162)) THEN z ELSE (x * y) ENDIF IN
                          	LET tmp = IF (x <= (-28368855981903414102920337465896913142028897505741484614367286972627935745057502704794255345263793652131194254318143559929623156480593682005064692930318415164947509765625e-220)) THEN (x * y) ELSE tmp_1 ENDIF IN
                          	tmp
                          END code
                          \begin{array}{l}
                          \mathbf{if}\;x \leq -2.8368855981903414 \cdot 10^{-51}:\\
                          \;\;\;\;x \cdot y\\
                          
                          \mathbf{elif}\;x \leq 9.957315309628584 \cdot 10^{-34}:\\
                          \;\;\;\;z\\
                          
                          \mathbf{else}:\\
                          \;\;\;\;x \cdot y\\
                          
                          
                          \end{array}
                          
                          Derivation
                          1. Split input into 2 regimes
                          2. if x < -2.8368855981903414e-51 or 9.9573153096285843e-34 < x

                            1. Initial program 98.0%

                              \[x \cdot y + \left(1 - x\right) \cdot z \]
                            2. Step-by-step derivation
                              1. Applied rewrites100.0%

                                \[\leadsto \mathsf{fma}\left(-x, z - y, z\right) \]
                              2. Step-by-step derivation
                                1. Applied rewrites100.0%

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

                                  \[\leadsto x \cdot y \]
                                3. Step-by-step derivation
                                  1. Applied rewrites41.5%

                                    \[\leadsto x \cdot y \]

                                  if -2.8368855981903414e-51 < x < 9.9573153096285843e-34

                                  1. Initial program 98.0%

                                    \[x \cdot y + \left(1 - x\right) \cdot z \]
                                  2. Step-by-step derivation
                                    1. Applied rewrites100.0%

                                      \[\leadsto \mathsf{fma}\left(-x, z - y, z\right) \]
                                    2. Step-by-step derivation
                                      1. Applied rewrites100.0%

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

                                        \[\leadsto x \cdot \left(y - z\right) \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites64.9%

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

                                          \[\leadsto z \]
                                        3. Step-by-step derivation
                                          1. Applied rewrites36.9%

                                            \[\leadsto z \]
                                        4. Recombined 2 regimes into one program.
                                        5. Add Preprocessing

                                        Alternative 6: 36.9% accurate, 12.2× speedup?

                                        \[z \]
                                        (FPCore (x y z)
                                          :precision binary64
                                          :pre TRUE
                                          z)
                                        double code(double x, double y, double z) {
                                        	return z;
                                        }
                                        
                                        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 = z
                                        end function
                                        
                                        public static double code(double x, double y, double z) {
                                        	return z;
                                        }
                                        
                                        def code(x, y, z):
                                        	return z
                                        
                                        function code(x, y, z)
                                        	return z
                                        end
                                        
                                        function tmp = code(x, y, z)
                                        	tmp = z;
                                        end
                                        
                                        code[x_, y_, z_] := z
                                        
                                        f(x, y, z):
                                        	x in [-inf, +inf],
                                        	y in [-inf, +inf],
                                        	z in [-inf, +inf]
                                        code: THEORY
                                        BEGIN
                                        f(x, y, z: real): real =
                                        	z
                                        END code
                                        z
                                        
                                        Derivation
                                        1. Initial program 98.0%

                                          \[x \cdot y + \left(1 - x\right) \cdot z \]
                                        2. Step-by-step derivation
                                          1. Applied rewrites100.0%

                                            \[\leadsto \mathsf{fma}\left(-x, z - y, z\right) \]
                                          2. Step-by-step derivation
                                            1. Applied rewrites100.0%

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

                                              \[\leadsto x \cdot \left(y - z\right) \]
                                            3. Step-by-step derivation
                                              1. Applied rewrites64.9%

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

                                                \[\leadsto z \]
                                              3. Step-by-step derivation
                                                1. Applied rewrites36.9%

                                                  \[\leadsto z \]
                                                2. Add Preprocessing

                                                Reproduce

                                                ?
                                                herbie shell --seed 2026092 
                                                (FPCore (x y z)
                                                  :name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
                                                  :precision binary64
                                                  (+ (* x y) (* (- 1.0 x) z)))