Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 94.1% → 97.0%
Time: 2.2s
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: 94.1% 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: 97.0% 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: 97.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+306}:\\ \;\;\;\;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+306) (- (* x x) (* y y)) (* y (- y))))
double code(double x, double y) {
	double tmp;
	if ((y * y) <= 2e+306) {
		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+306) 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+306) {
		tmp = (x * x) - (y * y);
	} else {
		tmp = y * -y;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y * y) <= 2e+306:
		tmp = (x * x) - (y * y)
	else:
		tmp = y * -y
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(y * y) <= 2e+306)
		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+306)
		tmp = (x * x) - (y * y);
	else
		tmp = y * -y;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e+306], 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^{+306}:\\
\;\;\;\;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) < 2.00000000000000003e306

    1. Initial program 100.0%

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

    if 2.00000000000000003e306 < (*.f64 y y)

    1. Initial program 77.4%

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

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

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

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

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

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

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

Alternative 3: 65.2% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 7.5 \cdot 10^{-66} \lor \neg \left(y \leq 2.65 \cdot 10^{-39}\right) \land y \leq 2.4 \cdot 10^{+48}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= y 7.5e-66) (and (not (<= y 2.65e-39)) (<= y 2.4e+48)))
   (* x x)
   (* y (- y))))
double code(double x, double y) {
	double tmp;
	if ((y <= 7.5e-66) || (!(y <= 2.65e-39) && (y <= 2.4e+48))) {
		tmp = x * x;
	} 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 <= 7.5d-66) .or. (.not. (y <= 2.65d-39)) .and. (y <= 2.4d+48)) then
        tmp = x * x
    else
        tmp = y * -y
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if ((y <= 7.5e-66) || (!(y <= 2.65e-39) && (y <= 2.4e+48))) {
		tmp = x * x;
	} else {
		tmp = y * -y;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if (y <= 7.5e-66) or (not (y <= 2.65e-39) and (y <= 2.4e+48)):
		tmp = x * x
	else:
		tmp = y * -y
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y <= 7.5e-66) || (!(y <= 2.65e-39) && (y <= 2.4e+48)))
		tmp = Float64(x * x);
	else
		tmp = Float64(y * Float64(-y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y <= 7.5e-66) || (~((y <= 2.65e-39)) && (y <= 2.4e+48)))
		tmp = x * x;
	else
		tmp = y * -y;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[Or[LessEqual[y, 7.5e-66], And[N[Not[LessEqual[y, 2.65e-39]], $MachinePrecision], LessEqual[y, 2.4e+48]]], N[(x * x), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq 7.5 \cdot 10^{-66} \lor \neg \left(y \leq 2.65 \cdot 10^{-39}\right) \land y \leq 2.4 \cdot 10^{+48}:\\
\;\;\;\;x \cdot x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 7.49999999999999995e-66 or 2.65000000000000002e-39 < y < 2.4000000000000001e48

    1. Initial program 96.9%

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

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

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

      \[\leadsto \color{blue}{x \cdot x} \]

    if 7.49999999999999995e-66 < y < 2.65000000000000002e-39 or 2.4000000000000001e48 < y

    1. Initial program 87.1%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 7.5 \cdot 10^{-66} \lor \neg \left(y \leq 2.65 \cdot 10^{-39}\right) \land y \leq 2.4 \cdot 10^{+48}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \end{array} \]

Alternative 4: 54.2% 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 54.9%

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

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

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

    \[\leadsto x \cdot x \]

Reproduce

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