Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.6% → 96.8%
Time: 2.0s
Alternatives: 4
Speedup: 0.6×

Specification

?
\[\begin{array}{l} \\ x \cdot x - y \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
	return (x * x) - (y * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
	return (x * x) - (y * y);
}
def code(x, y):
	return (x * x) - (y * y)
function code(x, y)
	return Float64(Float64(x * x) - Float64(y * y))
end
function tmp = code(x, y)
	tmp = (x * x) - (y * y);
end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x - y \cdot y
\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: 93.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot x - y \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
	return (x * x) - (y * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
	return (x * x) - (y * y);
}
def code(x, y):
	return (x * x) - (y * y)
function code(x, y)
	return Float64(Float64(x * x) - Float64(y * y))
end
function tmp = code(x, y)
	tmp = (x * x) - (y * y);
end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x - y \cdot y
\end{array}

Alternative 1: 96.8% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right) \end{array} \]
(FPCore (x y) :precision binary64 (fma x x (* y (- y))))
double code(double x, double y) {
	return fma(x, x, (y * -y));
}
function code(x, y)
	return fma(x, x, Float64(y * Float64(-y)))
end
code[x_, y_] := N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)
\end{array}
Derivation
  1. Initial program 94.5%

    \[x \cdot x - y \cdot y \]
  2. Step-by-step derivation
    1. fma-neg97.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, -y \cdot y\right)} \]
    2. distribute-rgt-neg-in97.7%

      \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{y \cdot \left(-y\right)}\right) \]
  3. Simplified97.7%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)} \]
  4. Final simplification97.7%

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

Alternative 2: 77.5% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1200 \lor \neg \left(x \cdot x \leq 5.3 \cdot 10^{+96}\right) \land x \cdot x \leq 1.15 \cdot 10^{+128}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= (* x x) 1200.0)
         (and (not (<= (* x x) 5.3e+96)) (<= (* x x) 1.15e+128)))
   (* y (- y))
   (* x x)))
double code(double x, double y) {
	double tmp;
	if (((x * x) <= 1200.0) || (!((x * x) <= 5.3e+96) && ((x * x) <= 1.15e+128))) {
		tmp = y * -y;
	} else {
		tmp = x * x;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (((x * x) <= 1200.0d0) .or. (.not. ((x * x) <= 5.3d+96)) .and. ((x * x) <= 1.15d+128)) then
        tmp = y * -y
    else
        tmp = x * x
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (((x * x) <= 1200.0) || (!((x * x) <= 5.3e+96) && ((x * x) <= 1.15e+128))) {
		tmp = y * -y;
	} else {
		tmp = x * x;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if ((x * x) <= 1200.0) or (not ((x * x) <= 5.3e+96) and ((x * x) <= 1.15e+128)):
		tmp = y * -y
	else:
		tmp = x * x
	return tmp
function code(x, y)
	tmp = 0.0
	if ((Float64(x * x) <= 1200.0) || (!(Float64(x * x) <= 5.3e+96) && (Float64(x * x) <= 1.15e+128)))
		tmp = Float64(y * Float64(-y));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (((x * x) <= 1200.0) || (~(((x * x) <= 5.3e+96)) && ((x * x) <= 1.15e+128)))
		tmp = y * -y;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 1200.0], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 5.3e+96]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 1.15e+128]]], N[(y * (-y)), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1200 \lor \neg \left(x \cdot x \leq 5.3 \cdot 10^{+96}\right) \land x \cdot x \leq 1.15 \cdot 10^{+128}:\\
\;\;\;\;y \cdot \left(-y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 1200 or 5.29999999999999971e96 < (*.f64 x x) < 1.14999999999999999e128

    1. Initial program 100.0%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around 0 82.5%

      \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
    3. Step-by-step derivation
      1. unpow282.5%

        \[\leadsto -1 \cdot \color{blue}{\left(y \cdot y\right)} \]
      2. mul-1-neg82.5%

        \[\leadsto \color{blue}{-y \cdot y} \]
      3. distribute-rgt-neg-in82.5%

        \[\leadsto \color{blue}{y \cdot \left(-y\right)} \]
    4. Simplified82.5%

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

    if 1200 < (*.f64 x x) < 5.29999999999999971e96 or 1.14999999999999999e128 < (*.f64 x x)

    1. Initial program 88.7%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around inf 80.7%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow280.7%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified80.7%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification81.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1200 \lor \neg \left(x \cdot x \leq 5.3 \cdot 10^{+96}\right) \land x \cdot x \leq 1.15 \cdot 10^{+128}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 3: 95.8% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+254}:\\
\;\;\;\;x \cdot x - y \cdot y\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 1.9999999999999999e254

    1. Initial program 100.0%

      \[x \cdot x - y \cdot y \]

    if 1.9999999999999999e254 < (*.f64 y y)

    1. Initial program 80.0%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around 0 91.4%

      \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
    3. Step-by-step derivation
      1. unpow291.4%

        \[\leadsto -1 \cdot \color{blue}{\left(y \cdot y\right)} \]
      2. mul-1-neg91.4%

        \[\leadsto \color{blue}{-y \cdot y} \]
      3. distribute-rgt-neg-in91.4%

        \[\leadsto \color{blue}{y \cdot \left(-y\right)} \]
    4. Simplified91.4%

      \[\leadsto \color{blue}{y \cdot \left(-y\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+254}:\\ \;\;\;\;x \cdot x - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \end{array} \]

Alternative 4: 53.1% accurate, 2.3× speedup?

\[\begin{array}{l} \\ x \cdot x \end{array} \]
(FPCore (x y) :precision binary64 (* x x))
double code(double x, double y) {
	return x * x;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * x
end function
public static double code(double x, double y) {
	return x * x;
}
def code(x, y):
	return x * x
function code(x, y)
	return Float64(x * x)
end
function tmp = code(x, y)
	tmp = x * x;
end
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}

\\
x \cdot x
\end{array}
Derivation
  1. Initial program 94.5%

    \[x \cdot x - y \cdot y \]
  2. Taylor expanded in x around inf 52.9%

    \[\leadsto \color{blue}{{x}^{2}} \]
  3. Step-by-step derivation
    1. unpow252.9%

      \[\leadsto \color{blue}{x \cdot x} \]
  4. Simplified52.9%

    \[\leadsto \color{blue}{x \cdot x} \]
  5. Final simplification52.9%

    \[\leadsto x \cdot x \]

Reproduce

?
herbie shell --seed 2023195 
(FPCore (x y)
  :name "Examples.Basics.BasicTests:f2 from sbv-4.4"
  :precision binary64
  (- (* x x) (* y y)))