
(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 7 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-def97.6%
associate-+l+97.6%
fma-def99.5%
count-299.5%
Simplified99.5%
Final simplification99.5%
(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.5%
associate-+l+97.5%
fma-def99.5%
associate-+r+99.5%
distribute-lft-out99.5%
distribute-lft-out99.5%
remove-double-neg99.5%
unsub-neg99.5%
count-299.5%
neg-mul-199.5%
distribute-rgt-out--99.5%
metadata-eval99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 2e+298) (+ (* 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+298) {
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+298) 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+298) {
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+298: 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+298) 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+298) 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+298], 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^{+298}:\\
\;\;\;\;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) < 1.9999999999999999e298Initial program 99.8%
if 1.9999999999999999e298 < (*.f64 z z) Initial program 89.2%
Taylor expanded in x around 0 98.2%
Simplified98.2%
add-sqr-sqrt98.2%
pow298.2%
sqrt-prod98.1%
unpow298.1%
sqrt-prod46.3%
add-sqr-sqrt98.1%
Applied egg-rr98.1%
unpow298.1%
*-commutative98.1%
*-commutative98.1%
swap-sqr98.1%
metadata-eval98.1%
metadata-eval98.1%
add-sqr-sqrt98.2%
metadata-eval98.2%
metadata-eval98.2%
sqrt-pow298.1%
associate-*r*98.1%
sqrt-pow298.2%
metadata-eval98.2%
metadata-eval98.2%
metadata-eval98.2%
Applied egg-rr98.2%
Final simplification99.4%
(FPCore (x y z) :precision binary64 (if (or (<= z 1.15e-65) (and (not (<= z 1.7e-48)) (<= z 2.2e-10))) (* x y) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.15e-65) || (!(z <= 1.7e-48) && (z <= 2.2e-10))) {
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 <= 1.15d-65) .or. (.not. (z <= 1.7d-48)) .and. (z <= 2.2d-10)) 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 <= 1.15e-65) || (!(z <= 1.7e-48) && (z <= 2.2e-10))) {
tmp = x * y;
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 1.15e-65) or (not (z <= 1.7e-48) and (z <= 2.2e-10)): tmp = x * y else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 1.15e-65) || (!(z <= 1.7e-48) && (z <= 2.2e-10))) 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 <= 1.15e-65) || (~((z <= 1.7e-48)) && (z <= 2.2e-10))) tmp = x * y; else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.15e-65], And[N[Not[LessEqual[z, 1.7e-48]], $MachinePrecision], LessEqual[z, 2.2e-10]]], N[(x * y), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.15 \cdot 10^{-65} \lor \neg \left(z \leq 1.7 \cdot 10^{-48}\right) \land z \leq 2.2 \cdot 10^{-10}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if z < 1.15e-65 or 1.70000000000000014e-48 < z < 2.1999999999999999e-10Initial program 99.3%
+-commutative99.3%
fma-def99.4%
associate-+l+99.4%
fma-def99.9%
count-299.9%
Simplified99.9%
add-sqr-sqrt99.8%
pow299.8%
*-commutative99.8%
sqrt-prod99.8%
sqrt-prod32.5%
add-sqr-sqrt99.8%
Applied egg-rr99.8%
Taylor expanded in x around inf 82.7%
Taylor expanded in z around 0 67.2%
if 1.15e-65 < z < 1.70000000000000014e-48 or 2.1999999999999999e-10 < z Initial program 91.8%
Taylor expanded in x around 0 71.6%
Simplified71.6%
add-sqr-sqrt71.6%
pow271.6%
sqrt-prod71.4%
unpow271.4%
sqrt-prod71.2%
add-sqr-sqrt71.4%
Applied egg-rr71.4%
unpow271.4%
*-commutative71.4%
*-commutative71.4%
swap-sqr71.3%
metadata-eval71.3%
metadata-eval71.3%
add-sqr-sqrt71.6%
metadata-eval71.6%
metadata-eval71.6%
sqrt-pow271.4%
associate-*r*71.3%
sqrt-pow271.6%
metadata-eval71.6%
metadata-eval71.6%
metadata-eval71.6%
Applied egg-rr71.6%
Final simplification68.3%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 1e-25) (+ (* z z) (+ (* z z) (* x y))) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 1e-25) {
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) <= 1d-25) 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) <= 1e-25) {
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) <= 1e-25: 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) <= 1e-25) 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) <= 1e-25) 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], 1e-25], 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 10^{-25}:\\
\;\;\;\;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) < 1.00000000000000004e-25Initial program 100.0%
Taylor expanded in x around inf 94.4%
if 1.00000000000000004e-25 < (*.f64 z z) Initial program 95.0%
Taylor expanded in x around 0 80.6%
Simplified80.6%
add-sqr-sqrt80.4%
pow280.4%
sqrt-prod80.4%
unpow280.4%
sqrt-prod33.2%
add-sqr-sqrt80.4%
Applied egg-rr80.4%
unpow280.4%
*-commutative80.4%
*-commutative80.4%
swap-sqr80.3%
metadata-eval80.3%
metadata-eval80.3%
add-sqr-sqrt80.6%
metadata-eval80.6%
metadata-eval80.6%
sqrt-pow280.3%
associate-*r*80.3%
sqrt-pow280.6%
metadata-eval80.6%
metadata-eval80.6%
metadata-eval80.6%
Applied egg-rr80.6%
Final simplification87.6%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 1e-25) (+ (* z z) (* x y)) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 1e-25) {
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) <= 1d-25) 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) <= 1e-25) {
tmp = (z * z) + (x * y);
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 1e-25: 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) <= 1e-25) 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) <= 1e-25) 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], 1e-25], 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 10^{-25}:\\
\;\;\;\;z \cdot z + x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 1.00000000000000004e-25Initial program 100.0%
Taylor expanded in x around inf 94.4%
Taylor expanded in x around inf 94.3%
if 1.00000000000000004e-25 < (*.f64 z z) Initial program 95.0%
Taylor expanded in x around 0 80.6%
Simplified80.6%
add-sqr-sqrt80.4%
pow280.4%
sqrt-prod80.4%
unpow280.4%
sqrt-prod33.2%
add-sqr-sqrt80.4%
Applied egg-rr80.4%
unpow280.4%
*-commutative80.4%
*-commutative80.4%
swap-sqr80.3%
metadata-eval80.3%
metadata-eval80.3%
add-sqr-sqrt80.6%
metadata-eval80.6%
metadata-eval80.6%
sqrt-pow280.3%
associate-*r*80.3%
sqrt-pow280.6%
metadata-eval80.6%
metadata-eval80.6%
metadata-eval80.6%
Applied egg-rr80.6%
Final simplification87.5%
(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%
+-commutative97.5%
fma-def97.6%
associate-+l+97.6%
fma-def99.5%
count-299.5%
Simplified99.5%
add-sqr-sqrt99.4%
pow299.4%
*-commutative99.4%
sqrt-prod99.4%
sqrt-prod48.3%
add-sqr-sqrt99.4%
Applied egg-rr99.4%
Taylor expanded in x around inf 78.3%
Taylor expanded in z around 0 58.3%
Final simplification58.3%
(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 2024041
(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)))