x*x + 1e18 - 1e9*x

Percentage Accurate: 99.4% → 100.0%
Time: 3.0s
Alternatives: 4
Speedup: 1.7×

Specification

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

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

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

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

Alternative 1: 100.0% accurate, 1.7× speedup?

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x - 1000000000, 1000000000000000000\right)} \]
    10. lower--.f64100.0

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

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

Alternative 2: 22.6% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 99.4%

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

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

        \[\leadsto \color{blue}{-1000000000 \cdot x + 1000000000000000000} \]
      2. lower-fma.f6423.8

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

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

    if 6.2e8 < x

    1. Initial program 99.4%

      \[\left(x \cdot x + 10^{+18}\right) - 1000000000 \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-*.f6421.7

        \[\leadsto \color{blue}{x \cdot x} \]
    5. Applied rewrites21.7%

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

Alternative 3: 21.7% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 1000000000:\\ \;\;\;\;10^{+18}\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= x 1000000000.0) 1e+18 (* x x)))
double code(double x) {
	double tmp;
	if (x <= 1000000000.0) {
		tmp = 1e+18;
	} else {
		tmp = x * x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 1000000000.0d0) then
        tmp = 1d+18
    else
        tmp = x * x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= 1000000000.0) {
		tmp = 1e+18;
	} else {
		tmp = x * x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= 1000000000.0:
		tmp = 1e+18
	else:
		tmp = x * x
	return tmp
function code(x)
	tmp = 0.0
	if (x <= 1000000000.0)
		tmp = 1e+18;
	else
		tmp = Float64(x * x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 1000000000.0)
		tmp = 1e+18;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, 1000000000.0], 1e+18, N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 1000000000:\\
\;\;\;\;10^{+18}\\

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


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

    1. Initial program 99.3%

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

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

        \[\leadsto \color{blue}{10^{+18}} \]

      if 1e9 < x

      1. Initial program 99.4%

        \[\left(x \cdot x + 10^{+18}\right) - 1000000000 \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-*.f6421.8

          \[\leadsto \color{blue}{x \cdot x} \]
      5. Applied rewrites21.8%

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

    Alternative 4: 19.4% accurate, 17.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 99.4%

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

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

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

      Reproduce

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