
(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 6 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}
NOTE: z should be positive before calling this function (FPCore (x y z) :precision binary64 (fma (* (* z 3.0) (sqrt z)) (sqrt z) (* x y)))
z = abs(z);
double code(double x, double y, double z) {
return fma(((z * 3.0) * sqrt(z)), sqrt(z), (x * y));
}
z = abs(z) function code(x, y, z) return fma(Float64(Float64(z * 3.0) * sqrt(z)), sqrt(z), Float64(x * y)) end
NOTE: z should be positive before calling this function code[x_, y_, z_] := N[(N[(N[(z * 3.0), $MachinePrecision] * N[Sqrt[z], $MachinePrecision]), $MachinePrecision] * N[Sqrt[z], $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
z = |z|\\
\\
\mathsf{fma}\left(\left(z \cdot 3\right) \cdot \sqrt{z}, \sqrt{z}, x \cdot y\right)
\end{array}
Initial program 99.1%
associate-+l+99.1%
associate-+l+99.1%
fma-def99.1%
count-299.1%
distribute-rgt1-in99.1%
*-commutative99.1%
associate-*l*99.1%
metadata-eval99.1%
Simplified99.1%
fma-udef99.1%
+-commutative99.1%
Applied egg-rr99.1%
associate-*r*99.1%
*-commutative99.1%
associate-*r*99.1%
*-commutative99.1%
add-sqr-sqrt50.3%
associate-*r*50.3%
fma-def50.7%
Applied egg-rr50.7%
Final simplification50.7%
NOTE: z should be positive before calling this function (FPCore (x y z) :precision binary64 (fma z z (fma x y (* z (+ z z)))))
z = abs(z);
double code(double x, double y, double z) {
return fma(z, z, fma(x, y, (z * (z + z))));
}
z = abs(z) function code(x, y, z) return fma(z, z, fma(x, y, Float64(z * Float64(z + z)))) end
NOTE: z should be positive before calling this function code[x_, y_, z_] := N[(z * z + N[(x * y + N[(z * N[(z + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
z = |z|\\
\\
\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, z \cdot \left(z + z\right)\right)\right)
\end{array}
Initial program 99.1%
+-commutative99.1%
fma-def99.2%
associate-+l+99.2%
fma-def99.2%
distribute-lft-out99.2%
Simplified99.2%
Final simplification99.2%
NOTE: z should be positive before calling this function (FPCore (x y z) :precision binary64 (if (or (<= (* z z) 5e-153) (and (not (<= (* z z) 5e+19)) (<= (* z z) 2e+90))) (* x y) (* 3.0 (* z z))))
z = abs(z);
double code(double x, double y, double z) {
double tmp;
if (((z * z) <= 5e-153) || (!((z * z) <= 5e+19) && ((z * z) <= 2e+90))) {
tmp = x * y;
} else {
tmp = 3.0 * (z * z);
}
return tmp;
}
NOTE: z should be positive before calling this function
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) <= 5d-153) .or. (.not. ((z * z) <= 5d+19)) .and. ((z * z) <= 2d+90)) then
tmp = x * y
else
tmp = 3.0d0 * (z * z)
end if
code = tmp
end function
z = Math.abs(z);
public static double code(double x, double y, double z) {
double tmp;
if (((z * z) <= 5e-153) || (!((z * z) <= 5e+19) && ((z * z) <= 2e+90))) {
tmp = x * y;
} else {
tmp = 3.0 * (z * z);
}
return tmp;
}
z = abs(z) def code(x, y, z): tmp = 0 if ((z * z) <= 5e-153) or (not ((z * z) <= 5e+19) and ((z * z) <= 2e+90)): tmp = x * y else: tmp = 3.0 * (z * z) return tmp
z = abs(z) function code(x, y, z) tmp = 0.0 if ((Float64(z * z) <= 5e-153) || (!(Float64(z * z) <= 5e+19) && (Float64(z * z) <= 2e+90))) tmp = Float64(x * y); else tmp = Float64(3.0 * Float64(z * z)); end return tmp end
z = abs(z) function tmp_2 = code(x, y, z) tmp = 0.0; if (((z * z) <= 5e-153) || (~(((z * z) <= 5e+19)) && ((z * z) <= 2e+90))) tmp = x * y; else tmp = 3.0 * (z * z); end tmp_2 = tmp; end
NOTE: z should be positive before calling this function code[x_, y_, z_] := If[Or[LessEqual[N[(z * z), $MachinePrecision], 5e-153], And[N[Not[LessEqual[N[(z * z), $MachinePrecision], 5e+19]], $MachinePrecision], LessEqual[N[(z * z), $MachinePrecision], 2e+90]]], N[(x * y), $MachinePrecision], N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
z = |z|\\
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 5 \cdot 10^{-153} \lor \neg \left(z \cdot z \leq 5 \cdot 10^{+19}\right) \land z \cdot z \leq 2 \cdot 10^{+90}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;3 \cdot \left(z \cdot z\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 5.00000000000000033e-153 or 5e19 < (*.f64 z z) < 1.99999999999999993e90Initial program 100.0%
Taylor expanded in x around inf 93.6%
if 5.00000000000000033e-153 < (*.f64 z z) < 5e19 or 1.99999999999999993e90 < (*.f64 z z) Initial program 98.4%
Taylor expanded in x around 0 82.8%
unpow282.8%
unpow282.8%
distribute-rgt1-in82.8%
metadata-eval82.8%
Simplified82.8%
Final simplification87.9%
NOTE: z should be positive before calling this function (FPCore (x y z) :precision binary64 (+ (* z z) (+ (* z z) (+ (* x y) (* z z)))))
z = abs(z);
double code(double x, double y, double z) {
return (z * z) + ((z * z) + ((x * y) + (z * z)));
}
NOTE: z should be positive before calling this function
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (z * z) + ((z * z) + ((x * y) + (z * z)))
end function
z = Math.abs(z);
public static double code(double x, double y, double z) {
return (z * z) + ((z * z) + ((x * y) + (z * z)));
}
z = abs(z) def code(x, y, z): return (z * z) + ((z * z) + ((x * y) + (z * z)))
z = abs(z) function code(x, y, z) return Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(Float64(x * y) + Float64(z * z)))) end
z = abs(z) function tmp = code(x, y, z) tmp = (z * z) + ((z * z) + ((x * y) + (z * z))); end
NOTE: z should be positive before calling this function code[x_, y_, z_] := N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
z = |z|\\
\\
z \cdot z + \left(z \cdot z + \left(x \cdot y + z \cdot z\right)\right)
\end{array}
Initial program 99.1%
Final simplification99.1%
NOTE: z should be positive before calling this function (FPCore (x y z) :precision binary64 (+ (* x y) (* z (* z 3.0))))
z = abs(z);
double code(double x, double y, double z) {
return (x * y) + (z * (z * 3.0));
}
NOTE: z should be positive before calling this function
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 * 3.0d0))
end function
z = Math.abs(z);
public static double code(double x, double y, double z) {
return (x * y) + (z * (z * 3.0));
}
z = abs(z) def code(x, y, z): return (x * y) + (z * (z * 3.0))
z = abs(z) function code(x, y, z) return Float64(Float64(x * y) + Float64(z * Float64(z * 3.0))) end
z = abs(z) function tmp = code(x, y, z) tmp = (x * y) + (z * (z * 3.0)); end
NOTE: z should be positive before calling this function code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
z = |z|\\
\\
x \cdot y + z \cdot \left(z \cdot 3\right)
\end{array}
Initial program 99.1%
associate-+l+99.1%
associate-+l+99.1%
fma-def99.1%
count-299.1%
distribute-rgt1-in99.1%
*-commutative99.1%
associate-*l*99.1%
metadata-eval99.1%
Simplified99.1%
fma-udef99.1%
+-commutative99.1%
Applied egg-rr99.1%
Final simplification99.1%
NOTE: z should be positive before calling this function (FPCore (x y z) :precision binary64 (* x y))
z = abs(z);
double code(double x, double y, double z) {
return x * y;
}
NOTE: z should be positive before calling this function
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
z = Math.abs(z);
public static double code(double x, double y, double z) {
return x * y;
}
z = abs(z) def code(x, y, z): return x * y
z = abs(z) function code(x, y, z) return Float64(x * y) end
z = abs(z) function tmp = code(x, y, z) tmp = x * y; end
NOTE: z should be positive before calling this function code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
z = |z|\\
\\
x \cdot y
\end{array}
Initial program 99.1%
Taylor expanded in x around inf 55.4%
Final simplification55.4%
(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 2023230
(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)))