Data.Colour.SRGB:transferFunction from colour-2.3.3

Percentage Accurate: 100.0% → 100.0%
Time: 4.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.9% accurate, 0.6× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -2.6e9 or 1 < 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 \color{blue}{\left(1 + x\right) \cdot y} \]
      2. +-commutativeN/A

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

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

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

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

    if -2.6e9 < y < 1

    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 rewrites98.2%

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

    Alternative 3: 85.9% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot y - x\\ \mathbf{if}\;x \leq -3.05 \cdot 10^{-28}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 6 \cdot 10^{-36}:\\ \;\;\;\;\mathsf{fma}\left(x, y, y\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (let* ((t_0 (- (* x y) x)))
       (if (<= x -3.05e-28) t_0 (if (<= x 6e-36) (fma x y y) t_0))))
    double code(double x, double y) {
    	double t_0 = (x * y) - x;
    	double tmp;
    	if (x <= -3.05e-28) {
    		tmp = t_0;
    	} else if (x <= 6e-36) {
    		tmp = fma(x, y, y);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    function code(x, y)
    	t_0 = Float64(Float64(x * y) - x)
    	tmp = 0.0
    	if (x <= -3.05e-28)
    		tmp = t_0;
    	elseif (x <= 6e-36)
    		tmp = fma(x, y, y);
    	else
    		tmp = t_0;
    	end
    	return tmp
    end
    
    code[x_, y_] := Block[{t$95$0 = N[(N[(x * y), $MachinePrecision] - x), $MachinePrecision]}, If[LessEqual[x, -3.05e-28], t$95$0, If[LessEqual[x, 6e-36], N[(x * y + y), $MachinePrecision], t$95$0]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := x \cdot y - x\\
    \mathbf{if}\;x \leq -3.05 \cdot 10^{-28}:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;x \leq 6 \cdot 10^{-36}:\\
    \;\;\;\;\mathsf{fma}\left(x, y, y\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_0\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if x < -3.05e-28 or 6.0000000000000003e-36 < x

      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. lower-*.f6495.8

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

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

      if -3.05e-28 < x < 6.0000000000000003e-36

      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 \color{blue}{\left(1 + x\right) \cdot y} \]
        2. +-commutativeN/A

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

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

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

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

    Alternative 4: 85.6% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.25 \cdot 10^{-103}:\\ \;\;\;\;\mathsf{fma}\left(x, y, y\right)\\ \mathbf{elif}\;y \leq 1.8 \cdot 10^{-23}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, y, y\right)\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= y -1.25e-103) (fma x y y) (if (<= y 1.8e-23) (- x) (fma x y y))))
    double code(double x, double y) {
    	double tmp;
    	if (y <= -1.25e-103) {
    		tmp = fma(x, y, y);
    	} else if (y <= 1.8e-23) {
    		tmp = -x;
    	} else {
    		tmp = fma(x, y, y);
    	}
    	return tmp;
    }
    
    function code(x, y)
    	tmp = 0.0
    	if (y <= -1.25e-103)
    		tmp = fma(x, y, y);
    	elseif (y <= 1.8e-23)
    		tmp = Float64(-x);
    	else
    		tmp = fma(x, y, y);
    	end
    	return tmp
    end
    
    code[x_, y_] := If[LessEqual[y, -1.25e-103], N[(x * y + y), $MachinePrecision], If[LessEqual[y, 1.8e-23], (-x), N[(x * y + y), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq -1.25 \cdot 10^{-103}:\\
    \;\;\;\;\mathsf{fma}\left(x, y, y\right)\\
    
    \mathbf{elif}\;y \leq 1.8 \cdot 10^{-23}:\\
    \;\;\;\;-x\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{fma}\left(x, y, y\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -1.24999999999999992e-103 or 1.7999999999999999e-23 < 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 \color{blue}{\left(1 + x\right) \cdot y} \]
        2. +-commutativeN/A

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

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

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

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

      if -1.24999999999999992e-103 < y < 1.7999999999999999e-23

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

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

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

    Alternative 5: 61.7% accurate, 0.7× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
    (FPCore (x y)
     :precision binary64
     (if (<= y -1.0) (* x y) (if (<= y 1.0) (- x) (* x y))))
    double code(double x, double y) {
    	double tmp;
    	if (y <= -1.0) {
    		tmp = x * y;
    	} else if (y <= 1.0) {
    		tmp = -x;
    	} else {
    		tmp = x * y;
    	}
    	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 = x * y
        else if (y <= 1.0d0) then
            tmp = -x
        else
            tmp = x * y
        end if
        code = tmp
    end function
    
    public static double code(double x, double y) {
    	double tmp;
    	if (y <= -1.0) {
    		tmp = x * y;
    	} else if (y <= 1.0) {
    		tmp = -x;
    	} else {
    		tmp = x * y;
    	}
    	return tmp;
    }
    
    def code(x, y):
    	tmp = 0
    	if y <= -1.0:
    		tmp = x * y
    	elif y <= 1.0:
    		tmp = -x
    	else:
    		tmp = x * y
    	return tmp
    
    function code(x, y)
    	tmp = 0.0
    	if (y <= -1.0)
    		tmp = Float64(x * y);
    	elseif (y <= 1.0)
    		tmp = Float64(-x);
    	else
    		tmp = Float64(x * y);
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y)
    	tmp = 0.0;
    	if (y <= -1.0)
    		tmp = x * y;
    	elseif (y <= 1.0)
    		tmp = -x;
    	else
    		tmp = x * y;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_] := If[LessEqual[y, -1.0], N[(x * y), $MachinePrecision], If[LessEqual[y, 1.0], (-x), N[(x * y), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;y \leq -1:\\
    \;\;\;\;x \cdot y\\
    
    \mathbf{elif}\;y \leq 1:\\
    \;\;\;\;-x\\
    
    \mathbf{else}:\\
    \;\;\;\;x \cdot y\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -1 or 1 < 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 \color{blue}{\left(1 + x\right) \cdot y} \]
        2. +-commutativeN/A

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

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

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

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

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

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

        if -1 < y < 1

        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.f6471.3

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

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

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

      Alternative 6: 38.8% 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.f6436.9

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

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

      Reproduce

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