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

Percentage Accurate: 100.0% → 100.0%
Time: 4.6s
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. Taylor expanded in x around 0

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

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

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

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

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

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

Alternative 2: 84.7% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{2} + y \cdot x\\ \mathbf{if}\;t\_0 \leq -1 \cdot 10^{+134} \lor \neg \left(t\_0 \leq 2 \cdot 10^{+86}\right):\\ \;\;\;\;\left(y - -0.5\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, x, z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (+ (/ x 2.0) (* y x))))
   (if (or (<= t_0 -1e+134) (not (<= t_0 2e+86)))
     (* (- y -0.5) x)
     (fma 0.5 x z))))
double code(double x, double y, double z) {
	double t_0 = (x / 2.0) + (y * x);
	double tmp;
	if ((t_0 <= -1e+134) || !(t_0 <= 2e+86)) {
		tmp = (y - -0.5) * x;
	} else {
		tmp = fma(0.5, x, z);
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(Float64(x / 2.0) + Float64(y * x))
	tmp = 0.0
	if ((t_0 <= -1e+134) || !(t_0 <= 2e+86))
		tmp = Float64(Float64(y - -0.5) * x);
	else
		tmp = fma(0.5, x, z);
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -1e+134], N[Not[LessEqual[t$95$0, 2e+86]], $MachinePrecision]], N[(N[(y - -0.5), $MachinePrecision] * x), $MachinePrecision], N[(0.5 * x + z), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x}{2} + y \cdot x\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{+134} \lor \neg \left(t\_0 \leq 2 \cdot 10^{+86}\right):\\
\;\;\;\;\left(y - -0.5\right) \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 (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x)) < -9.99999999999999921e133 or 2e86 < (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x))

    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.f6438.3

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

      \[\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}{1 \cdot \frac{1}{2}}\right) \cdot x \]
      5. rgt-mult-inverseN/A

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

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

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

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

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

        \[\leadsto \left(y - \left(-1 \cdot y\right) \cdot \color{blue}{\left(\frac{1}{y} \cdot \frac{1}{2}\right)}\right) \cdot x \]
      11. associate-*r*N/A

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

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

        \[\leadsto \left(y - \color{blue}{\left(\mathsf{neg}\left(y \cdot \frac{1}{y}\right)\right)} \cdot \frac{1}{2}\right) \cdot x \]
      14. rgt-mult-inverseN/A

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

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

        \[\leadsto \left(y - \color{blue}{\frac{-1}{2}}\right) \cdot x \]
      17. lower--.f6491.4

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

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

    if -9.99999999999999921e133 < (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x)) < 2e86

    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.f6485.0

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

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

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

Alternative 3: 84.3% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -360000000000 \lor \neg \left(y \leq 6.2 \cdot 10^{+31}\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 -360000000000.0) (not (<= y 6.2e+31))) (* y x) (fma 0.5 x z)))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -360000000000.0) || !(y <= 6.2e+31)) {
		tmp = y * x;
	} else {
		tmp = fma(0.5, x, z);
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if ((y <= -360000000000.0) || !(y <= 6.2e+31))
		tmp = Float64(y * x);
	else
		tmp = fma(0.5, x, z);
	end
	return tmp
end
code[x_, y_, z_] := If[Or[LessEqual[y, -360000000000.0], N[Not[LessEqual[y, 6.2e+31]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(0.5 * x + z), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -360000000000 \lor \neg \left(y \leq 6.2 \cdot 10^{+31}\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 < -3.6e11 or 6.2000000000000004e31 < 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.f6424.7

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

      \[\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-*.f6476.2

        \[\leadsto \color{blue}{y \cdot x} \]
    8. Applied rewrites76.2%

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

    if -3.6e11 < y < 6.2000000000000004e31

    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.f6497.3

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

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

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

Alternative 4: 59.8% accurate, 1.3× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -6e10 or 0.5 < 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.f6426.3

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

      \[\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-*.f6473.8

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

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

    if -6e10 < y < 0.5

    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.f6499.2

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

      \[\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 rewrites53.0%

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

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

    Alternative 5: 26.0% 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.f6463.9

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

      \[\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 rewrites29.1%

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

      Reproduce

      ?
      herbie shell --seed 2024326 
      (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))