
(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 (+ (* x x) (fma y (* y 2.0) (* y y))))
double code(double x, double y) {
return (x * x) + fma(y, (y * 2.0), (y * y));
}
function code(x, y) return Float64(Float64(x * x) + fma(y, Float64(y * 2.0), Float64(y * y))) end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * 2.0), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \mathsf{fma}\left(y, y \cdot 2, y \cdot y\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
count-299.9%
fma-def99.9%
associate-+l+99.9%
*-un-lft-identity99.9%
*-un-lft-identity99.9%
associate-+l+99.9%
associate-+l+99.9%
count-299.9%
associate-*r*99.9%
*-commutative99.9%
fma-def99.9%
*-commutative99.9%
Applied egg-rr99.9%
Final simplification99.9%
(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%
cancel-sign-sub99.9%
neg-mul-199.9%
associate-*l*99.9%
count-299.9%
distribute-rgt-out--99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (fma (* y y) 3.0 (* x x)))
double code(double x, double y) {
return fma((y * y), 3.0, (x * x));
}
function code(x, y) return fma(Float64(y * y), 3.0, Float64(x * x)) end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] * 3.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y \cdot y, 3, x \cdot x\right)
\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%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(if (or (<= (* x x) 1.08e-138)
(and (not (<= (* x x) 90000000.0)) (<= (* x x) 1.7e+116)))
(* y (* y 3.0))
(* x x)))
double code(double x, double y) {
double tmp;
if (((x * x) <= 1.08e-138) || (!((x * x) <= 90000000.0) && ((x * x) <= 1.7e+116))) {
tmp = y * (y * 3.0);
} else {
tmp = x * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((x * x) <= 1.08d-138) .or. (.not. ((x * x) <= 90000000.0d0)) .and. ((x * x) <= 1.7d+116)) then
tmp = y * (y * 3.0d0)
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((x * x) <= 1.08e-138) || (!((x * x) <= 90000000.0) && ((x * x) <= 1.7e+116))) {
tmp = y * (y * 3.0);
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) <= 1.08e-138) or (not ((x * x) <= 90000000.0) and ((x * x) <= 1.7e+116)): tmp = y * (y * 3.0) else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if ((Float64(x * x) <= 1.08e-138) || (!(Float64(x * x) <= 90000000.0) && (Float64(x * x) <= 1.7e+116))) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((x * x) <= 1.08e-138) || (~(((x * x) <= 90000000.0)) && ((x * x) <= 1.7e+116))) tmp = y * (y * 3.0); else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 1.08e-138], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 90000000.0]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 1.7e+116]]], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1.08 \cdot 10^{-138} \lor \neg \left(x \cdot x \leq 90000000\right) \land x \cdot x \leq 1.7 \cdot 10^{+116}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 1.0799999999999999e-138 or 9e7 < (*.f64 x x) < 1.70000000000000011e116Initial program 99.8%
Taylor expanded in x around 0 87.7%
Simplified87.7%
if 1.0799999999999999e-138 < (*.f64 x x) < 9e7 or 1.70000000000000011e116 < (*.f64 x x) Initial program 99.9%
+-commutative99.9%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
count-299.9%
fma-def99.9%
associate-+l+99.9%
*-un-lft-identity99.9%
*-un-lft-identity99.9%
associate-+l+99.9%
associate-+l+99.9%
count-299.9%
associate-*r*99.9%
*-commutative99.9%
fma-def99.9%
*-commutative99.9%
Applied egg-rr99.9%
Taylor expanded in y around 0 99.9%
Simplified99.9%
Taylor expanded in x around inf 84.7%
unpow284.7%
Simplified84.7%
Final simplification86.1%
(FPCore (x y)
:precision binary64
(if (<= (* x x) 2.65e-135)
(* (* y y) 3.0)
(if (<= (* x x) 31000000.0)
(* x x)
(if (<= (* x x) 3.2e+116) (* y (* y 3.0)) (* x x)))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 2.65e-135) {
tmp = (y * y) * 3.0;
} else if ((x * x) <= 31000000.0) {
tmp = x * x;
} else if ((x * x) <= 3.2e+116) {
tmp = y * (y * 3.0);
} else {
tmp = x * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x * x) <= 2.65d-135) then
tmp = (y * y) * 3.0d0
else if ((x * x) <= 31000000.0d0) then
tmp = x * x
else if ((x * x) <= 3.2d+116) then
tmp = y * (y * 3.0d0)
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x * x) <= 2.65e-135) {
tmp = (y * y) * 3.0;
} else if ((x * x) <= 31000000.0) {
tmp = x * x;
} else if ((x * x) <= 3.2e+116) {
tmp = y * (y * 3.0);
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 2.65e-135: tmp = (y * y) * 3.0 elif (x * x) <= 31000000.0: tmp = x * x elif (x * x) <= 3.2e+116: tmp = y * (y * 3.0) else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 2.65e-135) tmp = Float64(Float64(y * y) * 3.0); elseif (Float64(x * x) <= 31000000.0) tmp = Float64(x * x); elseif (Float64(x * x) <= 3.2e+116) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 2.65e-135) tmp = (y * y) * 3.0; elseif ((x * x) <= 31000000.0) tmp = x * x; elseif ((x * x) <= 3.2e+116) tmp = y * (y * 3.0); else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 2.65e-135], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 31000000.0], N[(x * x), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 3.2e+116], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 2.65 \cdot 10^{-135}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\mathbf{elif}\;x \cdot x \leq 31000000:\\
\;\;\;\;x \cdot x\\
\mathbf{elif}\;x \cdot x \leq 3.2 \cdot 10^{+116}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 2.65e-135Initial program 99.8%
Taylor expanded in x around 0 90.4%
Simplified90.4%
add-sqr-sqrt90.2%
pow290.2%
associate-*r*90.2%
sqrt-prod90.1%
sqrt-prod43.5%
add-sqr-sqrt90.1%
Applied egg-rr90.1%
unpow290.1%
swap-sqr89.9%
add-sqr-sqrt90.4%
Applied egg-rr90.4%
if 2.65e-135 < (*.f64 x x) < 3.1e7 or 3.2e116 < (*.f64 x x) Initial program 99.9%
+-commutative99.9%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
count-299.9%
fma-def99.9%
associate-+l+99.9%
*-un-lft-identity99.9%
*-un-lft-identity99.9%
associate-+l+99.9%
associate-+l+99.9%
count-299.9%
associate-*r*99.9%
*-commutative99.9%
fma-def99.9%
*-commutative99.9%
Applied egg-rr99.9%
Taylor expanded in y around 0 99.9%
Simplified99.9%
Taylor expanded in x around inf 84.7%
unpow284.7%
Simplified84.7%
if 3.1e7 < (*.f64 x x) < 3.2e116Initial program 99.9%
Taylor expanded in x around 0 71.7%
Simplified71.7%
Final simplification86.1%
(FPCore (x y) :precision binary64 (+ (* x x) (* (* y y) 3.0)))
double code(double x, double y) {
return (x * x) + ((y * y) * 3.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + ((y * y) * 3.0d0)
end function
public static double code(double x, double y) {
return (x * x) + ((y * y) * 3.0);
}
def code(x, y): return (x * x) + ((y * y) * 3.0)
function code(x, y) return Float64(Float64(x * x) + Float64(Float64(y * y) * 3.0)) end
function tmp = code(x, y) tmp = (x * x) + ((y * y) * 3.0); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \left(y \cdot y\right) \cdot 3
\end{array}
Initial program 99.9%
+-commutative99.9%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
count-299.9%
fma-def99.9%
associate-+l+99.9%
*-un-lft-identity99.9%
*-un-lft-identity99.9%
associate-+l+99.9%
associate-+l+99.9%
count-299.9%
associate-*r*99.9%
*-commutative99.9%
fma-def99.9%
*-commutative99.9%
Applied egg-rr99.9%
Taylor expanded in y around 0 99.9%
Simplified99.9%
Final simplification99.9%
(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%
+-commutative99.9%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
count-299.9%
fma-def99.9%
associate-+l+99.9%
*-un-lft-identity99.9%
*-un-lft-identity99.9%
associate-+l+99.9%
associate-+l+99.9%
count-299.9%
associate-*r*99.9%
*-commutative99.9%
fma-def99.9%
*-commutative99.9%
Applied egg-rr99.9%
Taylor expanded in y around 0 99.9%
Simplified99.9%
Taylor expanded in x around inf 58.5%
unpow258.5%
Simplified58.5%
Final simplification58.5%
(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 2023271
(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)))