
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (((x * x) + (y * y)) + (y * y)) + (y * y)
end function
public static double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
def code(x, y): return (((x * x) + (y * y)) + (y * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = (((x * x) + (y * y)) + (y * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (((x * x) + (y * y)) + (y * y)) + (y * y)
end function
public static double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
def code(x, y): return (((x * x) + (y * y)) + (y * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = (((x * x) + (y * y)) + (y * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma y y (fma x x (* 2.0 (* y y)))))
double code(double x, double y) {
return fma(y, y, fma(x, x, (2.0 * (y * y))));
}
function code(x, y) return fma(y, y, fma(x, x, Float64(2.0 * Float64(y * y)))) end
code[x_, y_] := N[(y * y + N[(x * x + N[(2.0 * N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, 2 \cdot \left(y \cdot y\right)\right)\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
sqr-neg99.9%
+-commutative99.9%
sqr-neg99.9%
+-commutative99.9%
fma-define100.0%
sqr-neg100.0%
sqr-neg100.0%
associate-+l+100.0%
fma-define100.0%
count-2100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (+ (* y y) (fma (* y 2.0) y (* x x))))
double code(double x, double y) {
return (y * y) + fma((y * 2.0), y, (x * x));
}
function code(x, y) return Float64(Float64(y * y) + fma(Float64(y * 2.0), y, Float64(x * x))) end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] + N[(N[(y * 2.0), $MachinePrecision] * y + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y + \mathsf{fma}\left(y \cdot 2, y, x \cdot x\right)
\end{array}
Initial program 99.9%
associate-+l+99.9%
+-commutative99.9%
count-299.9%
*-commutative99.9%
*-commutative99.9%
associate-*r*99.9%
fma-define99.9%
*-commutative99.9%
pow299.9%
Applied egg-rr99.9%
unpow280.4%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= x 1.8e-135) (* y (* y 3.0)) (* (* x x) (+ 1.0 (* 3.0 (* (/ y x) (/ y x)))))))
double code(double x, double y) {
double tmp;
if (x <= 1.8e-135) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x))));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 1.8d-135) then
tmp = y * (y * 3.0d0)
else
tmp = (x * x) * (1.0d0 + (3.0d0 * ((y / x) * (y / x))))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 1.8e-135) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x))));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1.8e-135: tmp = y * (y * 3.0) else: tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x)))) return tmp
function code(x, y) tmp = 0.0 if (x <= 1.8e-135) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(Float64(x * x) * Float64(1.0 + Float64(3.0 * Float64(Float64(y / x) * Float64(y / x))))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1.8e-135) tmp = y * (y * 3.0); else tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x)))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1.8e-135], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] * N[(1.0 + N[(3.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.8 \cdot 10^{-135}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(1 + 3 \cdot \left(\frac{y}{x} \cdot \frac{y}{x}\right)\right)\\
\end{array}
\end{array}
if x < 1.79999999999999989e-135Initial program 99.9%
add-sqr-sqrt99.7%
pow299.7%
+-commutative99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
+-commutative99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
hypot-define99.7%
Applied egg-rr99.7%
Taylor expanded in y around inf 67.2%
unpow-prod-down67.1%
pow267.1%
pow267.1%
rem-square-sqrt67.4%
associate-*l*67.4%
Applied egg-rr67.4%
if 1.79999999999999989e-135 < x Initial program 99.9%
Taylor expanded in x around inf 86.6%
Simplified86.6%
pow286.6%
unpow286.6%
times-frac96.7%
Applied egg-rr96.7%
unpow296.7%
Applied egg-rr96.7%
Final simplification77.6%
(FPCore (x y) :precision binary64 (if (<= x 1.3e-134) (* y (* y 3.0)) (* (* x x) (+ 1.0 (* 3.0 (/ y (* x (/ x y))))))))
double code(double x, double y) {
double tmp;
if (x <= 1.3e-134) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * (y / (x * (x / y)))));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 1.3d-134) then
tmp = y * (y * 3.0d0)
else
tmp = (x * x) * (1.0d0 + (3.0d0 * (y / (x * (x / y)))))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 1.3e-134) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * (y / (x * (x / y)))));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1.3e-134: tmp = y * (y * 3.0) else: tmp = (x * x) * (1.0 + (3.0 * (y / (x * (x / y))))) return tmp
function code(x, y) tmp = 0.0 if (x <= 1.3e-134) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(Float64(x * x) * Float64(1.0 + Float64(3.0 * Float64(y / Float64(x * Float64(x / y)))))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1.3e-134) tmp = y * (y * 3.0); else tmp = (x * x) * (1.0 + (3.0 * (y / (x * (x / y))))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1.3e-134], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] * N[(1.0 + N[(3.0 * N[(y / N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.3 \cdot 10^{-134}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(1 + 3 \cdot \frac{y}{x \cdot \frac{x}{y}}\right)\\
\end{array}
\end{array}
if x < 1.30000000000000011e-134Initial program 99.9%
add-sqr-sqrt99.7%
pow299.7%
+-commutative99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
+-commutative99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
hypot-define99.7%
Applied egg-rr99.7%
Taylor expanded in y around inf 67.2%
unpow-prod-down67.1%
pow267.1%
pow267.1%
rem-square-sqrt67.4%
associate-*l*67.4%
Applied egg-rr67.4%
if 1.30000000000000011e-134 < x Initial program 99.9%
Taylor expanded in x around inf 86.6%
Simplified86.6%
pow286.6%
unpow286.6%
times-frac96.7%
Applied egg-rr96.7%
unpow296.7%
Applied egg-rr96.7%
clear-num96.7%
frac-times96.8%
*-un-lft-identity96.8%
Applied egg-rr96.8%
Final simplification77.6%
(FPCore (x y) :precision binary64 (+ (* y y) (+ (* y y) (+ (* y y) (* x x)))))
double code(double x, double y) {
return (y * y) + ((y * y) + ((y * y) + (x * x)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y * y) + ((y * y) + ((y * y) + (x * x)))
end function
public static double code(double x, double y) {
return (y * y) + ((y * y) + ((y * y) + (x * x)));
}
def code(x, y): return (y * y) + ((y * y) + ((y * y) + (x * x)))
function code(x, y) return Float64(Float64(y * y) + Float64(Float64(y * y) + Float64(Float64(y * y) + Float64(x * x)))) end
function tmp = code(x, y) tmp = (y * y) + ((y * y) + ((y * y) + (x * x))); end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y + \left(y \cdot y + \left(y \cdot y + x \cdot x\right)\right)
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (* y (* y 3.0)))
double code(double x, double y) {
return y * (y * 3.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * (y * 3.0d0)
end function
public static double code(double x, double y) {
return y * (y * 3.0);
}
def code(x, y): return y * (y * 3.0)
function code(x, y) return Float64(y * Float64(y * 3.0)) end
function tmp = code(x, y) tmp = y * (y * 3.0); end
code[x_, y_] := N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(y \cdot 3\right)
\end{array}
Initial program 99.9%
add-sqr-sqrt99.8%
pow299.8%
+-commutative99.8%
add-sqr-sqrt99.7%
hypot-define99.8%
+-commutative99.8%
add-sqr-sqrt99.8%
hypot-define99.8%
hypot-define99.8%
Applied egg-rr99.8%
Taylor expanded in y around inf 58.7%
unpow-prod-down58.6%
pow258.6%
pow258.6%
rem-square-sqrt58.8%
associate-*l*58.8%
Applied egg-rr58.8%
Final simplification58.8%
(FPCore (x y) :precision binary64 (+ (* x x) (* y (+ y (+ y y)))))
double code(double x, double y) {
return (x * x) + (y * (y + (y + y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + (y * (y + (y + y)))
end function
public static double code(double x, double y) {
return (x * x) + (y * (y + (y + y)));
}
def code(x, y): return (x * x) + (y * (y + (y + y)))
function code(x, y) return Float64(Float64(x * x) + Float64(y * Float64(y + Float64(y + y)))) end
function tmp = code(x, y) tmp = (x * x) + (y * (y + (y + y))); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * N[(y + N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + y \cdot \left(y + \left(y + y\right)\right)
\end{array}
herbie shell --seed 2024060
(FPCore (x y)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
:precision binary64
:alt
(+ (* x x) (* y (+ y (+ y y))))
(+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))