Graphics.Rasterific.Linear:$cquadrance from Rasterific-0.6.1

Percentage Accurate: 100.0% → 100.0%
Time: 1.9s
Alternatives: 2
Speedup: 1.0×

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 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: 100.0% 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: 100.0% accurate, 0.0× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ [x, y_m] = \mathsf{sort}([x, y_m])\\ \\ \mathsf{fma}\left(y_m, y_m, {x}^{2}\right) \end{array} \]
y_m = (fabs.f64 y)
NOTE: x and y_m should be sorted in increasing order before calling this function.
(FPCore (x y_m) :precision binary64 (fma y_m y_m (pow x 2.0)))
y_m = fabs(y);
assert(x < y_m);
double code(double x, double y_m) {
	return fma(y_m, y_m, pow(x, 2.0));
}
y_m = abs(y)
x, y_m = sort([x, y_m])
function code(x, y_m)
	return fma(y_m, y_m, (x ^ 2.0))
end
y_m = N[Abs[y], $MachinePrecision]
NOTE: x and y_m should be sorted in increasing order before calling this function.
code[x_, y$95$m_] := N[(y$95$m * y$95$m + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|
\\
[x, y_m] = \mathsf{sort}([x, y_m])\\
\\
\mathsf{fma}\left(y_m, y_m, {x}^{2}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[x \cdot x + y \cdot y \]
  2. Taylor expanded in x around 0 100.0%

    \[\leadsto \color{blue}{{x}^{2} + {y}^{2}} \]
  3. Step-by-step derivation
    1. +-commutative100.0%

      \[\leadsto \color{blue}{{y}^{2} + {x}^{2}} \]
    2. unpow2100.0%

      \[\leadsto \color{blue}{y \cdot y} + {x}^{2} \]
    3. fma-udef100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, y, {x}^{2}\right)} \]
  4. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y, y, {x}^{2}\right)} \]
  5. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(y, y, {x}^{2}\right) \]

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} y_m = \left|y\right| \\ [x, y_m] = \mathsf{sort}([x, y_m])\\ \\ x \cdot x + y_m \cdot y_m \end{array} \]
y_m = (fabs.f64 y)
NOTE: x and y_m should be sorted in increasing order before calling this function.
(FPCore (x y_m) :precision binary64 (+ (* x x) (* y_m y_m)))
y_m = fabs(y);
assert(x < y_m);
double code(double x, double y_m) {
	return (x * x) + (y_m * y_m);
}
y_m = abs(y)
NOTE: x and y_m should be sorted in increasing order before calling this function.
real(8) function code(x, y_m)
    real(8), intent (in) :: x
    real(8), intent (in) :: y_m
    code = (x * x) + (y_m * y_m)
end function
y_m = Math.abs(y);
assert x < y_m;
public static double code(double x, double y_m) {
	return (x * x) + (y_m * y_m);
}
y_m = math.fabs(y)
[x, y_m] = sort([x, y_m])
def code(x, y_m):
	return (x * x) + (y_m * y_m)
y_m = abs(y)
x, y_m = sort([x, y_m])
function code(x, y_m)
	return Float64(Float64(x * x) + Float64(y_m * y_m))
end
y_m = abs(y);
x, y_m = num2cell(sort([x, y_m])){:}
function tmp = code(x, y_m)
	tmp = (x * x) + (y_m * y_m);
end
y_m = N[Abs[y], $MachinePrecision]
NOTE: x and y_m should be sorted in increasing order before calling this function.
code[x_, y$95$m_] := N[(N[(x * x), $MachinePrecision] + N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|
\\
[x, y_m] = \mathsf{sort}([x, y_m])\\
\\
x \cdot x + y_m \cdot y_m
\end{array}
Derivation
  1. Initial program 100.0%

    \[x \cdot x + y \cdot y \]
  2. Final simplification100.0%

    \[\leadsto x \cdot x + y \cdot y \]

Reproduce

?
herbie shell --seed 2023334 
(FPCore (x y)
  :name "Graphics.Rasterific.Linear:$cquadrance from Rasterific-0.6.1"
  :precision binary64
  (+ (* x x) (* y y)))