
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (((x * y) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma z z (fma x y (* 2.0 (* z z)))))
double code(double x, double y, double z) {
return fma(z, z, fma(x, y, (2.0 * (z * z))));
}
function code(x, y, z) return fma(z, z, fma(x, y, Float64(2.0 * Float64(z * z)))) end
code[x_, y_, z_] := N[(z * z + N[(x * y + N[(2.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, 2 \cdot \left(z \cdot z\right)\right)\right)
\end{array}
Initial program 97.5%
+-commutative97.5%
fma-define97.6%
associate-+l+97.6%
fma-define99.2%
count-299.2%
Simplified99.2%
Final simplification99.2%
(FPCore (x y z) :precision binary64 (fma x y (* z (* z 3.0))))
double code(double x, double y, double z) {
return fma(x, y, (z * (z * 3.0)));
}
function code(x, y, z) return fma(x, y, Float64(z * Float64(z * 3.0))) end
code[x_, y_, z_] := N[(x * y + N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, y, z \cdot \left(z \cdot 3\right)\right)
\end{array}
Initial program 97.5%
associate-+l+97.6%
associate-+l+97.5%
fma-define99.1%
associate-+r+99.1%
distribute-lft-out99.1%
distribute-lft-out99.1%
remove-double-neg99.1%
unsub-neg99.1%
count-299.1%
neg-mul-199.1%
distribute-rgt-out--99.1%
metadata-eval99.1%
Simplified99.1%
Final simplification99.1%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 2e+294) (+ (* z z) (+ (* z z) (+ (* z z) (* x y)))) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 2e+294) {
tmp = (z * z) + ((z * z) + ((z * z) + (x * y)));
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((z * z) <= 2d+294) then
tmp = (z * z) + ((z * z) + ((z * z) + (x * y)))
else
tmp = z * (z * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 2e+294) {
tmp = (z * z) + ((z * z) + ((z * z) + (x * y)));
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 2e+294: tmp = (z * z) + ((z * z) + ((z * z) + (x * y))) else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 2e+294) tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(x * y)))); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 2e+294) tmp = (z * z) + ((z * z) + ((z * z) + (x * y))); else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 2e+294], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 2 \cdot 10^{+294}:\\
\;\;\;\;z \cdot z + \left(z \cdot z + \left(z \cdot z + x \cdot y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 2.00000000000000013e294Initial program 99.9%
if 2.00000000000000013e294 < (*.f64 z z) Initial program 91.3%
Taylor expanded in x around 0 97.1%
distribute-lft1-in97.1%
metadata-eval97.1%
*-commutative97.1%
Simplified97.1%
add-sqr-sqrt97.1%
sqrt-unprod94.6%
swap-sqr94.6%
pow-prod-up94.6%
metadata-eval94.6%
metadata-eval94.6%
Applied egg-rr94.6%
*-commutative94.6%
sqrt-prod94.6%
metadata-eval94.6%
metadata-eval94.6%
metadata-eval94.6%
sqrt-pow294.6%
sqrt-pow197.0%
metadata-eval97.0%
unpow297.0%
associate-*r*97.1%
sqrt-pow297.1%
metadata-eval97.1%
metadata-eval97.1%
Applied egg-rr97.1%
Final simplification99.1%
(FPCore (x y z) :precision binary64 (if (or (<= z 6.2e-57) (and (not (<= z 2.1e-41)) (<= z 0.155))) (* x y) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 6.2e-57) || (!(z <= 2.1e-41) && (z <= 0.155))) {
tmp = x * y;
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((z <= 6.2d-57) .or. (.not. (z <= 2.1d-41)) .and. (z <= 0.155d0)) then
tmp = x * y
else
tmp = z * (z * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 6.2e-57) || (!(z <= 2.1e-41) && (z <= 0.155))) {
tmp = x * y;
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 6.2e-57) or (not (z <= 2.1e-41) and (z <= 0.155)): tmp = x * y else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 6.2e-57) || (!(z <= 2.1e-41) && (z <= 0.155))) tmp = Float64(x * y); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 6.2e-57) || (~((z <= 2.1e-41)) && (z <= 0.155))) tmp = x * y; else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 6.2e-57], And[N[Not[LessEqual[z, 2.1e-41]], $MachinePrecision], LessEqual[z, 0.155]]], N[(x * y), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 6.2 \cdot 10^{-57} \lor \neg \left(z \leq 2.1 \cdot 10^{-41}\right) \land z \leq 0.155:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if z < 6.19999999999999952e-57 or 2.10000000000000013e-41 < z < 0.154999999999999999Initial program 99.4%
Taylor expanded in x around inf 87.2%
Taylor expanded in x around inf 86.8%
Taylor expanded in x around inf 71.6%
if 6.19999999999999952e-57 < z < 2.10000000000000013e-41 or 0.154999999999999999 < z Initial program 92.8%
Taylor expanded in x around 0 91.7%
distribute-lft1-in91.7%
metadata-eval91.7%
*-commutative91.7%
Simplified91.7%
add-sqr-sqrt91.5%
sqrt-unprod78.8%
swap-sqr78.8%
pow-prod-up78.8%
metadata-eval78.8%
metadata-eval78.8%
Applied egg-rr78.8%
*-commutative78.8%
sqrt-prod78.8%
metadata-eval78.8%
metadata-eval78.8%
metadata-eval78.8%
sqrt-pow278.6%
sqrt-pow191.3%
metadata-eval91.3%
unpow291.3%
associate-*r*91.3%
sqrt-pow291.7%
metadata-eval91.7%
metadata-eval91.7%
Applied egg-rr91.7%
Final simplification77.2%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 0.001) (+ (* z z) (+ (* z z) (* x y))) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 0.001) {
tmp = (z * z) + ((z * z) + (x * y));
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((z * z) <= 0.001d0) then
tmp = (z * z) + ((z * z) + (x * y))
else
tmp = z * (z * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 0.001) {
tmp = (z * z) + ((z * z) + (x * y));
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 0.001: tmp = (z * z) + ((z * z) + (x * y)) else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 0.001) tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(x * y))); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 0.001) tmp = (z * z) + ((z * z) + (x * y)); else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 0.001], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 0.001:\\
\;\;\;\;z \cdot z + \left(z \cdot z + x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 1e-3Initial program 99.9%
Taylor expanded in x around inf 91.8%
if 1e-3 < (*.f64 z z) Initial program 94.9%
Taylor expanded in x around 0 88.1%
distribute-lft1-in88.1%
metadata-eval88.1%
*-commutative88.1%
Simplified88.1%
add-sqr-sqrt87.9%
sqrt-unprod75.9%
swap-sqr75.9%
pow-prod-up75.9%
metadata-eval75.9%
metadata-eval75.9%
Applied egg-rr75.9%
*-commutative75.9%
sqrt-prod75.9%
metadata-eval75.9%
metadata-eval75.9%
metadata-eval75.9%
sqrt-pow275.7%
sqrt-pow187.7%
metadata-eval87.7%
unpow287.7%
associate-*r*87.8%
sqrt-pow288.1%
metadata-eval88.1%
metadata-eval88.1%
Applied egg-rr88.1%
Final simplification90.0%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 0.001) (+ (* z z) (+ (* z z) (* x y))) (+ (* z z) (+ (* z z) (* z z)))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 0.001) {
tmp = (z * z) + ((z * z) + (x * y));
} else {
tmp = (z * z) + ((z * z) + (z * z));
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((z * z) <= 0.001d0) then
tmp = (z * z) + ((z * z) + (x * y))
else
tmp = (z * z) + ((z * z) + (z * z))
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 0.001) {
tmp = (z * z) + ((z * z) + (x * y));
} else {
tmp = (z * z) + ((z * z) + (z * z));
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 0.001: tmp = (z * z) + ((z * z) + (x * y)) else: tmp = (z * z) + ((z * z) + (z * z)) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 0.001) tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(x * y))); else tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(z * z))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 0.001) tmp = (z * z) + ((z * z) + (x * y)); else tmp = (z * z) + ((z * z) + (z * z)); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 0.001], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 0.001:\\
\;\;\;\;z \cdot z + \left(z \cdot z + x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot z + \left(z \cdot z + z \cdot z\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 1e-3Initial program 99.9%
Taylor expanded in x around inf 91.8%
if 1e-3 < (*.f64 z z) Initial program 94.9%
add-cube-cbrt94.6%
pow394.6%
fma-define97.9%
pow297.9%
Applied egg-rr97.9%
rem-cbrt-cube97.8%
add-cube-cbrt97.4%
pow397.4%
rem-cbrt-cube97.5%
Applied egg-rr97.5%
Taylor expanded in x around 0 85.7%
unpow1/387.6%
Simplified87.6%
unpow387.5%
add-cube-cbrt87.9%
rem-cube-cbrt88.1%
unpow288.1%
Applied egg-rr88.1%
Final simplification90.1%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 0.001) (+ (* z z) (* x y)) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 0.001) {
tmp = (z * z) + (x * y);
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((z * z) <= 0.001d0) then
tmp = (z * z) + (x * y)
else
tmp = z * (z * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 0.001) {
tmp = (z * z) + (x * y);
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 0.001: tmp = (z * z) + (x * y) else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 0.001) tmp = Float64(Float64(z * z) + Float64(x * y)); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 0.001) tmp = (z * z) + (x * y); else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 0.001], N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 0.001:\\
\;\;\;\;z \cdot z + x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 1e-3Initial program 99.9%
Taylor expanded in x around inf 91.8%
Taylor expanded in x around inf 91.6%
if 1e-3 < (*.f64 z z) Initial program 94.9%
Taylor expanded in x around 0 88.1%
distribute-lft1-in88.1%
metadata-eval88.1%
*-commutative88.1%
Simplified88.1%
add-sqr-sqrt87.9%
sqrt-unprod75.9%
swap-sqr75.9%
pow-prod-up75.9%
metadata-eval75.9%
metadata-eval75.9%
Applied egg-rr75.9%
*-commutative75.9%
sqrt-prod75.9%
metadata-eval75.9%
metadata-eval75.9%
metadata-eval75.9%
sqrt-pow275.7%
sqrt-pow187.7%
metadata-eval87.7%
unpow287.7%
associate-*r*87.8%
sqrt-pow288.1%
metadata-eval88.1%
metadata-eval88.1%
Applied egg-rr88.1%
Final simplification89.9%
(FPCore (x y z) :precision binary64 (* x y))
double code(double x, double y, double z) {
return x * y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x * y
end function
public static double code(double x, double y, double z) {
return x * y;
}
def code(x, y, z): return x * y
function code(x, y, z) return Float64(x * y) end
function tmp = code(x, y, z) tmp = x * y; end
code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y
\end{array}
Initial program 97.5%
Taylor expanded in x around inf 80.0%
Taylor expanded in x around inf 79.5%
Taylor expanded in x around inf 55.8%
Final simplification55.8%
(FPCore (x y z) :precision binary64 (+ (* (* 3.0 z) z) (* y x)))
double code(double x, double y, double z) {
return ((3.0 * z) * z) + (y * x);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((3.0d0 * z) * z) + (y * x)
end function
public static double code(double x, double y, double z) {
return ((3.0 * z) * z) + (y * x);
}
def code(x, y, z): return ((3.0 * z) * z) + (y * x)
function code(x, y, z) return Float64(Float64(Float64(3.0 * z) * z) + Float64(y * x)) end
function tmp = code(x, y, z) tmp = ((3.0 * z) * z) + (y * x); end
code[x_, y_, z_] := N[(N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(3 \cdot z\right) \cdot z + y \cdot x
\end{array}
herbie shell --seed 2024039
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
:precision binary64
:herbie-target
(+ (* (* 3.0 z) z) (* y x))
(+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))