Language.Haskell.HsColour.ColourHighlight:unbase from hscolour-1.23

Percentage Accurate: 99.9% → 99.9%
Time: 2.4s
Alternatives: 4
Speedup: 1.1×

Specification

?
\[\left(x \cdot y + z\right) \cdot y + t \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (+ (* (+ (* x y) z) y) t))
double code(double x, double y, double z, double t) {
	return (((x * y) + z) * y) + t;
}
real(8) function code(x, y, z, t)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (((x * y) + z) * y) + t
end function
public static double code(double x, double y, double z, double t) {
	return (((x * y) + z) * y) + t;
}
def code(x, y, z, t):
	return (((x * y) + z) * y) + t
function code(x, y, z, t)
	return Float64(Float64(Float64(Float64(x * y) + z) * y) + t)
end
function tmp = code(x, y, z, t)
	tmp = (((x * y) + z) * y) + t;
end
code[x_, y_, z_, t_] := N[(N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision] + t), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	(((x * y) + z) * y) + t
END code
\left(x \cdot y + z\right) \cdot y + t

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: 99.9% accurate, 1.0× speedup?

\[\left(x \cdot y + z\right) \cdot y + t \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (+ (* (+ (* x y) z) y) t))
double code(double x, double y, double z, double t) {
	return (((x * y) + z) * y) + t;
}
real(8) function code(x, y, z, t)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (((x * y) + z) * y) + t
end function
public static double code(double x, double y, double z, double t) {
	return (((x * y) + z) * y) + t;
}
def code(x, y, z, t):
	return (((x * y) + z) * y) + t
function code(x, y, z, t)
	return Float64(Float64(Float64(Float64(x * y) + z) * y) + t)
end
function tmp = code(x, y, z, t)
	tmp = (((x * y) + z) * y) + t;
end
code[x_, y_, z_, t_] := N[(N[(N[(N[(x * y), $MachinePrecision] + z), $MachinePrecision] * y), $MachinePrecision] + t), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	(((x * y) + z) * y) + t
END code
\left(x \cdot y + z\right) \cdot y + t

Alternative 1: 99.9% accurate, 1.1× speedup?

\[\mathsf{fma}\left(y, \mathsf{fma}\left(y, x, z\right), t\right) \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (fma y (fma y x z) t))
double code(double x, double y, double z, double t) {
	return fma(y, fma(y, x, z), t);
}
function code(x, y, z, t)
	return fma(y, fma(y, x, z), t)
end
code[x_, y_, z_, t_] := N[(y * N[(y * x + z), $MachinePrecision] + t), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	(y * ((y * x) + z)) + t
END code
\mathsf{fma}\left(y, \mathsf{fma}\left(y, x, z\right), t\right)
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot y + z\right) \cdot y + t \]
  2. Step-by-step derivation
    1. Applied rewrites99.9%

      \[\leadsto \mathsf{fma}\left(y, \mathsf{fma}\left(y, x, z\right), t\right) \]
    2. Add Preprocessing

    Alternative 2: 89.2% accurate, 0.7× speedup?

    \[\begin{array}{l} t_1 := \mathsf{fma}\left(y, x, z\right) \cdot y\\ \mathbf{if}\;y \leq -362737.36445094895:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;y \leq 27842210377009184:\\ \;\;\;\;\mathsf{fma}\left(y, z, t\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \]
    (FPCore (x y z t)
      :precision binary64
      :pre TRUE
      (let* ((t_1 (* (fma y x z) y)))
      (if (<= y -362737.36445094895)
        t_1
        (if (<= y 27842210377009184.0) (fma y z t) t_1))))
    double code(double x, double y, double z, double t) {
    	double t_1 = fma(y, x, z) * y;
    	double tmp;
    	if (y <= -362737.36445094895) {
    		tmp = t_1;
    	} else if (y <= 27842210377009184.0) {
    		tmp = fma(y, z, t);
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    function code(x, y, z, t)
    	t_1 = Float64(fma(y, x, z) * y)
    	tmp = 0.0
    	if (y <= -362737.36445094895)
    		tmp = t_1;
    	elseif (y <= 27842210377009184.0)
    		tmp = fma(y, z, t);
    	else
    		tmp = t_1;
    	end
    	return tmp
    end
    
    code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(y * x + z), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -362737.36445094895], t$95$1, If[LessEqual[y, 27842210377009184.0], N[(y * z + t), $MachinePrecision], t$95$1]]]
    
    f(x, y, z, t):
    	x in [-inf, +inf],
    	y in [-inf, +inf],
    	z in [-inf, +inf],
    	t in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y, z, t: real): real =
    	LET t_1 = (((y * x) + z) * y) IN
    		LET tmp_1 = IF (y <= (27842210377009184)) THEN ((y * z) + t) ELSE t_1 ENDIF IN
    		LET tmp = IF (y <= (-3627373644509489531628787517547607421875e-34)) THEN t_1 ELSE tmp_1 ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    t_1 := \mathsf{fma}\left(y, x, z\right) \cdot y\\
    \mathbf{if}\;y \leq -362737.36445094895:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;y \leq 27842210377009184:\\
    \;\;\;\;\mathsf{fma}\left(y, z, t\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if y < -362737.36445094895 or 27842210377009184 < y

      1. Initial program 99.9%

        \[\left(x \cdot y + z\right) \cdot y + t \]
      2. Taylor expanded in t around inf

        \[\leadsto t \cdot \left(1 + \frac{y \cdot \left(z + x \cdot y\right)}{t}\right) \]
      3. Step-by-step derivation
        1. Applied rewrites92.3%

          \[\leadsto t \cdot \left(1 + \frac{y \cdot \left(z + x \cdot y\right)}{t}\right) \]
        2. Step-by-step derivation
          1. Applied rewrites92.2%

            \[\leadsto t \cdot \frac{1}{\frac{t}{\mathsf{fma}\left(\mathsf{fma}\left(y, x, z\right), y, t\right)}} \]
          2. Taylor expanded in t around 0

            \[\leadsto t \cdot \frac{1}{\frac{t}{y \cdot \left(z + x \cdot y\right)}} \]
          3. Step-by-step derivation
            1. Applied rewrites55.4%

              \[\leadsto t \cdot \frac{1}{\frac{t}{y \cdot \left(z + x \cdot y\right)}} \]
            2. Step-by-step derivation
              1. Applied rewrites63.2%

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

              if -362737.36445094895 < y < 27842210377009184

              1. Initial program 99.9%

                \[\left(x \cdot y + z\right) \cdot y + t \]
              2. Taylor expanded in x around 0

                \[\leadsto z \cdot y + t \]
              3. Step-by-step derivation
                1. Applied rewrites65.7%

                  \[\leadsto z \cdot y + t \]
                2. Step-by-step derivation
                  1. Applied rewrites65.7%

                    \[\leadsto \mathsf{fma}\left(y, z, t\right) \]
                3. Recombined 2 regimes into one program.
                4. Add Preprocessing

                Alternative 3: 65.7% accurate, 2.0× speedup?

                \[\mathsf{fma}\left(y, z, t\right) \]
                (FPCore (x y z t)
                  :precision binary64
                  :pre TRUE
                  (fma y z t))
                double code(double x, double y, double z, double t) {
                	return fma(y, z, t);
                }
                
                function code(x, y, z, t)
                	return fma(y, z, t)
                end
                
                code[x_, y_, z_, t_] := N[(y * z + t), $MachinePrecision]
                
                f(x, y, z, t):
                	x in [-inf, +inf],
                	y in [-inf, +inf],
                	z in [-inf, +inf],
                	t in [-inf, +inf]
                code: THEORY
                BEGIN
                f(x, y, z, t: real): real =
                	(y * z) + t
                END code
                \mathsf{fma}\left(y, z, t\right)
                
                Derivation
                1. Initial program 99.9%

                  \[\left(x \cdot y + z\right) \cdot y + t \]
                2. Taylor expanded in x around 0

                  \[\leadsto z \cdot y + t \]
                3. Step-by-step derivation
                  1. Applied rewrites65.7%

                    \[\leadsto z \cdot y + t \]
                  2. Step-by-step derivation
                    1. Applied rewrites65.7%

                      \[\leadsto \mathsf{fma}\left(y, z, t\right) \]
                    2. Add Preprocessing

                    Alternative 4: 38.6% accurate, 12.2× speedup?

                    \[t \]
                    (FPCore (x y z t)
                      :precision binary64
                      :pre TRUE
                      t)
                    double code(double x, double y, double z, double t) {
                    	return t;
                    }
                    
                    real(8) function code(x, y, z, t)
                    use fmin_fmax_functions
                        real(8), intent (in) :: x
                        real(8), intent (in) :: y
                        real(8), intent (in) :: z
                        real(8), intent (in) :: t
                        code = t
                    end function
                    
                    public static double code(double x, double y, double z, double t) {
                    	return t;
                    }
                    
                    def code(x, y, z, t):
                    	return t
                    
                    function code(x, y, z, t)
                    	return t
                    end
                    
                    function tmp = code(x, y, z, t)
                    	tmp = t;
                    end
                    
                    code[x_, y_, z_, t_] := t
                    
                    f(x, y, z, t):
                    	x in [-inf, +inf],
                    	y in [-inf, +inf],
                    	z in [-inf, +inf],
                    	t in [-inf, +inf]
                    code: THEORY
                    BEGIN
                    f(x, y, z, t: real): real =
                    	t
                    END code
                    t
                    
                    Derivation
                    1. Initial program 99.9%

                      \[\left(x \cdot y + z\right) \cdot y + t \]
                    2. Taylor expanded in y around 0

                      \[\leadsto t \]
                    3. Step-by-step derivation
                      1. Applied rewrites38.6%

                        \[\leadsto t \]
                      2. Add Preprocessing

                      Reproduce

                      ?
                      herbie shell --seed 2026092 
                      (FPCore (x y z t)
                        :name "Language.Haskell.HsColour.ColourHighlight:unbase from hscolour-1.23"
                        :precision binary64
                        (+ (* (+ (* x y) z) y) t))