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

Percentage Accurate: 97.8% → 99.9%
Time: 5.0s
Alternatives: 7
Speedup: 0.8×

Specification

?
\[\begin{array}{l} \\ x \cdot y + \left(1 - x\right) \cdot z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * 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 = (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]
\begin{array}{l}

\\
x \cdot y + \left(1 - x\right) \cdot z
\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 7 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: 97.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot y + \left(1 - x\right) \cdot z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * 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 = (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]
\begin{array}{l}

\\
x \cdot y + \left(1 - x\right) \cdot z
\end{array}

Alternative 1: 99.9% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
t_0 := \left(1 - x\right) \cdot z\\
\mathbf{if}\;x \cdot y + t\_0 \leq 2 \cdot 10^{+306}:\\
\;\;\;\;\mathsf{fma}\left(y, x, t\_0\right)\\

\mathbf{else}:\\
\;\;\;\;\left(y - z\right) \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 x y) (*.f64 (-.f64 #s(literal 1 binary64) x) z)) < 2.00000000000000003e306

    1. Initial program 100.0%

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(y, x, \color{blue}{\left(1 - x\right) \cdot z}\right) \]
      8. lift--.f64100.0

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

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

    if 2.00000000000000003e306 < (+.f64 (*.f64 x y) (*.f64 (-.f64 #s(literal 1 binary64) x) z))

    1. Initial program 71.4%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \left(z + -1 \cdot y\right)\right)} \]
    4. Applied rewrites100.0%

      \[\leadsto \color{blue}{\left(-x\right) \cdot \left(z - y\right)} \]
    5. Step-by-step derivation
      1. *-commutative100.0

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

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

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

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

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

        \[\leadsto \left(-1 \cdot x\right) \cdot z - \color{blue}{\left(-1 \cdot x\right) \cdot y} \]
      7. associate-*r*N/A

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

        \[\leadsto -1 \cdot \left(x \cdot z\right) - \left(\mathsf{neg}\left(x\right)\right) \cdot y \]
      9. fp-cancel-sign-sub-invN/A

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

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

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

        \[\leadsto x \cdot \left(-1 \cdot z\right) + x \cdot y \]
      13. distribute-lft-inN/A

        \[\leadsto x \cdot \color{blue}{\left(-1 \cdot z + y\right)} \]
      14. +-commutativeN/A

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

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

        \[\leadsto \left(y + -1 \cdot z\right) \cdot \color{blue}{x} \]
      17. fp-cancel-sign-sub-invN/A

        \[\leadsto \left(y - \left(\mathsf{neg}\left(-1\right)\right) \cdot z\right) \cdot x \]
      18. metadata-evalN/A

        \[\leadsto \left(y - 1 \cdot z\right) \cdot x \]
      19. *-lft-identityN/A

        \[\leadsto \left(y - z\right) \cdot x \]
      20. lower--.f64100.0

        \[\leadsto \left(y - z\right) \cdot x \]
    6. Applied rewrites100.0%

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

Alternative 2: 98.6% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.45 \cdot 10^{-18}\right):\\ \;\;\;\;\left(y - z\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -1.0) (not (<= x 1.45e-18))) (* (- y z) x) (fma y x z)))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.45e-18)) {
		tmp = (y - z) * x;
	} else {
		tmp = fma(y, x, z);
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if ((x <= -1.0) || !(x <= 1.45e-18))
		tmp = Float64(Float64(y - z) * x);
	else
		tmp = fma(y, x, z);
	end
	return tmp
end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.45e-18]], $MachinePrecision]], N[(N[(y - z), $MachinePrecision] * x), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.45 \cdot 10^{-18}\right):\\
\;\;\;\;\left(y - z\right) \cdot x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1 or 1.45e-18 < x

    1. Initial program 93.5%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \left(z + -1 \cdot y\right)\right)} \]
    4. Applied rewrites99.0%

      \[\leadsto \color{blue}{\left(-x\right) \cdot \left(z - y\right)} \]
    5. Step-by-step derivation
      1. *-commutative99.0

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

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

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

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

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

        \[\leadsto \left(-1 \cdot x\right) \cdot z - \color{blue}{\left(-1 \cdot x\right) \cdot y} \]
      7. associate-*r*N/A

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

        \[\leadsto -1 \cdot \left(x \cdot z\right) - \left(\mathsf{neg}\left(x\right)\right) \cdot y \]
      9. fp-cancel-sign-sub-invN/A

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

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

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

        \[\leadsto x \cdot \left(-1 \cdot z\right) + x \cdot y \]
      13. distribute-lft-inN/A

        \[\leadsto x \cdot \color{blue}{\left(-1 \cdot z + y\right)} \]
      14. +-commutativeN/A

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

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

        \[\leadsto \left(y + -1 \cdot z\right) \cdot \color{blue}{x} \]
      17. fp-cancel-sign-sub-invN/A

        \[\leadsto \left(y - \left(\mathsf{neg}\left(-1\right)\right) \cdot z\right) \cdot x \]
      18. metadata-evalN/A

        \[\leadsto \left(y - 1 \cdot z\right) \cdot x \]
      19. *-lft-identityN/A

        \[\leadsto \left(y - z\right) \cdot x \]
      20. lower--.f6499.0

        \[\leadsto \left(y - z\right) \cdot x \]
    6. Applied rewrites99.0%

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

    if -1 < x < 1.45e-18

    1. Initial program 100.0%

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

      \[\leadsto x \cdot y + \color{blue}{z} \]
    4. Step-by-step derivation
      1. Applied rewrites99.9%

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

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

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

          \[\leadsto \color{blue}{y \cdot x} + z \]
        4. lower-fma.f6499.9

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, z\right)} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification99.5%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1.45 \cdot 10^{-18}\right):\\ \;\;\;\;\left(y - z\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 86.9% accurate, 0.8× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -2.3 \cdot 10^{+83} \lor \neg \left(z \leq 3.4 \cdot 10^{+15}\right):\\ \;\;\;\;\left(1 - x\right) \cdot z\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\ \end{array} \end{array} \]
    (FPCore (x y z)
     :precision binary64
     (if (or (<= z -2.3e+83) (not (<= z 3.4e+15))) (* (- 1.0 x) z) (fma y x z)))
    double code(double x, double y, double z) {
    	double tmp;
    	if ((z <= -2.3e+83) || !(z <= 3.4e+15)) {
    		tmp = (1.0 - x) * z;
    	} else {
    		tmp = fma(y, x, z);
    	}
    	return tmp;
    }
    
    function code(x, y, z)
    	tmp = 0.0
    	if ((z <= -2.3e+83) || !(z <= 3.4e+15))
    		tmp = Float64(Float64(1.0 - x) * z);
    	else
    		tmp = fma(y, x, z);
    	end
    	return tmp
    end
    
    code[x_, y_, z_] := If[Or[LessEqual[z, -2.3e+83], N[Not[LessEqual[z, 3.4e+15]], $MachinePrecision]], N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;z \leq -2.3 \cdot 10^{+83} \lor \neg \left(z \leq 3.4 \cdot 10^{+15}\right):\\
    \;\;\;\;\left(1 - x\right) \cdot z\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if z < -2.29999999999999995e83 or 3.4e15 < z

      1. Initial program 93.9%

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

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

          \[\leadsto \left(1 - x\right) \cdot \color{blue}{z} \]
        2. lift-*.f64N/A

          \[\leadsto \left(1 - x\right) \cdot \color{blue}{z} \]
        3. lift--.f6490.3

          \[\leadsto \left(1 - x\right) \cdot z \]
      5. Applied rewrites90.3%

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

      if -2.29999999999999995e83 < z < 3.4e15

      1. Initial program 99.3%

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

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

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

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

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

            \[\leadsto \color{blue}{y \cdot x} + z \]
          4. lower-fma.f6493.1

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

          \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, z\right)} \]
      5. Recombined 2 regimes into one program.
      6. Final simplification91.8%

        \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -2.3 \cdot 10^{+83} \lor \neg \left(z \leq 3.4 \cdot 10^{+15}\right):\\ \;\;\;\;\left(1 - x\right) \cdot z\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\ \end{array} \]
      7. Add Preprocessing

      Alternative 4: 76.2% accurate, 0.8× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2.05 \cdot 10^{+234} \lor \neg \left(x \leq 1.35 \cdot 10^{+67}\right):\\ \;\;\;\;\left(-x\right) \cdot z\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\ \end{array} \end{array} \]
      (FPCore (x y z)
       :precision binary64
       (if (or (<= x -2.05e+234) (not (<= x 1.35e+67))) (* (- x) z) (fma y x z)))
      double code(double x, double y, double z) {
      	double tmp;
      	if ((x <= -2.05e+234) || !(x <= 1.35e+67)) {
      		tmp = -x * z;
      	} else {
      		tmp = fma(y, x, z);
      	}
      	return tmp;
      }
      
      function code(x, y, z)
      	tmp = 0.0
      	if ((x <= -2.05e+234) || !(x <= 1.35e+67))
      		tmp = Float64(Float64(-x) * z);
      	else
      		tmp = fma(y, x, z);
      	end
      	return tmp
      end
      
      code[x_, y_, z_] := If[Or[LessEqual[x, -2.05e+234], N[Not[LessEqual[x, 1.35e+67]], $MachinePrecision]], N[((-x) * z), $MachinePrecision], N[(y * x + z), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;x \leq -2.05 \cdot 10^{+234} \lor \neg \left(x \leq 1.35 \cdot 10^{+67}\right):\\
      \;\;\;\;\left(-x\right) \cdot z\\
      
      \mathbf{else}:\\
      \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if x < -2.04999999999999987e234 or 1.35e67 < x

        1. Initial program 92.5%

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

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

            \[\leadsto \left(1 - x\right) \cdot \color{blue}{z} \]
          2. lift-*.f64N/A

            \[\leadsto \left(1 - x\right) \cdot \color{blue}{z} \]
          3. lift--.f6466.5

            \[\leadsto \left(1 - x\right) \cdot z \]
        5. Applied rewrites66.5%

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

          \[\leadsto \left(-1 \cdot x\right) \cdot z \]
        7. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot z \]
          2. lower-neg.f6466.5

            \[\leadsto \left(-x\right) \cdot z \]
        8. Applied rewrites66.5%

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

        if -2.04999999999999987e234 < x < 1.35e67

        1. Initial program 98.0%

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

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

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

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

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

              \[\leadsto \color{blue}{y \cdot x} + z \]
            4. lower-fma.f6489.3

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

            \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, z\right)} \]
        5. Recombined 2 regimes into one program.
        6. Final simplification84.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.05 \cdot 10^{+234} \lor \neg \left(x \leq 1.35 \cdot 10^{+67}\right):\\ \;\;\;\;\left(-x\right) \cdot z\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, z\right)\\ \end{array} \]
        7. Add Preprocessing

        Alternative 5: 62.0% accurate, 0.9× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -4200 \lor \neg \left(x \leq 1.75 \cdot 10^{-45}\right):\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \end{array} \]
        (FPCore (x y z)
         :precision binary64
         (if (or (<= x -4200.0) (not (<= x 1.75e-45))) (* y x) z))
        double code(double x, double y, double z) {
        	double tmp;
        	if ((x <= -4200.0) || !(x <= 1.75e-45)) {
        		tmp = y * x;
        	} else {
        		tmp = z;
        	}
        	return tmp;
        }
        
        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
            real(8) :: tmp
            if ((x <= (-4200.0d0)) .or. (.not. (x <= 1.75d-45))) then
                tmp = y * x
            else
                tmp = z
            end if
            code = tmp
        end function
        
        public static double code(double x, double y, double z) {
        	double tmp;
        	if ((x <= -4200.0) || !(x <= 1.75e-45)) {
        		tmp = y * x;
        	} else {
        		tmp = z;
        	}
        	return tmp;
        }
        
        def code(x, y, z):
        	tmp = 0
        	if (x <= -4200.0) or not (x <= 1.75e-45):
        		tmp = y * x
        	else:
        		tmp = z
        	return tmp
        
        function code(x, y, z)
        	tmp = 0.0
        	if ((x <= -4200.0) || !(x <= 1.75e-45))
        		tmp = Float64(y * x);
        	else
        		tmp = z;
        	end
        	return tmp
        end
        
        function tmp_2 = code(x, y, z)
        	tmp = 0.0;
        	if ((x <= -4200.0) || ~((x <= 1.75e-45)))
        		tmp = y * x;
        	else
        		tmp = z;
        	end
        	tmp_2 = tmp;
        end
        
        code[x_, y_, z_] := If[Or[LessEqual[x, -4200.0], N[Not[LessEqual[x, 1.75e-45]], $MachinePrecision]], N[(y * x), $MachinePrecision], z]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;x \leq -4200 \lor \neg \left(x \leq 1.75 \cdot 10^{-45}\right):\\
        \;\;\;\;y \cdot x\\
        
        \mathbf{else}:\\
        \;\;\;\;z\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if x < -4200 or 1.75e-45 < x

          1. Initial program 93.8%

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

            \[\leadsto \color{blue}{x \cdot y} \]
          4. Step-by-step derivation
            1. *-commutativeN/A

              \[\leadsto y \cdot \color{blue}{x} \]
            2. lower-*.f6458.1

              \[\leadsto y \cdot \color{blue}{x} \]
          5. Applied rewrites58.1%

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

          if -4200 < x < 1.75e-45

          1. Initial program 100.0%

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

            \[\leadsto \color{blue}{z} \]
          4. Step-by-step derivation
            1. Applied rewrites71.8%

              \[\leadsto \color{blue}{z} \]
          5. Recombined 2 regimes into one program.
          6. Final simplification64.9%

            \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4200 \lor \neg \left(x \leq 1.75 \cdot 10^{-45}\right):\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
          7. Add Preprocessing

          Alternative 6: 76.5% accurate, 2.4× speedup?

          \[\begin{array}{l} \\ \mathsf{fma}\left(y, x, z\right) \end{array} \]
          (FPCore (x y z) :precision binary64 (fma y x z))
          double code(double x, double y, double z) {
          	return fma(y, x, z);
          }
          
          function code(x, y, z)
          	return fma(y, x, z)
          end
          
          code[x_, y_, z_] := N[(y * x + z), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \mathsf{fma}\left(y, x, z\right)
          \end{array}
          
          Derivation
          1. Initial program 96.9%

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

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

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

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

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

                \[\leadsto \color{blue}{y \cdot x} + z \]
              4. lower-fma.f6479.3

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

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

            Alternative 7: 36.8% accurate, 17.0× speedup?

            \[\begin{array}{l} \\ z \end{array} \]
            (FPCore (x y z) :precision binary64 z)
            double code(double x, double y, double z) {
            	return 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 = 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
            
            \begin{array}{l}
            
            \\
            z
            \end{array}
            
            Derivation
            1. Initial program 96.9%

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

              \[\leadsto \color{blue}{z} \]
            4. Step-by-step derivation
              1. Applied rewrites37.6%

                \[\leadsto \color{blue}{z} \]
              2. Final simplification37.6%

                \[\leadsto z \]
              3. Add Preprocessing

              Reproduce

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