fma_test1

Percentage Accurate: 3.4% → 99.4%
Time: 7.3s
Alternatives: 2
Speedup: 3.5×

Specification

?
\[0.9 \leq t \land t \leq 1.1\]
\[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\ t\_1 \cdot t\_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \end{array} \end{array} \]
(FPCore (t)
 :precision binary64
 (let* ((t_1 (+ 1.0 (* t 2e-16))))
   (+ (* t_1 t_1) (- -1.0 (* 2.0 (* t 2e-16))))))
double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
real(8) function code(t)
    real(8), intent (in) :: t
    real(8) :: t_1
    t_1 = 1.0d0 + (t * 2d-16)
    code = (t_1 * t_1) + ((-1.0d0) - (2.0d0 * (t * 2d-16)))
end function
public static double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
def code(t):
	t_1 = 1.0 + (t * 2e-16)
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)))
function code(t)
	t_1 = Float64(1.0 + Float64(t * 2e-16))
	return Float64(Float64(t_1 * t_1) + Float64(-1.0 - Float64(2.0 * Float64(t * 2e-16))))
end
function tmp = code(t)
	t_1 = 1.0 + (t * 2e-16);
	tmp = (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * t$95$1), $MachinePrecision] + N[(-1.0 - N[(2.0 * N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\
t\_1 \cdot t\_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)
\end{array}
\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 2 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: 3.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\ t\_1 \cdot t\_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \end{array} \end{array} \]
(FPCore (t)
 :precision binary64
 (let* ((t_1 (+ 1.0 (* t 2e-16))))
   (+ (* t_1 t_1) (- -1.0 (* 2.0 (* t 2e-16))))))
double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
real(8) function code(t)
    real(8), intent (in) :: t
    real(8) :: t_1
    t_1 = 1.0d0 + (t * 2d-16)
    code = (t_1 * t_1) + ((-1.0d0) - (2.0d0 * (t * 2d-16)))
end function
public static double code(double t) {
	double t_1 = 1.0 + (t * 2e-16);
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
}
def code(t):
	t_1 = 1.0 + (t * 2e-16)
	return (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)))
function code(t)
	t_1 = Float64(1.0 + Float64(t * 2e-16))
	return Float64(Float64(t_1 * t_1) + Float64(-1.0 - Float64(2.0 * Float64(t * 2e-16))))
end
function tmp = code(t)
	t_1 = 1.0 + (t * 2e-16);
	tmp = (t_1 * t_1) + (-1.0 - (2.0 * (t * 2e-16)));
end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * t$95$1), $MachinePrecision] + N[(-1.0 - N[(2.0 * N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\
t\_1 \cdot t\_1 + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)
\end{array}
\end{array}

Alternative 1: 99.4% accurate, 2.4× speedup?

\[\begin{array}{l} \\ \left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot t\right) \cdot 2 \cdot 10^{-16} \end{array} \]
(FPCore (t) :precision binary64 (* (* (* t 2e-16) t) 2e-16))
double code(double t) {
	return ((t * 2e-16) * t) * 2e-16;
}
real(8) function code(t)
    real(8), intent (in) :: t
    code = ((t * 2d-16) * t) * 2d-16
end function
public static double code(double t) {
	return ((t * 2e-16) * t) * 2e-16;
}
def code(t):
	return ((t * 2e-16) * t) * 2e-16
function code(t)
	return Float64(Float64(Float64(t * 2e-16) * t) * 2e-16)
end
function tmp = code(t)
	tmp = ((t * 2e-16) * t) * 2e-16;
end
code[t_] := N[(N[(N[(t * 2e-16), $MachinePrecision] * t), $MachinePrecision] * 2e-16), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot t\right) \cdot 2 \cdot 10^{-16}
\end{array}
Derivation
  1. Initial program 3.4%

    \[\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \]
  2. Add Preprocessing
  3. Taylor expanded in t around 0

    \[\leadsto \color{blue}{\frac{1}{25000000000000000000000000000000} \cdot {t}^{2}} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{{t}^{2} \cdot \frac{1}{25000000000000000000000000000000}} \]
    2. lower-*.f64N/A

      \[\leadsto \color{blue}{{t}^{2} \cdot \frac{1}{25000000000000000000000000000000}} \]
    3. unpow2N/A

      \[\leadsto \color{blue}{\left(t \cdot t\right)} \cdot \frac{1}{25000000000000000000000000000000} \]
    4. lower-*.f6499.3

      \[\leadsto \color{blue}{\left(t \cdot t\right)} \cdot 4 \cdot 10^{-32} \]
  5. Applied rewrites99.3%

    \[\leadsto \color{blue}{\left(t \cdot t\right) \cdot 4 \cdot 10^{-32}} \]
  6. Step-by-step derivation
    1. Applied rewrites99.5%

      \[\leadsto \left(\left(2 \cdot 10^{-16} \cdot t\right) \cdot t\right) \cdot \color{blue}{2 \cdot 10^{-16}} \]
    2. Final simplification99.5%

      \[\leadsto \left(\left(t \cdot 2 \cdot 10^{-16}\right) \cdot t\right) \cdot 2 \cdot 10^{-16} \]
    3. Add Preprocessing

    Alternative 2: 99.4% accurate, 3.5× speedup?

    \[\begin{array}{l} \\ \left(4 \cdot 10^{-32} \cdot t\right) \cdot t \end{array} \]
    (FPCore (t) :precision binary64 (* (* 4e-32 t) t))
    double code(double t) {
    	return (4e-32 * t) * t;
    }
    
    real(8) function code(t)
        real(8), intent (in) :: t
        code = (4d-32 * t) * t
    end function
    
    public static double code(double t) {
    	return (4e-32 * t) * t;
    }
    
    def code(t):
    	return (4e-32 * t) * t
    
    function code(t)
    	return Float64(Float64(4e-32 * t) * t)
    end
    
    function tmp = code(t)
    	tmp = (4e-32 * t) * t;
    end
    
    code[t_] := N[(N[(4e-32 * t), $MachinePrecision] * t), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \left(4 \cdot 10^{-32} \cdot t\right) \cdot t
    \end{array}
    
    Derivation
    1. Initial program 3.4%

      \[\left(1 + t \cdot 2 \cdot 10^{-16}\right) \cdot \left(1 + t \cdot 2 \cdot 10^{-16}\right) + \left(-1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \]
    2. Add Preprocessing
    3. Taylor expanded in t around 0

      \[\leadsto \color{blue}{\frac{1}{25000000000000000000000000000000} \cdot {t}^{2}} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{{t}^{2} \cdot \frac{1}{25000000000000000000000000000000}} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{{t}^{2} \cdot \frac{1}{25000000000000000000000000000000}} \]
      3. unpow2N/A

        \[\leadsto \color{blue}{\left(t \cdot t\right)} \cdot \frac{1}{25000000000000000000000000000000} \]
      4. lower-*.f6499.3

        \[\leadsto \color{blue}{\left(t \cdot t\right)} \cdot 4 \cdot 10^{-32} \]
    5. Applied rewrites99.3%

      \[\leadsto \color{blue}{\left(t \cdot t\right) \cdot 4 \cdot 10^{-32}} \]
    6. Taylor expanded in t around 0

      \[\leadsto \color{blue}{\frac{1}{25000000000000000000000000000000} \cdot {t}^{2}} \]
    7. Step-by-step derivation
      1. unpow2N/A

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

        \[\leadsto \color{blue}{\left(\frac{1}{25000000000000000000000000000000} \cdot t\right) \cdot t} \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\frac{1}{25000000000000000000000000000000} \cdot t\right) \cdot t} \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\left(t \cdot \frac{1}{25000000000000000000000000000000}\right)} \cdot t \]
      5. lower-*.f6499.3

        \[\leadsto \color{blue}{\left(t \cdot 4 \cdot 10^{-32}\right)} \cdot t \]
    8. Applied rewrites99.3%

      \[\leadsto \color{blue}{\left(t \cdot 4 \cdot 10^{-32}\right) \cdot t} \]
    9. Final simplification99.3%

      \[\leadsto \left(4 \cdot 10^{-32} \cdot t\right) \cdot t \]
    10. Add Preprocessing

    Developer Target 1: 21.0% accurate, 1.1× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\ \mathsf{fma}\left(t\_1, t\_1, -1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right) \end{array} \end{array} \]
    (FPCore (t)
     :precision binary64
     (let* ((t_1 (+ 1.0 (* t 2e-16)))) (fma t_1 t_1 (- -1.0 (* 2.0 (* t 2e-16))))))
    double code(double t) {
    	double t_1 = 1.0 + (t * 2e-16);
    	return fma(t_1, t_1, (-1.0 - (2.0 * (t * 2e-16))));
    }
    
    function code(t)
    	t_1 = Float64(1.0 + Float64(t * 2e-16))
    	return fma(t_1, t_1, Float64(-1.0 - Float64(2.0 * Float64(t * 2e-16))))
    end
    
    code[t_] := Block[{t$95$1 = N[(1.0 + N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]}, N[(t$95$1 * t$95$1 + N[(-1.0 - N[(2.0 * N[(t * 2e-16), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_1 := 1 + t \cdot 2 \cdot 10^{-16}\\
    \mathsf{fma}\left(t\_1, t\_1, -1 - 2 \cdot \left(t \cdot 2 \cdot 10^{-16}\right)\right)
    \end{array}
    \end{array}
    

    Reproduce

    ?
    herbie shell --seed 2024272 
    (FPCore (t)
      :name "fma_test1"
      :precision binary64
      :pre (and (<= 0.9 t) (<= t 1.1))
    
      :alt
      (! :herbie-platform default (let ((x (+ 1 (* t 1/5000000000000000))) (z (- -1 (* 2 (* t 1/5000000000000000))))) (fma x x z)))
    
      (+ (* (+ 1.0 (* t 2e-16)) (+ 1.0 (* t 2e-16))) (- -1.0 (* 2.0 (* t 2e-16)))))