Data.Colour.SRGB:transferFunction from colour-2.3.3

Percentage Accurate: 100.0% → 100.0%
Time: 2.8s
Alternatives: 7
Speedup: 1.0×

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 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: 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, 0.1× speedup?

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

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

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

      \[\leadsto \color{blue}{\left(x + 1\right) \cdot y + \left(-x\right)} \]
    2. *-commutative100.0%

      \[\leadsto \color{blue}{y \cdot \left(x + 1\right)} + \left(-x\right) \]
    3. +-commutative100.0%

      \[\leadsto y \cdot \color{blue}{\left(1 + x\right)} + \left(-x\right) \]
    4. distribute-lft-in100.0%

      \[\leadsto \color{blue}{\left(y \cdot 1 + y \cdot x\right)} + \left(-x\right) \]
    5. *-rgt-identity100.0%

      \[\leadsto \left(\color{blue}{y} + y \cdot x\right) + \left(-x\right) \]
    6. associate-+l+100.0%

      \[\leadsto \color{blue}{y + \left(y \cdot x + \left(-x\right)\right)} \]
    7. *-commutative100.0%

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

      \[\leadsto \color{blue}{\left(x \cdot y + \left(-x\right)\right) + y} \]
    9. *-commutative100.0%

      \[\leadsto \left(\color{blue}{y \cdot x} + \left(-x\right)\right) + y \]
    10. neg-mul-1100.0%

      \[\leadsto \left(y \cdot x + \color{blue}{-1 \cdot x}\right) + y \]
    11. distribute-rgt-out100.0%

      \[\leadsto \color{blue}{x \cdot \left(y + -1\right)} + y \]
    12. fma-define100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y + -1, y\right)} \]
  3. Simplified100.0%

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

Alternative 2: 75.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -7.5 \cdot 10^{+82} \lor \neg \left(y \leq 2.6 \cdot 10^{+66} \lor \neg \left(y \leq 2.8 \cdot 10^{+188}\right) \land y \leq 1.7 \cdot 10^{+245}\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;y - x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= y -7.5e+82)
         (not (or (<= y 2.6e+66) (and (not (<= y 2.8e+188)) (<= y 1.7e+245)))))
   (* x y)
   (- y x)))
double code(double x, double y) {
	double tmp;
	if ((y <= -7.5e+82) || !((y <= 2.6e+66) || (!(y <= 2.8e+188) && (y <= 1.7e+245)))) {
		tmp = x * y;
	} 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 <= (-7.5d+82)) .or. (.not. (y <= 2.6d+66) .or. (.not. (y <= 2.8d+188)) .and. (y <= 1.7d+245))) then
        tmp = x * y
    else
        tmp = y - x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y <= -7.5e+82) || !((y <= 2.6e+66) || (!(y <= 2.8e+188) && (y <= 1.7e+245)))) {
		tmp = x * y;
	} else {
		tmp = y - x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y <= -7.5e+82) or not ((y <= 2.6e+66) or (not (y <= 2.8e+188) and (y <= 1.7e+245))):
		tmp = x * y
	else:
		tmp = y - x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y <= -7.5e+82) || !((y <= 2.6e+66) || (!(y <= 2.8e+188) && (y <= 1.7e+245))))
		tmp = Float64(x * y);
	else
		tmp = Float64(y - x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= -7.5e+82) || ~(((y <= 2.6e+66) || (~((y <= 2.8e+188)) && (y <= 1.7e+245)))))
		tmp = x * y;
	else
		tmp = y - x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[y, -7.5e+82], N[Not[Or[LessEqual[y, 2.6e+66], And[N[Not[LessEqual[y, 2.8e+188]], $MachinePrecision], LessEqual[y, 1.7e+245]]]], $MachinePrecision]], N[(x * y), $MachinePrecision], N[(y - x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -7.5 \cdot 10^{+82} \lor \neg \left(y \leq 2.6 \cdot 10^{+66} \lor \neg \left(y \leq 2.8 \cdot 10^{+188}\right) \land y \leq 1.7 \cdot 10^{+245}\right):\\
\;\;\;\;x \cdot y\\

\mathbf{else}:\\
\;\;\;\;y - x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -7.4999999999999999e82 or 2.60000000000000012e66 < y < 2.7999999999999998e188 or 1.69999999999999999e245 < y

    1. Initial program 100.0%

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

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

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

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

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

    if -7.4999999999999999e82 < y < 2.60000000000000012e66 or 2.7999999999999998e188 < y < 1.69999999999999999e245

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{y} - x \]
  3. Recombined 2 regimes into one program.
  4. Final simplification83.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -7.5 \cdot 10^{+82} \lor \neg \left(y \leq 2.6 \cdot 10^{+66} \lor \neg \left(y \leq 2.8 \cdot 10^{+188}\right) \land y \leq 1.7 \cdot 10^{+245}\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;y - x\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 99.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\ \;\;\;\;x \cdot y - x\\ \mathbf{else}:\\ \;\;\;\;y - x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= x -1.0) (not (<= x 1.0))) (- (* x y) x) (- y x)))
double code(double x, double y) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.0)) {
		tmp = (x * y) - 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 ((x <= (-1.0d0)) .or. (.not. (x <= 1.0d0))) then
        tmp = (x * y) - x
    else
        tmp = y - x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.0)) {
		tmp = (x * y) - x;
	} else {
		tmp = y - x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (x <= -1.0) or not (x <= 1.0):
		tmp = (x * y) - x
	else:
		tmp = y - x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((x <= -1.0) || !(x <= 1.0))
		tmp = Float64(Float64(x * y) - x);
	else
		tmp = Float64(y - x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x <= -1.0) || ~((x <= 1.0)))
		tmp = (x * y) - x;
	else
		tmp = y - x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(N[(x * y), $MachinePrecision] - x), $MachinePrecision], N[(y - x), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;y - x\\


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

    1. Initial program 100.0%

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

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

        \[\leadsto \color{blue}{y \cdot x} - x \]
    5. Simplified98.0%

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

    if -1 < x < 1

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{y} - x \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\ \;\;\;\;x \cdot y - x\\ \mathbf{else}:\\ \;\;\;\;y - x\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 61.8% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -11000 \lor \neg \left(y \leq 72\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= y -11000.0) (not (<= y 72.0))) (* x y) (- x)))
double code(double x, double y) {
	double tmp;
	if ((y <= -11000.0) || !(y <= 72.0)) {
		tmp = x * y;
	} else {
		tmp = -x;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y <= (-11000.0d0)) .or. (.not. (y <= 72.0d0))) then
        tmp = x * y
    else
        tmp = -x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y <= -11000.0) || !(y <= 72.0)) {
		tmp = x * y;
	} else {
		tmp = -x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y <= -11000.0) or not (y <= 72.0):
		tmp = x * y
	else:
		tmp = -x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y <= -11000.0) || !(y <= 72.0))
		tmp = Float64(x * y);
	else
		tmp = Float64(-x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= -11000.0) || ~((y <= 72.0)))
		tmp = x * y;
	else
		tmp = -x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[y, -11000.0], N[Not[LessEqual[y, 72.0]], $MachinePrecision]], N[(x * y), $MachinePrecision], (-x)]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -11000 \lor \neg \left(y \leq 72\right):\\
\;\;\;\;x \cdot y\\

\mathbf{else}:\\
\;\;\;\;-x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -11000 or 72 < y

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{x \cdot y} - x \]
    4. Step-by-step derivation
      1. *-commutative55.9%

        \[\leadsto \color{blue}{y \cdot x} - x \]
    5. Simplified55.9%

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

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

    if -11000 < y < 72

    1. Initial program 100.0%

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

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

        \[\leadsto \color{blue}{y \cdot x} - x \]
    5. Simplified78.1%

      \[\leadsto \color{blue}{y \cdot x} - x \]
    6. Taylor expanded in y around 0 77.7%

      \[\leadsto \color{blue}{-1 \cdot x} \]
    7. Step-by-step derivation
      1. neg-mul-177.7%

        \[\leadsto \color{blue}{-x} \]
    8. Simplified77.7%

      \[\leadsto \color{blue}{-x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification67.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -11000 \lor \neg \left(y \leq 72\right):\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 100.0% accurate, 1.0× speedup?

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

\\
\left(y + x \cdot y\right) - x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x + 1\right) \cdot y - x \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. *-commutative100.0%

      \[\leadsto \color{blue}{y \cdot \left(x + 1\right)} - x \]
    2. distribute-lft-in100.0%

      \[\leadsto \color{blue}{\left(y \cdot x + y \cdot 1\right)} - x \]
    3. *-commutative100.0%

      \[\leadsto \left(y \cdot x + \color{blue}{1 \cdot y}\right) - x \]
    4. *-un-lft-identity100.0%

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

    \[\leadsto \color{blue}{\left(y \cdot x + y\right)} - x \]
  5. Final simplification100.0%

    \[\leadsto \left(y + x \cdot y\right) - x \]
  6. Add Preprocessing

Alternative 6: 100.0% accurate, 1.0× speedup?

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

\\
y \cdot \left(x + 1\right) - x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(x + 1\right) \cdot y - x \]
  2. Add Preprocessing
  3. Final simplification100.0%

    \[\leadsto y \cdot \left(x + 1\right) - x \]
  4. Add Preprocessing

Alternative 7: 37.8% accurate, 3.5× 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 x around inf 67.9%

    \[\leadsto \color{blue}{x \cdot y} - x \]
  4. Step-by-step derivation
    1. *-commutative67.9%

      \[\leadsto \color{blue}{y \cdot x} - x \]
  5. Simplified67.9%

    \[\leadsto \color{blue}{y \cdot x} - x \]
  6. Taylor expanded in y around 0 43.2%

    \[\leadsto \color{blue}{-1 \cdot x} \]
  7. Step-by-step derivation
    1. neg-mul-143.2%

      \[\leadsto \color{blue}{-x} \]
  8. Simplified43.2%

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

Reproduce

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