Data.Histogram.Bin.BinF:$cfromIndex from histogram-fill-0.8.4.1

Percentage Accurate: 100.0% → 100.0%
Time: 4.8s
Alternatives: 5
Speedup: 2.3×

Specification

?
\[\begin{array}{l} \\ \left(\frac{x}{2} + y \cdot x\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* y x)) z))
double code(double x, double y, double z) {
	return ((x / 2.0) + (y * x)) + 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 / 2.0d0) + (y * x)) + z
end function
public static double code(double x, double y, double z) {
	return ((x / 2.0) + (y * x)) + z;
}
def code(x, y, z):
	return ((x / 2.0) + (y * x)) + z
function code(x, y, z)
	return Float64(Float64(Float64(x / 2.0) + Float64(y * x)) + z)
end
function tmp = code(x, y, z)
	tmp = ((x / 2.0) + (y * x)) + z;
end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{x}{2} + y \cdot x\right) + z
\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 5 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(\frac{x}{2} + y \cdot x\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* y x)) z))
double code(double x, double y, double z) {
	return ((x / 2.0) + (y * x)) + 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 / 2.0d0) + (y * x)) + z
end function
public static double code(double x, double y, double z) {
	return ((x / 2.0) + (y * x)) + z;
}
def code(x, y, z):
	return ((x / 2.0) + (y * x)) + z
function code(x, y, z)
	return Float64(Float64(Float64(x / 2.0) + Float64(y * x)) + z)
end
function tmp = code(x, y, z)
	tmp = ((x / 2.0) + (y * x)) + z;
end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{x}{2} + y \cdot x\right) + z
\end{array}

Alternative 1: 100.0% accurate, 2.3× speedup?

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

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

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

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

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

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

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

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

      \[\leadsto \left(x \cdot \frac{1}{2} + \color{blue}{x \cdot y}\right) + z \]
    7. distribute-lft-outN/A

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

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

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{1}{2} + y}, x, z\right) \]
    11. metadata-eval100.0

      \[\leadsto \mathsf{fma}\left(\color{blue}{0.5} + y, x, z\right) \]
  4. Applied rewrites100.0%

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

Alternative 2: 84.5% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -6.8 \cdot 10^{+55}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 5 \cdot 10^{+66}:\\ \;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\ \mathbf{else}:\\ \;\;\;\;\left(y - -0.5\right) \cdot x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -6.8e+55) (* y x) (if (<= y 5e+66) (fma 0.5 x z) (* (- y -0.5) x))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -6.8e+55) {
		tmp = y * x;
	} else if (y <= 5e+66) {
		tmp = fma(0.5, x, z);
	} else {
		tmp = (y - -0.5) * x;
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (y <= -6.8e+55)
		tmp = Float64(y * x);
	elseif (y <= 5e+66)
		tmp = fma(0.5, x, z);
	else
		tmp = Float64(Float64(y - -0.5) * x);
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[y, -6.8e+55], N[(y * x), $MachinePrecision], If[LessEqual[y, 5e+66], N[(0.5 * x + z), $MachinePrecision], N[(N[(y - -0.5), $MachinePrecision] * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.8 \cdot 10^{+55}:\\
\;\;\;\;y \cdot x\\

\mathbf{elif}\;y \leq 5 \cdot 10^{+66}:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\

\mathbf{else}:\\
\;\;\;\;\left(y - -0.5\right) \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < -6.7999999999999996e55

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6411.1

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites11.1%

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

      \[\leadsto \color{blue}{x \cdot y} \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} \]
      2. lower-*.f6487.0

        \[\leadsto \color{blue}{y \cdot x} \]
    8. Applied rewrites87.0%

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

    if -6.7999999999999996e55 < y < 4.99999999999999991e66

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6489.9

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites89.9%

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

    if 4.99999999999999991e66 < y

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6421.6

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

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

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{2} + y\right)} \]
    7. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

        \[\leadsto \color{blue}{\left(y - \frac{-1}{2}\right)} \cdot x \]
      6. lower--.f6478.8

        \[\leadsto \color{blue}{\left(y - -0.5\right)} \cdot x \]
    8. Applied rewrites78.8%

      \[\leadsto \color{blue}{\left(y - -0.5\right) \cdot x} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 84.5% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -6.8 \cdot 10^{+55} \lor \neg \left(y \leq 5 \cdot 10^{+66}\right):\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= y -6.8e+55) (not (<= y 5e+66))) (* y x) (fma 0.5 x z)))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -6.8e+55) || !(y <= 5e+66)) {
		tmp = y * x;
	} else {
		tmp = fma(0.5, x, z);
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if ((y <= -6.8e+55) || !(y <= 5e+66))
		tmp = Float64(y * x);
	else
		tmp = fma(0.5, x, z);
	end
	return tmp
end
code[x_, y_, z_] := If[Or[LessEqual[y, -6.8e+55], N[Not[LessEqual[y, 5e+66]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(0.5 * x + z), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.8 \cdot 10^{+55} \lor \neg \left(y \leq 5 \cdot 10^{+66}\right):\\
\;\;\;\;y \cdot x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -6.7999999999999996e55 or 4.99999999999999991e66 < y

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6416.6

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

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

      \[\leadsto \color{blue}{x \cdot y} \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} \]
      2. lower-*.f6482.7

        \[\leadsto \color{blue}{y \cdot x} \]
    8. Applied rewrites82.7%

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

    if -6.7999999999999996e55 < y < 4.99999999999999991e66

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6489.9

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites89.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -6.8 \cdot 10^{+55} \lor \neg \left(y \leq 5 \cdot 10^{+66}\right):\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 59.3% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.8 \cdot 10^{-14} \lor \neg \left(y \leq 86\right):\\
\;\;\;\;y \cdot x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.7999999999999999e-14 or 86 < y

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6430.2

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites30.2%

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

      \[\leadsto \color{blue}{x \cdot y} \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{y \cdot x} \]
      2. lower-*.f6468.1

        \[\leadsto \color{blue}{y \cdot x} \]
    8. Applied rewrites68.1%

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

    if -1.7999999999999999e-14 < y < 86

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6498.3

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites98.3%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{x} \]
    8. Recombined 2 regimes into one program.
    9. Final simplification58.4%

      \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.8 \cdot 10^{-14} \lor \neg \left(y \leq 86\right):\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot x\\ \end{array} \]
    10. Add Preprocessing

    Alternative 5: 26.5% accurate, 3.8× speedup?

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

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

      \[\leadsto \color{blue}{z + \frac{1}{2} \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot x + z} \]
      2. lower-fma.f6461.6

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

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

      \[\leadsto \frac{1}{2} \cdot \color{blue}{x} \]
    7. Step-by-step derivation
      1. Applied rewrites23.6%

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

      Reproduce

      ?
      herbie shell --seed 2024314 
      (FPCore (x y z)
        :name "Data.Histogram.Bin.BinF:$cfromIndex from histogram-fill-0.8.4.1"
        :precision binary64
        (+ (+ (/ x 2.0) (* y x)) z))