Data.Colour.SRGB:transferFunction from colour-2.3.3

Percentage Accurate: 100.0% → 100.0%
Time: 5.2s
Alternatives: 6
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ \left(x + 1\right) \cdot y - x \end{array} \]
(FPCore (x y) :precision binary64 (- (* (+ x 1.0) y) x))
double code(double x, double y) {
	return ((x + 1.0) * y) - x;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = ((x + 1.0d0) * y) - x
end function
public static double code(double x, double y) {
	return ((x + 1.0) * y) - x;
}
def code(x, y):
	return ((x + 1.0) * y) - x
function code(x, y)
	return Float64(Float64(Float64(x + 1.0) * y) - x)
end
function tmp = code(x, y)
	tmp = ((x + 1.0) * y) - x;
end
code[x_, y_] := N[(N[(N[(x + 1.0), $MachinePrecision] * y), $MachinePrecision] - x), $MachinePrecision]
\begin{array}{l}

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

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

Alternative 1: 100.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y, x, y - x\right) \end{array} \]
(FPCore (x y) :precision binary64 (fma y x (- y x)))
double code(double x, double y) {
	return fma(y, x, (y - x));
}
function code(x, y)
	return fma(y, x, Float64(y - x))
end
code[x_, y_] := N[(y * x + N[(y - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y, x, y - x\right)
\end{array}
Derivation
  1. Initial program 100.0%

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

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

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

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

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

      \[\leadsto y \cdot \color{blue}{\left(1 + x\right)} - x \]
    6. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left(1 \cdot y + x \cdot y\right)} - x \]
    7. *-lft-identityN/A

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, y - x\right)} \]
    12. lower--.f64100.0

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

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

Alternative 2: 98.7% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \mathbf{elif}\;y \leq 0.00027:\\ \;\;\;\;y \cdot 1 - x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= y -1.0) (fma y x y) (if (<= y 0.00027) (- (* y 1.0) x) (fma y x y))))
double code(double x, double y) {
	double tmp;
	if (y <= -1.0) {
		tmp = fma(y, x, y);
	} else if (y <= 0.00027) {
		tmp = (y * 1.0) - x;
	} else {
		tmp = fma(y, x, y);
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (y <= -1.0)
		tmp = fma(y, x, y);
	elseif (y <= 0.00027)
		tmp = Float64(Float64(y * 1.0) - x);
	else
		tmp = fma(y, x, y);
	end
	return tmp
end
code[x_, y_] := If[LessEqual[y, -1.0], N[(y * x + y), $MachinePrecision], If[LessEqual[y, 0.00027], N[(N[(y * 1.0), $MachinePrecision] - x), $MachinePrecision], N[(y * x + y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1:\\
\;\;\;\;\mathsf{fma}\left(y, x, y\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1 or 2.70000000000000003e-4 < y

    1. Initial program 99.9%

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

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

        \[\leadsto y \cdot \color{blue}{\left(x + 1\right)} \]
      2. distribute-lft-inN/A

        \[\leadsto \color{blue}{y \cdot x + y \cdot 1} \]
      3. *-rgt-identityN/A

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

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

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

    if -1 < y < 2.70000000000000003e-4

    1. Initial program 100.0%

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

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

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \mathbf{elif}\;y \leq 0.00027:\\ \;\;\;\;y \cdot 1 - x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 86.1% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -8.2 \cdot 10^{-66}:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \mathbf{elif}\;y \leq 7.5 \cdot 10^{-16}:\\ \;\;\;\;y \cdot x - x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= y -8.2e-66)
       (fma y x y)
       (if (<= y 7.5e-16) (- (* y x) x) (fma y x y))))
    double code(double x, double y) {
    	double tmp;
    	if (y <= -8.2e-66) {
    		tmp = fma(y, x, y);
    	} else if (y <= 7.5e-16) {
    		tmp = (y * x) - x;
    	} else {
    		tmp = fma(y, x, y);
    	}
    	return tmp;
    }
    
    function code(x, y)
    	tmp = 0.0
    	if (y <= -8.2e-66)
    		tmp = fma(y, x, y);
    	elseif (y <= 7.5e-16)
    		tmp = Float64(Float64(y * x) - x);
    	else
    		tmp = fma(y, x, y);
    	end
    	return tmp
    end
    
    code[x_, y_] := If[LessEqual[y, -8.2e-66], N[(y * x + y), $MachinePrecision], If[LessEqual[y, 7.5e-16], N[(N[(y * x), $MachinePrecision] - x), $MachinePrecision], N[(y * x + y), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq -8.2 \cdot 10^{-66}:\\
    \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
    
    \mathbf{elif}\;y \leq 7.5 \cdot 10^{-16}:\\
    \;\;\;\;y \cdot x - x\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -8.19999999999999996e-66 or 7.5e-16 < y

      1. Initial program 100.0%

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

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

          \[\leadsto y \cdot \color{blue}{\left(x + 1\right)} \]
        2. distribute-lft-inN/A

          \[\leadsto \color{blue}{y \cdot x + y \cdot 1} \]
        3. *-rgt-identityN/A

          \[\leadsto y \cdot x + \color{blue}{y} \]
        4. lower-fma.f6495.6

          \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, y\right)} \]
      5. Applied rewrites95.6%

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

      if -8.19999999999999996e-66 < y < 7.5e-16

      1. Initial program 100.0%

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

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

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

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

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

    Alternative 4: 86.1% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -8.2 \cdot 10^{-66}:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \mathbf{elif}\;y \leq 7.5 \cdot 10^{-16}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= y -8.2e-66) (fma y x y) (if (<= y 7.5e-16) (- x) (fma y x y))))
    double code(double x, double y) {
    	double tmp;
    	if (y <= -8.2e-66) {
    		tmp = fma(y, x, y);
    	} else if (y <= 7.5e-16) {
    		tmp = -x;
    	} else {
    		tmp = fma(y, x, y);
    	}
    	return tmp;
    }
    
    function code(x, y)
    	tmp = 0.0
    	if (y <= -8.2e-66)
    		tmp = fma(y, x, y);
    	elseif (y <= 7.5e-16)
    		tmp = Float64(-x);
    	else
    		tmp = fma(y, x, y);
    	end
    	return tmp
    end
    
    code[x_, y_] := If[LessEqual[y, -8.2e-66], N[(y * x + y), $MachinePrecision], If[LessEqual[y, 7.5e-16], (-x), N[(y * x + y), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq -8.2 \cdot 10^{-66}:\\
    \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
    
    \mathbf{elif}\;y \leq 7.5 \cdot 10^{-16}:\\
    \;\;\;\;-x\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -8.19999999999999996e-66 or 7.5e-16 < y

      1. Initial program 100.0%

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

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

          \[\leadsto y \cdot \color{blue}{\left(x + 1\right)} \]
        2. distribute-lft-inN/A

          \[\leadsto \color{blue}{y \cdot x + y \cdot 1} \]
        3. *-rgt-identityN/A

          \[\leadsto y \cdot x + \color{blue}{y} \]
        4. lower-fma.f6495.6

          \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, y\right)} \]
      5. Applied rewrites95.6%

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

      if -8.19999999999999996e-66 < y < 7.5e-16

      1. Initial program 100.0%

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

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

          \[\leadsto \color{blue}{\mathsf{neg}\left(x\right)} \]
        2. lower-neg.f6480.3

          \[\leadsto \color{blue}{-x} \]
      5. Applied rewrites80.3%

        \[\leadsto \color{blue}{-x} \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 5: 62.2% accurate, 0.7× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 1500:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= y -1.0) (* y x) (if (<= y 1500.0) (- x) (* y x))))
    double code(double x, double y) {
    	double tmp;
    	if (y <= -1.0) {
    		tmp = y * x;
    	} else if (y <= 1500.0) {
    		tmp = -x;
    	} else {
    		tmp = y * x;
    	}
    	return tmp;
    }
    
    real(8) function code(x, y)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8) :: tmp
        if (y <= (-1.0d0)) then
            tmp = y * x
        else if (y <= 1500.0d0) then
            tmp = -x
        else
            tmp = y * x
        end if
        code = tmp
    end function
    
    public static double code(double x, double y) {
    	double tmp;
    	if (y <= -1.0) {
    		tmp = y * x;
    	} else if (y <= 1500.0) {
    		tmp = -x;
    	} else {
    		tmp = y * x;
    	}
    	return tmp;
    }
    
    def code(x, y):
    	tmp = 0
    	if y <= -1.0:
    		tmp = y * x
    	elif y <= 1500.0:
    		tmp = -x
    	else:
    		tmp = y * x
    	return tmp
    
    function code(x, y)
    	tmp = 0.0
    	if (y <= -1.0)
    		tmp = Float64(y * x);
    	elseif (y <= 1500.0)
    		tmp = Float64(-x);
    	else
    		tmp = Float64(y * x);
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y)
    	tmp = 0.0;
    	if (y <= -1.0)
    		tmp = y * x;
    	elseif (y <= 1500.0)
    		tmp = -x;
    	else
    		tmp = y * x;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_] := If[LessEqual[y, -1.0], N[(y * x), $MachinePrecision], If[LessEqual[y, 1500.0], (-x), N[(y * x), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq -1:\\
    \;\;\;\;y \cdot x\\
    
    \mathbf{elif}\;y \leq 1500:\\
    \;\;\;\;-x\\
    
    \mathbf{else}:\\
    \;\;\;\;y \cdot x\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -1 or 1500 < y

      1. Initial program 99.9%

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

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

          \[\leadsto y \cdot \color{blue}{\left(x + 1\right)} \]
        2. distribute-lft-inN/A

          \[\leadsto \color{blue}{y \cdot x + y \cdot 1} \]
        3. *-rgt-identityN/A

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, x, y\right)} \]
      6. Taylor expanded in x around inf

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

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

        if -1 < y < 1500

        1. Initial program 100.0%

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

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

            \[\leadsto \color{blue}{\mathsf{neg}\left(x\right)} \]
          2. lower-neg.f6475.2

            \[\leadsto \color{blue}{-x} \]
        5. Applied rewrites75.2%

          \[\leadsto \color{blue}{-x} \]
      8. Recombined 2 regimes into one program.
      9. Final simplification63.7%

        \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 1500:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]
      10. Add Preprocessing

      Alternative 6: 39.4% accurate, 4.0× speedup?

      \[\begin{array}{l} \\ -x \end{array} \]
      (FPCore (x y) :precision binary64 (- x))
      double code(double x, double y) {
      	return -x;
      }
      
      real(8) function code(x, y)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          code = -x
      end function
      
      public static double code(double x, double y) {
      	return -x;
      }
      
      def code(x, y):
      	return -x
      
      function code(x, y)
      	return Float64(-x)
      end
      
      function tmp = code(x, y)
      	tmp = -x;
      end
      
      code[x_, y_] := (-x)
      
      \begin{array}{l}
      
      \\
      -x
      \end{array}
      
      Derivation
      1. Initial program 100.0%

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

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

          \[\leadsto \color{blue}{\mathsf{neg}\left(x\right)} \]
        2. lower-neg.f6441.5

          \[\leadsto \color{blue}{-x} \]
      5. Applied rewrites41.5%

        \[\leadsto \color{blue}{-x} \]
      6. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024233 
      (FPCore (x y)
        :name "Data.Colour.SRGB:transferFunction from colour-2.3.3"
        :precision binary64
        (- (* (+ x 1.0) y) x))