Data.Colour.CIE:lightness from colour-2.3.3

Percentage Accurate: 100.0% → 100.0%
Time: 2.0s
Alternatives: 3
Speedup: 1.3×

Specification

?
\[\begin{array}{l} \\ x \cdot 116 - 16 \end{array} \]
(FPCore (x) :precision binary64 (- (* x 116.0) 16.0))
double code(double x) {
	return (x * 116.0) - 16.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (x * 116.0d0) - 16.0d0
end function
public static double code(double x) {
	return (x * 116.0) - 16.0;
}
def code(x):
	return (x * 116.0) - 16.0
function code(x)
	return Float64(Float64(x * 116.0) - 16.0)
end
function tmp = code(x)
	tmp = (x * 116.0) - 16.0;
end
code[x_] := N[(N[(x * 116.0), $MachinePrecision] - 16.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot 116 - 16
\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 3 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} \\ x \cdot 116 - 16 \end{array} \]
(FPCore (x) :precision binary64 (- (* x 116.0) 16.0))
double code(double x) {
	return (x * 116.0) - 16.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (x * 116.0d0) - 16.0d0
end function
public static double code(double x) {
	return (x * 116.0) - 16.0;
}
def code(x):
	return (x * 116.0) - 16.0
function code(x)
	return Float64(Float64(x * 116.0) - 16.0)
end
function tmp = code(x)
	tmp = (x * 116.0) - 16.0;
end
code[x_] := N[(N[(x * 116.0), $MachinePrecision] - 16.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot 116 - 16
\end{array}

Alternative 1: 100.0% accurate, 1.3× speedup?

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

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

    \[x \cdot 116 - 16 \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \color{blue}{x \cdot 116 + \left(\mathsf{neg}\left(16\right)\right)} \]
    2. accelerator-lowering-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 116, \mathsf{neg}\left(16\right)\right)} \]
    3. metadata-eval100.0

      \[\leadsto \mathsf{fma}\left(x, 116, \color{blue}{-16}\right) \]
  4. Applied egg-rr100.0%

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

Alternative 2: 97.9% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot 116 \leq -100:\\ \;\;\;\;x \cdot 116\\ \mathbf{elif}\;x \cdot 116 \leq 0.04:\\ \;\;\;\;-16\\ \mathbf{else}:\\ \;\;\;\;x \cdot 116\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (* x 116.0) -100.0)
   (* x 116.0)
   (if (<= (* x 116.0) 0.04) -16.0 (* x 116.0))))
double code(double x) {
	double tmp;
	if ((x * 116.0) <= -100.0) {
		tmp = x * 116.0;
	} else if ((x * 116.0) <= 0.04) {
		tmp = -16.0;
	} else {
		tmp = x * 116.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x * 116.0d0) <= (-100.0d0)) then
        tmp = x * 116.0d0
    else if ((x * 116.0d0) <= 0.04d0) then
        tmp = -16.0d0
    else
        tmp = x * 116.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x * 116.0) <= -100.0) {
		tmp = x * 116.0;
	} else if ((x * 116.0) <= 0.04) {
		tmp = -16.0;
	} else {
		tmp = x * 116.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x * 116.0) <= -100.0:
		tmp = x * 116.0
	elif (x * 116.0) <= 0.04:
		tmp = -16.0
	else:
		tmp = x * 116.0
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(x * 116.0) <= -100.0)
		tmp = Float64(x * 116.0);
	elseif (Float64(x * 116.0) <= 0.04)
		tmp = -16.0;
	else
		tmp = Float64(x * 116.0);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x * 116.0) <= -100.0)
		tmp = x * 116.0;
	elseif ((x * 116.0) <= 0.04)
		tmp = -16.0;
	else
		tmp = x * 116.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(x * 116.0), $MachinePrecision], -100.0], N[(x * 116.0), $MachinePrecision], If[LessEqual[N[(x * 116.0), $MachinePrecision], 0.04], -16.0, N[(x * 116.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot 116 \leq -100:\\
\;\;\;\;x \cdot 116\\

\mathbf{elif}\;x \cdot 116 \leq 0.04:\\
\;\;\;\;-16\\

\mathbf{else}:\\
\;\;\;\;x \cdot 116\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x #s(literal 116 binary64)) < -100 or 0.0400000000000000008 < (*.f64 x #s(literal 116 binary64))

    1. Initial program 100.0%

      \[x \cdot 116 - 16 \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{116 \cdot x} \]
    4. Step-by-step derivation
      1. *-lowering-*.f6497.0

        \[\leadsto \color{blue}{116 \cdot x} \]
    5. Simplified97.0%

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

    if -100 < (*.f64 x #s(literal 116 binary64)) < 0.0400000000000000008

    1. Initial program 100.0%

      \[x \cdot 116 - 16 \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{-16} \]
    4. Step-by-step derivation
      1. Simplified97.0%

        \[\leadsto \color{blue}{-16} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification97.0%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot 116 \leq -100:\\ \;\;\;\;x \cdot 116\\ \mathbf{elif}\;x \cdot 116 \leq 0.04:\\ \;\;\;\;-16\\ \mathbf{else}:\\ \;\;\;\;x \cdot 116\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 49.7% accurate, 9.0× speedup?

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

      \[x \cdot 116 - 16 \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{-16} \]
    4. Step-by-step derivation
      1. Simplified46.3%

        \[\leadsto \color{blue}{-16} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024204 
      (FPCore (x)
        :name "Data.Colour.CIE:lightness from colour-2.3.3"
        :precision binary64
        (- (* x 116.0) 16.0))