
(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 7 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 (* y (+ y y)))))
double code(double x, double y) {
return fma(y, y, fma(x, x, (y * (y + y))));
}
function code(x, y) return fma(y, y, fma(x, x, Float64(y * Float64(y + y)))) end
code[x_, y_] := N[(y * y + N[(x * x + N[(y * N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, y \cdot \left(y + y\right)\right)\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
fma-def99.9%
associate-+l+100.0%
fma-def100.0%
distribute-lft-out100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (fma x x (* (* y y) 3.0)))
double code(double x, double y) {
return fma(x, x, ((y * y) * 3.0));
}
function code(x, y) return fma(x, x, Float64(Float64(y * y) * 3.0)) end
code[x_, y_] := N[(x * x + N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 3\right)
\end{array}
Initial program 99.9%
associate-+l+99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
distribute-rgt1-in99.9%
*-commutative99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (or (<= x -1.25e-144) (not (<= x 4.6e-84))) (+ (* y y) (* x x)) (* (* y y) 3.0)))
double code(double x, double y) {
double tmp;
if ((x <= -1.25e-144) || !(x <= 4.6e-84)) {
tmp = (y * y) + (x * x);
} else {
tmp = (y * y) * 3.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-1.25d-144)) .or. (.not. (x <= 4.6d-84))) then
tmp = (y * y) + (x * x)
else
tmp = (y * y) * 3.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.25e-144) || !(x <= 4.6e-84)) {
tmp = (y * y) + (x * x);
} else {
tmp = (y * y) * 3.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.25e-144) or not (x <= 4.6e-84): tmp = (y * y) + (x * x) else: tmp = (y * y) * 3.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.25e-144) || !(x <= 4.6e-84)) tmp = Float64(Float64(y * y) + Float64(x * x)); else tmp = Float64(Float64(y * y) * 3.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.25e-144) || ~((x <= 4.6e-84))) tmp = (y * y) + (x * x); else tmp = (y * y) * 3.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.25e-144], N[Not[LessEqual[x, 4.6e-84]], $MachinePrecision]], N[(N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.25 \cdot 10^{-144} \lor \neg \left(x \leq 4.6 \cdot 10^{-84}\right):\\
\;\;\;\;y \cdot y + x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\end{array}
\end{array}
if x < -1.2499999999999999e-144 or 4.59999999999999961e-84 < x Initial program 99.9%
add-sqr-sqrt99.9%
fma-def99.9%
+-commutative99.9%
add-sqr-sqrt99.9%
hypot-def99.9%
hypot-def99.9%
+-commutative99.9%
add-sqr-sqrt99.9%
hypot-def99.9%
hypot-def99.9%
Applied egg-rr99.9%
fma-udef99.9%
unpow299.9%
Simplified99.9%
Taylor expanded in y around 0 91.1%
unpow291.1%
Simplified91.1%
if -1.2499999999999999e-144 < x < 4.59999999999999961e-84Initial program 99.8%
Taylor expanded in x around 0 96.0%
unpow296.0%
*-commutative96.0%
associate-*l*96.0%
*-commutative96.0%
count-296.0%
Simplified96.0%
distribute-lft-out95.9%
count-295.9%
metadata-eval95.9%
*-un-lft-identity95.9%
distribute-rgt-out95.9%
metadata-eval95.9%
metadata-eval95.9%
associate-*r*96.0%
Applied egg-rr96.0%
Final simplification92.6%
(FPCore (x y) :precision binary64 (if (<= (* y y) 5e+101) (* x x) (* y (* y 3.0))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+101) {
tmp = x * x;
} else {
tmp = y * (y * 3.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y * y) <= 5d+101) then
tmp = x * x
else
tmp = y * (y * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+101) {
tmp = x * x;
} else {
tmp = y * (y * 3.0);
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 5e+101: tmp = x * x else: tmp = y * (y * 3.0) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 5e+101) tmp = Float64(x * x); else tmp = Float64(y * Float64(y * 3.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 5e+101) tmp = x * x; else tmp = y * (y * 3.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 5e+101], N[(x * x), $MachinePrecision], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 5 \cdot 10^{+101}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 4.99999999999999989e101Initial program 99.9%
Taylor expanded in x around inf 85.0%
unpow285.0%
Simplified85.0%
if 4.99999999999999989e101 < (*.f64 y y) Initial program 99.9%
Taylor expanded in x around 0 88.9%
unpow288.9%
unpow288.9%
distribute-rgt1-in88.9%
metadata-eval88.9%
*-commutative88.9%
associate-*r*88.9%
Simplified88.9%
Final simplification86.9%
(FPCore (x y) :precision binary64 (+ (* y (* y 3.0)) (* x x)))
double code(double x, double y) {
return (y * (y * 3.0)) + (x * x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y * (y * 3.0d0)) + (x * x)
end function
public static double code(double x, double y) {
return (y * (y * 3.0)) + (x * x);
}
def code(x, y): return (y * (y * 3.0)) + (x * x)
function code(x, y) return Float64(Float64(y * Float64(y * 3.0)) + Float64(x * x)) end
function tmp = code(x, y) tmp = (y * (y * 3.0)) + (x * x); end
code[x_, y_] := N[(N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(y \cdot 3\right) + x \cdot x
\end{array}
Initial program 99.9%
associate-+l+99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
distribute-rgt1-in99.9%
*-commutative99.9%
metadata-eval99.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
associate-*l*99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= (* y y) 1.7e+304) (* x x) (* y y)))
double code(double x, double y) {
double tmp;
if ((y * y) <= 1.7e+304) {
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 * y) <= 1.7d+304) 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 * y) <= 1.7e+304) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 1.7e+304: tmp = x * x else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 1.7e+304) tmp = Float64(x * x); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 1.7e+304) tmp = x * x; else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1.7e+304], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 1.7 \cdot 10^{+304}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if (*.f64 y y) < 1.7000000000000001e304Initial program 99.8%
Taylor expanded in x around inf 70.0%
unpow270.0%
Simplified70.0%
if 1.7000000000000001e304 < (*.f64 y y) Initial program 100.0%
add-sqr-sqrt100.0%
fma-def100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
hypot-def100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
hypot-def100.0%
Applied egg-rr100.0%
fma-udef100.0%
unpow2100.0%
Simplified100.0%
Taylor expanded in y around 0 97.9%
unpow297.9%
Simplified97.9%
Taylor expanded in x around 0 97.9%
unpow297.9%
Simplified97.9%
Final simplification78.5%
(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%
Taylor expanded in x around inf 57.6%
unpow257.6%
Simplified57.6%
Final simplification57.6%
(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 2023196
(FPCore (x y)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
:precision binary64
:herbie-target
(+ (* x x) (* y (+ y (+ y y))))
(+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))