
(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 (if (<= (* y x) (- INFINITY)) (* y x) (+ (* z z) (+ (* z z) (+ (* y x) (* z z))))))
double code(double x, double y, double z) {
double tmp;
if ((y * x) <= -((double) INFINITY)) {
tmp = y * x;
} else {
tmp = (z * z) + ((z * z) + ((y * x) + (z * z)));
}
return tmp;
}
public static double code(double x, double y, double z) {
double tmp;
if ((y * x) <= -Double.POSITIVE_INFINITY) {
tmp = y * x;
} else {
tmp = (z * z) + ((z * z) + ((y * x) + (z * z)));
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y * x) <= -math.inf: tmp = y * x else: tmp = (z * z) + ((z * z) + ((y * x) + (z * z))) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(y * x) <= Float64(-Inf)) tmp = Float64(y * x); else tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(Float64(y * x) + Float64(z * z)))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y * x) <= -Inf) tmp = y * x; else tmp = (z * z) + ((z * z) + ((y * x) + (z * z))); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(y * x), $MachinePrecision], (-Infinity)], N[(y * x), $MachinePrecision], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(N[(y * x), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot x \leq -\infty:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot z + \left(z \cdot z + \left(y \cdot x + z \cdot z\right)\right)\\
\end{array}
\end{array}
if (*.f64 x y) < -inf.0Initial program 62.5%
Taylor expanded in x around 0 62.5%
unpow262.5%
+-commutative62.5%
*-commutative62.5%
associate-+r+62.5%
unpow262.5%
distribute-rgt1-in62.5%
metadata-eval62.5%
*-commutative62.5%
associate-*r*62.5%
fma-def87.5%
*-commutative87.5%
Simplified87.5%
Taylor expanded in z around 0 87.5%
if -inf.0 < (*.f64 x y) Initial program 99.9%
Final simplification99.1%
(FPCore (x y z) :precision binary64 (fma z (* z 3.0) (* y x)))
double code(double x, double y, double z) {
return fma(z, (z * 3.0), (y * x));
}
function code(x, y, z) return fma(z, Float64(z * 3.0), Float64(y * x)) end
code[x_, y_, z_] := N[(z * N[(z * 3.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, z \cdot 3, y \cdot x\right)
\end{array}
Initial program 97.5%
Taylor expanded in x around 0 97.5%
unpow297.5%
+-commutative97.5%
*-commutative97.5%
associate-+r+97.5%
unpow297.5%
distribute-rgt1-in97.5%
metadata-eval97.5%
*-commutative97.5%
associate-*r*97.5%
fma-def99.1%
*-commutative99.1%
Simplified99.1%
Final simplification99.1%
(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-def98.3%
count-298.3%
distribute-rgt1-in98.3%
*-commutative98.3%
associate-*l*98.3%
metadata-eval98.3%
Simplified98.3%
Final simplification98.3%
(FPCore (x y z) :precision binary64 (if (<= (* y x) (- INFINITY)) (* y x) (+ (* y x) (* z (* z 3.0)))))
double code(double x, double y, double z) {
double tmp;
if ((y * x) <= -((double) INFINITY)) {
tmp = y * x;
} else {
tmp = (y * x) + (z * (z * 3.0));
}
return tmp;
}
public static double code(double x, double y, double z) {
double tmp;
if ((y * x) <= -Double.POSITIVE_INFINITY) {
tmp = y * x;
} else {
tmp = (y * x) + (z * (z * 3.0));
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y * x) <= -math.inf: tmp = y * x else: tmp = (y * x) + (z * (z * 3.0)) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(y * x) <= Float64(-Inf)) tmp = Float64(y * x); else tmp = Float64(Float64(y * x) + Float64(z * Float64(z * 3.0))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y * x) <= -Inf) tmp = y * x; else tmp = (y * x) + (z * (z * 3.0)); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(y * x), $MachinePrecision], (-Infinity)], N[(y * x), $MachinePrecision], N[(N[(y * x), $MachinePrecision] + N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot x \leq -\infty:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot x + z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 x y) < -inf.0Initial program 62.5%
Taylor expanded in x around 0 62.5%
unpow262.5%
+-commutative62.5%
*-commutative62.5%
associate-+r+62.5%
unpow262.5%
distribute-rgt1-in62.5%
metadata-eval62.5%
*-commutative62.5%
associate-*r*62.5%
fma-def87.5%
*-commutative87.5%
Simplified87.5%
Taylor expanded in z around 0 87.5%
if -inf.0 < (*.f64 x y) Initial program 99.9%
associate-+l+99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
distribute-rgt1-in99.9%
*-commutative99.9%
associate-*l*99.8%
metadata-eval99.8%
Simplified99.8%
fma-udef99.8%
+-commutative99.8%
Applied egg-rr99.8%
Final simplification99.1%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 1e-73) (* y x) (* 3.0 (* z z))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 1e-73) {
tmp = y * x;
} else {
tmp = 3.0 * (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) <= 1d-73) then
tmp = y * x
else
tmp = 3.0d0 * (z * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 1e-73) {
tmp = y * x;
} else {
tmp = 3.0 * (z * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 1e-73: tmp = y * x else: tmp = 3.0 * (z * z) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 1e-73) tmp = Float64(y * x); else tmp = Float64(3.0 * Float64(z * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 1e-73) tmp = y * x; else tmp = 3.0 * (z * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 1e-73], N[(y * x), $MachinePrecision], N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 10^{-73}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z \cdot z\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 9.99999999999999997e-74Initial program 99.9%
Taylor expanded in x around 0 100.0%
unpow2100.0%
+-commutative100.0%
*-commutative100.0%
associate-+r+100.0%
unpow2100.0%
distribute-rgt1-in100.0%
metadata-eval100.0%
*-commutative100.0%
associate-*r*100.0%
fma-def100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in z around 0 93.4%
if 9.99999999999999997e-74 < (*.f64 z z) Initial program 95.7%
Taylor expanded in x around 0 84.8%
unpow284.8%
*-commutative84.8%
associate-*l*84.8%
*-commutative84.8%
count-284.8%
Simplified84.8%
distribute-lft-out84.8%
count-284.8%
*-un-lft-identity84.8%
distribute-rgt-out84.8%
metadata-eval84.8%
associate-*r*84.8%
Applied egg-rr84.8%
Final simplification88.5%
(FPCore (x y z) :precision binary64 (if (<= z 2.7e-36) (* y x) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.7e-36) {
tmp = y * x;
} 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 <= 2.7d-36) then
tmp = y * x
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 <= 2.7e-36) {
tmp = y * x;
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 2.7e-36: tmp = y * x else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 2.7e-36) tmp = Float64(y * x); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 2.7e-36) tmp = y * x; else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 2.7e-36], N[(y * x), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.7 \cdot 10^{-36}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if z < 2.70000000000000007e-36Initial program 98.3%
Taylor expanded in x around 0 98.3%
unpow298.3%
+-commutative98.3%
*-commutative98.3%
associate-+r+98.3%
unpow298.3%
distribute-rgt1-in98.3%
metadata-eval98.3%
*-commutative98.3%
associate-*r*98.3%
fma-def99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in z around 0 61.3%
if 2.70000000000000007e-36 < z Initial program 95.3%
Taylor expanded in x around 0 83.5%
unpow283.5%
unpow283.5%
distribute-rgt1-in83.5%
metadata-eval83.5%
*-commutative83.5%
associate-*r*83.5%
Simplified83.5%
Final simplification67.0%
(FPCore (x y z) :precision binary64 (* y x))
double code(double x, double y, double z) {
return 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 = y * x
end function
public static double code(double x, double y, double z) {
return y * x;
}
def code(x, y, z): return y * x
function code(x, y, z) return Float64(y * x) end
function tmp = code(x, y, z) tmp = y * x; end
code[x_, y_, z_] := N[(y * x), $MachinePrecision]
\begin{array}{l}
\\
y \cdot x
\end{array}
Initial program 97.5%
Taylor expanded in x around 0 97.5%
unpow297.5%
+-commutative97.5%
*-commutative97.5%
associate-+r+97.5%
unpow297.5%
distribute-rgt1-in97.5%
metadata-eval97.5%
*-commutative97.5%
associate-*r*97.5%
fma-def99.1%
*-commutative99.1%
Simplified99.1%
Taylor expanded in z around 0 50.6%
Final simplification50.6%
(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 2023199
(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)))