
(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 3.0) (* x y)))
double code(double x, double y, double z) {
return fma(z, (z * 3.0), (x * y));
}
function code(x, y, z) return fma(z, Float64(z * 3.0), Float64(x * y)) end
code[x_, y_, z_] := N[(z * N[(z * 3.0), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, z \cdot 3, x \cdot y\right)
\end{array}
Initial program 97.9%
associate-+l+N/A
associate-+l+N/A
+-commutativeN/A
distribute-lft-outN/A
distribute-lft-outN/A
accelerator-lowering-fma.f64N/A
+-lowering-+.f64N/A
+-lowering-+.f64N/A
*-lowering-*.f6499.5
Applied egg-rr99.5%
count-2N/A
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
*-lowering-*.f6499.5
Applied egg-rr99.5%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (fma (+ z z) z (* x y))))
(if (<= (* x y) -5.5e-244)
t_0
(if (<= (* x y) 4.3e-150) (* z (* z 3.0)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma((z + z), z, (x * y));
double tmp;
if ((x * y) <= -5.5e-244) {
tmp = t_0;
} else if ((x * y) <= 4.3e-150) {
tmp = z * (z * 3.0);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(Float64(z + z), z, Float64(x * y)) tmp = 0.0 if (Float64(x * y) <= -5.5e-244) tmp = t_0; elseif (Float64(x * y) <= 4.3e-150) tmp = Float64(z * Float64(z * 3.0)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(z + z), $MachinePrecision] * z + N[(x * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * y), $MachinePrecision], -5.5e-244], t$95$0, If[LessEqual[N[(x * y), $MachinePrecision], 4.3e-150], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(z + z, z, x \cdot y\right)\\
\mathbf{if}\;x \cdot y \leq -5.5 \cdot 10^{-244}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \cdot y \leq 4.3 \cdot 10^{-150}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (*.f64 x y) < -5.4999999999999998e-244 or 4.30000000000000004e-150 < (*.f64 x y) Initial program 97.6%
associate-+l+N/A
+-commutativeN/A
count-2N/A
associate-*r*N/A
count-2N/A
accelerator-lowering-fma.f64N/A
+-lowering-+.f64N/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f6498.1
Applied egg-rr98.1%
Taylor expanded in x around inf
*-lowering-*.f6488.5
Simplified88.5%
if -5.4999999999999998e-244 < (*.f64 x y) < 4.30000000000000004e-150Initial program 99.5%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6490.5
Simplified90.5%
*-commutativeN/A
associate-*r*N/A
metadata-evalN/A
associate-/r/N/A
*-lft-identityN/A
*-lowering-*.f64N/A
*-lft-identityN/A
associate-/r/N/A
metadata-evalN/A
*-lowering-*.f6490.8
Applied egg-rr90.8%
Final simplification88.9%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 4e+290) (fma 3.0 (* z z) (* x y)) (fma z z (* x y))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 4e+290) {
tmp = fma(3.0, (z * z), (x * y));
} else {
tmp = fma(z, z, (x * y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 4e+290) tmp = fma(3.0, Float64(z * z), Float64(x * y)); else tmp = fma(z, z, Float64(x * y)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 4e+290], N[(3.0 * N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(z * z + N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 4 \cdot 10^{+290}:\\
\;\;\;\;\mathsf{fma}\left(3, z \cdot z, x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, z, x \cdot y\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 4.00000000000000025e290Initial program 99.8%
associate-+l+N/A
associate-+l+N/A
+-commutativeN/A
count-2N/A
distribute-lft1-inN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f6499.8
Applied egg-rr99.8%
if 4.00000000000000025e290 < (*.f64 z z) Initial program 93.8%
associate-+l+N/A
associate-+l+N/A
+-commutativeN/A
distribute-lft-outN/A
distribute-lft-outN/A
accelerator-lowering-fma.f64N/A
+-lowering-+.f64N/A
+-lowering-+.f64N/A
*-lowering-*.f6498.8
Applied egg-rr98.8%
Applied egg-rr98.8%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 5e+68) (fma z z (* x y)) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 5e+68) {
tmp = fma(z, z, (x * y));
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 5e+68) tmp = fma(z, z, Float64(x * y)); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 5e+68], N[(z * z + 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 5 \cdot 10^{+68}:\\
\;\;\;\;\mathsf{fma}\left(z, z, x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 5.0000000000000004e68Initial program 99.9%
associate-+l+N/A
associate-+l+N/A
+-commutativeN/A
distribute-lft-outN/A
distribute-lft-outN/A
accelerator-lowering-fma.f64N/A
+-lowering-+.f64N/A
+-lowering-+.f64N/A
*-lowering-*.f6499.9
Applied egg-rr99.9%
Applied egg-rr84.4%
if 5.0000000000000004e68 < (*.f64 z z) Initial program 95.4%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6490.4
Simplified90.4%
*-commutativeN/A
associate-*r*N/A
metadata-evalN/A
associate-/r/N/A
*-lft-identityN/A
*-lowering-*.f64N/A
*-lft-identityN/A
associate-/r/N/A
metadata-evalN/A
*-lowering-*.f6490.5
Applied egg-rr90.5%
Final simplification87.1%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 5e+68) (* x y) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 5e+68) {
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 * z) <= 5d+68) 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 * z) <= 5e+68) {
tmp = x * y;
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 5e+68: tmp = x * y else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 5e+68) 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 * z) <= 5e+68) tmp = x * y; else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 5e+68], N[(x * y), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 5 \cdot 10^{+68}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 5.0000000000000004e68Initial program 99.9%
Taylor expanded in x around inf
*-lowering-*.f6481.9
Simplified81.9%
if 5.0000000000000004e68 < (*.f64 z z) Initial program 95.4%
Taylor expanded in x around 0
distribute-lft1-inN/A
metadata-evalN/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6490.4
Simplified90.4%
*-commutativeN/A
associate-*r*N/A
metadata-evalN/A
associate-/r/N/A
*-lft-identityN/A
*-lowering-*.f64N/A
*-lft-identityN/A
associate-/r/N/A
metadata-evalN/A
*-lowering-*.f6490.5
Applied egg-rr90.5%
Final simplification85.7%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 2.35e+237) (* x y) (* z z)))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 2.35e+237) {
tmp = x * y;
} else {
tmp = 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) <= 2.35d+237) then
tmp = x * y
else
tmp = z * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 2.35e+237) {
tmp = x * y;
} else {
tmp = z * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 2.35e+237: tmp = x * y else: tmp = z * z return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 2.35e+237) tmp = Float64(x * y); else tmp = Float64(z * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 2.35e+237) tmp = x * y; else tmp = z * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 2.35e+237], N[(x * y), $MachinePrecision], N[(z * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 2.35 \cdot 10^{+237}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot z\\
\end{array}
\end{array}
if (*.f64 z z) < 2.35000000000000009e237Initial program 99.8%
Taylor expanded in x around inf
*-lowering-*.f6472.3
Simplified72.3%
if 2.35000000000000009e237 < (*.f64 z z) Initial program 94.1%
Taylor expanded in x around inf
*-lowering-*.f6489.4
Simplified89.4%
Taylor expanded in x around 0
unpow2N/A
*-lowering-*.f6490.6
Simplified90.6%
(FPCore (x y z) :precision binary64 (fma y x (* 3.0 (* z z))))
double code(double x, double y, double z) {
return fma(y, x, (3.0 * (z * z)));
}
function code(x, y, z) return fma(y, x, Float64(3.0 * Float64(z * z))) end
code[x_, y_, z_] := N[(y * x + N[(3.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, 3 \cdot \left(z \cdot z\right)\right)
\end{array}
Initial program 97.9%
associate-+l+N/A
associate-+l+N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
count-2N/A
distribute-lft1-inN/A
metadata-evalN/A
*-lowering-*.f64N/A
*-lowering-*.f6498.3
Applied egg-rr98.3%
(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.9%
Taylor expanded in x around inf
*-lowering-*.f6453.0
Simplified53.0%
(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 2024205
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
:precision binary64
:alt
(! :herbie-platform default (+ (* (* 3 z) z) (* y x)))
(+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))