Data.Colour.RGBSpace.HSV:hsv from colour-2.3.3, I

Percentage Accurate: 96.4% → 97.1%
Time: 6.8s
Alternatives: 4
Speedup: 1.0×

Specification

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

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

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

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

Alternative 1: 97.1% accurate, 0.7× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x\_s \cdot \begin{array}{l} \mathbf{if}\;x\_m \leq 2 \cdot 10^{-183}:\\ \;\;\;\;\mathsf{fma}\left(x\_m \cdot z, -y, x\_m\right)\\ \mathbf{else}:\\ \;\;\;\;x\_m \cdot \left(1 - y \cdot z\right)\\ \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (*
  x_s
  (if (<= x_m 2e-183) (fma (* x_m z) (- y) x_m) (* x_m (- 1.0 (* y z))))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (x_m <= 2e-183) {
		tmp = fma((x_m * z), -y, x_m);
	} else {
		tmp = x_m * (1.0 - (y * z));
	}
	return x_s * tmp;
}
x\_m = abs(x)
x\_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	tmp = 0.0
	if (x_m <= 2e-183)
		tmp = fma(Float64(x_m * z), Float64(-y), x_m);
	else
		tmp = Float64(x_m * Float64(1.0 - Float64(y * z)));
	end
	return Float64(x_s * tmp)
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[x$95$m, 2e-183], N[(N[(x$95$m * z), $MachinePrecision] * (-y) + x$95$m), $MachinePrecision], N[(x$95$m * N[(1.0 - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 2 \cdot 10^{-183}:\\
\;\;\;\;\mathsf{fma}\left(x\_m \cdot z, -y, x\_m\right)\\

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


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

    1. Initial program 93.7%

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\left(x \cdot z\right) \cdot \left(\mathsf{neg}\left(y\right)\right)} + x \cdot 1 \]
      9. *-rgt-identityN/A

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot z}, \mathsf{neg}\left(y\right), x\right) \]
      12. lower-neg.f6491.5

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

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

    if 2.00000000000000001e-183 < x

    1. Initial program 99.0%

      \[x \cdot \left(1 - y \cdot z\right) \]
    2. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 94.4% accurate, 0.4× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x\_s \cdot \begin{array}{l} \mathbf{if}\;y \cdot z \leq -200 \lor \neg \left(y \cdot z \leq 0.2\right):\\ \;\;\;\;x\_m \cdot \left(\left(-y\right) \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;x\_m \cdot 1\\ \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (*
  x_s
  (if (or (<= (* y z) -200.0) (not (<= (* y z) 0.2)))
    (* x_m (* (- y) z))
    (* x_m 1.0))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (((y * z) <= -200.0) || !((y * z) <= 0.2)) {
		tmp = x_m * (-y * z);
	} else {
		tmp = x_m * 1.0;
	}
	return x_s * tmp;
}
x\_m = abs(x)
x\_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (((y * z) <= (-200.0d0)) .or. (.not. ((y * z) <= 0.2d0))) then
        tmp = x_m * (-y * z)
    else
        tmp = x_m * 1.0d0
    end if
    code = x_s * tmp
end function
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (((y * z) <= -200.0) || !((y * z) <= 0.2)) {
		tmp = x_m * (-y * z);
	} else {
		tmp = x_m * 1.0;
	}
	return x_s * tmp;
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	tmp = 0
	if ((y * z) <= -200.0) or not ((y * z) <= 0.2):
		tmp = x_m * (-y * z)
	else:
		tmp = x_m * 1.0
	return x_s * tmp
x\_m = abs(x)
x\_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	tmp = 0.0
	if ((Float64(y * z) <= -200.0) || !(Float64(y * z) <= 0.2))
		tmp = Float64(x_m * Float64(Float64(-y) * z));
	else
		tmp = Float64(x_m * 1.0);
	end
	return Float64(x_s * tmp)
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp_2 = code(x_s, x_m, y, z)
	tmp = 0.0;
	if (((y * z) <= -200.0) || ~(((y * z) <= 0.2)))
		tmp = x_m * (-y * z);
	else
		tmp = x_m * 1.0;
	end
	tmp_2 = x_s * tmp;
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[Or[LessEqual[N[(y * z), $MachinePrecision], -200.0], N[Not[LessEqual[N[(y * z), $MachinePrecision], 0.2]], $MachinePrecision]], N[(x$95$m * N[((-y) * z), $MachinePrecision]), $MachinePrecision], N[(x$95$m * 1.0), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;y \cdot z \leq -200 \lor \neg \left(y \cdot z \leq 0.2\right):\\
\;\;\;\;x\_m \cdot \left(\left(-y\right) \cdot z\right)\\

\mathbf{else}:\\
\;\;\;\;x\_m \cdot 1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < -200 or 0.20000000000000001 < (*.f64 y z)

    1. Initial program 91.2%

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

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

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

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

        \[\leadsto x \cdot \left(\color{blue}{\left(\mathsf{neg}\left(y\right)\right)} \cdot z\right) \]
      4. lower-neg.f6490.1

        \[\leadsto x \cdot \left(\color{blue}{\left(-y\right)} \cdot z\right) \]
    5. Applied rewrites90.1%

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

    if -200 < (*.f64 y z) < 0.20000000000000001

    1. Initial program 100.0%

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

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

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

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

    Alternative 3: 96.4% accurate, 1.0× speedup?

    \[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x\_s \cdot \left(x\_m \cdot \left(1 - y \cdot z\right)\right) \end{array} \]
    x\_m = (fabs.f64 x)
    x\_s = (copysign.f64 #s(literal 1 binary64) x)
    NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
    (FPCore (x_s x_m y z) :precision binary64 (* x_s (* x_m (- 1.0 (* y z)))))
    x\_m = fabs(x);
    x\_s = copysign(1.0, x);
    assert(x_m < y && y < z);
    double code(double x_s, double x_m, double y, double z) {
    	return x_s * (x_m * (1.0 - (y * z)));
    }
    
    x\_m = abs(x)
    x\_s = copysign(1.0d0, x)
    NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
    real(8) function code(x_s, x_m, y, z)
        real(8), intent (in) :: x_s
        real(8), intent (in) :: x_m
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        code = x_s * (x_m * (1.0d0 - (y * z)))
    end function
    
    x\_m = Math.abs(x);
    x\_s = Math.copySign(1.0, x);
    assert x_m < y && y < z;
    public static double code(double x_s, double x_m, double y, double z) {
    	return x_s * (x_m * (1.0 - (y * z)));
    }
    
    x\_m = math.fabs(x)
    x\_s = math.copysign(1.0, x)
    [x_m, y, z] = sort([x_m, y, z])
    def code(x_s, x_m, y, z):
    	return x_s * (x_m * (1.0 - (y * z)))
    
    x\_m = abs(x)
    x\_s = copysign(1.0, x)
    x_m, y, z = sort([x_m, y, z])
    function code(x_s, x_m, y, z)
    	return Float64(x_s * Float64(x_m * Float64(1.0 - Float64(y * z))))
    end
    
    x\_m = abs(x);
    x\_s = sign(x) * abs(1.0);
    x_m, y, z = num2cell(sort([x_m, y, z])){:}
    function tmp = code(x_s, x_m, y, z)
    	tmp = x_s * (x_m * (1.0 - (y * z)));
    end
    
    x\_m = N[Abs[x], $MachinePrecision]
    x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
    code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * N[(x$95$m * N[(1.0 - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    x\_m = \left|x\right|
    \\
    x\_s = \mathsf{copysign}\left(1, x\right)
    \\
    [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
    \\
    x\_s \cdot \left(x\_m \cdot \left(1 - y \cdot z\right)\right)
    \end{array}
    
    Derivation
    1. Initial program 95.8%

      \[x \cdot \left(1 - y \cdot z\right) \]
    2. Add Preprocessing
    3. Add Preprocessing

    Alternative 4: 50.6% accurate, 2.3× speedup?

    \[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x\_s \cdot \left(x\_m \cdot 1\right) \end{array} \]
    x\_m = (fabs.f64 x)
    x\_s = (copysign.f64 #s(literal 1 binary64) x)
    NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
    (FPCore (x_s x_m y z) :precision binary64 (* x_s (* x_m 1.0)))
    x\_m = fabs(x);
    x\_s = copysign(1.0, x);
    assert(x_m < y && y < z);
    double code(double x_s, double x_m, double y, double z) {
    	return x_s * (x_m * 1.0);
    }
    
    x\_m = abs(x)
    x\_s = copysign(1.0d0, x)
    NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
    real(8) function code(x_s, x_m, y, z)
        real(8), intent (in) :: x_s
        real(8), intent (in) :: x_m
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        code = x_s * (x_m * 1.0d0)
    end function
    
    x\_m = Math.abs(x);
    x\_s = Math.copySign(1.0, x);
    assert x_m < y && y < z;
    public static double code(double x_s, double x_m, double y, double z) {
    	return x_s * (x_m * 1.0);
    }
    
    x\_m = math.fabs(x)
    x\_s = math.copysign(1.0, x)
    [x_m, y, z] = sort([x_m, y, z])
    def code(x_s, x_m, y, z):
    	return x_s * (x_m * 1.0)
    
    x\_m = abs(x)
    x\_s = copysign(1.0, x)
    x_m, y, z = sort([x_m, y, z])
    function code(x_s, x_m, y, z)
    	return Float64(x_s * Float64(x_m * 1.0))
    end
    
    x\_m = abs(x);
    x\_s = sign(x) * abs(1.0);
    x_m, y, z = num2cell(sort([x_m, y, z])){:}
    function tmp = code(x_s, x_m, y, z)
    	tmp = x_s * (x_m * 1.0);
    end
    
    x\_m = N[Abs[x], $MachinePrecision]
    x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
    code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * N[(x$95$m * 1.0), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    x\_m = \left|x\right|
    \\
    x\_s = \mathsf{copysign}\left(1, x\right)
    \\
    [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
    \\
    x\_s \cdot \left(x\_m \cdot 1\right)
    \end{array}
    
    Derivation
    1. Initial program 95.8%

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

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

        \[\leadsto x \cdot \color{blue}{1} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024337 
      (FPCore (x y z)
        :name "Data.Colour.RGBSpace.HSV:hsv from colour-2.3.3, I"
        :precision binary64
        (* x (- 1.0 (* y z))))