
(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%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(if (<= (* x x) 7e-187)
(* y (* y 3.0))
(if (or (<= (* x x) 1.42e-149) (not (<= (* x x) 1.26e-83)))
(+ (* y y) (* x x))
(* (* y y) 3.0))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 7e-187) {
tmp = y * (y * 3.0);
} else if (((x * x) <= 1.42e-149) || !((x * x) <= 1.26e-83)) {
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 * x) <= 7d-187) then
tmp = y * (y * 3.0d0)
else if (((x * x) <= 1.42d-149) .or. (.not. ((x * x) <= 1.26d-83))) 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 * x) <= 7e-187) {
tmp = y * (y * 3.0);
} else if (((x * x) <= 1.42e-149) || !((x * x) <= 1.26e-83)) {
tmp = (y * y) + (x * x);
} else {
tmp = (y * y) * 3.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 7e-187: tmp = y * (y * 3.0) elif ((x * x) <= 1.42e-149) or not ((x * x) <= 1.26e-83): tmp = (y * y) + (x * x) else: tmp = (y * y) * 3.0 return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 7e-187) tmp = Float64(y * Float64(y * 3.0)); elseif ((Float64(x * x) <= 1.42e-149) || !(Float64(x * x) <= 1.26e-83)) 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 * x) <= 7e-187) tmp = y * (y * 3.0); elseif (((x * x) <= 1.42e-149) || ~(((x * x) <= 1.26e-83))) tmp = (y * y) + (x * x); else tmp = (y * y) * 3.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 7e-187], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[N[(x * x), $MachinePrecision], 1.42e-149], N[Not[LessEqual[N[(x * x), $MachinePrecision], 1.26e-83]], $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 \cdot x \leq 7 \cdot 10^{-187}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{elif}\;x \cdot x \leq 1.42 \cdot 10^{-149} \lor \neg \left(x \cdot x \leq 1.26 \cdot 10^{-83}\right):\\
\;\;\;\;y \cdot y + x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\end{array}
\end{array}
if (*.f64 x x) < 6.99999999999999958e-187Initial program 99.7%
Taylor expanded in x around 0 95.9%
Simplified95.9%
if 6.99999999999999958e-187 < (*.f64 x x) < 1.42e-149 or 1.2600000000000001e-83 < (*.f64 x x) Initial program 99.9%
associate-+l+99.9%
count-299.9%
flip-+20.3%
pow220.3%
pow220.3%
pow-prod-up20.2%
metadata-eval20.2%
*-commutative20.2%
*-commutative20.2%
swap-sqr20.2%
pow220.2%
pow220.2%
pow-prod-up20.2%
metadata-eval20.2%
metadata-eval20.2%
*-commutative20.2%
associate-*l*20.2%
Applied egg-rr20.2%
Taylor expanded in x around inf 92.4%
unpow292.4%
Simplified92.4%
if 1.42e-149 < (*.f64 x x) < 1.2600000000000001e-83Initial program 99.8%
Taylor expanded in x around 0 85.3%
Simplified85.3%
Taylor expanded in y around 0 85.3%
unpow285.3%
Simplified85.3%
Final simplification93.3%
(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%
+-commutative99.9%
+-commutative99.9%
associate-+r+99.9%
count-299.9%
distribute-lft1-in99.9%
metadata-eval99.9%
*-commutative99.9%
fma-def99.9%
metadata-eval99.9%
metadata-eval99.9%
Simplified99.9%
fma-udef99.9%
associate-*l*99.9%
Applied egg-rr99.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(Float64(y * y) * 3.0) end
function tmp = code(x, y) tmp = (y * y) * 3.0; end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision]
\begin{array}{l}
\\
\left(y \cdot y\right) \cdot 3
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 61.8%
Simplified61.8%
Taylor expanded in y around 0 61.8%
unpow261.8%
Simplified61.8%
Final simplification61.8%
(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%
Taylor expanded in x around 0 61.8%
Simplified61.8%
Final simplification61.8%
(FPCore (x y) :precision binary64 (* y y))
double code(double x, double y) {
return y * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * y
end function
public static double code(double x, double y) {
return y * y;
}
def code(x, y): return y * y
function code(x, y) return Float64(y * y) end
function tmp = code(x, y) tmp = y * y; end
code[x_, y_] := N[(y * y), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y
\end{array}
Initial program 99.9%
associate-+l+99.9%
count-299.9%
flip-+26.2%
pow226.2%
pow226.2%
pow-prod-up26.1%
metadata-eval26.1%
*-commutative26.1%
*-commutative26.1%
swap-sqr26.1%
pow226.1%
pow226.1%
pow-prod-up26.1%
metadata-eval26.1%
metadata-eval26.1%
*-commutative26.1%
associate-*l*26.1%
Applied egg-rr26.1%
Taylor expanded in x around 0 16.3%
Taylor expanded in y around 0 39.8%
unpow239.8%
Simplified39.8%
Final simplification39.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 2023287
(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)))