Data.Array.Repa.Algorithms.ColorRamp:rampColorHotToCold from repa-algorithms-3.4.0.1, A

Percentage Accurate: 99.7% → 100.0%
Time: 8.9s
Alternatives: 9
Speedup: 1.5×

Specification

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

\\
1 + \frac{4 \cdot \left(\left(x + y \cdot 0.75\right) - z\right)}{y}
\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 9 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: 99.7% accurate, 1.0× speedup?

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

\\
1 + \frac{4 \cdot \left(\left(x + y \cdot 0.75\right) - z\right)}{y}
\end{array}

Alternative 1: 100.0% accurate, 1.5× speedup?

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

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

    \[1 + \frac{4 \cdot \left(\left(x + y \cdot 0.75\right) - z\right)}{y} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{1 + \left(4 \cdot \frac{x}{y} + 4 \cdot \frac{\frac{3}{4} \cdot y - z}{y}\right)} \]
  4. Applied rewrites99.8%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x - z, \frac{4}{y}, 4\right)} \]
  5. Step-by-step derivation
    1. Applied rewrites100.0%

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

    Alternative 2: 98.3% accurate, 0.4× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\left(x - z\right) \cdot 4}{y}\\ t_1 := \frac{\left(\left(0.75 \cdot y + x\right) - z\right) \cdot 4}{y}\\ \mathbf{if}\;t\_1 \leq -100000000:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;t\_1 \leq 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(\frac{z}{y}, -4, 4\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
    (FPCore (x y z)
     :precision binary64
     (let* ((t_0 (/ (* (- x z) 4.0) y)) (t_1 (/ (* (- (+ (* 0.75 y) x) z) 4.0) y)))
       (if (<= t_1 -100000000.0)
         t_0
         (if (<= t_1 1e+14) (fma (/ z y) -4.0 4.0) t_0))))
    double code(double x, double y, double z) {
    	double t_0 = ((x - z) * 4.0) / y;
    	double t_1 = ((((0.75 * y) + x) - z) * 4.0) / y;
    	double tmp;
    	if (t_1 <= -100000000.0) {
    		tmp = t_0;
    	} else if (t_1 <= 1e+14) {
    		tmp = fma((z / y), -4.0, 4.0);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    function code(x, y, z)
    	t_0 = Float64(Float64(Float64(x - z) * 4.0) / y)
    	t_1 = Float64(Float64(Float64(Float64(Float64(0.75 * y) + x) - z) * 4.0) / y)
    	tmp = 0.0
    	if (t_1 <= -100000000.0)
    		tmp = t_0;
    	elseif (t_1 <= 1e+14)
    		tmp = fma(Float64(z / y), -4.0, 4.0);
    	else
    		tmp = t_0;
    	end
    	return tmp
    end
    
    code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[(x - z), $MachinePrecision] * 4.0), $MachinePrecision] / y), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(N[(0.75 * y), $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision] * 4.0), $MachinePrecision] / y), $MachinePrecision]}, If[LessEqual[t$95$1, -100000000.0], t$95$0, If[LessEqual[t$95$1, 1e+14], N[(N[(z / y), $MachinePrecision] * -4.0 + 4.0), $MachinePrecision], t$95$0]]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \frac{\left(x - z\right) \cdot 4}{y}\\
    t_1 := \frac{\left(\left(0.75 \cdot y + x\right) - z\right) \cdot 4}{y}\\
    \mathbf{if}\;t\_1 \leq -100000000:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;t\_1 \leq 10^{+14}:\\
    \;\;\;\;\mathsf{fma}\left(\frac{z}{y}, -4, 4\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_0\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (/.f64 (*.f64 #s(literal 4 binary64) (-.f64 (+.f64 x (*.f64 y #s(literal 3/4 binary64))) z)) y) < -1e8 or 1e14 < (/.f64 (*.f64 #s(literal 4 binary64) (-.f64 (+.f64 x (*.f64 y #s(literal 3/4 binary64))) z)) y)

      1. Initial program 99.7%

        \[1 + \frac{4 \cdot \left(\left(x + y \cdot 0.75\right) - z\right)}{y} \]
      2. Add Preprocessing
      3. Taylor expanded in y around 0

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

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

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

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

      if -1e8 < (/.f64 (*.f64 #s(literal 4 binary64) (-.f64 (+.f64 x (*.f64 y #s(literal 3/4 binary64))) z)) y) < 1e14

      1. Initial program 99.8%

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

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

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

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

          \[\leadsto \frac{\color{blue}{4 \cdot \left(\left(x + y \cdot \frac{3}{4}\right) - z\right)}}{y} + 1 \]
        5. associate-/l*N/A

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

          \[\leadsto \color{blue}{\frac{\left(x + y \cdot \frac{3}{4}\right) - z}{y} \cdot 4} + 1 \]
        7. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\left(x + y \cdot \frac{3}{4}\right) - z}{y}, 4, 1\right)} \]
        8. lower-/.f6499.8

          \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\left(x + y \cdot 0.75\right) - z}{y}}, 4, 1\right) \]
        9. lift--.f64N/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\left(x + y \cdot \frac{3}{4}\right) - z}}{y}, 4, 1\right) \]
        10. lift-+.f64N/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\left(x + y \cdot \frac{3}{4}\right)} - z}{y}, 4, 1\right) \]
        11. +-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\left(y \cdot \frac{3}{4} + x\right)} - z}{y}, 4, 1\right) \]
        12. associate--l+N/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{y \cdot \frac{3}{4} + \left(x - z\right)}}{y}, 4, 1\right) \]
        13. lift-*.f64N/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{y \cdot \frac{3}{4}} + \left(x - z\right)}{y}, 4, 1\right) \]
        14. *-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\frac{3}{4} \cdot y} + \left(x - z\right)}{y}, 4, 1\right) \]
        15. lower-fma.f64N/A

          \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\mathsf{fma}\left(\frac{3}{4}, y, x - z\right)}}{y}, 4, 1\right) \]
        16. lower--.f6499.8

          \[\leadsto \mathsf{fma}\left(\frac{\mathsf{fma}\left(0.75, y, \color{blue}{x - z}\right)}{y}, 4, 1\right) \]
      4. Applied rewrites99.8%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{fma}\left(0.75, y, x - z\right)}{y}, 4, 1\right)} \]
      5. Taylor expanded in x around 0

        \[\leadsto \color{blue}{1 + 4 \cdot \frac{\frac{3}{4} \cdot y - z}{y}} \]
      6. Step-by-step derivation
        1. div-subN/A

          \[\leadsto 1 + 4 \cdot \color{blue}{\left(\frac{\frac{3}{4} \cdot y}{y} - \frac{z}{y}\right)} \]
        2. sub-negN/A

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

          \[\leadsto 1 + 4 \cdot \color{blue}{\left(\left(\mathsf{neg}\left(\frac{z}{y}\right)\right) + \frac{\frac{3}{4} \cdot y}{y}\right)} \]
        4. associate-/l*N/A

          \[\leadsto 1 + 4 \cdot \left(\left(\mathsf{neg}\left(\frac{z}{y}\right)\right) + \color{blue}{\frac{3}{4} \cdot \frac{y}{y}}\right) \]
        5. *-inversesN/A

          \[\leadsto 1 + 4 \cdot \left(\left(\mathsf{neg}\left(\frac{z}{y}\right)\right) + \frac{3}{4} \cdot \color{blue}{1}\right) \]
        6. metadata-evalN/A

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

          \[\leadsto 1 + 4 \cdot \color{blue}{\left(\frac{3}{4} + \left(\mathsf{neg}\left(\frac{z}{y}\right)\right)\right)} \]
        8. sub-negN/A

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

          \[\leadsto 1 + \color{blue}{\left(4 \cdot \left(\frac{3}{4} - \frac{z}{y}\right)\right) \cdot 1} \]
        10. distribute-rgt1-inN/A

          \[\leadsto \color{blue}{\left(4 \cdot \left(\frac{3}{4} - \frac{z}{y}\right) + 1\right) \cdot 1} \]
      7. Applied rewrites96.2%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{-4}{y}, z, 4\right)} \]
      8. Step-by-step derivation
        1. Applied rewrites96.2%

          \[\leadsto \mathsf{fma}\left(\frac{z}{y}, \color{blue}{-4}, 4\right) \]
      9. Recombined 2 regimes into one program.
      10. Final simplification98.3%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(\left(0.75 \cdot y + x\right) - z\right) \cdot 4}{y} \leq -100000000:\\ \;\;\;\;\frac{\left(x - z\right) \cdot 4}{y}\\ \mathbf{elif}\;\frac{\left(\left(0.75 \cdot y + x\right) - z\right) \cdot 4}{y} \leq 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(\frac{z}{y}, -4, 4\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - z\right) \cdot 4}{y}\\ \end{array} \]
      11. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (x y z)
        :name "Data.Array.Repa.Algorithms.ColorRamp:rampColorHotToCold from repa-algorithms-3.4.0.1, A"
        :precision binary64
        (+ 1.0 (/ (* 4.0 (- (+ x (* y 0.75)) z)) y)))