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

Percentage Accurate: 100.0% → 100.0%
Time: 4.1s
Alternatives: 5
Speedup: N/A×

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.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{2} + y \cdot x\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{+137} \lor \neg \left(t\_0 \leq 2 \cdot 10^{+179}\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 -4e+137) (not (<= t_0 2e+179)))
     (* (- 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 <= -4e+137) || !(t_0 <= 2e+179)) {
		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 <= -4e+137) || !(t_0 <= 2e+179))
		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, -4e+137], N[Not[LessEqual[t$95$0, 2e+179]], $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 -4 \cdot 10^{+137} \lor \neg \left(t\_0 \leq 2 \cdot 10^{+179}\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)) < -4.0000000000000001e137 or 1.99999999999999996e179 < (+.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.f6435.1

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

      \[\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--.f6494.3

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

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

    if -4.0000000000000001e137 < (+.f64 (/.f64 x #s(literal 2 binary64)) (*.f64 y x)) < 1.99999999999999996e179

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{2} + y \cdot x \leq -4 \cdot 10^{+137} \lor \neg \left(\frac{x}{2} + y \cdot x \leq 2 \cdot 10^{+179}\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.4% accurate, 1.2× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -7.5 \cdot 10^{+39} \lor \neg \left(y \leq 2.5 \cdot 10^{+61}\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 < -7.5000000000000005e39 or 2.50000000000000009e61 < 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.f6425.6

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites25.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-*.f6476.3

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

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

    if -7.5000000000000005e39 < y < 2.50000000000000009e61

    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.1

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

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

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

Alternative 4: 58.9% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -9.2 \cdot 10^{-22} \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 -9.2e-22) (not (<= y 0.5))) (* y x) (* 0.5 x)))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -9.2e-22) || !(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 <= (-9.2d-22)) .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 <= -9.2e-22) || !(y <= 0.5)) {
		tmp = y * x;
	} else {
		tmp = 0.5 * x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (y <= -9.2e-22) 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 <= -9.2e-22) || !(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 <= -9.2e-22) || ~((y <= 0.5)))
		tmp = y * x;
	else
		tmp = 0.5 * x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[y, -9.2e-22], 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 -9.2 \cdot 10^{-22} \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 < -9.1999999999999992e-22 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.f6434.8

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

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

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

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

    if -9.1999999999999992e-22 < 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.9

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, x, z\right)} \]
    5. Applied rewrites99.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 rewrites44.0%

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -9.2 \cdot 10^{-22} \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.1% 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.f6468.4

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

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

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

      Reproduce

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