
(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 y) (fma y y (* x x))))
double code(double x, double y) {
return fma(y, (y + y), fma(y, y, (x * x)));
}
function code(x, y) return fma(y, Float64(y + y), fma(y, y, Float64(x * x))) end
code[x_, y_] := N[(y * N[(y + y), $MachinePrecision] + N[(y * y + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y + y, \mathsf{fma}\left(y, y, x \cdot x\right)\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (if (<= y 4.5e-57) (* x x) (* 3.0 (* y y))))
double code(double x, double y) {
double tmp;
if (y <= 4.5e-57) {
tmp = x * x;
} else {
tmp = 3.0 * (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 <= 4.5d-57) then
tmp = x * x
else
tmp = 3.0d0 * (y * y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 4.5e-57) {
tmp = x * x;
} else {
tmp = 3.0 * (y * y);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 4.5e-57: tmp = x * x else: tmp = 3.0 * (y * y) return tmp
function code(x, y) tmp = 0.0 if (y <= 4.5e-57) tmp = Float64(x * x); else tmp = Float64(3.0 * Float64(y * y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 4.5e-57) tmp = x * x; else tmp = 3.0 * (y * y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 4.5e-57], N[(x * x), $MachinePrecision], N[(3.0 * N[(y * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.5 \cdot 10^{-57}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(y \cdot y\right)\\
\end{array}
\end{array}
if y < 4.49999999999999973e-57Initial program 99.9%
Applied rewrites59.9%
Taylor expanded in y around 0
Applied rewrites99.9%
Taylor expanded in x around inf
distribute-rgt-inN/A
*-commutativeN/A
mul-1-negN/A
distribute-neg-fracN/A
associate-*r/N/A
distribute-rgt1-inN/A
metadata-evalN/A
mul0-lftN/A
metadata-evalN/A
mul0-rgtN/A
mul0-lftN/A
metadata-evalN/A
distribute-rgt1-inN/A
*-lft-identityN/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
Applied rewrites61.5%
if 4.49999999999999973e-57 < y Initial program 99.9%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6484.4
Applied rewrites84.4%
(FPCore (x y) :precision binary64 (fma 3.0 (* y y) (* x x)))
double code(double x, double y) {
return fma(3.0, (y * y), (x * x));
}
function code(x, y) return fma(3.0, Float64(y * y), Float64(x * x)) end
code[x_, y_] := N[(3.0 * N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(3, y \cdot y, x \cdot x\right)
\end{array}
Initial program 99.9%
Applied rewrites52.2%
Taylor expanded in y around 0
Applied rewrites99.9%
(FPCore (x y) :precision binary64 (fma (* 3.0 y) y (* x x)))
double code(double x, double y) {
return fma((3.0 * y), y, (x * x));
}
function code(x, y) return fma(Float64(3.0 * y), y, Float64(x * x)) end
code[x_, y_] := N[(N[(3.0 * y), $MachinePrecision] * y + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(3 \cdot y, y, x \cdot x\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
+-commutativeN/A
associate-+r+N/A
distribute-lft1-inN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.9
Applied rewrites99.9%
(FPCore (x y) :precision binary64 (if (<= y 8.4e+86) (* x x) (* y y)))
double code(double x, double y) {
double tmp;
if (y <= 8.4e+86) {
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 <= 8.4d+86) 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 <= 8.4e+86) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 8.4e+86: tmp = x * x else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (y <= 8.4e+86) tmp = Float64(x * x); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 8.4e+86) tmp = x * x; else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 8.4e+86], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 8.4 \cdot 10^{+86}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 8.3999999999999996e86Initial program 99.9%
Applied rewrites62.9%
Taylor expanded in y around 0
Applied rewrites99.9%
Taylor expanded in x around inf
distribute-rgt-inN/A
*-commutativeN/A
mul-1-negN/A
distribute-neg-fracN/A
associate-*r/N/A
distribute-rgt1-inN/A
metadata-evalN/A
mul0-lftN/A
metadata-evalN/A
mul0-rgtN/A
mul0-lftN/A
metadata-evalN/A
distribute-rgt1-inN/A
*-lft-identityN/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
Applied rewrites59.8%
if 8.3999999999999996e86 < y Initial program 99.9%
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
Applied rewrites100.0%
lift-+.f64N/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6497.9
Applied rewrites97.9%
lift-fma.f64N/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
rem-square-sqrtN/A
sqrt-prodN/A
sqr-neg-revN/A
sqrt-unprodN/A
rem-square-sqrtN/A
lower--.f64N/A
lift-fma.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
distribute-lft-inN/A
fp-cancel-sign-sub-invN/A
Applied rewrites74.2%
Final simplification62.3%
(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}
Initial program 99.9%
Applied rewrites52.2%
Taylor expanded in y around 0
Applied rewrites99.9%
Taylor expanded in x around inf
distribute-rgt-inN/A
*-commutativeN/A
mul-1-negN/A
distribute-neg-fracN/A
associate-*r/N/A
distribute-rgt1-inN/A
metadata-evalN/A
mul0-lftN/A
metadata-evalN/A
mul0-rgtN/A
mul0-lftN/A
metadata-evalN/A
distribute-rgt1-inN/A
*-lft-identityN/A
*-lft-identityN/A
metadata-evalN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
Applied rewrites52.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 2024339
(FPCore (x y)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
:precision binary64
:alt
(! :herbie-platform default (+ (* x x) (* y (+ y (+ y y)))))
(+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))