x*x + 1e18 - 2*1e9*x

Percentage Accurate: 97.7% → 99.6%
Time: 3.9s
Alternatives: 3
Speedup: 2.2×

Specification

?
\[100000000 \leq x \land x \leq 10000000000\]
\[\begin{array}{l} \\ \left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x \end{array} \]
(FPCore (x)
 :precision binary64
 (- (+ (* x x) 1e+18) (* (* 2.0 1000000000.0) x)))
double code(double x) {
	return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((x * x) + 1d+18) - ((2.0d0 * 1000000000.0d0) * x)
end function
public static double code(double x) {
	return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x);
}
def code(x):
	return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x)
function code(x)
	return Float64(Float64(Float64(x * x) + 1e+18) - Float64(Float64(2.0 * 1000000000.0) * x))
end
function tmp = code(x)
	tmp = ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x);
end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] + 1e+18), $MachinePrecision] - N[(N[(2.0 * 1000000000.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x
\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 3 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: 97.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x \end{array} \]
(FPCore (x)
 :precision binary64
 (- (+ (* x x) 1e+18) (* (* 2.0 1000000000.0) x)))
double code(double x) {
	return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((x * x) + 1d+18) - ((2.0d0 * 1000000000.0d0) * x)
end function
public static double code(double x) {
	return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x);
}
def code(x):
	return ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x)
function code(x)
	return Float64(Float64(Float64(x * x) + 1e+18) - Float64(Float64(2.0 * 1000000000.0) * x))
end
function tmp = code(x)
	tmp = ((x * x) + 1e+18) - ((2.0 * 1000000000.0) * x);
end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] + 1e+18), $MachinePrecision] - N[(N[(2.0 * 1000000000.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x
\end{array}

Alternative 1: 99.6% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, x - 2000000000, 10^{+18}\right) \end{array} \]
(FPCore (x) :precision binary64 (fma x (- x 2000000000.0) 1e+18))
double code(double x) {
	return fma(x, (x - 2000000000.0), 1e+18);
}
function code(x)
	return fma(x, Float64(x - 2000000000.0), 1e+18)
end
code[x_] := N[(x * N[(x - 2000000000.0), $MachinePrecision] + 1e+18), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, x - 2000000000, 10^{+18}\right)
\end{array}
Derivation
  1. Initial program 98.0%

    \[\left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

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

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

      \[\leadsto \color{blue}{\left(1000000000000000000 + x \cdot x\right)} - \left(2 \cdot 1000000000\right) \cdot x \]
    4. associate--l+N/A

      \[\leadsto \color{blue}{1000000000000000000 + \left(x \cdot x - \left(2 \cdot 1000000000\right) \cdot x\right)} \]
    5. +-commutativeN/A

      \[\leadsto \color{blue}{\left(x \cdot x - \left(2 \cdot 1000000000\right) \cdot x\right) + 1000000000000000000} \]
    6. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{x \cdot x} - \left(2 \cdot 1000000000\right) \cdot x\right) + 1000000000000000000 \]
    7. lift-*.f64N/A

      \[\leadsto \left(x \cdot x - \color{blue}{\left(2 \cdot 1000000000\right) \cdot x}\right) + 1000000000000000000 \]
    8. distribute-rgt-out--N/A

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x - 2 \cdot 1000000000, 1000000000000000000\right)} \]
    10. lower--.f6499.4

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{x - 2 \cdot 1000000000}, 10^{+18}\right) \]
    11. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x, x - \color{blue}{2 \cdot 1000000000}, 1000000000000000000\right) \]
    12. metadata-eval99.4

      \[\leadsto \mathsf{fma}\left(x, x - \color{blue}{2000000000}, 10^{+18}\right) \]
  4. Applied rewrites99.4%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, x - 2000000000, 10^{+18}\right)} \]
  5. Add Preprocessing

Alternative 2: 20.0% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 420000000:\\ \;\;\;\;\mathsf{fma}\left(-2000000000, x, 10^{+18}\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x 420000000.0) (fma -2000000000.0 x 1e+18) (* x x)))
double code(double x) {
	double tmp;
	if (x <= 420000000.0) {
		tmp = fma(-2000000000.0, x, 1e+18);
	} else {
		tmp = x * x;
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (x <= 420000000.0)
		tmp = fma(-2000000000.0, x, 1e+18);
	else
		tmp = Float64(x * x);
	end
	return tmp
end
code[x_] := If[LessEqual[x, 420000000.0], N[(-2000000000.0 * x + 1e+18), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 420000000:\\
\;\;\;\;\mathsf{fma}\left(-2000000000, x, 10^{+18}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 4.2e8

    1. Initial program 99.4%

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

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

        \[\leadsto \color{blue}{-2000000000 \cdot x + 1000000000000000000} \]
      2. lower-fma.f6424.5

        \[\leadsto \color{blue}{\mathsf{fma}\left(-2000000000, x, 10^{+18}\right)} \]
    5. Applied rewrites24.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(-2000000000, x, 10^{+18}\right)} \]

    if 4.2e8 < x

    1. Initial program 97.3%

      \[\left(x \cdot x + 10^{+18}\right) - \left(2 \cdot 1000000000\right) \cdot x \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{{x}^{2}} \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \color{blue}{x \cdot x} \]
      2. lower-*.f6418.4

        \[\leadsto \color{blue}{x \cdot x} \]
    5. Applied rewrites18.4%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 17.5% accurate, 22.0× speedup?

\[\begin{array}{l} \\ 10^{+18} \end{array} \]
(FPCore (x) :precision binary64 1e+18)
double code(double x) {
	return 1e+18;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1d+18
end function
public static double code(double x) {
	return 1e+18;
}
def code(x):
	return 1e+18
function code(x)
	return 1e+18
end
function tmp = code(x)
	tmp = 1e+18;
end
code[x_] := 1e+18
\begin{array}{l}

\\
10^{+18}
\end{array}
Derivation
  1. Initial program 98.0%

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

    \[\leadsto \color{blue}{1000000000000000000} \]
  4. Step-by-step derivation
    1. Applied rewrites17.6%

      \[\leadsto \color{blue}{10^{+18}} \]
    2. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 5 
    (FPCore (x)
      :name "x*x + 1e18 - 2*1e9*x"
      :precision binary64
      :pre (and (<= 100000000.0 x) (<= x 10000000000.0))
      (- (+ (* x x) 1e+18) (* (* 2.0 1000000000.0) x)))